def add(args): print("Adding to requirements...") f = open("requirements.txt", "a") f.write(args.pkg + "\n") f.close() print("Installing...") cmd = InstallCommand("install", "") cmd.main([args.pkg])
def test_missing_hash_with_require_hashes_in_reqs_file(self, data, tmpdir): """--require-hashes in a requirements file should make its way to the RequirementSet. """ req_set = RequirementSet(require_hashes=False) session = PipSession() finder = PackageFinder([data.find_links], [], session=session) command = InstallCommand() with requirements_file('--require-hashes', tmpdir) as reqs_file: options, args = command.parse_args(['-r', reqs_file]) command.populate_requirement_set( req_set, args, options, finder, session, command.name, wheel_cache=None) assert req_set.require_hashes
def test_missing_hash_with_require_hashes_in_reqs_file(self, data, tmpdir): """--require-hashes in a requirements file should make its way to the RequirementSet. """ req_set = RequirementSet(require_hashes=False) session = PipSession() finder = PackageFinder([data.find_links], [], session=session) command = InstallCommand() with requirements_file('--require-hashes', tmpdir) as reqs_file: options, args = command.parse_args(['-r', reqs_file]) command.populate_requirement_set( req_set, args, options, finder, session, command.name, wheel_cache=None, ) assert req_set.require_hashes
def preparer(finder): session = PipSession() rc = InstallCommand("x", "y") o = rc.parse_args([]) with global_tempdir_manager(): with TempDirectory() as tmp: with get_requirement_tracker() as tracker: preparer = RequirementCommand.make_requirement_preparer( tmp, options=o[0], req_tracker=tracker, session=session, finder=finder, use_user_site=False) yield preparer
def install_pip(): temp_dir = tempfile.gettempdir() wheel_fn = [] for wheel in wheels: fn = os.path.join(temp_dir, wheel) print(f'{wheels_base + wheel} => {fn}') urllib.request.urlretrieve(wheels_base + wheel, fn) wheel_fn.append(fn) install_cmd = InstallCommand('install', 'install packages') if install_cmd.main(wheel_fn) or install_cmd.main(['-r', 'requirements.txt']): print('Failed to execute pip install.') exit(1) for wheel in wheel_fn: print(f'Removing {wheel}') os.unlink(wheel)
def preparer(finder: PackageFinder) -> Iterator[RequirementPreparer]: session = PipSession() rc = InstallCommand("x", "y") o = rc.parse_args([]) with global_tempdir_manager(): with TempDirectory() as tmp: with get_build_tracker() as tracker: preparer = RequirementCommand.make_requirement_preparer( tmp, options=o[0], build_tracker=tracker, session=session, finder=finder, use_user_site=False, verbosity=0, ) yield preparer
def run(self): if os.path.exists(self.bdist_base): shutil.rmtree(self.bdist_base) os.makedirs(self.bdist_base) if os.path.exists(self.assembly_dir): shutil.rmtree(self.assembly_dir) dist_dir = os.path.join(self.bdist_base, 'pyassembly') # install deps, if needed if os.path.exists(self.requirements_file): with tempfile.NamedTemporaryFile(mode='w+') as tf, open( self.requirements_file) as f: for ln in f: ln = ln.lstrip() if not (ln.startswith("#") or ln.startswith("pyassembly")): tf.write(ln) tf.flush() try: install_command = InstallCommand('install', 'Install packages.', isolated=False) except TypeError: # pip < 20 install_command = InstallCommand(isolated=False) install_command.main(args=['-r', tf.name, '-t', dist_dir]) bdist_egg = self.distribution.get_command_obj( 'bdist_egg') # type: Command bdist_egg.bdist_dir = dist_dir bdist_egg.dist_dir = self.assembly_dir bdist_egg.keep_temp = 1 # wtf?, it is True or False and not 1 or 0 bdist_egg.ensure_finalized() if self.destination_format == 'zip': cmd = self.reinitialize_command('build') cmd.build_purelib = dist_dir self.run_command('build') basename = Distribution( None, None, self.distribution.get_name(), self.distribution.get_version(), get_python_version(), self.distribution.has_ext_modules() and get_build_platform()).egg_name() shutil.make_archive(base_name=os.path.join(self.assembly_dir, basename), format='zip', root_dir=dist_dir) else: bdist_egg.run() shutil.rmtree(self.egg_info) shutil.rmtree(self.build_base)
def _repackage_with_requires(zfile, reqfile, extra_args = None): from pip._internal.commands.install import InstallCommand workdir = os.path.dirname(reqfile) default_args = '--root {0} --prefix vendors --ignore-installed -r {1}'.format(workdir, reqfile) logger.info("install to temp dir :%s", workdir) args = default_args.split() + ( extra_args or [] ) InstallCommand().main(args) origzip = os.path.abspath(zfile) assemblezip = os.path.splitext(origzip)[0] + '_with_dependences.zip' shutil.copyfile(origzip, assemblezip ) with zipfile.ZipFile(assemblezip, 'a') as zip: os.chdir(workdir) for root, _, files in os.walk('vendors'): for name in files: filepath = os.path.join(root, name) zip.write(filepath) return assemblezip
def install(params): """ Install third-party Mod """ try: from pip._internal import main as pip_main from pip._internal.commands.install import InstallCommand except ImportError: from pip import main as pip_main from pip.commands.install import InstallCommand params = [param for param in params] options, mod_list = InstallCommand().parse_args(params) mod_list = [mod_name for mod_name in mod_list if mod_name != "."] params = ["install"] + params for mod_name in mod_list: mod_name_index = params.index(mod_name) if mod_name.startswith("rqalpha_mod_sys_"): six.print_('System Mod can not be installed or uninstalled') return if "rqalpha_mod_" in mod_name: lib_name = mod_name else: lib_name = "rqalpha_mod_" + mod_name params[mod_name_index] = lib_name # Install Mod installed_result = pip_main(params) # Export config from rqalpha.utils.config import load_yaml, user_mod_conf_path user_conf = load_yaml(user_mod_conf_path()) if os.path.exists(user_mod_conf_path()) else {'mod': {}} if installed_result == 0: # 如果为0,则说明安装成功 if len(mod_list) == 0: """ 主要是方便 `pip install -e .` 这种方式 本地调试 Mod 使用,需要满足以下条件: 1. `rqalpha mod install -e .` 命令是在对应 自定义 Mod 的根目录下 2. 该 Mod 必须包含 `setup.py` 文件(否则也不能正常的 `pip install -e .` 来安装) 3. 该 Mod 包名必须按照 RQAlpha 的规范来命名,具体规则如下 * 必须以 `rqalpha-mod-` 来开头,比如 `rqalpha-mod-xxx-yyy` * 对应import的库名必须要 `rqalpha_mod_` 来开头,并且需要和包名后半部分一致,但是 `-` 需要替换为 `_`, 比如 `rqalpha_mod_xxx_yyy` """ mod_name = _detect_package_name_from_dir() mod_name = mod_name.replace("-", "_").replace("rqalpha_mod_", "") mod_list.append(mod_name) for mod_name in mod_list: if "rqalpha_mod_" in mod_name: mod_name = mod_name.replace("rqalpha_mod_", "") if "==" in mod_name: mod_name = mod_name.split('==')[0] user_conf['mod'][mod_name] = {} user_conf['mod'][mod_name]['enabled'] = False dump_config(user_mod_conf_path(), user_conf) return installed_result
def install(args): cmd = InstallCommand("install", "") cmd.main(["-r", "requirements.txt"])