diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-09-19 07:58:41 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-09-19 07:58:41 +0000 |
commit | 5be3af7f9288f503fc7fe08c97d53c920f235594 (patch) | |
tree | c589eb8ea3387ca7ab888f6f716c757a0351e138 /numpy/oldnumeric/alter_code1.py | |
parent | b18173d63ce02ab0f978178518173da534ce052b (diff) | |
download | numpy-5be3af7f9288f503fc7fe08c97d53c920f235594.tar.gz |
Add convertsrc to alter_code1 and clean it up so it doesn't always write the .orig file
Diffstat (limited to 'numpy/oldnumeric/alter_code1.py')
-rw-r--r-- | numpy/oldnumeric/alter_code1.py | 81 |
1 files changed, 55 insertions, 26 deletions
diff --git a/numpy/oldnumeric/alter_code1.py b/numpy/oldnumeric/alter_code1.py index 0a27fa15b..662b589f5 100644 --- a/numpy/oldnumeric/alter_code1.py +++ b/numpy/oldnumeric/alter_code1.py @@ -28,7 +28,7 @@ Makes the following changes: * Converts uses of type(...) is <type> isinstance(..., <type>) """ -__all__ = ['convertfile', 'convertall', 'converttree'] +__all__ = ['convertfile', 'convertall', 'converttree', 'convertsrc'] import sys import os @@ -130,6 +130,7 @@ def replaceother(astr): import datetime def fromstr(filestr): + savestr = filestr[:] filestr = fixtypechars(filestr) filestr = fixistesting(filestr) filestr, fromall1 = changeimports(filestr, 'Numeric', 'numpy.oldnumeric') @@ -151,56 +152,84 @@ def fromstr(filestr): fromall = fromall1 or fromall2 or fromall3 filestr = replaceattr(filestr) filestr = replaceother(filestr) - today = datetime.date.today().strftime('%b %d, %Y') - name = os.path.split(sys.argv[0])[-1] - filestr = '## Automatically adapted for '\ - 'numpy.oldnumeric %s by %s\n\n%s' % (today, name, filestr) - return filestr + if savestr != filestr: + today = datetime.date.today().strftime('%b %d, %Y') + name = os.path.split(sys.argv[0])[-1] + filestr = '## Automatically adapted for '\ + 'numpy.oldnumeric %s by %s\n\n%s' % (today, name, filestr) + return filestr, 1 + return filestr, 0 def makenewfile(name, filestr): fid = file(name, 'w') fid.write(filestr) fid.close() -def getandcopy(name): - fid = file(name) - filestr = fid.read() - fid.close() - base, ext = os.path.splitext(name) - makenewfile(base+'.orig', filestr) - return filestr - -def convertfile(filename): +def convertfile(filename, orig=1): """Convert the filename given from using Numeric to using NumPy Copies the file to filename.orig and then over-writes the file with the updated code """ - filestr = getandcopy(filename) - filestr = fromstr(filestr) - makenewfile(filename, filestr) + fid = open(filename) + filestr = fid.read() + fid.close() + filestr, changed = fromstr(filestr) + if changed: + if orig: + base, ext = os.path.splitext(filename) + os.rename(filename, base+".orig") + else: + os.remove(filename) + makenewfile(filename, filestr) def fromargs(args): filename = args[1] - convertfile(filename) + converttree(filename) -def convertall(direc=os.path.curdir): +def convertall(direc=os.path.curdir, orig=1): """Convert all .py files to use numpy.oldnumeric (from Numeric) in the directory given - For each file, a backup of <usesnumeric>.py is made as + For each changed file, a backup of <usesnumeric>.py is made as <usesnumeric>.py.orig. A new file named <usesnumeric>.py is then written with the updated code. """ files = glob.glob(os.path.join(direc,'*.py')) for afile in files: - convertfile(afile) - + if afile[-8:] == 'setup.py': continue # skip these + convertfile(afile, orig) + +header_re = re.compile(r'(Numeric/arrayobject.h)') + +def convertsrc(direc=os.path.curdir, ext=None, orig=1): + """Replace Numeric/arrayobject.h with numpy/oldnumeric.h in all files in the + directory with extension give by list ext (if ext is None, then all files are + replaced).""" + if ext is None: + files = glob.glob(os.path.join(direc,'*')) + else: + files = [] + for aext in ext: + files.extend(glob.glob(os.path.join(direc,"*.%s" % aext))) + for afile in files: + fid = open(afile) + fstr = fid.read() + fid.close() + fstr, n = header_re.subn(r'numpy/oldnumeric.h',fstr) + if n > 0: + if orig: + base, ext = os.path.splitext(afile) + os.rename(afile, base+".orig") + else: + os.remove(afile) + makenewfile(afile, fstr) + def _func(arg, dirname, fnames): - convertall(dirname) + convertall(dirname, orig=0) + convertsrc(dirname, ext=['h','c'], orig=0) def converttree(direc=os.path.curdir): - """Convert all .py files in the tree given - + """Convert all .py files and source code files in the tree given """ os.path.walk(direc, _func, None) |