forked from SeattleTestbed/seattlelib_v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundler.py
453 lines (349 loc) · 10.7 KB
/
bundler.py
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""
<Program Name>
bundler.py
<Purpose>
Bundles simplify the transferring of repy programs and associated data to and
from vessels. A bundle is a self-extracting repy program that contains a
repy program and embedded files that the contained program depends on.
Bundles have a .bundle.repy extension.
Embedded files within a bundle are extracted into the local file system
before the flow of execution reaches the contained program. Bundles do not
necessarily have to contain a repy program, and can be used solely to pack
data into a single unit.
This program is a command line wrapper to access the Bundle class
functionality. You can use this to perform the following operations
on bundles:
- Create a new bundle
- Add files to/Remove files from a bundle
- Extract files from a bundle
- Show a bundle's contents
- Wipe a bundle's contents
<Usage>
First, use this program to create the bundle. Then, use this program to add
the files you want to the bundle.
For further usage information, run this program without any arguments.
<Example Usage>
# program.bundle.repy is the name of the output file to generate.
# If you dont supply this value, then the bundle will be created in-place.
$ python bundler.py create program.repy [program.bundle.repy]
$ python bundler.py add program.bundle.repy file1 file2 file3
$ python repy.py restrictions_file program.bundle.repy
"""
import sys
import repyhelper
repyhelper.translate_and_import('bundle.repy')
PROGRAM_HELPSTRING = """
Valid commands:
$ bundle.repy create [source] bundlename
$ bundle.repy wipe bundlename [outputfile]
$ bundle.repy [list | extract-all]
$ bundle.repy [add | extract | remove] bundlename {filename}
For more information on how to use a particular command:
$ bundle.repy help [command]
"""
class BadArgumentsError(BaseException):
""" The user gave us bad arguments. """
def _bundle_create(args):
"""
<Purpose>
Creates a bundle with the specified name.
Optionally takes in a source argument.
<Arguments>
args:
A list of filenames. This function's behavior changes depending on
how many filenames are passed.
Valid arguments:
[source, bundle name]
[bundle name]
<Side Effects>
Overwrites the existing file at bundle name if one exists. If a source
file is not specified, we will treat an existing file as the source file.
<Exceptions>
None
<Returns>
None
"""
# User passed us bad arguments
if not len(args) in [1, 2]:
raise BadArgumentsError("Expected Arguments: {source_filename} bundle_filename")
# Args are: bundle_fn
if len(args) == 1:
bundle_Bundle(args[0], 'w')
# Args are: src_fn, bundle_fn
else:
bundle_Bundle(args[1], 'w', srcfn=args[0])
def _bundle_list(args):
"""
<Purpose>
Lists the contents of the specified bundle.
<Arguments>
args:
A list containing a single filename, which corresponds to a bundle name.
<Side Effects>
The contents of the specified bundle will be listed with their file sizes.
<Exceptions>
None
<Returns>
None
"""
fname = args[0]
headers = ("Filename", "Size (bytes)")
bundle = bundle_Bundle(fname, 'r')
filedata = bundle.list()
# Get longest filename
max_length = 0
for fname in filedata:
if len(fname) > max_length:
max_length = len(fname)
# Give the first column enough room
if max_length < len(headers[0]):
max_length = len(headers[0])
# Cap the maximum length so we don't overflow every single line
elif max_length > 60:
max_length = 60
# Filename Size (bytes)
# file1 14
# file2 14234
# ...
base_format = "%"+str(max_length)+"s %"+str(len(headers[1]))
header_format = (base_format + 's')
filelist_format = (base_format + 'i')
# Display the file list sorted
file_list = filedata.keys()
file_list.sort()
print "Bundle contents:"
print header_format % headers
for file in file_list:
print header_format % (file, filedata[file])
def _bundle_add(args):
"""
<Purpose>
Adds the specified files to the bundle.
<Arguments>
args:
A list of filenames.
The first filename is the bundlename. Following filenames are files to
add to the bundle.
<Side Effects>
Adds the specified files to the bundle.
<Exceptions>
None
<Returns>
None
"""
fname = args[0]
add_fnames = args[1:]
bundle = bundle_Bundle(fname, 'a')
failed = bundle.add_files(add_fnames)
if failed:
for key, value in failed.iteritems():
print key, value
def _bundle_extract(args):
"""
<Purpose>
Extracts the specified files.
<Arguments>
args:
A list of filenames.
The first filename is the bundlename. Following filenames are files to
extract from the bundle.
<Side Effects>
Specified files within the bundle with be extracted to the local directory.
Any conflicting files will be overwritten.
<Exceptions>
None
<Returns>
None
"""
fname = args[0]
extract_fnames = args[1:]
bundle = bundle_Bundle(fname, 'r')
failed = bundle.extract_files(extract_fnames)
if failed:
for key, value in failed.iteritems():
print key, value
def _bundle_extract_all(args):
"""
<Purpose>
Extracts all files in a bundle.
<Arguments>
args:
A list containing a single filename, which corresponds to a bundle name.
<Side Effects>
Files within the bundle with be extracted to the local directory.
Any conflicting files will be overwritten.
<Exceptions>
None
<Returns>
None
"""
fname = args[0]
bundle = bundle_Bundle(fname, 'r')
failed = bundle.extract_all()
if failed:
for key, value in failed.iteritems():
print key, value
def _bundle_remove(args):
"""
<Purpose>
Removes the specified files.
<Arguments>
args:
A list of filenames.
The first filename is the bundlename. Following filenames are files to
remove from the bundle.
<Side Effects>
Existing files within the bundle with the specified names will be removed.
<Exceptions>
None
<Returns>
None
"""
fname = args[0]
remove_fnames = args[1:]
bundle = bundle_Bundle(fname, 'a')
failed = bundle.remove_files(remove_fnames)
if failed:
for key, value in failed.iteritems():
print key, value
def _bundle_wipe(args):
"""
<Purpose>
Wipes the bundle with the specified name.
Optionally takes in an output argument.
<Arguments>
args:
A list of filenames. This function's behavior changes depending on
how many filenames are passed.
Valid arguments:
[bundle name]
[bundle name, output_file]
<Side Effects>
Overwrites the existing file at bundle name if one exists. If an output
file is not specified, we will overwrite the original file.
<Exceptions>
None
<Returns>
None
"""
# User passed us bad arguments
if not len(args) in [1, 2]:
raise BadArgumentsError("Expected Arguments: bundle_filename {output_filename} ")
fname = args[0]
# Args are: bundle_fn
if len(args) == 1:
try:
bundle_clear_bundle_from_file(fname)
except bundle_InvalidOperationError:
print fname, "is not a valid bundle"
# Args are: bundle_fn, outputfile
else:
output_fname = args[1]
# Copy over file contents
_bundle_copy_file(fname, output_fname)
try:
bundle_clear_bundle_from_file(output_fname)
except bundle_InvalidOperationError:
print fname, "is not a valid bundle"
def _display_help(args):
"""
<Purpose>
Displays the helpstrings for the bundler.
<Arguments>
args:
The name of the command to query
<Side Effects>
None
<Exceptions>
None
<Returns>
None
"""
if not args:
print PROGRAM_HELPSTRING
return
if not args[0] in ACTIONDICT:
print "'"+args[0]+"' is not a known command."
print PROGRAM_HELPSTRING
return
print ACTIONDICT[args[0]]['helptext']
ACTIONDICT = {
'create': {
'callback': _bundle_create,
'helptext': """
$ bundle.repy create [source] bundlename
Creates a new bundle at the specified output file. If no source file is given
and the output file already exists, the existing file will be embedded into
the bundle. Otherwise, the existing file will be overwritten.
""",},
'list': {
'callback': _bundle_list,
'helptext': """
$ bundle.repy list bundlename
Lists the contents of the specified bundle and their embedded file sizes.
The sizes of extracted files will be slightly smaller than the sizes listed
here.
""",},
'add': {
'callback': _bundle_add,
'helptext': """
$ bundle.repy add bundlename file_1 [file_2] ... [file_n]
Adds the specified files to the bundle. You must provide at least one file,
and file names must not have any spaces.
""",},
'extract': {
'callback': _bundle_extract,
'helptext': """
$ bundle.repy extract bundlename file_1 [file_2] ... [file_n]
Extracts the specified files to the currently working directory. You must
provide at least one file, and file names must not have any spaces.
WARNING:
If there are files in the working directory with same filenames as
any of the specified files, they are overwritten!
""",},
'extract-all': {
'callback': _bundle_extract_all,
'helptext': """
$ bundle.repy extract-all bundlename
Extracts all files to the current working directory.
WARNING:
If there are files in the working directory with same filenames as
a file in those in the bundle, they are overwritten!
"""},
'remove': {
'callback': _bundle_remove,
'helptext': """
$ bundle.repy remove bundlename file_1 [file_2] ... [file_n]
Removes the specified files from the bundle. You must provide at least one
file, and file names must not have any spaces.
""",},
'wipe': {
'callback': _bundle_wipe,
'helptext': """
$ bundle.repy wipe bundlename [output_filename]
Wipes the bundle of embedded files and auto-extraction scripts.
WARNING:
If no output file is provided, then all original bundle contents will be
lost.
"""},
'help': {
'callback': _display_help,
'helptext': PROGRAM_HELPSTRING
}
}
if __name__ == '__main__':
callargs = sys.argv[1:]
# Perform a single task based on what the user inputted.
if not callargs:
print PROGRAM_HELPSTRING
exitall()
action = callargs[0].lower()
arguments = callargs[1:]
if action not in ACTIONDICT:
print "Action is unsupported."
print PROGRAM_HELPSTRING
exit()
try:
ACTIONDICT[action]['callback'](arguments)
except BadArgumentsError, e:
print str(e)