def get_requirement_info_by_name(self, req, requirements_by_name=dict()): """ Given a requirement, it provides its valid information to be included in the final file """ if self.use_uniform_requirements: # If it is a repo and not a package, this will be True if req.link: requirement_name = parse_requirement(_get_valid_link_req(req.req)).name else: requirement_name = parse_requirement(str(req.req)).name # If the requirement is not in the dict, it is because it was not needed as a dependency in the original # set containing all requirements if requirement_name in requirements_by_name.keys(): # In case we want to show the source for inherited dependencies if self.show_comes_from_info and type(req.comes_from) == InstallRequirement: return "{}\t\t\t# From: {}".format(requirements_by_name[requirement_name].requirement, str(req.comes_from.req)) else: return requirements_by_name[requirement_name].requirement else: return None else: # If not using uniform versions, we just need to provide the information based on wether it is # a repository or a package if self.show_comes_from_info and type(req.comes_from) == InstallRequirement: return "{}\t\t\t# From: {}".format(format_requirement(req) if not req.link else str(req.req), str(req.comes_from.req)) else: return format_requirement(req) if not req.link else str(req.req)
def check_duplicate_dependencies_nonuniform(self, extra, resolver): """ Checks there are not duplicate dependencies, when use_uniform_requirements==False """ requirements_by_name = {} for req in resolver.resolve(max_rounds=10): requirements_by_name[parse_requirement(str(req.req)).name] = SimpleNamespace( requirement=format_requirement(req).strip().replace(" ", ""), url=req.link ) self._check_duplicate_dependencies_by_extra(extra, requirements_by_name)
def test_format_requirement_ireq_with_hashes(from_line): ireq = from_line("pytz==2017.2") ireq_hashes = [ "sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67", "sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589", ] expected = ("pytz==2017.2 \\\n" " --hash=sha256:d1d6729c85acea542367138286" "8627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n" " --hash=sha256:f5c056e8f62d45ba8215e5cb8f5" "0dfccb198b4b9fbea8500674f3443e4689589") assert format_requirement(ireq, hashes=ireq_hashes) == expected
def test_format_requirement_ireq_with_hashes_and_markers(from_line): ireq = from_line('pytz==2017.2') marker = 'python_version<"3.0"' ireq_hashes = [ 'sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67', 'sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589', ] expected = ( 'pytz==2017.2 ; python_version<"3.0" \\\n' ' --hash=sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n' ' --hash=sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589' ) assert format_requirement(ireq, marker, hashes=ireq_hashes) == expected
def test_format_requirement_ireq_with_hashes(from_line): ireq = from_line("pytz==2017.2") ireq_hashes = [ "sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67", "sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589", ] expected = ( "pytz==2017.2 \\\n" " --hash=sha256:d1d6729c85acea542367138286" "8627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n" " --hash=sha256:f5c056e8f62d45ba8215e5cb8f5" "0dfccb198b4b9fbea8500674f3443e4689589" ) assert format_requirement(ireq, hashes=ireq_hashes) == expected
def on_end(self, event): # Our config object python_config = event.config["python"] # Pip / PyPI pip_command = get_pip_command() pip_options, _ = pip_command.parse_args([]) session = pip_command._build_session(pip_options) repository = PyPIRepository(pip_options, session) for extra in itertools.chain((None, ), python_config.get_extras()): requirements_file = "requirements{}.txt".format( "-" + extra if extra else "") if python_config.override_requirements or not os.path.exists( requirements_file): tmpfile = tempfile.NamedTemporaryFile(mode="wt", delete=False) if extra: tmpfile.write("\n".join( python_config.get_requirements(extra=extra))) else: tmpfile.write("\n".join(python_config.get_requirements())) tmpfile.flush() constraints = list( parse_requirements(tmpfile.name, finder=repository.finder, session=repository.session, options=pip_options)) resolver = Resolver(constraints, repository, prereleases=False, clear_caches=False, allow_unsafe=False) self.render_file_inline( requirements_file, "\n".join(( "-e .{}".format("[" + extra + "]" if extra else ""), *(("-r requirements.txt", ) if extra else ()), *python_config.get_vendors(extra=extra), *sorted( format_requirement(req) for req in resolver.resolve(max_rounds=10) if req.name != python_config.get("name")), )), override=python_config.override_requirements, )
def on_end(self, event): # Our config object python_config = event.config["python"] # Pip / PyPI pip_command = get_pip_command() pip_options, _ = pip_command.parse_args([]) session = pip_command._build_session(pip_options) repository = PyPIRepository(pip_options, session) for extra in itertools.chain((None,), python_config.get_extras()): requirements_file = "requirements{}.txt".format("-" + extra if extra else "") if python_config.override_requirements or not os.path.exists(requirements_file): tmpfile = tempfile.NamedTemporaryFile(mode="wt", delete=False) if extra: tmpfile.write("\n".join(python_config.get_requirements(extra=extra))) else: tmpfile.write("\n".join(python_config.get_requirements())) tmpfile.flush() constraints = list( parse_requirements( tmpfile.name, finder=repository.finder, session=repository.session, options=pip_options ) ) resolver = Resolver(constraints, repository, prereleases=False, clear_caches=False, allow_unsafe=False) self.render_file_inline( requirements_file, "\n".join( ( "-e .{}".format("[" + extra + "]" if extra else ""), *(("-r requirements.txt",) if extra else ()), *python_config.get_vendors(extra=extra), *sorted( format_requirement(req) for req in resolver.resolve(max_rounds=10) if req.name != python_config.get("name") ), ) ), override=python_config.override_requirements, )
def on_end(self, event): # Our config object python_config = event.config['python'] # Pip / PyPI pip_command = get_pip_command() pip_options, _ = pip_command.parse_args([]) session = pip_command._build_session(pip_options) repository = PyPIRepository(pip_options, session) for extra in itertools.chain((None, ), python_config.get_extras()): tmpfile = tempfile.NamedTemporaryFile(mode='wt', delete=False) if extra: tmpfile.write('\n'.join( python_config.get_requirements(extra=extra))) else: tmpfile.write('\n'.join(python_config.get_requirements())) tmpfile.flush() constraints = list( parse_requirements(tmpfile.name, finder=repository.finder, session=repository.session, options=pip_options)) resolver = Resolver(constraints, repository, prereleases=False, clear_caches=False, allow_unsafe=False) self.render_file_inline( 'requirements{}.txt'.format('-' + extra if extra else ''), '\n'.join(( '-e .{}'.format('[' + extra + ']' if extra else ''), *(('-r requirements.txt', ) if extra else ()), *sorted( format_requirement(req) for req in resolver.resolve(max_rounds=10) if req.name != python_config.get('name')), )))
def test_format_requirement_editable(from_editable): ireq = from_editable('git+git://fake.org/x/y.git#egg=y') assert format_requirement(ireq) == '-e git+git://fake.org/x/y.git#egg=y'
def test_format_requirement_url(from_line, line, expected): ireq = from_line(line) assert format_requirement(ireq) == expected
def test_format_requirement_editable_local_path(from_editable): ireq = from_editable("file:///home/user/package") assert format_requirement(ireq) == "-e file:///home/user/package"
def test_format_requirement_editable_vcs_with_password(from_editable): ireq = from_editable("git+git://user:[email protected]/x/y.git#egg=y") assert (format_requirement(ireq) == "-e git+git://user:[email protected]/x/y.git#egg=y")
def test_format_requirement_editable_vcs(from_editable): ireq = from_editable("git+git://fake.org/x/y.git#egg=y") assert format_requirement(ireq) == "-e git+git://fake.org/x/y.git#egg=y"
def test_format_requirement_url(from_line): ireq = from_line("https://example.com/example.zip") assert format_requirement(ireq) == "https://example.com/example.zip"
def test_format_requirement(from_line): ireq = from_line("test==1.2") assert format_requirement(ireq) == "test==1.2"
def test_format_requirement(from_line): ireq = from_line('test==1.2') assert format_requirement(ireq) == 'test==1.2'
def test_format_requirement(from_line): ireq = from_line('test==1.2') assert format_requirement(ireq) == 'test==1.2' assert format_requirement(ireq, include_specifier=False) == 'test'
def on_end(self, event): # Our config object python_config = event.config["python"] # Pip / PyPI repository = PyPIRepository([], cache_dir=CACHE_DIR) # We just need to construct this structure if use_uniform_requirements == True requirements_by_name = {} if python_config.use_uniform_requirements: tmpfile = tempfile.NamedTemporaryFile(mode="wt", delete=False) for extra in itertools.chain((None,), python_config.get_extras()): tmpfile.write("\n".join(python_config.get_requirements(extra=extra)) + "\n") tmpfile.flush() constraints = list( parse_requirements( tmpfile.name, finder=repository.finder, session=repository.session, options=repository.options ) ) # This resolver is able to evaluate ALL the dependencies along the extras resolver = Resolver( constraints, repository, cache=DependencyCache(CACHE_DIR), # cache=DependencyCache(tempfile.tempdir), prereleases=False, clear_caches=False, allow_unsafe=False, ) for req in resolver.resolve(max_rounds=10): requirements_by_name[parse_requirement(str(req.req)).name] = SimpleNamespace( requirement=format_requirement(req).strip().replace(" ", ""), url=req.link ) python_config.check_duplicate_dependencies_uniform(requirements_by_name) # Now it iterates along the versions in extras and looks for the requirements and its dependencies, using the # structure created above to select the unified versions (unless the flag indicates otherwise). for extra in itertools.chain((None,), python_config.get_extras()): requirements_file = "requirements{}.txt".format("-" + extra if extra else "") if python_config.override_requirements or not os.path.exists(requirements_file): tmpfile = tempfile.NamedTemporaryFile(mode="wt", delete=False) tmpfile.write("\n".join(python_config.get_requirements(extra=extra)) + "\n") tmpfile.flush() constraints = list( parse_requirements( tmpfile.name, finder=repository.finder, session=repository.session, options=repository.options ) ) resolver = Resolver( constraints, repository, cache=DependencyCache(CACHE_DIR), prereleases=False, clear_caches=False, allow_unsafe=False, ) if not python_config.use_uniform_requirements: python_config.check_duplicate_dependencies_nonuniform(extra, resolver) requirements_list = [] for req in resolver.resolve(max_rounds=10): if req.name != python_config.get("name"): requirement = python_config.get_requirement_info_by_name(req, requirements_by_name) if requirement: requirements_list.append(requirement) self.render_file_inline( requirements_file, "\n".join( ( "-e .{}".format("[" + extra + "]" if extra else ""), *(("-r requirements.txt",) if extra else ()), *python_config.get_vendors(extra=extra), *sorted(requirements_list), ) ), override=python_config.override_requirements, ) # Updates setup file setup = python_config.get_setup() context = { "url": setup.pop("url", ""), "download_url": setup.pop("download_url", ""), } for k, v in context.items(): context[k] = context[k].format(name=setup["name"], user=getuser(), version="{version}") context.update( { "entry_points": setup.pop("entry_points", {}), "extras_require": python_config.get("extras_require"), "install_requires": python_config.get("install_requires"), "python": python_config, "setup": setup, "banner": get_override_warning_banner(), } ) # Render (with overwriting) the allmighty setup.py self.render_file("setup.py", "python/setup.py.j2", context, override=True)