[docs] Limit git submodule recurse to depth 1
[releng.git] / releases / scripts / repos.py
1 #!/usr/bin/env python2
2 # SPDX-License-Identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 """
11 List Release Repos
12 """
13
14 import argparse
15 import yaml
16
17
18 class Repo(object):
19     """Object representing a repo listed in the release file.
20
21     Includes eq, hash, and ne methods so set comparisons work
22     """
23
24     def __init__(self, repo=None, ref=None, version=None):
25         self.repo = repo
26         self.ref = ref
27         self.version = version
28
29     def __repr__(self):
30         if self.version:
31             return "%s %s %s" % (self.repo, self.ref, self.version)
32         elif self.ref:
33             return "%s %s" % (self.repo, self.ref)
34         return "%s" % self.repo
35
36     def __eq__(self, obj):
37         if isinstance(obj, Repo):
38             return ((self.repo == obj.repo) and
39                     (self.ref == obj.ref) and
40                     (self.version == obj.version))
41         return False
42
43     def __ne__(self, obj):
44         return (not self.__eq__(obj))
45
46     def __hash__(self):
47         return hash(self.__repr__())
48
49
50 def main():
51     """Given a release yamlfile list the repos it contains"""
52
53     parser = argparse.ArgumentParser()
54     parser.add_argument('--file', '-f',
55                         type=argparse.FileType('r'),
56                         required=True)
57     parser.add_argument('--names', '-n',
58                         action='store_true',
59                         default=False,
60                         help="Only print the names of repos, "
61                              "not their SHAs")
62     parser.add_argument('--release', '-r',
63                         type=str,
64                         help="Only print"
65                              "SHAs for the specified release")
66     args = parser.parse_args()
67
68     project = yaml.safe_load(args.file)
69
70     list_repos(project, args)
71
72
73 def list_repos(project, args):
74     """List repositories in the project file"""
75
76     lookup = project.get('releases', [])
77     if 'releases' not in project:
78         exit(0)
79
80     repos = set()
81     for item in lookup:
82         repo, ref = next(iter(item['location'].items()))
83         if args.names:
84             repos.add(Repo(repo))
85         elif args.release and item['version'] == args.release:
86             repos.add(Repo(repo, ref))
87         elif not args.release:
88             repos.add(Repo(repo, item['version'], ref))
89     for repo in repos:
90         print(repo)
91
92
93 if __name__ == "__main__":
94     main()