-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy patharchive.py
72 lines (63 loc) · 2.85 KB
/
archive.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
import argparse
import datetime
import os
import shutil
from pathlib import Path, PurePosixPath
archive_dir = os.path.join(os.getcwd(), "html_archive")
nb_dir = os.path.join(os.getcwd(), "jwst_validation_notebooks")
out_base = "/grp/jwst/wit/jwst_validation_notebooks"
date_dir = datetime.datetime.now().strftime("%Y-%m-%d")
out_dir = os.path.join(out_base, date_dir)
do_zip = True
verbose = False
archive_help = "Base directory for local HTML archive. Default is {}".format(archive_dir)
out_help = "Directory to copy the archive to. Default is {}".format(out_dir)
nb_help = "Directory to search for notebook HTML files. Default is {}".format(nb_dir)
verbose_help = "Print diagnostic information while running."
zip_help = "Should the central storage archive be zipped. Default is {}".format(do_zip)
parser = argparse.ArgumentParser(description="")
parser.add_argument('-i', '--input', dest='nb_dir', help=nb_help,
default=nb_dir)
parser.add_argument('-o', '--output', dest='out_dir', help=out_help,
default=out_dir)
parser.add_argument('-a', '--archive_dir', dest='archive_dir', help=archive_help,
default=archive_dir)
parser.add_argument('-z', '--zip', dest="zip", action='store_false', default=do_zip,
help=zip_help)
parser.add_argument('-v', '--verbose', dest="verbose",
action='store_true', default=verbose, help=verbose_help)
res = parser.parse_args()
if __name__ == "__main__":
archive_dir = res.archive_dir
nb_dir = res.nb_dir
out_dir = res.out_dir
do_zip = res.zip
verbose = res.verbose
if os.path.exists(archive_dir):
shutil.rmtree(archive_dir)
os.makedirs(archive_dir)
for path in Path(nb_dir).rglob('*.html'):
if verbose:
print("Archiving {}".format(path))
pure_path = PurePosixPath(path)
rel_path = pure_path.relative_to(nb_dir)
archive_path = os.path.join(archive_dir, rel_path)
if verbose:
print("\tCopying to {}".format(archive_path))
if not os.path.exists(os.path.split(archive_path)[0]):
os.makedirs(os.path.split(archive_path)[0])
shutil.copy(path, archive_path)
central_path = os.path.join(out_dir, rel_path)
if verbose:
print("\tCopying to {}".format(central_path))
if not os.path.exists(os.path.split(central_path)[0]):
os.makedirs(os.path.split(central_path)[0])
shutil.copy(path, central_path)
if os.path.isfile("index.html"):
shutil.copy("index.html", os.path.join(archive_dir, "index.html"))
shutil.copy("index.html", os.path.join(out_dir, "index.html"))
if do_zip:
if verbose:
print("Creating zipped archive")
shutil.make_archive(date_dir, 'zip', archive_dir)
shutil.copy(date_dir+".zip", os.path.join(out_base, date_dir+".zip"))