forked from lcnetdev/lds-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarcxml2bf-done
executable file
·156 lines (122 loc) · 3.94 KB
/
marcxml2bf-done
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
#!/usr/bin/env python
import yaml
import sys
import os
import subprocess
import glob
import datetime
from time import gmtime, strftime, sleep
from shutil import rmtree
from multiprocessing.dummy import Pool as ThreadPool
import itertools
from os import listdir
from os.path import isdir, isfile, join
from modules.config_parser import args
def Convert(config, j):
config = config
n = j["pos"]
print("Processing job: " + str(n) + "; " + j["infile"] + "; " + j["outfile"])
cmd = config["command"]
cmd = cmd.replace('%INFILE%', j["infile"])
cmd = cmd.replace('%OUTFILE%', j["tmpfile"])
returned_value = subprocess.Popen(cmd, shell=True).wait()
graphcmd = "cd %TMPDIR% && for f in *.xml ; do xsltproc %MODULES%/graphiphy.xsl $f > $f.1 ; done"
graphcmd = graphcmd.replace('%MODULES%', j["modulesdir"])
graphcmd = graphcmd.replace('%TMPDIR%', j["tmpdir"])
returned_value = subprocess.Popen(graphcmd, shell=True).wait()
baseurlcmd = "cd %TMPDIR% && for f in *.1 ; do sed -e 's|//staging.lds.loc.gov/|https://id.loc.gov/|g'; 's|http://localhost:10080/|https://id.loc.gov/|g' < $f > %OUTDIR%/$f ; done"
baseurlcmd.replace('%TMPDIR%', j["tmpdir"])
baseurlcmd.replace('%OUTDIR%', j["outdir"])
print(baseurlcmd)
returned_value = subprocess.Popen(baseurlcmd, shell=True).wait()
# not waiting, so this crashes the last copying:
# rmtree(j["tmpdir"])
def dircontents(path, files):
for f in listdir(path):
fpath = join(path, f)
if isfile(fpath) and fpath.endswith('.xml'):
files.append(fpath)
elif isdir(fpath):
dircontents(fpath, files)
return files
config = yaml.safe_load(open(args.config))
print()
print("Config:")
print(config)
print()
jobconfig = config["marcxml2bfdone"]
print("Job config:")
print(jobconfig)
print()
files = []
files = dircontents(jobconfig["source_directory"], files)
#print(str(len(files)))
# files = files[:10]
#print(files)
#print(str(len(files)))
#print()
pos = 1
dirs = []
jobs = []
for f in files:
infile = f
modulesdir = jobconfig["modules_directory"]
tmpdir = jobconfig["tmp_processing_directory"]
if jobconfig["target_directory_single_dir"]:
outfile = f.replace(jobconfig["source_directory"], '')
# outfile = outfile.replace('/', '_')
outfile = jobconfig["target_directory"] + outfile
outdir = jobconfig["target_directory"]
tmpfile = tmpdir + f.replace(jobconfig["source_directory"], "")
else:
outfile = f.replace(jobconfig["source_directory"], jobconfig["target_directory"])
outdir = jobconfig["target_directory"]
dirs.append(outfile)
j = {
"pos": pos,
"infile": infile,
"outfile": outfile,
"outdir": outdir,
"tmpdir": tmpdir,
"modulesdir": modulesdir,
"tmpfile": tmpfile
}
jobs.append(j)
pos += 1
if jobconfig["clean_target_directory"]:
for f in glob.glob(jobconfig["target_directory"] + "*", recursive=True):
if isfile(f):
os.unlink(f)
for d in dirs:
os.makedirs(os.path.dirname(d), exist_ok=True)
print()
print("Number of threads: " + str(jobconfig["threads"]))
print("Total number of jobs: " + str(len(jobs)))
print()
st = datetime.datetime.now()
starttime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
'''
threadPool = threading.BoundedSemaphore(jobconfig["threads"])
handler=ThreadHandler(jobconfig, jobs)
handler.start()
handler.join()
'''
# make the Pool of workers
pool = ThreadPool(jobconfig["threads"])
# open the urls in their own threads
# and return the results
results = pool.starmap(Convert, zip(itertools.repeat(jobconfig), jobs))
# close the pool and wait for the work to finish
pool.close()
pool.join()
gt = gmtime()
endtime = strftime("%Y-%m-%d %H:%M:%S", gt)
et = datetime.datetime.now()
timedelta = et - st
print()
print()
print ("Task started at: " + starttime)
print ("Task ended at: " + endtime)
print ("Elapsed time: ", str(timedelta))
print()
print()