Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/tmp/deps_calc/bin/python3
- import json
- import re
- from typing import Dict, List
- from poetry.packages import Dependency
- from poetry.packages import ProjectPackage
- from poetry.puzzle import Solver
- from poetry.repositories.repository import Repository
- from poetry.semver import parse_constraint
- def determine_requirements(requires: List) -> List:
- if not requires:
- return []
- requires = parse_name_version_pairs(requires)
- result = []
- for requirement in requires:
- if "version" not in requirement:
- requirement["version"] = "*"
- result.append("{} {}".format(requirement["name"],
- requirement["version"]))
- return result
- def parse_name_version_pairs(pairs: List) -> List:
- result = []
- for i in range(len(pairs)):
- pair = re.sub("^([^=: ]+)[=: ](.*)$", "\\1 \\2", pairs[i].strip())
- pair = pair.strip()
- if " " in pair:
- name, version = pair.split(" ", 2)
- result.append({"name": name, "version": version})
- else:
- result.append({"name": pair})
- return result
- def format_requirements(requirements: List) -> Dict:
- requires = {}
- requirements = parse_name_version_pairs(requirements)
- for requirement in requirements:
- requires[requirement["name"]] = requirement["version"]
- return requires
- def resolve_package_deps(
- packages: List[str],
- extras_wanted: List[str],
- py_version: str,
- ) -> Dict:
- req_list = determine_requirements(packages)
- requirements = format_requirements(req_list)
- # validate requirements format
- for constraint in requirements.values():
- parse_constraint(constraint)
- dependencies = []
- for name, constraint in requirements.items():
- dep = Dependency(name, constraint)
- if extras_wanted:
- extras: List[str] = []
- for extra in extras_wanted:
- if " " in extra:
- extras += [e.strip() for e in extra.split(" ")]
- else:
- extras.append(extra)
- for ex in extras:
- dep.extras.append(ex)
- dependencies.append(dep)
- # TODO: Work out what these need to be
- package = ProjectPackage("fake-resolve", "69")
- package.python_versions = (py_version)
- for dep in dependencies:
- package.requires.append(dep)
- # TODO: Create a pool
- solver = Solver(package, poetry.pool, Repository(), Repository(), output)
- ops = solver.solve()
- pkgs: Dict = {}
- for op in ops:
- package = op.package
- pkgs[package.name] = package.version
- return pkgs
- if __name__ == '__main__':
- deps = resolve_package_deps(["peerme"], [], "~3.6")
- json.dumps(deps, sort_keys=True, indent=4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement