-
Notifications
You must be signed in to change notification settings - Fork 2
/
marcxml2bf
executable file
·129 lines (101 loc) · 2.8 KB
/
marcxml2bf
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
#!/usr/bin/env python
import yaml
import sys
import os
import glob
import datetime
from time import gmtime, strftime, sleep
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["outfile"])
# print(cmd)
os.system(cmd)
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["marcxml2bf"]
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
if jobconfig["target_directory_single_dir"]:
outfile = f.replace(jobconfig["source_directory"], '')
outfile = outfile.replace('/', '_')
outfile = jobconfig["target_directory"] + outfile
else:
outfile = f.replace(jobconfig["source_directory"], jobconfig["target_directory"])
dirs.append(outfile)
j = {
"pos": pos,
"infile": infile,
"outfile": outfile
}
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()