def main(params): # initiate the virtualenv, it will be created if it does not exists env = VirtualEnvironment(os.path.join(PATH, VIRTUALENV)) # use main function to extend the application of the script # env.upgrade_all() # env.uninstall() # rest of the code will be executed with regard to the args # passed to the script, this part will not be executed if no # arguments are given if params['--install_all']: env.install('petl') env.install('pysftp') # env.install('pandas') # env.install('matplotlib') # env.install('numpy') # env.install('scipy') # env.install('statsmodels') # env.install('ggplot') env.install('ipython[notebook]') print '\n\nInstalled packages:\n' for package in env.installed_packages: print package if params['--list_all']: for package in env.installed_packages: print package if params['--is_installed']: print env.is_installed(params['--is_installed'])
def __init__(self, module, environmentDirectory, logging): self.environmentDirectory = environmentDirectory self.logging = logging if not environmentDirectory: logging.debug('No virtual environment will be set up for %s', module.__name__) return if not virtualEnvManagerAvailable: logging.error( '%s requires virtualenv, but virtualenvapi is not installed!', module.__name__ ) sys.exit(10) if not os.path.exists(environmentDirectory): try: os.makedirs(environmentDirectory) except Exception as e: logging.error('Could not create virtualenv directory %s: %s', environmentDirectory, e) venv = VirtualEnvironment(environmentDirectory) for requirement in set(module.requirements): logging.debug( 'Checking if %s is already installed in %s', requirement, environmentDirectory ) try: if not venv.is_installed(requirement): logging.debug('%s not installed in %s', requirement, environmentDirectory) venv.install(requirement) logging.debug('%s installed in %s', requirement, environmentDirectory) else: logging.debug('%s already in %s', requirement, environmentDirectory) except: logging.error('Could not install dependency %s in %s', requirement, environmentDirectory)
def install_venv(name, path, requirements, python_path): print('[*] Setting up %s virtualenv...' % log.color('1;34', name)) try: # Convert target path to absolute path. path = os.path.abspath(path) # Make sure that base path exists. if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) # Create new virtualenv if it doesn't exist. if not os.path.exists(path): env = VirtualEnvironment(path, python=python_path) env.open_or_create() # Reopen virtualenv to use the correct Python path env = VirtualEnvironment(path) assert env.path == os.path.abspath(path) # Install requirements for the virtualenv. # TODO: Shebang lines in binaries installed this way # use the path in python_path instead of this virtualenv's, # which I haven't been able to solve yet. A workaround # is just to pip uninstall and then install it back. requirements = get_requirements(requirements) for requirement in requirements: requirement = requirement.split(' ') package, options = requirement[0], requirement[1:] if not env.is_installed(package): print(' Installing %s...' % log.color('1;33', package)) env.install(package, options=options) except AssertionError as e: raise e except VirtualenvCreationException as e: # Could not create virtualenv, which executes `virtualenv` as a subprocess under the hood. log.error('Exception occurred while creating virtualenv: %s' % e) # Check if we can find the virtualenv bin. which = shutil.which('virtualenv') if which is None: log.error( 'Most probable error: Could not locate `virtualenv` in $PATH.') return False log.error('Possible errors:\n' + \ '1. Check the shebang of %s' % which) return False except Exception as e: log.error('Exception (%s) occurred while setting up %s: %s' % (e.__class__, name, e)) traceback.print_exc() return False return True
def example(path=tempfile.mkdtemp('virtualenv.test')): print('Env path', path) env = VirtualEnvironment(path) print('django 1.5 installed?', env.is_installed('django==1.5')) print('mezzanine installed?', env.is_installed('mezzanine')) env.install('mezzanine') print('mezzanine installed?', env.is_installed('mezzanine')) print(env.installed_packages) repo = 'git+git://github.com/sjkingo/django_auth_ldap3.git' pkg = repo.split('/')[-1].replace('.git', '') print('django_auth_ldap3 installed?', env.is_installed(pkg)) env.install(repo) print('django_auth_ldap3 installed?', env.is_installed(pkg)) print(env.installed_packages) env.uninstall('mezzanine') print('mezzanine installed?', env.is_installed('mezzanine')) print(env.installed_packages) pkgs = env.search('requests') print('search for \'requests\':') print(len(pkgs), 'found:') print(pkgs)
def example(path='/tmp/virtualenv.test'): env = VirtualEnvironment(path) print 'django 1.5 installed?', env.is_installed('django==1.5') print 'mezzanine installed?', env.is_installed('mezzanine') env.install('mezzanine') print 'mezzanine installed?', env.is_installed('mezzanine') print env.installed_packages payments_repo = 'git+git://github.com/sjkingo/cartridge-payments.git' print 'cartridge-payments installed?', env.is_installed(payments_repo) env.install(payments_repo) print 'cartridge-payments installed?', env.is_installed(payments_repo) print env.installed_packages env.uninstall('mezzanine') print 'mezzanine installed?', env.is_installed('mezzanine') print env.installed_packages pkgs = env.search('requests') print 'search for \'requests\':' print len(pkgs), 'found:' print pkgs
def example(path=tempfile.mkdtemp('virtualenv.test')): print('Env path', path) env = VirtualEnvironment(path) print('django 1.5 installed?', env.is_installed('django==1.5')) print('mezzanine installed?', env.is_installed('mezzanine')) env.install('mezzanine') print('mezzanine installed?', env.is_installed('mezzanine')) print(env.installed_packages) payments_repo = 'git+git://github.com/sjkingo/cartridge-payments.git' print('cartridge-payments installed?', env.is_installed(payments_repo)) env.install(payments_repo) print('cartridge-payments installed?', env.is_installed(payments_repo)) print(env.installed_packages) env.uninstall('mezzanine') print('mezzanine installed?', env.is_installed('mezzanine')) print(env.installed_packages) pkgs = env.search('requests') print('search for \'requests\':') print(len(pkgs), 'found:') print(pkgs)
def test_install_package_dependencies(self): print("test install package dependencies") test_repo_name = "__testrepo__" repo_dir = get_repo_dir(test_repo_name) if os.path.exists(repo_dir): shutil.rmtree(repo_dir) os.makedirs(repo_dir) self.assertTrue(os.path.isdir(repo_dir)) requirements = ["virtualenv-api==2.1.16"] #, "six"] requirements_file = open(repo_dir + "requirements.txt", "w") requirements_file.write('\n'.join(requirements)) requirements_file.close() install_project_dependencies_into_virtual_env(test_repo_name) self.assertTrue(os.path.isdir(repo_dir + "virtual_env")) # Check that requirements are installed env = VirtualEnvironment(repo_dir + "virtual_env") for req in requirements: self.assertTrue(env.is_installed(req))
class BaseTest(TestCase): env_path = None def setUp(self): self.env_path = self.setup_env() self.virtual_env_obj = VirtualEnvironment(self.env_path) def setup_env(self): env_path = self.env_path if env_path is None: env_path = tempfile.mkdtemp("test_env") virt_env = VirtualEnvironment(env_path) virt_env._create() for pack in packages_for_pre_install: virt_env.install(pack) return env_path def _install_packages(self, packages): for pack in packages: self.virtual_env_obj.install(pack) def _uninstall_packages(self, packages): for pack in packages: self.virtual_env_obj.uninstall(pack) def test_installed(self): for pack in packages_for_pre_install: self.assertTrue(self.virtual_env_obj.is_installed(pack)) self.assertFalse(self.virtual_env_obj.is_installed("".join(random.sample(string.ascii_letters, 30)))) def test_install(self): self._uninstall_packages(packages_for_tests) for pack in packages_for_tests: self.virtual_env_obj.install(pack) self.assertTrue(self.virtual_env_obj.is_installed(pack)) def test_uninstall(self): self._install_packages(packages_for_tests) for pack in packages_for_tests: if pack.endswith(".git"): pack = pack.split("/")[-1].replace(".git", "") self.virtual_env_obj.uninstall(pack) self.assertFalse(self.virtual_env_obj.is_installed(pack)) def test_wheel(self): for pack in packages_for_tests: self.virtual_env_obj.wheel(pack, options=["--wheel-dir=/tmp/wheelhouse"]) self.virtual_env_obj.install(pack, options=["--no-index", "--find-links=/tmp/wheelhouse", "--use-wheel"]) self.assertTrue(self.virtual_env_obj.is_installed(pack)) def test_search(self): pack = packages_for_tests[0].lower() result = self.virtual_env_obj.search(pack) self.assertIsInstance(result, dict) self.assertTrue(bool(result)) if result: self.assertIn(pack, [k.split(" (")[0].lower() for k in result.keys()]) def test_search_names(self): pack = packages_for_tests[0].lower() result = self.virtual_env_obj.search_names(pack) self.assertIsInstance(result, list) self.assertIn(pack, [k.split(" (")[0].lower() for k in result]) def tearDown(self): if os.path.exists(self.env_path) and self.__class__.env_path is None: shutil.rmtree(self.env_path)
class BaseTest(TestCase): env_path = None def setUp(self): self.env_path = self.setup_env() self.virtual_env_obj = VirtualEnvironment(self.env_path) def setup_env(self): env_path = self.env_path if env_path is None: env_path = tempfile.mkdtemp('test_env') virt_env = VirtualEnvironment(env_path) virt_env._create() for pack in packages_for_pre_install: virt_env.install(pack) return env_path def _install_packages(self, packages): for pack in packages: self.virtual_env_obj.install(pack) def _uninstall_packages(self, packages): for pack in packages: self.virtual_env_obj.uninstall(pack) def test_installed(self): for pack in packages_for_pre_install: self.assertTrue(self.virtual_env_obj.is_installed(pack)) self.assertFalse(self.virtual_env_obj.is_installed(''.join(random.sample(string.ascii_letters, 30)))) def test_install(self): self._uninstall_packages(packages_for_tests) for pack in packages_for_tests: self.virtual_env_obj.install(pack) self.assertTrue(self.virtual_env_obj.is_installed(pack)) def test_uninstall(self): self._install_packages(packages_for_tests) for pack in packages_for_tests: self.virtual_env_obj.uninstall(pack) self.assertFalse(self.virtual_env_obj.is_installed(pack)) def test_search(self): pack = packages_for_tests[0].lower() result = self.virtual_env_obj.search(pack) self.assertIsInstance(result, list) self.assertTrue(bool(result)) if result: self.assertTrue(isinstance(result[0], (tuple, list))) self.assertIn(pack, (n.lower() for n in dict(result).keys())) def test_search_names(self): pack = packages_for_tests[0].lower() result = self.virtual_env_obj.search_names(pack) self.assertIsInstance(result, list) self.assertIn(pack, (n.lower() for n in result)) def tearDown(self): if os.path.exists(self.env_path) and self.__class__.env_path is None: shutil.rmtree(self.env_path)
def assert_installed_packages(*packages): from virtualenvapi.manage import VirtualEnvironment venv = VirtualEnvironment(str(workon_home.join(venv_name))) for i_package in packages: assert venv.is_installed(i_package)
from virtualenvapi.manage import VirtualEnvironment env = VirtualEnvironment('.') env.install('pandas') print(env.installed_package_names) print(env.installed_packages) print(env.name) print(env.root) print(env.is_installed('pandas'))
class BaseTest(TestCase): env_path = None def setUp(self): self.env_path = self.setup_env() self.virtual_env_obj = VirtualEnvironment(self.env_path) def setup_env(self): env_path = self.env_path if env_path is None: env_path = tempfile.mkdtemp('test_env') virt_env = VirtualEnvironment(env_path) virt_env._create() for pack in packages_for_pre_install: virt_env.install(pack) return env_path def _install_packages(self, packages): for pack in packages: self.virtual_env_obj.install(pack) def _uninstall_packages(self, packages): for pack in packages: self.virtual_env_obj.uninstall(pack) def test_installed(self): for pack in packages_for_pre_install: self.assertTrue(self.virtual_env_obj.is_installed(pack)) self.assertFalse( self.virtual_env_obj.is_installed(''.join( random.sample(string.ascii_letters, 30)))) def test_install(self): self._uninstall_packages(packages_for_tests) for pack in packages_for_tests: self.virtual_env_obj.install(pack) self.assertTrue(self.virtual_env_obj.is_installed(pack)) def test_uninstall(self): self._install_packages(packages_for_tests) for pack in packages_for_tests: if pack.endswith('.git'): pack = pack.split('/')[-1].replace('.git', '') self.virtual_env_obj.uninstall(pack) self.assertFalse(self.virtual_env_obj.is_installed(pack)) def test_wheel(self): for pack in packages_for_tests: self.virtual_env_obj.wheel(pack, options=['--wheel-dir=/tmp/wheelhouse']) self.virtual_env_obj.install(pack, options=[ '--no-index', '--find-links=/tmp/wheelhouse', '--use-wheel' ]) self.assertTrue(self.virtual_env_obj.is_installed(pack)) def test_search(self): pack = packages_for_tests[0].lower() result = self.virtual_env_obj.search(pack) self.assertIsInstance(result, dict) self.assertTrue(bool(result)) if result: self.assertIn(pack, [k.split(' (')[0].lower() for k in result.keys()]) def test_search_names(self): pack = packages_for_tests[0].lower() result = self.virtual_env_obj.search_names(pack) self.assertIsInstance(result, list) self.assertIn(pack, [k.split(' (')[0].lower() for k in result]) def tearDown(self): if os.path.exists(self.env_path) and self.__class__.env_path is None: shutil.rmtree(self.env_path)