-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathais_packager.py
executable file
·68 lines (51 loc) · 2.11 KB
/
ais_packager.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
#!/usr/bin/env python3
import argparse
import os
import sys
from pathlib import Path
from zipfile import ZipFile
PATH = Path.cwd()
class PackagerError(ValueError):
def __init__(self, k):
self.msg = f'Value {k} is not supplied'
ValueError.__init__(self, self.msg)
def zip_it_up(package_dir):
# zip the stuff
with ZipFile(os.path.join(PATH, f'{package_dir.name}.zip'), 'w') as zp:
for f in os.listdir(package_dir):
if '.zip' not in f:
zp.write(os.path.join(package_dir, f), arcname=f)
print(f'\n{package_dir.name}.zip written')
def manifest(email, cmodel, parent, obj_dir):
if not email:
raise PackagerError('\"email address\"')
if not cmodel:
raise PackagerError('\"content model\"')
if not parent:
raise PackagerError('\"parent collection\"')
with open(os.path.join(obj_dir, 'manifest.ini'), 'w') as manifest_file:
manifest_file.write(f'[{obj_dir.name}]')
manifest_file.write(f'\nsubmitter_email = {email}')
manifest_file.write(f'\ncontent_model = {cmodel}')
manifest_file.write(f'\nparent_collection = {parent}')
print(f'\nManifest written for {obj_dir.name}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Package books or newspapers for DigiNole\'s AIS')
parser.add_argument('-e', '--email', help='Submitter email')
parser.add_argument('-c', '--cmodel', help='Content model',
choices=['islandora:newspaperIssueCModel', 'islandora:bookCModel'])
parser.add_argument('-p', '--parent', help='Parent collection')
parser.add_argument('-z', '--zip', action='store_true',
help='Zip directory when finished?')
parser.add_argument('dir', help='Directory to work in')
args = parser.parse_args()
p = Path(args.dir)
obj_dirs = [f.resolve() for f in p.iterdir() if f.is_dir()]
if not obj_dirs:
obj_dirs = [p.resolve()]
for obj in obj_dirs:
manifest(args.email, args.cmodel, args.parent, obj)
if args.zip:
zip_it_up(obj)
print('\nAll done.')
sys.exit()