How to use venv_dir method in localstack

Best Python code snippet using localstack_python

pip.py

Source:pip.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2'''3 :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`4 :copyright: © 2012-2013 by the SaltStack Team, see AUTHORS for more details5 :license: Apache 2.0, see LICENSE for more details.6 tests.integration.states.pip7 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~8'''9# Import python libs10import os11import pwd12import glob13import shutil14# Import Salt Testing libs15from salttesting import skipIf16from salttesting.helpers import (17 destructiveTest,18 ensure_in_syspath,19 with_system_account20)21ensure_in_syspath('../../')22# Import salt libs23import integration24import salt.utils25from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES26@skipIf(salt.utils.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed')27class PipStateTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):28 def test_pip_installed_errors(self):29 venv_dir = os.path.join(30 integration.SYS_TMP_DIR, 'pip-installed-errors'31 )32 try:33 # Since we don't have the virtualenv created, pip.installed will34 # thrown and error.35 # Example error strings:36 # * "Error installing 'supervisor': /tmp/pip-installed-errors: not found"37 # * "Error installing 'supervisor': /bin/sh: 1: /tmp/pip-installed-errors: not found"38 # * "Error installing 'supervisor': /bin/bash: /tmp/pip-installed-errors: No such file or directory"39 os.environ['SHELL'] = '/bin/sh'40 ret = self.run_function('state.sls', mods='pip-installed-errors')41 self.assertSaltFalseReturn(ret)42 self.assertSaltCommentRegexpMatches(43 ret,44 'Error installing \'supervisor\':(?:.*)'45 '/tmp/pip-installed-errors(?:.*)'46 '([nN]o such file or directory|not found)'47 )48 # We now create the missing virtualenv49 ret = self.run_function('virtualenv.create', [venv_dir])50 self.assertEqual(ret['retcode'], 0)51 # The state should not have any issues running now52 ret = self.run_function('state.sls', mods='pip-installed-errors')53 self.assertSaltTrueReturn(ret)54 finally:55 if os.path.isdir(venv_dir):56 shutil.rmtree(venv_dir)57 def test_pip_installed_weird_install(self):58 ographite = '/opt/graphite'59 if os.path.isdir(ographite):60 self.skipTest(61 'You already have \'{0}\'. This test would overwrite this '62 'directory'.format(ographite)63 )64 try:65 os.makedirs(ographite)66 except OSError as err:67 if err.errno == 13:68 # Permission denied69 self.skipTest(70 'You don\'t have the required permissions to run this test'71 )72 finally:73 if os.path.isdir(ographite):74 shutil.rmtree(ographite)75 venv_dir = os.path.join(76 integration.SYS_TMP_DIR, 'pip-installed-weird-install'77 )78 try:79 # Since we don't have the virtualenv created, pip.installed will80 # thrown and error.81 ret = self.run_function(82 'state.sls', mods='pip-installed-weird-install'83 )84 self.assertSaltTrueReturn(ret)85 # We cannot use assertInSaltComment here because we need to skip86 # some of the state return parts87 for key in ret.keys():88 self.assertTrue(ret[key]['result'])89 if ret[key]['comment'] == 'Created new virtualenv':90 continue91 self.assertEqual(92 ret[key]['comment'],93 'There was no error installing package \'carbon\' '94 'although it does not show when calling \'pip.freeze\'.'95 )96 finally:97 if os.path.isdir(venv_dir):98 shutil.rmtree(venv_dir)99 if os.path.isdir('/opt/graphite'):100 shutil.rmtree('/opt/graphite')101 def test_issue_2028_pip_installed_state(self):102 ret = self.run_function('state.sls', mods='issue-2028-pip-installed')103 venv_dir = os.path.join(104 integration.SYS_TMP_DIR, 'issue-2028-pip-installed'105 )106 try:107 self.assertSaltTrueReturn(ret)108 self.assertTrue(109 os.path.isfile(os.path.join(venv_dir, 'bin', 'supervisord'))110 )111 finally:112 if os.path.isdir(venv_dir):113 shutil.rmtree(venv_dir)114 def test_issue_2087_missing_pip(self):115 venv_dir = os.path.join(116 integration.SYS_TMP_DIR, 'issue-2087-missing-pip'117 )118 try:119 # Let's create the testing virtualenv120 ret = self.run_function('virtualenv.create', [venv_dir])121 self.assertEqual(ret['retcode'], 0)122 # Let's remove the pip binary123 pip_bin = os.path.join(venv_dir, 'bin', 'pip')124 if not os.path.isfile(pip_bin):125 self.skipTest(126 'Failed to find the pip binary to the test virtualenv'127 )128 os.remove(pip_bin)129 # Let's run the state which should fail because pip is missing130 ret = self.run_function('state.sls', mods='issue-2087-missing-pip')131 self.assertSaltFalseReturn(ret)132 self.assertInSaltComment(133 'Error installing \'pep8\': Could not find a `pip` binary',134 ret135 )136 finally:137 if os.path.isdir(venv_dir):138 shutil.rmtree(venv_dir)139 def test_issue_5940_multiple_pip_mirrors(self):140 ret = self.run_function(141 'state.sls', mods='issue-5940-multiple-pip-mirrors'142 )143 venv_dir = os.path.join(144 integration.SYS_TMP_DIR, '5940-multiple-pip-mirrors'145 )146 try:147 self.assertSaltTrueReturn(ret)148 self.assertTrue(149 os.path.isfile(os.path.join(venv_dir, 'bin', 'pep8'))150 )151 finally:152 if os.path.isdir(venv_dir):153 shutil.rmtree(venv_dir)154 @destructiveTest155 @skipIf(os.geteuid() != 0, 'you must be root to run this test')156 @with_system_account('issue-6912', on_existing='delete', delete=True)157 def test_issue_6912_wrong_owner(self, username):158 venv_dir = os.path.join(159 integration.SYS_TMP_DIR, '6912-wrong-owner'160 )161 # ----- Using runas ------------------------------------------------->162 venv_create = self.run_function(163 'virtualenv.create', [venv_dir], runas=username164 )165 if venv_create['retcode'] > 0:166 self.skipTest(167 'Failed to create testcase virtual environment: {0}'.format(168 venv_create169 )170 )171 # Using the package name.172 try:173 ret = self.run_state(174 'pip.installed', name='pep8', runas=username, bin_env=venv_dir175 )176 self.assertSaltTrueReturn(ret)177 uinfo = pwd.getpwnam(username)178 for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),179 os.path.join(venv_dir, '*', '**', 'pep8*'),180 os.path.join(venv_dir, '*', '*', '**', 'pep8*')):181 for path in glob.glob(globmatch):182 self.assertEqual(183 uinfo.pw_uid, os.stat(path).st_uid184 )185 finally:186 if os.path.isdir(venv_dir):187 shutil.rmtree(venv_dir)188 # Using a requirements file189 venv_create = self.run_function(190 'virtualenv.create', [venv_dir], runas=username191 )192 if venv_create['retcode'] > 0:193 self.skipTest(194 'Failed to create testcase virtual environment: {0}'.format(195 ret196 )197 )198 req_filename = os.path.join(199 integration.TMP_STATE_TREE, 'issue-6912-requirements.txt'200 )201 with salt.utils.fopen(req_filename, 'wb') as reqf:202 reqf.write('pep8')203 try:204 ret = self.run_state(205 'pip.installed', name='', runas=username, bin_env=venv_dir,206 requirements='salt://issue-6912-requirements.txt'207 )208 self.assertSaltTrueReturn(ret)209 uinfo = pwd.getpwnam(username)210 for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),211 os.path.join(venv_dir, '*', '**', 'pep8*'),212 os.path.join(venv_dir, '*', '*', '**', 'pep8*')):213 for path in glob.glob(globmatch):214 self.assertEqual(215 uinfo.pw_uid, os.stat(path).st_uid216 )217 finally:218 if os.path.isdir(venv_dir):219 shutil.rmtree(venv_dir)220 os.unlink(req_filename)221 # <---- Using runas --------------------------------------------------222 # ----- Using user -------------------------------------------------->223 venv_create = self.run_function(224 'virtualenv.create', [venv_dir], runas=username225 )226 if venv_create['retcode'] > 0:227 self.skipTest(228 'Failed to create testcase virtual environment: {0}'.format(229 ret230 )231 )232 # Using the package name233 try:234 ret = self.run_state(235 'pip.installed', name='pep8', user=username, bin_env=venv_dir236 )237 self.assertSaltTrueReturn(ret)238 uinfo = pwd.getpwnam(username)239 for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),240 os.path.join(venv_dir, '*', '**', 'pep8*'),241 os.path.join(venv_dir, '*', '*', '**', 'pep8*')):242 for path in glob.glob(globmatch):243 self.assertEqual(244 uinfo.pw_uid, os.stat(path).st_uid245 )246 finally:247 if os.path.isdir(venv_dir):248 shutil.rmtree(venv_dir)249 # Using a requirements file250 venv_create = self.run_function(251 'virtualenv.create', [venv_dir], runas=username252 )253 if venv_create['retcode'] > 0:254 self.skipTest(255 'Failed to create testcase virtual environment: {0}'.format(256 ret257 )258 )259 req_filename = os.path.join(260 integration.TMP_STATE_TREE, 'issue-6912-requirements.txt'261 )262 with salt.utils.fopen(req_filename, 'wb') as reqf:263 reqf.write('pep8')264 try:265 ret = self.run_state(266 'pip.installed', name='', user=username, bin_env=venv_dir,267 requirements='salt://issue-6912-requirements.txt'268 )269 self.assertSaltTrueReturn(ret)270 uinfo = pwd.getpwnam(username)271 for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),272 os.path.join(venv_dir, '*', '**', 'pep8*'),273 os.path.join(venv_dir, '*', '*', '**', 'pep8*')):274 for path in glob.glob(globmatch):275 self.assertEqual(276 uinfo.pw_uid, os.stat(path).st_uid277 )278 finally:279 if os.path.isdir(venv_dir):280 shutil.rmtree(venv_dir)281 os.unlink(req_filename)282 # <---- Using user ---------------------------------------------------283 def test_issue_6833_pip_upgrade_pip(self):284 # Create the testing virtualenv285 venv_dir = os.path.join(286 integration.TMP, '6833-pip-upgrade-pip'287 )288 ret = self.run_function('virtualenv.create', [venv_dir])289 try:290 try:291 self.assertEqual(ret['retcode'], 0)292 self.assertIn(293 'New python executable',294 ret['stdout']295 )296 except AssertionError:297 import pprint298 pprint.pprint(ret)299 raise300 # Let's install a fixed version pip over whatever pip was301 # previously installed302 ret = self.run_function(303 'pip.install', ['pip==1.3.1'], upgrade=True,304 ignore_installed=True,305 bin_env=venv_dir306 )307 try:308 self.assertEqual(ret['retcode'], 0)309 self.assertIn(310 'Successfully installed pip',311 ret['stdout']312 )313 except AssertionError:314 import pprint315 pprint.pprint(ret)316 raise317 # Le't make sure we have pip 1.3.1 installed318 self.assertEqual(319 self.run_function('pip.list', ['pip'], bin_env=venv_dir),320 {'pip': '1.3.1'}321 )322 # Now the actual pip upgrade pip test323 ret = self.run_state(324 'pip.installed', name='pip==1.4.1', upgrade=True,325 bin_env=venv_dir326 )327 try:328 self.assertSaltTrueReturn(ret)329 self.assertInSaltReturn(330 'Installed',331 ret,332 ['changes', 'pip==1.4.1']333 )334 except AssertionError:335 import pprint336 pprint.pprint(ret)337 raise338 finally:339 if os.path.isdir(venv_dir):340 shutil.rmtree(venv_dir)341 def test_pip_installed_specific_env(self):342 # Create the testing virtualenv343 venv_dir = os.path.join(344 integration.TMP, 'pip-installed-specific-env'345 )346 # Let's write a requirements file347 requirements_file = os.path.join(348 integration.TMP_PRODENV_STATE_TREE, 'prod-env-requirements.txt'349 )350 with salt.utils.fopen(requirements_file, 'wb') as reqf:351 reqf.write('pep8\n')352 try:353 ret = self.run_function('virtualenv.create', [venv_dir])354 # The requirements file should not be found the base environment355 ret = self.run_state(356 'pip.installed', name='', bin_env=venv_dir,357 requirements='salt://prod-env-requirements.txt'358 )359 self.assertSaltFalseReturn(ret)360 self.assertInSaltComment(361 "'salt://prod-env-requirements.txt' not found", ret362 )363 # The requirements file must be found in the prod environment364 ret = self.run_state(365 'pip.installed', name='', bin_env=venv_dir, saltenv='prod',366 requirements='salt://prod-env-requirements.txt'367 )368 self.assertSaltTrueReturn(ret)369 self.assertInSaltComment(370 'Successfully processed requirements file '371 'salt://prod-env-requirements.txt', ret372 )373 # We're using the base environment but we're passing the prod374 # environment as an url arg to salt://375 ret = self.run_state(376 'pip.installed', name='', bin_env=venv_dir,377 requirements='salt://prod-env-requirements.txt?env=prod'378 )379 self.assertSaltTrueReturn(ret)380 self.assertInSaltComment(381 'Successfully processed requirements file '382 'salt://prod-env-requirements.txt', ret383 )384 ret = self.run_state(385 'pip.installed', name='', bin_env=venv_dir,386 requirements='salt://prod-env-requirements.txt?saltenv=prod'387 )388 self.assertSaltTrueReturn(ret)389 self.assertInSaltComment(390 'Successfully processed requirements file '391 'salt://prod-env-requirements.txt', ret392 )393 finally:394 if os.path.isdir(venv_dir):395 shutil.rmtree(venv_dir)396 if os.path.isfile(requirements_file):397 os.unlink(requirements_file)398if __name__ == '__main__':399 from integration import run_tests...

