def convert_dependencies( cls, package, dependencies ): # type: ("ProjectPackage", List["Dependency"]) -> Tuple[List[str], Dict[str, List[str]]] main = [] extras = defaultdict(list) req_regex = re.compile(r"^(.+) \((.+)\)$") for dependency in dependencies: if dependency.is_optional(): for extra_name, reqs in package.extras.items(): for req in reqs: if req.name == dependency.name: requirement = to_str( dependency.to_pep_508(with_extras=False) ) if ";" in requirement: requirement, conditions = requirement.split(";") requirement = requirement.strip() if req_regex.match(requirement): requirement = req_regex.sub( "\\1\\2", requirement.strip() ) extras[extra_name + ":" + conditions.strip()].append( requirement ) continue requirement = requirement.strip() if req_regex.match(requirement): requirement = req_regex.sub( "\\1\\2", requirement.strip() ) extras[extra_name].append(requirement) continue requirement = to_str(dependency.to_pep_508()) if ";" in requirement: requirement, conditions = requirement.split(";") requirement = requirement.strip() if req_regex.match(requirement): requirement = req_regex.sub("\\1\\2", requirement.strip()) extras[":" + conditions.strip()].append(requirement) continue requirement = requirement.strip() if req_regex.match(requirement): requirement = req_regex.sub("\\1\\2", requirement.strip()) main.append(requirement) return main, dict(extras)
def test_proper_python_requires_if_three_digits_precision_version_specified(): poetry = Factory().create_poetry(project("single_python")) builder = SdistBuilder(poetry) pkg_info = builder.build_pkg_info() p = Parser() parsed = p.parsestr(to_str(pkg_info)) assert parsed["Requires-Python"] == "==2.7.15"
def test_make_pkg_info_any_python(): poetry = Factory().create_poetry(project("module1")) builder = SdistBuilder(poetry) pkg_info = builder.build_pkg_info() p = Parser() parsed = p.parsestr(to_str(pkg_info)) assert "Requires-Python" not in parsed
def get_metadata_content(self): # type: () -> bytes content = METADATA_BASE.format( name=self._meta.name, version=self._meta.version, summary=to_str(self._meta.summary), ) # Optional fields if self._meta.home_page: content += "Home-page: {}\n".format(self._meta.home_page) if self._meta.license: content += "License: {}\n".format(self._meta.license) if self._meta.keywords: content += "Keywords: {}\n".format(self._meta.keywords) if self._meta.author: content += "Author: {}\n".format(to_str(self._meta.author)) if self._meta.author_email: content += "Author-email: {}\n".format( to_str(self._meta.author_email)) if self._meta.maintainer: content += "Maintainer: {}\n".format(to_str(self._meta.maintainer)) if self._meta.maintainer_email: content += "Maintainer-email: {}\n".format( to_str(self._meta.maintainer_email)) if self._meta.requires_python: content += "Requires-Python: {}\n".format( self._meta.requires_python) for classifier in self._meta.classifiers: content += "Classifier: {}\n".format(classifier) for extra in sorted(self._meta.provides_extra): content += "Provides-Extra: {}\n".format(extra) for dep in sorted(self._meta.requires_dist): content += "Requires-Dist: {}\n".format(dep) for url in sorted(self._meta.project_urls, key=lambda u: u[0]): content += "Project-URL: {}\n".format(to_str(url)) if self._meta.description_content_type: content += "Description-Content-Type: {}\n".format( self._meta.description_content_type) if self._meta.description is not None: content += "\n" + to_str(self._meta.description) + "\n" return content
def test_setup_py_context(): poetry = Factory().create_poetry(project("complete")) builder = SdistBuilder(poetry) project_setup_py = poetry.file.parent / "setup.py" assert not project_setup_py.exists() try: with builder.setup_py() as setup: assert setup.exists() assert project_setup_py == setup with open(str(setup), "rb") as f: # we convert to string and replace line endings here for compatibility data = to_str(encode(f.read())).replace("\r\n", "\n") assert data == to_str(builder.build_setup()) assert not project_setup_py.exists() finally: if project_setup_py.exists(): project_setup_py.unlink()
def test_make_pkg_info_multi_constraints_dependency(): poetry = Factory().create_poetry( Path(__file__).parent.parent.parent / "fixtures" / "project_with_multi_constraints_dependency") builder = SdistBuilder(poetry) pkg_info = builder.build_pkg_info() p = Parser() parsed = p.parsestr(to_str(pkg_info)) requires = parsed.get_all("Requires-Dist") assert requires == [ 'pendulum (>=1.5,<2.0); python_version < "3.4"', 'pendulum (>=2.0,<3.0); python_version >= "3.4" and python_version < "4.0"', ]
def build_setup(self): # type: () -> bytes before, extra, after = [], [], [] package_dir = {} # If we have a build script, use it if self._package.build_script: after += [ "from {} import *".format(self._package.build_script.split(".")[0]), "build(setup_kwargs)", ] modules = [] packages = [] package_data = {} for include in self._module.includes: if include.formats and "sdist" not in include.formats: continue if isinstance(include, PackageInclude): if include.is_package(): pkg_dir, _packages, _package_data = self.find_packages(include) if pkg_dir is not None: package_dir[""] = os.path.relpath(pkg_dir, str(self._path)) packages += [p for p in _packages if p not in packages] package_data.update(_package_data) else: module = include.elements[0].relative_to(include.base).stem if include.source is not None: package_dir[""] = str(include.base.relative_to(self._path)) if module not in modules: modules.append(module) else: pass if package_dir: before.append("package_dir = \\\n{}\n".format(pformat(package_dir))) extra.append("'package_dir': package_dir,") if packages: before.append("packages = \\\n{}\n".format(pformat(sorted(packages)))) extra.append("'packages': packages,") if package_data: before.append("package_data = \\\n{}\n".format(pformat(package_data))) extra.append("'package_data': package_data,") if modules: before.append("modules = \\\n{}".format(pformat(modules))) extra.append("'py_modules': modules,".format()) dependencies, extras = self.convert_dependencies( self._package, self._package.requires ) if dependencies: before.append( "install_requires = \\\n{}\n".format(pformat(sorted(dependencies))) ) extra.append("'install_requires': install_requires,") if extras: before.append("extras_require = \\\n{}\n".format(pformat(extras))) extra.append("'extras_require': extras_require,") entry_points = self.convert_entry_points() if entry_points: before.append("entry_points = \\\n{}\n".format(pformat(entry_points))) extra.append("'entry_points': entry_points,") if self._package.python_versions != "*": python_requires = self._meta.requires_python extra.append("'python_requires': {!r},".format(python_requires)) return encode( SETUP.format( before="\n".join(before), name=to_str(self._meta.name), version=to_str(self._meta.version), description=to_str(self._meta.summary), long_description=to_str(self._meta.description), author=to_str(self._meta.author), author_email=to_str(self._meta.author_email), maintainer=to_str(self._meta.maintainer), maintainer_email=to_str(self._meta.maintainer_email), url=to_str(self._meta.home_page), extra="\n ".join(extra), after="\n".join(after), ) )