summaryrefslogtreecommitdiff
path: root/pip2pacman
blob: ff1975b65a0b9d708398b112a1692b23d61f365b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python

#   Copyright (c) 2013, 2015, 2019 Luke Shumaker <lukeshu@parabola.nu>
#
#   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 <http://www.gnu.org/licenses/>.

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:])