aboutsummaryrefslogtreecommitdiff
path: root/share/requires.py
blob: 68c8f72b8bcc9f530bcaaaefa7634723f76912b2 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  requires.py
#
#  Copyright 2018 Neil Williams <neil.williams@linaro.org>
#
#  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 3 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 os
import sys
import yaml
import argparse


"""
Goals:
0: Convert a packaged list of dependencies and versions
   to a distro-specific list of dependencies.
1: Remove need for requirements.txt using Pip syntax as this is
   misleading.
2: output a list of binary package names for the requested distribution
   and suite to pass to docker scripts and LXC unit test jobs.
"""


def main():
    """
    Parse options and load requirements files.
    By default, outputs the same list as requirements.txt but
    with the Pip name replaced by the distro|suite package name.
    Use the -n option to only get the distro|suite package names.
    """
    parser = argparse.ArgumentParser(description="Handle dependency lists")
    parser.add_argument(
        "-p", "--package", required=True, help="Name of the LAVA package."
    )
    parser.add_argument(
        "-d", "--distribution", required=True, help="Only Debian is supported currently"
    )
    parser.add_argument(
        "-s", "--suite", required=True, help="The distribution suite / release"
    )
    parser.add_argument(
        "-i",
        "--inline",
        action="store_true",
        help="Print in a single line separated by whitespace. Requires --names",
    )
    parser.add_argument(
        "-n",
        "--names",
        action="store_true",
        help="Only output distribution package names, not versions",
    )
    args = parser.parse_args()
    req = os.path.join(
        os.path.dirname(__file__),
        "requirements",
        args.distribution,
        "%s.yaml" % args.suite,
    )
    if not os.path.exists(req):
        sys.stderr.write(
            "Unsupported suite|distribution: %s %s\n\n"
            % (args.distribution, args.suite)
        )
        return 1
    with open(req, "r") as data:
        depends = yaml.safe_load(data)
    if args.package not in depends:
        sys.stderr.write("Unknown package: %s\n\n" % args.package)
        return 2
    if args.names:
        msg = []
        for key, item in depends[args.package].items():
            if depends[args.package][key].get("system"):
                if args.inline:
                    msg.insert(0, key)
                else:
                    print(key)
                continue
            if args.inline:
                msg.append(item["name"])
            else:
                print(item["name"])
        if args.inline:
            print(" ".join(msg))
        return 0
    if not depends[args.package]:
        return 0
    for item in depends[args.package].keys():
        if depends[args.package][item].get("system"):
            continue
        print("%s%s" % (item, depends[args.package][item].get("version", "")))
    return 0


if __name__ == "__main__":
    sys.exit(main())