mirror of https://github.com/python/cpython
Various tweaks. It now returns the exact same files as Matthias' tool 95% of the time.
This commit is contained in:
parent
f0d750803d
commit
61c64c9de0
|
@ -5,9 +5,10 @@ import re
|
||||||
import sys
|
import sys
|
||||||
import macfs
|
import macfs
|
||||||
import os
|
import os
|
||||||
|
import macostools
|
||||||
|
|
||||||
BEGINDEFINITION=re.compile("^<<(?P<name>.*)>>=\s*")
|
BEGINDEFINITION=re.compile("^<<(?P<name>.*)>>=\s*")
|
||||||
USEDEFINITION=re.compile("^(?P<pre>.*)<<(?P<name>.*)>>[^=]")
|
USEDEFINITION=re.compile("^(?P<pre>.*)<<(?P<name>.*)>>(?P<post>[^=].*)")
|
||||||
ENDDEFINITION=re.compile("^@")
|
ENDDEFINITION=re.compile("^@")
|
||||||
|
|
||||||
class Processor:
|
class Processor:
|
||||||
|
@ -27,34 +28,36 @@ class Processor:
|
||||||
self.pushback = None
|
self.pushback = None
|
||||||
return rv
|
return rv
|
||||||
self.lineno = self.lineno + 1
|
self.lineno = self.lineno + 1
|
||||||
return self.fp.readline()
|
return self.lineno, self.fp.readline()
|
||||||
|
|
||||||
def _linedirective(self):
|
def _linedirective(self, lineno):
|
||||||
"""Return a #line cpp directive for the current input file position"""
|
"""Return a #line cpp directive for this file position"""
|
||||||
return '#line %d "%s"\n'%(self.lineno-2, os.path.split(self.filename)[1])
|
return '#line %d "%s"\n'%(lineno-3, os.path.split(self.filename)[1])
|
||||||
|
|
||||||
def _readitem(self):
|
def _readitem(self):
|
||||||
"""Read the definition of an item. Insert #line where needed. """
|
"""Read the definition of an item. Insert #line where needed. """
|
||||||
rv = [self._linedirective()]
|
rv = []
|
||||||
while 1:
|
while 1:
|
||||||
line = self._readline()
|
lineno, line = self._readline()
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
if ENDDEFINITION.search(line):
|
if ENDDEFINITION.search(line):
|
||||||
break
|
break
|
||||||
if BEGINDEFINITION.match(line):
|
if BEGINDEFINITION.match(line):
|
||||||
self.pushback = line
|
self.pushback = lineno, line
|
||||||
break
|
break
|
||||||
mo = USEDEFINITION.match(line)
|
mo = USEDEFINITION.match(line)
|
||||||
if mo:
|
if mo:
|
||||||
pre = mo.group('pre')
|
pre = mo.group('pre')
|
||||||
if pre:
|
if pre:
|
||||||
rv.append(pre+'\n')
|
rv.append((lineno, pre+'\n'))
|
||||||
rv.append(line)
|
rv.append((lineno, line))
|
||||||
# For simplicity we add #line directives now, if
|
# For simplicity we add #line directives now, if
|
||||||
# needed.
|
# needed.
|
||||||
if mo:
|
if mo:
|
||||||
rv.append(self._linedirective())
|
post = mo.group('post')
|
||||||
|
if post and post != '\n':
|
||||||
|
rv.append((lineno, post))
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
def _define(self, name, value):
|
def _define(self, name, value):
|
||||||
|
@ -67,7 +70,7 @@ class Processor:
|
||||||
def read(self):
|
def read(self):
|
||||||
"""Read the source file and store all definitions"""
|
"""Read the source file and store all definitions"""
|
||||||
while 1:
|
while 1:
|
||||||
line = self._readline()
|
lineno, line = self._readline()
|
||||||
if not line: break
|
if not line: break
|
||||||
mo = BEGINDEFINITION.search(line)
|
mo = BEGINDEFINITION.search(line)
|
||||||
if mo:
|
if mo:
|
||||||
|
@ -97,7 +100,7 @@ class Processor:
|
||||||
# No rest for the wicked: we have work to do.
|
# No rest for the wicked: we have work to do.
|
||||||
self.resolving[name] = 1
|
self.resolving[name] = 1
|
||||||
result = []
|
result = []
|
||||||
for line in self.items[name]:
|
for lineno, line in self.items[name]:
|
||||||
mo = USEDEFINITION.search(line)
|
mo = USEDEFINITION.search(line)
|
||||||
if mo:
|
if mo:
|
||||||
# We replace the complete line. Is this correct?
|
# We replace the complete line. Is this correct?
|
||||||
|
@ -105,7 +108,7 @@ class Processor:
|
||||||
replacement = self._resolve_one(macro)
|
replacement = self._resolve_one(macro)
|
||||||
result = result + replacement
|
result = result + replacement
|
||||||
else:
|
else:
|
||||||
result.append(line)
|
result.append((lineno, line))
|
||||||
self.items[name] = result
|
self.items[name] = result
|
||||||
self.resolved[name] = 1
|
self.resolved[name] = 1
|
||||||
del self.resolving[name]
|
del self.resolving[name]
|
||||||
|
@ -123,16 +126,19 @@ class Processor:
|
||||||
for name in self.items.keys():
|
for name in self.items.keys():
|
||||||
if pattern.search(name):
|
if pattern.search(name):
|
||||||
pathname = os.path.join(dir, name)
|
pathname = os.path.join(dir, name)
|
||||||
data = self._stripduplines(self.items[name])
|
data = self._addlinedirectives(self.items[name])
|
||||||
self._dosave(pathname, data)
|
self._dosave(pathname, data)
|
||||||
|
|
||||||
def _stripduplines(self, data):
|
def _addlinedirectives(self, data):
|
||||||
for i in range(len(data)-1, 0, -1):
|
curlineno = -100
|
||||||
if data[i][:5] == '#line' and data[i-1][:5] == '#line':
|
rv = []
|
||||||
del data[i-1]
|
for lineno, line in data:
|
||||||
if data[-1][:5] == '#line':
|
curlineno = curlineno + 1
|
||||||
del data[-1]
|
if line and line != '\n' and lineno != curlineno:
|
||||||
return data
|
rv.append(self._linedirective(lineno))
|
||||||
|
curlineno = lineno
|
||||||
|
rv.append(line)
|
||||||
|
return rv
|
||||||
|
|
||||||
def _dosave(self, pathname, data):
|
def _dosave(self, pathname, data):
|
||||||
"""Save data to pathname, unless it is identical to what is there"""
|
"""Save data to pathname, unless it is identical to what is there"""
|
||||||
|
@ -140,20 +146,41 @@ class Processor:
|
||||||
olddata = open(pathname).readlines()
|
olddata = open(pathname).readlines()
|
||||||
if olddata == data:
|
if olddata == data:
|
||||||
return
|
return
|
||||||
|
macostools.mkdirs(os.path.split(pathname)[0])
|
||||||
fp = open(pathname, "w").writelines(data)
|
fp = open(pathname, "w").writelines(data)
|
||||||
|
|
||||||
def process(file):
|
def process(file, config):
|
||||||
pr = Processor(file)
|
pr = Processor(file)
|
||||||
pr.read()
|
pr.read()
|
||||||
pr.resolve()
|
pr.resolve()
|
||||||
pr.save(":jacktest:src", "^.*\.cp$")
|
for pattern, folder in config:
|
||||||
pr.save(":jacktest:include", "^.*\.h")
|
pr.save(folder, pattern)
|
||||||
|
|
||||||
|
def readconfig():
|
||||||
|
"""Read a configuration file, if it doesn't exist create it."""
|
||||||
|
configname = sys.argv[0] + '.config'
|
||||||
|
if not os.path.exists(configname):
|
||||||
|
confstr = """config = [
|
||||||
|
("^.*\.cp$", ":unweave-src"),
|
||||||
|
("^.*\.h$", ":unweave-include"),
|
||||||
|
]"""
|
||||||
|
open(configname, "w").write(confstr)
|
||||||
|
print "Created config file", configname
|
||||||
|
## print "Please check and adapt (if needed)"
|
||||||
|
## sys.exit(0)
|
||||||
|
namespace = {}
|
||||||
|
execfile(configname, namespace)
|
||||||
|
return namespace['config']
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
config = readconfig()
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
for file in sys.argv:
|
for file in sys.argv[1:]:
|
||||||
|
if file[-3:] == '.nw':
|
||||||
print "Processing", file
|
print "Processing", file
|
||||||
process(file)
|
process(file, config)
|
||||||
|
else:
|
||||||
|
print "Skipping", file
|
||||||
else:
|
else:
|
||||||
fss, ok = macfs.PromptGetFile("Select .nw source file", "TEXT")
|
fss, ok = macfs.PromptGetFile("Select .nw source file", "TEXT")
|
||||||
if not ok:
|
if not ok:
|
||||||
|
|
Loading…
Reference in New Issue