-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelocate_files.py
76 lines (55 loc) · 1.73 KB
/
relocate_files.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
import xml.dom.minidom as minidom
import regex as re
import os
import time
import shutil
class Info:
def __init__(self, daac, date):
self.DAAC = daac
self.date = date
self.path = ''
def get_xml_list():
dir_path = "./xml"
xmls = []
for path in os.listdir(dir_path):
# check if current path is a file
if os.path.isfile(os.path.join(dir_path, path)):
if re.match(".*\.xml", path) and not re.search("unittest.*\.xml", path):
xmls.append(path)
return xmls
def parse_file(name):
match = re.search("(.*)-(\d{2})\.\d{2}\.(\d{4})", name)
if match:
daac = match.group(1).strip()
month = match.group(2).strip()
year = match.group(3).strip()
# print("DAAC: '" + daac + "' | month/year : '" + month + "/" + year + "'")
date = month + "_" + year
info = Info(daac, date)
return info
def check_dirs(info):
www_dir = "/var/www/html/pyreader-tests/"
daac_dir = os.path.join(www_dir, info.DAAC)
month_dir = os.path.join(daac_dir, info.date)
if not os.path.exists(daac_dir):
os.mkdir(daac_dir)
if not os.path.exists(month_dir):
os.mkdir(month_dir)
info.path = month_dir
def move_file(name, info):
old_dir = "./xml"
shutil.move(os.path.join(old_dir, name), os.path.join(info.path, name))
def driver(verbose):
print("Moving xml files ...")
xmls = get_xml_list()
for file in xmls:
info = parse_file(file)
print(info.DAAC + " | " + info.date) if verbose else print('.', end="", flush=True)
check_dirs(info)
move_file(file, info)
print("")
def main():
# main for testing script
driver(True)
if __name__ == "__main__":
main()