How to use _pcall method in tox

Best Python code snippet using tox_python

deploy.py

Source:deploy.py Github

copy

Full Screen

...35 return "%s returned ERROR: %s" % (self.cmd, self.retcode)36def _popen(cmd, **kwargs):37 print ' '.join(cmd)38 return subprocess.Popen(cmd, **kwargs)39def _pcall(cmd, **kwargs):40 print ' '.join(cmd)41 retcode = subprocess.call(cmd, **kwargs)42 if retcode:43 raise ProcessException(cmd, retcode)44 return retcode45def _getenv(name):46 try:47 var = os.environ[name]48 return var49 except:50 raise Usage("Error: environment variable '%s' not set" % name)51def _template(source, dest, mapping=os.environ):52 """53 Takes source file and writes it to dest using mapping.54 """55 # read the template into a string56 source_file = open(source)57 contents = source_file.read()58 source_file.close()59 # substitute the variables60 template = Template(contents)61 result = template.substitute(mapping)62 # write the template to dest63 dest_file = open(dest, 'w')64 dest_file.write(result)65 dest_file.close()66def _update_pgpass(hostname, database, username, password):67 """68 Using the DB_* env vars, updates the pgpass file.69 """70 pgpass_line = "%s:*:%s:%s" % (hostname, database, username)71 pgpass_filename = os.path.expanduser('~postgres/.pgpass')72 try:73 # if exists the .pgpass file, move it then write it out without74 os.rename(pgpass_filename, '%s.tmp' % pgpass_filename)75 pgpass_old = open('%s.tmp' % pgpass_filename, 'r')76 pgpass = open(pgpass_filename, 'w')77 for line in pgpass_old:78 # skip if the first part of the line matches our line79 if line[:len(pgpass_line)] != pgpass_line:80 pgpass.write(line)81 pgpass.close()82 except:83 # file doesn't exist84 pgpass = open(pgpass_filename, 'w')85 pgpass.close()86 # append new line87 pgpass_line = '%s:%s\n' % (pgpass_line, password)88 print "Updating .pgpass - %s" % pgpass_line89 pgpass = open(pgpass_filename, 'a')90 pgpass.write(pgpass_line)91 pgpass.close()92 os.chmod(pgpass_filename, 0600)93 os.chown(pgpass_filename, pwd.getpwnam('postgres')[2], pwd.getpwnam('postgres')[3])94def do_database():95 """96 If DB_SETUP is True then it wipes the database.97 98 If BUILD_TYPE is 'staging' then it migrates the db using 99 env vars DB_COPY_*100 101 Env vars used:102 * DB_USER103 * DB_PASS104 * DB_NAME105 """106 DB_USER = _getenv('DB_USER')107 DB_PASS = _getenv('DB_PASS')108 DB_NAME = _getenv('DB_NAME')109 DB_HOST = _getenv('DB_HOST')110 DB_SETUP = eval(_getenv('DB_SETUP'))111 DB_COPY = eval(os.environ.get('DB_COPY','False'))112 # dev only resets db 113 if DB_SETUP:114 print "Setting up the database"115 # update the .pgpass file116 _update_pgpass(DB_HOST, DB_NAME, DB_USER, DB_PASS)117 # drop db: sudo -u postgres dropdb $DB_NAME118 try:119 _pcall(['sudo', '-u', 'postgres', 'dropdb', DB_NAME], stderr=open('/dev/null', 'w'))120 except:121 pass122 # drop db user: sudo -u postgres dropuser $DB_USER123 try:124 _pcall(['sudo', '-u', 'postgres', 'dropuser', DB_USER], stderr=open('/dev/null', 'w'))125 except:126 pass127 # create user: sudo -u postgres psql postgres128 p = _popen(['sudo', '-u', 'postgres', 'psql'], stdin=subprocess.PIPE)129 p.stdin.write("CREATE ROLE %s PASSWORD '%s' NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;\n" % (DB_USER, DB_PASS))130 p.stdin.close()131 p.wait()132 # create db: sudo -u postgres createdb -O $DB_USER $DB_NAME133 _pcall(['sudo', '-u', 'postgres', 'createdb', '-O', DB_USER, DB_NAME])134 if DB_COPY:135 print "Copying database"136 # get the migration db details137 DB_COPY_USER = _getenv('DB_COPY_USER')138 DB_COPY_PASS = _getenv('DB_COPY_PASS')139 DB_COPY_NAME = _getenv('DB_COPY_NAME')140 DB_COPY_HOST = _getenv('DB_COPY_HOST')141 # update the .pgpass file142 _update_pgpass(DB_COPY_HOST, DB_COPY_NAME, DB_COPY_USER, DB_COPY_PASS)143 # dump the database to a tempfile144 import tempfile145 (tmp_fd, tfilename) = tempfile.mkstemp()146 print "Dumping database '%s' to file '%s'" % (DB_COPY_NAME, tfilename)147 tfile = os.fdopen(tmp_fd, 'rw')148 # pg_dump database > tempfile149 p = _popen(['sudo', '-u', 'postgres',150 'pg_dump', '-v',151 '-h', DB_COPY_HOST,152 '-U', DB_COPY_USER,153 '-O',154 DB_COPY_NAME],155 stdin=subprocess.PIPE,156 stdout=tfile)157 # prompts for password158 p.wait()159 tfile.seek(0)160 # load up dump into new db161 p = _popen(['sudo', '-u', 'postgres',162 'psql',163 '-d', DB_NAME,164 '-h', DB_HOST,165 '-U', DB_USER,166 ],167 stdin=subprocess.PIPE)168 p.stdin.write(tfile.read())169 p.stdin.close()170 p.wait()171def do_settingsfile(deploy_dir):172 """173 Writes the setting file from a template.174 """175 print "Writing out settings file"176 _template(177 os.path.join(deploy_dir, 'settings_template.py'),178 os.path.join(deploy_dir, 'settings_local.py'),179 )180def do_virtualenv(deploy_dir):181 """182 Set up the virtual environment.183 """184 print "Setting up virtual environment"185 # python lib/pinax/scripts/pinax-boot.py --development --source=lib/pinax pinax-env --django-version=$DJANGO_VERSION186 # '--django-version=%s' % _getenv('DJANGO_VERSION'),187 _pcall(['python', 'lib/pinax/scripts/pinax-boot.py', '--development', '--source=lib/pinax', 'pinax-env'])188 # activate it189 activate_this = os.path.join(deploy_dir, "pinax-env/bin/activate_this.py")190 execfile(activate_this, dict(__file__=activate_this))191 os.environ['PATH'] = '%s:%s' % (os.path.join(deploy_dir, 'pinax-env/bin'), _getenv('PATH'))192 # install requirements: pip install --no-deps --requirement requirements.txt193 _pcall(['pip', 'install', '--no-deps', '--requirement', 'requirements.txt'])194 195def do_django(deploy_dir):196 """197 This runs the various django commands to setup the media, database, etc198 """199 print "Running django commands"200 201 _pcall(['python', 'manage.py', 'build_static', '--noinput', ])202 _pcall(['sudo', 'apt-get', 'install', 'python-imaging', ])203 # syncdb: python manage.py syncdb --noinput204 _pcall(['python', 'manage.py', 'syncdb', '--noinput', ])205 DB_MIGRATE = eval(os.environ.get('DB_MIGRATE', 'False'))206 if DB_MIGRATE:207 # run custom migrate script208 if os.path.isfile(os.path.join(deploy_dir, 'deploy/db_migrate.sh')):209 os.chmod(os.path.join(deploy_dir, 'deploy/db_migrate.sh'), 0775)210 _pcall([os.path.join(deploy_dir, 'deploy/db_migrate.sh'), ])211 else:212 # run generic migrate: python manage.py migrate --all -v 2213 _pcall(['python', 'manage.py', 'migrate', '--all', '-v', '2'])214 # fixtures215 try:216 _pcall(['python', 'manage.py', 'loaddata', _getenv('FIXTURE_FILE'), ])217 except:218 # no fixture file219 pass220 # chown the deploy dir to be the apache user221 APACHE_USER = _getenv('APACHE_USER')222 _pcall(['chown', '-R', APACHE_USER, deploy_dir])223def do_cron(deploy_dir):224 """225 Setup the cron template226 """227 print "Creating cron file"228 # get vars229 cron_filename = _getenv('CRON_FILE')230 cronfile = os.path.join('/etc/cron.d/', cron_filename)231 # write out the cron template232 _template(233 os.path.join(deploy_dir, 'conf/cron.template'),234 cronfile,235 )236 # need to be owned by root237 os.chown(cronfile, pwd.getpwnam('root')[2], -1)238 # need to be exec239 os.chmod(cronfile, 0775)240 # make chonograph.sh executable241 os.chmod(os.path.join(deploy_dir, 'deploy/chronograph.sh'), 0775)242def do_apache(deploy_dir):243 """244 Setups apache245 """246 print "Setting up Apache2"247 # enable required mods248 print "Enabling mod_rewrite"249 _pcall(['a2enmod', 'rewrite'])250 apache_conf = os.path.join('/etc/apache2/sites-available', _getenv('APACHE_CONF'))251 # rewrite config252 print "Writing out Apache2 conf: %s" % apache_conf253 _template(254 os.path.join(deploy_dir, 'conf/http.conf.template'),255 apache_conf,256 )257 # enable if needed258 _pcall(['a2ensite', _getenv('APACHE_CONF')])259 print "Testing Apache2 config"260 retcode = _pcall(['apache2ctl', 'configtest'])261 if retcode:262 print "Error in Apache2 config"263 # disable site264 _pcall(['a2dissite', _getenv('APACHE_CONF')])265 raise Usage('Error in Apache2 config')266 print "Restarting Apache"267 _pcall(['apache2ctl', 'restart'])268def do_sitemedia(deploy_dir):269 """270 Copies the site media directory.271 """272 site_media_src = os.environ.get('COPY_SITE_MEDIA', None)273 if site_media_src:274 site_media_src = os.path.abspath(os.path.join(deploy_dir, site_media_src))275 # rsync -avz site_media_src site_media276 _pcall(['rsync', '-avz', site_media_src, deploy_dir])277def debug_env():278 import sys279 print "debug env"280 print "PATH=%s" % _getenv('PATH')281 print "sys path:"282 for path in sys.path:283 print path284def process(deploy_dir):285 """286 Deploys the server to the directory.287 """288 # setup the database289 do_database()290 # setup the settings file...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful