Merge "multiarch: Modify doctor x86_64, aarch64 verify jobs"
[releng.git] / utils / opnfv-artifacts.py
1 #!/usr/bin/python
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 The Linux Foundation and others
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #  http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 ##############################################################################
17
18 """
19 Generate JSON listing of OPNFV Artifacts
20
21 This produces a slimmed down version of metadata provided by Google
22 Storage for each artifact. Also excludes a large number of uninteresting
23 files.
24 """
25
26 from apiclient import discovery
27 from apiclient.errors import HttpError
28
29 import argparse
30 import json
31 import sys
32
33 api = {
34     'projects': {},
35     'docs': {},
36     'releases': {},
37 }
38
39 releases = [
40     'arno.2015.1.0',
41     'arno.2015.2.0',
42     'brahmaputra.1.0',
43 ]
44
45 # List of file extensions to filter out
46 ignore_extensions = [
47     '.buildinfo',
48     '.woff',
49     '.ttf',
50     '.svg',
51     '.eot',
52     '.pickle',
53     '.doctree',
54     '.js',
55     '.png',
56     '.css',
57     '.gif',
58     '.jpeg',
59     '.jpg',
60     '.bmp',
61 ]
62
63
64 parser = argparse.ArgumentParser(
65     description='OPNFV Artifacts JSON Generator')
66
67 parser.add_argument(
68     '-k',
69     dest='key',
70     default='',
71     help='API Key for Google Cloud Storage')
72
73 parser.add_argument(
74     '-p',
75     default=None,
76     dest='pretty',
77     action='store_const',
78     const=2,
79     help='pretty print the output')
80
81 # Parse and assign arguments
82 args = parser.parse_args()
83 key = args.key
84 pretty_print = args.pretty
85
86
87 def output(item, indent=2):
88     print(json.dumps(item, sort_keys=True, indent=indent))
89
90
91 def has_gerrit_review(dir_list):
92     """
93     If a directory contains an integer, it is assumed to be a gerrit
94     review number
95     """
96     for d in dir_list:
97         if d.isdigit():
98             return int(d)
99     return False
100
101
102 def has_release(dir_list):
103     """
104     Checks if any directory contains a release name
105     """
106     for d in dir_list:
107         if d in releases:
108             return d
109     return False
110
111
112 def has_documentation(dir_list):
113     """
114     Checks for a directory specifically named 'docs'
115     """
116     for d in dir_list:
117         if d == 'docs':
118             return True
119     return False
120
121
122 # Rename this or modify how gerrit review are handled
123 def has_logs(gerrit_review):
124     """
125     If a gerrit review exists, create a link to the review
126     """
127     if gerrit_review:
128         return "https://gerrit.opnfv.org/gerrit/#/c/%s" % gerrit_review
129     return False
130
131
132 def has_ignorable_extension(filename):
133     for extension in ignore_extensions:
134         if filename.lower().endswith(extension):
135             return True
136     return False
137
138
139 def get_results(key):
140     """
141     Pull down all metadata from artifacts.opnfv.org
142     and store it in projects as:
143     { 'PROJECT': [file ...], }
144     """
145     storage = discovery.build('storage', 'v1', developerKey=key)
146     files = storage.objects().list(bucket='artifacts.opnfv.org',
147                                    fields='nextPageToken,'
148                                           'items('
149                                    'name,'
150                                    'mediaLink,'
151                                    'updated,'
152                                    'contentType,'
153                                    'size'
154                                           ')')
155     while (files is not None):
156         sites = files.execute()
157
158         for site in sites['items']:
159             # Filter out unneeded files (js, images, css, buildinfo, etc)
160             if has_ignorable_extension(site['name']):
161                 continue
162
163             # Split /foo/bar/ into ['foo', 'bar'] and remove any extra
164             # slashes (ex. /foo//bar/)
165             site_split = filter(None, site['name'].split('/'))
166
167             # Don't do anything if we aren't given files multiple
168             # directories deep
169             if len(site_split) < 2:
170                 continue
171
172             project = site_split[0]
173             name = '/'.join(site_split[1:])
174             proxy = "http://build.opnfv.org/artifacts.opnfv.org/%s" % site[
175                 'name']
176             if name.endswith('.html'):
177                 href = "http://artifacts.opnfv.org/%s" % site['name']
178                 href_type = 'view'
179             else:
180                 href = site['mediaLink']
181                 href_type = 'download'
182
183             gerrit = has_gerrit_review(site_split)
184             logs = False  # has_logs(gerrit)
185             # documentation = has_documentation(site_split)
186             release = has_release(site_split)
187
188             category = 'project'
189             if gerrit:
190                 category = 'gerrit'
191             elif release:
192                 category = 'release'
193             elif logs:
194                 category = 'logs'
195
196             metadata = {
197                 'category': category,
198                 'gerritreview': gerrit,
199                 'release': release,
200                 'name': name,
201                 'size': site['size'],
202                 'time': site['updated'],
203                 'contentType': site['contentType'],
204                 'href': href,
205                 'href_type': href_type,
206                 'proxy_href': proxy,
207             }
208
209             if project in releases:
210                 if project not in api['releases']:
211                     api['releases'][project] = [metadata]
212                 else:
213                     api['releases'][project].append(metadata)
214             else:
215                 if project not in api['projects']:
216                     api['projects'][project] = [metadata]
217                 else:
218                     api['projects'][project].append(metadata)
219
220         files = storage.objects().list_next(files, sites)
221
222     return api
223
224
225 # Fail if there is an invalid response from GCE
226 try:
227     js = get_results(key)
228 except HttpError as e:
229     print >> sys.stderr, e
230     exit(1)
231
232 output(js, indent=pretty_print)