-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
88 lines (77 loc) · 2.7 KB
/
Copy pathutils.py
File metadata and controls
88 lines (77 loc) · 2.7 KB
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
import logging
import os
import zipfile
from glob import glob
from importlib import reload
from pathlib import Path
from tqdm import tqdm
# Very basic config
datadir = "data/"
archivefile = f"{datadir}archived_json.zip"
# Force logging
reload(logging)
logging.basicConfig(
format="%(asctime)s %(levelname)s:%(message)s",
level=logging.DEBUG,
datefmt="%I:%M:%S",
)
logger = logging.getLogger()
def archive_json(deleteafterarchiving=True):
rawfiles = list_loose_json()
logger.debug(f"{len(rawfiles):,} loose JSON files might get added to the ZIP.")
if len(rawfiles) == 0:
return
jsonlist = list_archived_json()
duplicates = []
for rawfile in sorted(rawfiles):
if rawfile in jsonlist:
duplicates.append(rawfile)
if len(duplicates) > 0:
message = (
f"{len(duplicates):,} duplicates found, both loose JSON and in the ZIP. "
)
message += f"You'll want to clear these out yourself. This program will neither replace nor "
message += "delete them."
logger.debug(message)
for duplicate in duplicates:
rawfiles.remove(duplicate)
logger.debug(f"{len(rawfiles):,} files remain to be added to the ZIP.")
if len(rawfiles) == 0:
return
logger.debug(f"Confirmed: Writing {len(rawfiles):,} files to archive.")
with zipfile.ZipFile(
archivefile, "a", compression=zipfile.ZIP_DEFLATED, compresslevel=9
) as myzip:
for rawfile in tqdm(rawfiles):
myzip.write(
filename=datadir + rawfile,
arcname=rawfile,
compress_type=zipfile.ZIP_DEFLATED,
compresslevel=9,
)
if deleteafterarchiving:
os.remove(datadir + rawfile)
# Need to upload to BLN after this ... or just handle with any in_production pushing?
return
def list_json():
rawfiles = list_loose_json()
zippedfiles = list_archived_json()
zippedfiles.extend(rawfiles)
logger.debug(f"{len(zippedfiles):,} total files found, with possible overlaps.")
return zippedfiles
def list_archived_json():
if not os.path.exists(archivefile):
logger.warning(f"No archive file at {archivefile} found")
zippedfiles = []
else:
with zipfile.ZipFile(archivefile, "r") as myzip:
zippedfiles = myzip.namelist()
logger.debug(f"{len(zippedfiles):,} files found in {archivefile}")
return zippedfiles
def list_loose_json():
rawfilesraw = glob(datadir + "*.json")
rawfiles = []
for rawfileraw in rawfilesraw:
rawfiles.append(str(Path(rawfileraw).relative_to(datadir)))
logger.debug(f"{len(rawfiles):,} files found loose.")
return rawfiles