Full Screen

Full Screen

fabfile.py

Source:fabfile.py Github

copy

Full Screen

1# coding=utf-82# filename: fabfile.py3"""4Fabric自动化部署Flask应用5把该文件放在Flask app同级文件夹即可。6Python3 install: pip3 install fabric37Windows下tar打包可以使用Cygwin,并安装tar命令8Cygwin官网:http://www.cygwin.com/9- 查看所有fab命令:10 fab -l 11- 第一次上传代码:12 fab push13 fab updatedep14 fab updateenv15- 更新代码:16 fab push17- 部署到Supervisor:18 fab deploy_sup19- 部署到Nginx:20 fab deploy_nginx21 OR22 fab deploy_nginx:1 # 部署并重启23"""24import os25import time26import platform27from fabric.api import *28from fabric.contrib.console import confirm29nowTime = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))30##########################################################################31# 配置32# 主机参数33# 登录用用户名、密码和主机:34env.hosts = ['127.0.0.1'] # 如果有多个主机,fabric会自动依次部署35env.user = 'root'36#env.password = ""37# 打包文件名38APP_NAME = "app"39TAR_PACK = APP_NAME + "-" + nowTime + ".tar.gz" # app_20180123150715.tar.gz40TAR_PACK_RM = APP_NAME + "-*.tar.gz" # 匹配旧的压缩包41# 远程服务器文件夹42REMOTE_DIR = APP_NAME43REMOTE_TAR = REMOTE_DIR + "/" + TAR_PACK44# 依赖文件45REQUIREMENTS_FILE = "requirements.txt"46# 需要打包的文件和文件夹47#TAR_FILES = ['*.py', 'static/*', 'templates/*', 'favicon.ico']48TAR_FILES = ['*.py', "*.ini", "*.sh", "*.conf",49 REQUIREMENTS_FILE, "app/*", "deploy/*"]50# 虚拟环境文件夹名称51VENV_DIR = "venv"52# 部署参数53DEPLOY_DIR = REMOTE_DIR + "/deploy"54# supervisor配置文件55SUPERVISOR_CONFIG_FILE = "supervisor_config_app.ini"56# 部署到Supervisor文件夹的配置文件名57SUPERVISOR_DEPLOY_NAME = "supervisor_config_" + APP_NAME + ".ini"58# 服务器放置Supervisor配置文件的文件夹路径59SUPERVISOR_PATH = "/etc/supervisord.d/"60# app的Nginx配置文件61NGINX_CONFIG_FILE = "nginx_config_app.conf"62NGINX_DEPLOY_NAME = "nginx_config_" + APP_NAME + ".conf"63NGINX_PATH = "/etc/nginx/conf.d/"64##########################################################################65def pack(pack_name=TAR_PACK):66 """67 本地代码打包68 """69 with settings(warn_only=True): # 用来忽略某些不存在的文件70 local("rm -f " + TAR_PACK_RM) # 首先删除旧的tar包71 local("tar -cvzf " + pack_name +72 " --exclude='*.tar.gz' --exclude='fabfile.py' --exclude=*.pyc %s" %73 ' '.join(TAR_FILES)74 )75def upload(pack_name=TAR_PACK):76 """77 本地代码压缩包上传78 """79 # 检查远程服务器文件夹80 ret = run("ls")81 if REMOTE_DIR not in ret:82 # mkdir(REMOTE_DIR)83 run("mkdir %s" % REMOTE_DIR)84 # 进入工程目录85 with cd(REMOTE_DIR):86 # 删除远程服务器tar包87 with settings(warn_only=True):88 #run('rm -f %s' % TAR_PACK)89 run('rm -f %s' % TAR_PACK_RM)90 # 上传tar文件至远程服务器91 put(TAR_PACK, TAR_PACK)92 # 解压93 with settings(warn_only=True):94 run("tar -xvzf %s" % TAR_PACK)95 # 改变权限96 run("chmod 775 -R ./*")97def push(venv_dir=VENV_DIR, pack_name=TAR_PACK):98 """99 本地代码打包并上传100 """101 # updatedep(venv_dir)102 pack(pack_name)103 upload(pack_name)104def createenv(venv_dir=VENV_DIR):105 """106 远程服务器创建虚拟环境107 """108 # 进入工程目录109 with cd(REMOTE_DIR):110 # 检查文件夹111 ls = run("ls")112 if venv_dir in ls:113 ret = confirm(venv_dir + "文件夹存在,是否删除重建?")114 if ret:115 run("rm -r ./" + venv_dir)116 run("python3 -m venv ./" + venv_dir)117 # 检测虚拟环境下的pip3118 run("./" + venv_dir + "/bin/pip3 -V")119def updateenv(venv_dir=VENV_DIR):120 """121 更新远程服务器虚拟环境(如果venv不存在则询问是否创建)122 """123 with cd(REMOTE_DIR):124 # 检查文件夹125 ls = run("ls")126 # 判断服务器端虚拟环境是否存在127 if venv_dir not in ls:128 ret = confirm(venv_dir + "不存在,是否创建?")129 if not ret:130 return # 不创建则退出131 run("python3 -m venv ./" + venv_dir)132 # 检测虚拟环境下的pip3133 run("./" + venv_dir + "/bin/pip3 -V")134 # 更新本地依赖文件135 updatedep(venv_dir)136 # 上传依赖文件137 put(REQUIREMENTS_FILE, REQUIREMENTS_FILE)138 run("./" + venv_dir + "/bin/pip3 install -r " + REQUIREMENTS_FILE)139def updatedep(venv_dir=VENV_DIR):140 """141 更新本地依赖文件142 """143 if "Windows" == platform.system():144 pip = os.path.join(".", venv_dir, "Scripts", "pip.exe")145 ret = os.path.exists(pip)146 if not ret:147 print("pip不存在!")148 return149 local(pip + " -V") # 验证pip路径150 local(pip + " freeze > " + REQUIREMENTS_FILE)151def deploy_sup(option="cp"):152 """153 部署app到Supervisor154 """155 with cd(DEPLOY_DIR):156 # 检查文件157 ls = run("ls")158 if SUPERVISOR_CONFIG_FILE not in ls:159 print(SUPERVISOR_CONFIG_FILE + "文件不存在!")160 return161 deploy_path = os.path.join(SUPERVISOR_PATH, SUPERVISOR_DEPLOY_NAME)162 sudo("rm -f " + deploy_path)163 if "cp" == option:164 sudo("cp " + SUPERVISOR_CONFIG_FILE + " " + deploy_path)165 elif "ln" == option:166 sudo("ln -s " + SUPERVISOR_CONFIG_FILE + " " + deploy_path)167 # Supervisor加载新的配置168 sudo("supervisorctl update")169def deploy_nginx(reload=False):170 """171 部署app到nginx172 """173 with cd(DEPLOY_DIR):174 # 检查文件175 ls = run("ls")176 if NGINX_CONFIG_FILE not in ls:177 print(NGINX_CONFIG_FILE + "文件不存在!")178 return179 deploy_path = os.path.join(NGINX_PATH, NGINX_DEPLOY_NAME)180 sudo("rm -f " + deploy_path)181 sudo("cp " + NGINX_CONFIG_FILE + " " + deploy_path)182 ret = sudo("sudo nginx -t") # 检查Nginx的配置183 if reload and ("ok" in ret):184 sudo("service nginx reload")185def deploy(mod):186 """187 部署188 """189 if "sup" == mod:190 deploy_sup()191 elif "nginx" == mod:192 deploy_nginx()193def clean():194 with settings(warn_only=True):...

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 localstack 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