def run_setup(self): if self.setup_py is not None and self.setup_py.exists(): target_cwd = self.setup_py.parent.as_posix() with cd(target_cwd), _suppress_distutils_logs(): script_name = self.setup_py.as_posix() args = ["egg_info", "--egg-base", self.base_dir] g = {"__file__": script_name, "__name__": "__main__"} local_dict = {} if sys.version_info < (3, 5): save_argv = sys.argv else: save_argv = sys.argv.copy() # This is for you, Hynek # see https://github.com/hynek/environ_config/blob/69b1c8a/setup.py try: global _setup_distribution, _setup_stop_after _setup_stop_after = "run" sys.argv[0] = script_name sys.argv[1:] = args with open(script_name, 'rb') as f: if sys.version_info < (3, 5): exec(f.read(), g, local_dict) else: exec(f.read(), g) # We couldn't import everything needed to run setup except NameError: python = os.environ.get('PIP_PYTHON_PATH', sys.executable) out, _ = run([python, "setup.py"] + args, cwd=target_cwd, block=True, combine_stderr=False, return_object=False, nospin=True) finally: _setup_stop_after = None sys.argv = save_argv dist = _setup_distribution if not dist: self.get_egg_metadata() return name = dist.get_name() if name: self.name = name if dist.python_requires and not self.python_requires: self.python_requires = packaging.specifiers.SpecifierSet( dist.python_requires ) if dist.extras_require and not self.extras: self.extras = dist.extras_require install_requires = dist.get_requires() if not install_requires: install_requires = dist.install_requires if install_requires and not self.requires: requirements = [init_requirement(req) for req in install_requires] self.requires.update({req.key: req for req in requirements}) if dist.setup_requires and not self.setup_requires: self.setup_requires = dist.setup_requires if not self.version: self.version = dist.get_version()
def get_info(self): # type: () -> Dict[S, Any] if self.setup_cfg and self.setup_cfg.exists(): with cd(self.base_dir): self.parse_setup_cfg() with cd(self.base_dir): self.run_pyproject() self.build() if self.setup_py and self.setup_py.exists() and self.metadata is None: if not self.requires or not self.name: try: with cd(self.base_dir): self.run_setup() except Exception: with cd(self.base_dir): self.get_egg_metadata() if self.metadata is None or not self.name: with cd(self.base_dir): self.get_egg_metadata() return self.as_dict()
def test_lockfile_requirements(pathlib_tmpdir): lockfile = pathlib_tmpdir.joinpath("Pipfile.lock") lockfile.write_text( textwrap.dedent(u""" { "_meta": { "hash": { "sha256": "88d256c1798cc297772ecd3d2152013e0b28201a5364a1c0f8e4dde79b6e200c" }, "pipfile-spec": 6, "requires": {}, "sources": [ { "name": "pypi", "url": "https://pypi.org/simple", "verify_ssl": true } ] }, "default": {}, "develop": { "alabaster": { "hashes": [ "sha256:674bb3bab080f598371f4443c5008cbfeb1a5e622dd312395d2d82af2c54c456", "sha256:b63b1f4dc77c074d386752ec4a8a7517600f6c0db8cd42980cae17ab7b3275d7" ], "version": "==0.7.11" }, "apipkg": { "hashes": [ "sha256:37228cda29411948b422fae072f57e31d3396d2ee1c9783775980ee9c9990af6", "sha256:58587dd4dc3daefad0487f6d9ae32b4542b185e1c36db6993290e7c41ca2b47c" ], "markers": "python_version >= '2.7' and python_version != '3.0.*' and python_version != '3.1.*' and python_version != '3.2.*' and python_version != '3.3.*'", "version": "==1.5" }, "appdirs": { "hashes": [ "sha256:9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92", "sha256:d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e" ], "version": "==1.4.3" }, "argparse": { "hashes": [ "sha256:62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4", "sha256:c31647edb69fd3d465a847ea3157d37bed1f95f19760b11a47aa91c04b666314" ], "markers": "python_version == '2.6'", "version": "==1.4.0" }, "certifi": { "hashes": [ "sha256:376690d6f16d32f9d1fe8932551d80b23e9d393a8578c5633a2ed39a64861638", "sha256:456048c7e371c089d0a77a5212fb37a2c2dce1e24146e3b7e0261736aaeaa22a" ], "version": "==2018.8.24" }, "chardet": { "hashes": [ "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" ], "version": "==3.0.4" }, "requests": { "path": ".", "editable": true } } } """.strip())) loaded = Lockfile.load(lockfile.as_posix()) dump_to = pathlib_tmpdir.joinpath("new_lockfile") dump_to.mkdir() from_data = Lockfile.from_data(dump_to.as_posix(), json.loads(lockfile.read_text()), meta_from_project=False) assert isinstance(loaded.dev_requirements[0], Requirement) assert isinstance(loaded.dev_requirements_list[0], dict) with cd(pathlib_tmpdir.as_posix()): auto_detected_path = Lockfile() assert (auto_detected_path.path.absolute().as_posix() == lockfile.absolute().as_posix()) assert auto_detected_path["develop-editable"]["requests"] is not None
def get_dependencies_from_index(dep, sources=None, pip_options=None, wheel_cache=None): """Retrieves dependencies for the given install requirement from the pip resolver. :param dep: A single InstallRequirement :type dep: :class:`~pip._internal.req.req_install.InstallRequirement` :param sources: Pipfile-formatted sources, defaults to None :type sources: list[dict], optional :return: A set of dependency lines for generating new InstallRequirements. :rtype: set(str) or None """ finder = get_finder(sources=sources, pip_options=pip_options) if not wheel_cache: wheel_cache = WHEEL_CACHE dep.is_direct = True reqset = RequirementSet() reqset.add_requirement(dep) requirements = None setup_requires = {} with temp_environ(), start_resolver(finder=finder, wheel_cache=wheel_cache) as resolver: os.environ['PIP_EXISTS_ACTION'] = 'i' dist = None if dep.editable and not dep.prepared and not dep.req: with cd(dep.setup_py_dir): from setuptools.dist import distutils try: dist = distutils.core.run_setup(dep.setup_py) except (ImportError, TypeError, AttributeError): dist = None else: setup_requires[dist.get_name()] = dist.setup_requires if not dist: try: dist = dep.get_dist() except (TypeError, ValueError, AttributeError): pass else: setup_requires[dist.get_name()] = dist.setup_requires resolver.require_hashes = False try: results = resolver._resolve_one(reqset, dep) except Exception: # FIXME: Needs to bubble the exception somehow to the user. results = [] finally: try: wheel_cache.cleanup() except AttributeError: pass resolver_requires_python = getattr(resolver, "requires_python", None) requires_python = getattr(reqset, "requires_python", resolver_requires_python) if requires_python: add_marker = fix_requires_python_marker(requires_python) reqset.remove(dep) if dep.req.marker: dep.req.marker._markers.extend([ 'and', ].extend(add_marker._markers)) else: dep.req.marker = add_marker reqset.add(dep) requirements = set() for r in results: if requires_python: if r.req.marker: r.req.marker._markers.extend([ 'and', ].extend(add_marker._markers)) else: r.req.marker = add_marker requirements.add(format_requirement(r)) for section in setup_requires: python_version = section not_python = not is_python(section) # This is for cleaning up :extras: formatted markers # by adding them to the results of the resolver # since any such extra would have been returned as a result anyway for value in setup_requires[section]: # This is a marker. if is_python(section): python_version = value[1:-1] else: not_python = True if ':' not in value and not_python: try: requirement_str = "{0}{1}".format( value, python_version).replace(":", ";") requirements.add( format_requirement( make_install_requirement( requirement_str).ireq)) # Anything could go wrong here -- can't be too careful. except Exception: pass if not dep.editable and is_pinned_requirement( dep) and requirements is not None: DEPENDENCY_CACHE[dep] = list(requirements) return requirements
def run_setup(self): # type: () -> None if self.setup_py is not None and self.setup_py.exists(): target_cwd = self.setup_py.parent.as_posix() with temp_path(), cd(target_cwd), _suppress_distutils_logs(): # This is for you, Hynek # see https://github.com/hynek/environ_config/blob/69b1c8a/setup.py script_name = self.setup_py.as_posix() args = ["egg_info", "--egg-base", self.egg_base] g = {"__file__": script_name, "__name__": "__main__"} sys.path.insert(0, os.path.dirname(os.path.abspath(script_name))) local_dict = {} if sys.version_info < (3, 5): save_argv = sys.argv else: save_argv = sys.argv.copy() try: global _setup_distribution, _setup_stop_after _setup_stop_after = "run" sys.argv[0] = script_name sys.argv[1:] = args with open(script_name, "rb") as f: if sys.version_info < (3, 5): exec(f.read(), g, local_dict) else: exec(f.read(), g) # We couldn't import everything needed to run setup except NameError: python = os.environ.get("PIP_PYTHON_PATH", sys.executable) out, _ = run( [python, "setup.py"] + args, cwd=target_cwd, block=True, combine_stderr=False, return_object=False, nospin=True, ) finally: _setup_stop_after = None sys.argv = save_argv dist = _setup_distribution if not dist: self.get_egg_metadata() return name = dist.get_name() if name: self.name = name if dist.python_requires and not self.python_requires: self.python_requires = packaging.specifiers.SpecifierSet( dist.python_requires) if not self._extras_requirements: self._extras_requirements = () if dist.extras_require and not self.extras: for extra, extra_requires in dist.extras_require: extras_tuple = tuple( BaseRequirement.from_req(req) for req in extra_requires) self._extras_requirements += ((extra, extras_tuple), ) install_requires = dist.get_requires() if not install_requires: install_requires = dist.install_requires if install_requires and not self.requires: requirements = set([ BaseRequirement.from_req(req) for req in install_requires ]) if getattr(self.ireq, "extras", None): for extra in self.ireq.extras: requirements |= set( list(self.extras.get(extra, []))) self._requirements = frozenset( set(self._requirements) | requirements) if dist.setup_requires and not self.setup_requires: self.setup_requires = tuple(dist.setup_requires) if not self.version: self.version = dist.get_version()