#!/usr/bin/env python # Copyright (c) 2013, 2015, 2019 Luke Shumaker # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import re from pip._internal.req import parse_requirements from pip._internal.download import PipSession from packaging.specifiers import Specifier vcspkgs = { 'git+git://github.com/fredj/cssmin.git@master#egg=cssmin': 'python2-cssmin-fredj', } def nextver(ver): parts = re.sub(r"(a|b|rc|\.dev|\.post|\+).*", '', ver).split('.') parts.pop() parts += [str(int(parts.pop())+1)] return '.'.join(parts) # https://pip.pypa.io/en/stable/reference/pip_install/#requirements-file-format # https://setuptools.readthedocs.io/en/latest/pkg_resources.html#requirements-parsing # https://www.python.org/dev/peps/pep-0440/ def main(args): for filename in args: for req in parse_requirements(filename, session=PipSession()): depname = re.sub(r"^(python2?-)?", 'python2-', req.name.lower()) if req.link: if req.link.url not in vcspkgs: raise f"no known package for VCS dep {req}" depname = vcspkgs[req.link.url] if req.specifier: for spec in req.specifier: # Hack: Allow some slop with '==', otherwise it would be a # nightmare with keeping things in sync. if spec.operator == "==": if len(spec.version.split('.')) <= 2: spec = Specifier(f"=={spec.version}.*") else: spec = Specifier(f"~={spec.version}") if spec.operator == "~=": print(f"{depname}>={spec.version} {depname}<{nextver(spec.version)}") elif spec.operator == "==": if spec.version.endswith(".*"): print(f"{depname}>={spec.version[:-2]} {depname}<{nextver(spec.version)}") else: print(f"{depname}={spec.version}") elif spec.operator == '!=': raise f"version exclusion is not supported: {req}" elif spec.operator in {'<=', '>=', '<', '>'}: print(f"{depname}{spec.operator}{spec.version}") elif spec.operator == "===": print(f"{depname}={spec.version}") else: raise f"unknown specifier operator: {spec.operator}: {req}" else: print(f"{depname}") if __name__ == '__main__': import sys main(sys.argv[1:])