-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
processor.py
executable file
·445 lines (395 loc) · 16.3 KB
/
processor.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/python -u
from __future__ import print_function
import sys,os,re
from datetime import datetime
from stat import *
import tempfile
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
try:
from cPickle import Pickler, Unpickler
except ImportError:
from pickle import Pickler, Unpickler
import subprocess as sub
import string
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
try:
reload(sys)
sys.setdefaultencoding("utf-8") # Needs Python Unicode build !
except:
pass
try:
import json
except:
import simplejson as json
class ElementMissing(Exception):
pass
class TooFewArgs(Exception):
pass
class NoFilesError(Exception):
pass
class FileNotFound(Exception):
pass
class MissingHumansFile(Exception):
pass
class NoLicense(Exception):
pass
def fixEndings(s):
s = s.replace('\r\n', '\n')
s = s.replace('\r', '\n')
return s
class ApplyLicense:
def __init__(self):
self.rex = r"(?sm)^^(/\*.*?^\s*\*/\n*)(.*)"
m = re.match(self.rex, fixEndings(open( os.path.join("src","load.js")).read()) )
if m:
self.license = "%s\n" % m.group(1).strip()
else:
raise NoLicense
print(self.license)
def apply(self):
for path in [".", "src", os.path.join("tests", "std"), os.path.join("tests","std","humans"),os.path.join("tests","std","bundled"), os.path.join("tests","std","machines"),os.path.join("tests","citeproc-js")]:
for file in os.listdir( path ):
if file == "CHANGES.txt" or file == "DESIDERATA.txt":
continue
self.process_file(path,file)
def process_file(self,path,file):
filepath = os.path.join( path, file)
if not filepath.endswith(".js") and not filepath.endswith(".txt") and not filepath.endswith(".json") and not filepath.endswith("README.txt"): return
text = fixEndings(open(filepath).read())
oldtext = text
m = re.match(self.rex,text)
if m:
text = "%s\n%s" % (self.license, m.group(2))
else:
text = "%s%s" % (self.license, text)
if text.strip() != oldtext.strip():
open(filepath,"w+").write(text)
class Params:
def __init__(self,opt,args,force=None):
self.opt = opt
self.args = args
self.script = os.path.split(sys.argv[0])[1]
self.pickle = ".".join((os.path.splitext( self.script )[0], "pkl"))
self.force = force
self.files = {}
self.tests = os.path.join(os.getcwd(), "processor-tests")
self.files['humans'] = {}
self.files['machines'] = []
mypath = os.path.split(sys.argv[0])[0]
self.base = os.path.join( mypath )
if len(self.base):
os.chdir(self.base)
self.initConfig()
def path(self):
if self.force:
return ( os.path.join( self.tests, self.force), )
else:
return (os.path.join(self.tests),)
def getSourcePaths(self):
if len(self.args) == 2:
filename = "%s_%s.txt" % tuple(self.args)
filepath = None
for path in self.path():
if os.path.exists( os.path.join(path, "humans", filename)):
filepath = (path,os.path.join("humans", filename))
break
if not filepath:
raise MissingHumansFile(filename,[os.path.join(p,"humans") for p in self.path()])
self.files['humans'][filename] = (filepath)
else:
for path in self.path():
for filename in os.listdir(os.path.join(path,"humans")):
if not filename.endswith(".txt"): continue
if args:
if not filename.startswith("%s_" % self.args[0]): continue
if not self.files['humans'].get(filename):
self.files['humans'][filename] = (path,os.path.join("humans",filename))
def clearSource(self):
for path in self.path():
mstd = os.path.join(path, "machines")
for file in os.listdir(mstd):
if not file.endswith(".json"): continue
os.unlink(os.path.join(mstd, file))
def refreshSource(self,force=False):
groups = {}
for filename in self.files['humans'].keys():
hpath = self.files['humans'][filename]
mpath = os.path.join( self.files['humans'][filename][0], "machines", "%s.json" % filename[:-4] )
hp = os.path.sep.join( hpath )
mp = os.path.join( mpath )
#if force:
# self.grindFile(hpath,filename,mp)
if not os.path.exists( mp ):
self.grindFile(hpath,filename,mp)
if self.opt.verbose:
print("Created: %s" % mp)
hmod = os.stat(hp)[ST_MTIME]
mmod = os.stat(mp)[ST_MTIME]
if hmod > mmod:
if self.opt.verbose:
print("Old: %s" % mp)
self.grindFile(hpath,filename,mp)
m = re.match("([a-z]*)_.*",filename)
if m:
groupkey = m.group(1)
if not groups.get(groupkey):
groups[groupkey] = {"mtime":0,"tests":[]}
groups[groupkey]["tests"].append(filename)
if hmod > groups[groupkey]["mtime"]:
groups[groupkey]["mtime"] = mmod
def grindFile(self,hpath,filename,mp):
if self.opt.verbose:
sys.stdout.write(".")
test = CslTest(opt,self.cp,hpath,filename)
test.parse()
test.repair()
test.dump(mp)
def validateSource(self):
skip_to_pos = 0
if os.path.exists( self.pickle ):
upfh = open(self.pickle, 'rb')
unpickler = Unpickler(upfh)
old_opt,old_pos = unpickler.load()
if self.opt == old_opt:
skip_to_pos = old_pos
pos = -1
for filename in sorted(self.files['humans']):
pos += 1
if pos < skip_to_pos: continue
p = self.files['humans'][filename]
test = CslTest(opt,self.cp,p,filename,pos=pos)
test.parse()
test.validate()
if os.path.exists( self.pickle ):
os.unlink(self.pickle)
def initConfig(self):
for path in self.path():
if not os.path.exists(os.path.join(path, "machines")):
os.makedirs(os.path.join(path, "machines"))
if not os.path.exists( os.path.join("config") ):
os.makedirs( os.path.join("config") )
if not os.path.exists( os.path.join("config", "processor.cnf") ):
test_template = '''[jing]
command: java -jar
path: ./jing/jing-20131210.jar
[csl]
v1.0: ./schema/csl.rnc
'''
ofh = open( os.path.join("config", "processor.cnf"), "w+" )
ofh.write(test_template)
ofh.close()
self.cp = ConfigParser()
self.cp.read(os.path.join("config", "processor.cnf"))
class CslTest:
def __init__(self,opt,cp,hpath,testname,pos=0):
self.opt = opt
self.cp = cp
self.pos = pos
self.testname = testname
self.hpath = hpath
self.hp = os.path.sep.join( hpath )
self.CREATORS = ["author","editor","translator","recipient","interviewer"]
self.CREATORS += ["composer","original-author","container-author","collection-editor"]
self.RE_ELEMENT = '(?sm)^(.*>>=[^\n]*%s[^\n]+)(.*)(\n<<=.*%s.*)'
self.RE_FILENAME = r"^[a-z]+_[a-zA-Z0-9]+\.txt$"
self.script = os.path.split(sys.argv[0])[1]
self.pickle = ".".join((os.path.splitext( self.script )[0], "pkl"))
self.data = {}
self.raw = fixEndings(open( os.path.sep.join(hpath), 'br').read().decode('utf-8'))
def parse(self):
for element in ["MODE","CSL"]:
self.extract(element,required=True,is_json=False)
if element == "CSL" and self.data['csl'].endswith('.csl'):
self.data['csl'] = fixEndings(open( os.path.join("styles", self.data['csl'])).read())
self.extract("VERSION",required=False,is_json=False)
self.extract("TAGS",required=False,is_json=False)
self.extract("RESULT",required=True,is_json=False)
self.extract("INPUT",required=True,is_json=True)
self.extract("CITATION-ITEMS",required=False,is_json=True)
self.extract("CITATIONS",required=False,is_json=True)
self.extract("BIBENTRIES",required=False,is_json=True)
self.extract("BIBSECTION",required=False,is_json=True)
self.extract("ABBREVIATIONS",required=False,is_json=True)
def extract(self,tag,required=False,is_json=False,rstrip=False):
m = re.match(self.RE_ELEMENT %(tag,tag),self.raw)
data = False
if m:
if rstrip:
data = m.group(2).rstrip()
else:
data = m.group(2).strip()
elif required:
raise ElementMissing(self.script,tag,self.testname)
if data != False:
if is_json:
data = json.loads(data)
self.data[tag.lower().replace('-','_')] = data
else:
self.data[tag.lower().replace('-','_')] = False
def repair(self):
self.fix_dates()
input_str = json.dumps(self.data["input"],indent=4,sort_keys=True,ensure_ascii=False)
m = re.match(self.RE_ELEMENT % ("INPUT", "INPUT"),self.raw)
newraw = m.group(1) + "\n" + input_str + m.group(3)
if self.data["citation_items"]:
citations_str = json.dumps(self.data["citation_items"],indent=4,sort_keys=True,ensure_ascii=False)
m = re.match(self.RE_ELEMENT % ("CITATION-ITEMS", "CITATION-ITEMS"),self.raw)
newraw = m.group(1) + "\n" + citations_str + m.group(3)
if self.data["citations"]:
citations_str = json.dumps(self.data["citations"],indent=4,sort_keys=True,ensure_ascii=False)
m = re.match(self.RE_ELEMENT % ("CITATIONS", "CITATIONS"),self.raw)
newraw = m.group(1) + "\n" + citations_str + m.group(3)
if self.data["abbreviations"]:
abbreviations_str = json.dumps(self.data["abbreviations"],indent=4,sort_keys=True,ensure_ascii=False)
m = re.match(self.RE_ELEMENT % ("ABBREVIATIONS", "ABBREVIATIONS"),self.raw)
newraw = m.group(1) + "\n" + abbreviations_str + m.group(3)
if self.raw != newraw:
open(self.hp,"w+").write(newraw)
def fix_dates(self):
for pos in range(0, len(self.data["input"]),1):
for k in ["issued", "event-date", "accessed", "container", "original-date"]:
if self.data["input"][pos].get(k):
newdate = []
if not self.data["input"][pos][k].get("date-parts"):
start = []
for e in ["year","month","day"]:
if self.data["input"][pos][k].get(e):
start.append( self.data["input"][pos][k][e] )
self.data["input"][pos][k].pop(e)
else:
break
if start:
newdate.append(start)
end = []
for e in ["year_end","month_end","day_end"]:
if self.data["input"][pos][k].get(e):
end.append( self.data["input"][pos][k][e] )
self.data["input"][pos][k].pop(e)
else:
break
if end:
newdate.append(end)
self.data["input"][pos][k]["date-parts"] = newdate
def dump(self, mpath):
json.dump(self.data, open(mpath,"w+"), indent=4, sort_keys=True, ensure_ascii=False )
def validate(self):
if self.opt.verbose:
print(self.testname)
if not os.path.exists(self.cp.get("jing", "path")):
print("Error: jing not found.")
print(" Looked in: %s" % self.cp.get("jing", "path"))
sys.exit()
m = re.match("(?sm).*version=\"([.0-9a-z]+)\".*",self.data["csl"])
if m:
rnc_path = os.path.join(self.cp.get("csl", "v%s" % m.group(1)))
else:
print("Error: Unable to find CSL version in %s" % self.hp)
sys.exit()
tfd,tfilename = tempfile.mkstemp(dir=".")
os.write(tfd,self.data["csl"].encode('utf8'))
os.close(tfd)
jfh = os.popen("%s %s -c %s %s" % (self.cp.get("jing", "command"), self.cp.get("jing", "path"),rnc_path,tfilename))
success = True
plural = ""
while 1:
line = jfh.readline()
if not line: break
line = line.strip()
e = re.match("^fatal:",line)
if e:
print(line)
sys.exit()
m = re.match(".*:([0-9]+):([0-9]+): *error:(.*)",line)
if m:
if success:
print("\n##")
print("#### Error%s in CSL for test: %s" % (plural,self.hp))
print("##\n")
success = False
print(" %s @ line %s" %(m.group(3).upper(),m.group(1)))
plural = "s"
jfh.close()
os.unlink(tfilename)
if not success:
print("")
io = StringIO()
io.write(self.data["csl"])
io.seek(0)
linepos = 1
while 1:
cslline = io.readline()
if not cslline: break
cslline = cslline.rstrip()
print("%3d %s" % (linepos,cslline))
linepos += 1
pfh = open( self.pickle,"wb+")
pickler = Pickler( pfh )
pickler.dump( (opt, self.pos) )
sys.exit()
if __name__ == "__main__":
from optparse import OptionParser
os.environ['LANG'] = "en_US.UTF-8"
usage = '\n %prog [options]'
description="This script."
parser = OptionParser(usage=usage,description=description,epilog="Happy testing!")
parser.add_option("-c", "--cranky", dest="cranky",
default=False,
action="store_true",
help='Attempt to validate style code for testing against the CSL schema.')
parser.add_option("-g", "--grind", dest="grind",
default=False,
action="store_true",
help='Force grinding of human-readable test code into machine-readable form.')
parser.add_option("-v", "--verbose", dest="verbose",
default=False,
action="store_true",
help='Display test names during processing.')
(opt, args) = parser.parse_args()
if not opt.grind and not opt.cranky:
parser.print_help()
sys.exit()
# Testing sequence:
# + Get single tests working
# Get automatic grinding for single tests working
# Get forced grinding for single tests working
# Get forced grinding and testing for single tests working
# Get CSL integrity check working for single tests
# Check running of all tests
# Check grinding of all tests followed by testing
# Check CSL integrity check of all tests
#
# Set up paths engine
#
params = Params(opt,args)
try:
params.getSourcePaths()
if opt.grind:
params.clearSource()
params.refreshSource(force=True)
print("")
else:
params.refreshSource()
if opt.cranky:
params.validateSource()
except (KeyboardInterrupt, SystemExit):
for file in os.listdir("."):
if not file.startswith("tmp") or not len(file) == 9: continue
os.unlink(file)
sys.exit()
except MissingHumansFile as error:
parser.print_help()
print('''\nError: File \"%s\" not found.
Looked in:''' % error[0])
for path in error[1]:
print(' %s' % path)
except NoFilesError:
print('\nError: No files to process!\n')
except NoLicense:
print('\nError: No license found in load.js')
print("Processor tests successfully compiled")