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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
#!/usr/bin/env python
# Move artifacts from a temporary location to a permanent location on s.l.o
import argparse
import fnmatch
import os
import os.path
import shutil
import sys
uploads_path = '/srv/snapshots.linaro.org/uploads/'
target_path = '/srv/snapshots.linaro.org/www/'
staging_uploads_path = '/srv/staging.snapshots.linaro.org/uploads/'
staging_target_path = '/srv/staging.snapshots.linaro.org/www/'
PASS = 0
FAIL = 1
acceptable_job_types = [
'android',
'prebuilt',
'kernel-hwpack',
'ubuntu-hwpacks',
'ubuntu-images',
'ubuntu-restricted',
'ubuntu-sysroots',
'openembedded',
'binaries'
]
def setup_parser():
"""Set up the argument parser for publish_to_snapshots script."""
parser = argparse.ArgumentParser()
parser.add_argument(
"-s", "--staging", dest="staging", default=False,
action='store_true',
help=("Perform sanitization on the file to not expose any"
"potentially sensitive data. Used for staging deployment."))
parser.add_argument(
"-t", "--job-type", dest="job_type",
help="Specify the job type (Ex: android/kernel-hwpack)")
parser.add_argument(
"-j", "--job-name", dest="job_name",
help=("Specify the job name which resulted the archive to "
"be stored. Ex: ${JOB_NAME} should be specified for "
"android/ubuntu-{hwpacks,images,sysroots}/binaries and for"
"kernel-hwpack ${KERNEL_JOB_NAME}"))
parser.add_argument(
"-n", "--build-num", dest="build_num", type=int,
help=("Specify the job build number for android/"
"ubuntu-{hwpacks,images,sysroots}/binaries"))
parser.add_argument(
"-m", "--manifest", dest="manifest",
action='store_true',
help="Optional parameter to generate MANIFEST file")
return parser
class PublisherArgumentException(Exception):
"""There was a problem with one of the publisher arguments."""
pass
class SnapshotsPublisher(object):
# Files that need no sanitization even when publishing to staging.
STAGING_ACCEPTED_FILES = [
'BUILD-INFO.txt',
'EULA.txt',
'OPEN-EULA.txt',
'*.EULA.txt.*',
]
def __init__(self, argument_parser=None):
"""Allow moving files around for publishing on snapshots.l.o."""
self.argument_parser = argument_parser
@classmethod
def is_accepted_for_staging(cls, filename):
"""Is filename is in a list of globs in STAGING_ACCEPTED_FILES?"""
filename = os.path.basename(filename)
for accepted_names in cls.STAGING_ACCEPTED_FILES:
if fnmatch.fnmatch(filename, accepted_names):
return True
return False
@classmethod
def sanitize_file(cls, file_path):
"""This truncates the file and fills it with its own filename."""
assert not cls.is_accepted_for_staging(file_path)
base_file_name = os.path.basename(file_path)
protected_file = open(file_path, "w")
# Nice property of this is that we are also saving on disk space
# needed.
protected_file.truncate()
# To help distinguish files more easily when they are downloaded,
# we write out the base file name as the contents.
protected_file.write(base_file_name)
protected_file.close()
def validate_args(self, args):
# Validate that all the required information
# is passed on the command line
if (args.job_type == None or args.job_name == None or
args.build_num == None):
raise PublisherArgumentException(
"\nYou must specify job-type, job-name and build-num")
if (args.job_type not in acceptable_job_types):
raise PublisherArgumentException("Invalid job type")
return True
def jobname_to_target_subdir(self, args, jobname):
ret_val = None
if args.job_type == "android":
ret_val = jobname.split("_")
elif args.job_type == "kernel-hwpack":
ret_val = jobname.split('_')[0].replace(".", "_")
elif (args.job_type == "ubuntu-hwpacks" or
args.job_type == "ubuntu-images" or
args.job_type == "ubuntu-restricted" or
args.job_type == "ubuntu-sysroots"):
ret_val = jobname.split('-', 2)
elif args.job_type == "openembedded":
ret_val = jobname.split('-', 2)
elif args.job_type == "prebuilt":
# Return value must not be None when we want to ignore it.
ret_val = ''
elif args.job_type == "binaries":
ret_val = jobname.split('-', 2)
return ret_val
def validate_paths(self, args, uploads_path, target_path):
build_dir_path = target_dir_path = None
ret_val = self.jobname_to_target_subdir(args, args.job_name)
if ret_val != None:
if args.job_type == "android":
build_path = '/'.join([args.job_type, args.job_name,
str(args.build_num)])
build_dir_path = os.path.join(uploads_path, build_path)
user_name = ret_val[0]
job_name = '_'.join(ret_val[1:])
target_dir = '/'.join([args.job_type, "~%s" % user_name,
job_name, str(args.build_num)])
target_dir_path = os.path.join(target_path, target_dir)
elif args.job_type == "kernel-hwpack":
kernel_tree = ret_val
build_path = '/'.join([args.job_type, args.job_name,
str(args.build_num)])
build_dir_path = os.path.join(uploads_path, build_path)
target_dir = '/'.join([args.job_type, kernel_tree,
args.job_name])
target_dir_path = os.path.join(target_path, target_dir)
elif (args.job_type == "ubuntu-hwpacks" or
args.job_type == "ubuntu-images" or
args.job_type == "ubuntu-restricted" or
args.job_type == "ubuntu-sysroots"):
dist_name = ret_val[0]
hwpack_image = args.job_type.split("-")[1]
board_rootfs_name = ret_val[2]
build_dir_path = os.path.join(uploads_path, args.job_name,
str(args.build_num))
target_dir = '/'.join(
[dist_name, hwpack_image, board_rootfs_name,
str(args.build_num)])
target_dir_path = os.path.join(target_path, target_dir)
elif args.job_type == "openembedded":
build_dir_path = os.path.join(uploads_path,
args.job_type,
args.job_name)
target_dir_path = os.path.join(target_path,
args.job_type,
args.job_name)
elif args.job_type == "prebuilt":
build_path = '%s/%d' % (args.job_name, args.build_num)
build_dir_path = os.path.join(uploads_path, build_path)
target_dir_path = target_path
elif args.job_type == "binaries":
build_dir_path = os.path.join(uploads_path,
args.job_name,
str(args.build_num))
ts_file = os.path.join(build_dir_path, 'TIMESTAMP')
# FIXME: test file and content
f = open(ts_file)
timestamp = f.read().strip()
f.close()
os.remove(ts_file)
target_dir_path = os.path.join(
target_path, 'android', args.job_type, ret_val[0],
timestamp)
else:
return None, None
if not os.path.isdir(build_dir_path):
build_paths = "'%s' or '%s'" % (uploads_path, build_dir_path)
print "Missing build paths: ", build_paths
return None, None
if not os.path.isdir(target_path):
print "Missing target path", target_path
return None, None
return build_dir_path, target_dir_path
def create_symlink(self, args, target_dir_path):
symlink_path = os.path.dirname(target_dir_path)
if args.job_type == "android":
symlink_path = os.path.join(symlink_path, "lastSuccessful")
else:
symlink_path = os.path.join(symlink_path, "latest")
header_path = os.path.join(target_path, "HEADER.html")
header_symlink_path = os.path.join(target_dir_path, "HEADER.html")
try:
if os.path.islink(symlink_path):
os.unlink(symlink_path)
if os.path.islink(header_symlink_path):
os.unlink(header_symlink_path)
os.symlink(header_path, header_symlink_path)
os.symlink(target_dir_path, symlink_path)
print "The latest build is now linked to ", target_dir_path
return PASS
except Exception, details:
print "Failed to create symlink", symlink_path, ":", details
return FAIL
def create_manifest_file(self, target_dir):
orig_dir = os.getcwd()
os.chdir(target_dir)
fn = os.path.join(target_dir, "MANIFEST")
lines = []
try:
for path, subdirs, files in os.walk("."):
for name in files:
lines.append(
os.path.join(path, name).split("./")[1] + "\n")
if len(lines) != 0:
fd = open(fn, "w+")
for line in lines:
if not "MANIFEST" or not "HEADER.html" in line:
fd.write(line)
fd.close()
else:
raise Exception("Uploads directory was empty, "
"nothing got moved to destination")
os.chdir(orig_dir)
if os.path.isfile(fn):
print "Manifest file", fn, "generated"
return PASS
except Exception, details:
print "Got Exception in create_manifest_file: ", details
os.chdir(orig_dir)
return FAIL
def move_dir_content(self, src_dir, dest_dir, sanitize=False):
filelist = os.listdir(src_dir)
try:
for file in filelist:
src = os.path.join(src_dir, file)
dest = os.path.join(dest_dir, file)
if os.path.exists(dest):
if os.path.isdir(dest):
self.move_dir_content(src, dest)
continue
else:
os.remove(dest)
if sanitize and not self.is_accepted_for_staging(src):
# Perform the sanitization before moving the file
# into place.
print "Sanitizing contents of '%s'." % src
self.sanitize_file(src)
print "Moving the src '", src, "'to dest'", dest, "'"
shutil.move(src, dest)
except shutil.Error:
print "Error while moving the content"
def move_artifacts(self, args, build_dir_path, target_dir_path):
try:
if not os.path.isdir(target_dir_path):
# umask 0 is required, otherwise mode value of
# makedirs won't take effect
os.umask(0)
# Set write permission to the group explicitly,
# as default umask is 022
os.makedirs(target_dir_path, 0775)
if not os.path.isdir(target_dir_path):
raise OSError
self.move_dir_content(build_dir_path, target_dir_path,
sanitize=args.staging)
if (args.job_type == "android" or
args.job_type == "ubuntu-hwpacks" or
args.job_type == "ubuntu-images" or
args.job_type == "ubuntu-restricted" or
args.job_type == "ubuntu-sysroots"):
ret = self.create_symlink(args, target_dir_path)
if ret != PASS:
return ret
if args.manifest:
ret = self.create_manifest_file(target_dir_path)
if ret != PASS:
print "Failed to create manifest file"
return ret
print (
"Moved the files from '%s' to '%s'" % (
build_dir_path, target_dir_path))
return PASS
except OSError, details:
print (
"Failed to create the target path %s: %s" % (
target_dir_path, details))
return FAIL
except shutil.Error:
print "Failed to move files destination path", target_dir_path
return FAIL
def main():
global uploads_path
global target_path
argument_parser = setup_parser()
publisher = SnapshotsPublisher(argument_parser)
args = argument_parser.parse_args()
try:
publisher.validate_args(args)
except PublisherArgumentException as exception:
argument_parser.error(exception.message)
if args.staging:
uploads_path = staging_uploads_path
target_path = staging_target_path
try:
build_dir_path, target_dir_path = publisher.validate_paths(
args, uploads_path, target_path)
if build_dir_path == None or target_dir_path == None:
print "Problem with build/target path, move failed"
return FAIL
ret = publisher.move_artifacts(args, build_dir_path, target_dir_path)
if ret != PASS:
print "Move Failed"
return FAIL
else:
print "Move succeeded"
return PASS
except Exception, details:
print "In main() Exception details:", details
return FAIL
if __name__ == '__main__':
sys.exit(main())
|