[Numpy-svn] r3186 - in trunk/numpy: numarray oldnumeric

numpy-svn at scipy.org numpy-svn at scipy.org
Tue Sep 19 02:58:46 CDT 2006


Author: oliphant
Date: 2006-09-19 02:58:41 -0500 (Tue, 19 Sep 2006)
New Revision: 3186

Modified:
   trunk/numpy/numarray/alter_code1.py
   trunk/numpy/oldnumeric/alter_code1.py
Log:
Add convertsrc to alter_code1 and clean it up so it doesn't always write the .orig file

Modified: trunk/numpy/numarray/alter_code1.py
===================================================================
--- trunk/numpy/numarray/alter_code1.py	2006-09-19 00:20:56 UTC (rev 3185)
+++ trunk/numpy/numarray/alter_code1.py	2006-09-19 07:58:41 UTC (rev 3186)
@@ -52,7 +52,7 @@
    - .setimaginary() --> .imag
    
 """
-__all__ = ['convertfile', 'convertall', 'converttree']
+__all__ = ['convertfile', 'convertall', 'converttree', 'convertsrc']
 
 import sys
 import os
@@ -164,6 +164,7 @@
     
 import datetime
 def fromstr(filestr):
+    savestr = filestr[:]
     filestr, fromall = changeimports(filestr, 'numarray', 'numpy.numarray')
     base = 'numarray'
     newbase = 'numpy.numarray'
@@ -175,40 +176,42 @@
 
     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.numarray %s by %s\n\n%s' % (today, name, filestr)
-    return filestr
+    if savestr != filestr:
+        name = os.path.split(sys.argv[0])[-1]
+        today = datetime.date.today().strftime('%b %d, %Y')
+        filestr = '## Automatically adapted for '\
+                  'numpy.numarray %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, orig=1):
+    """Convert the filename given from using Numarray to using NumPy
 
-def convertfile(filename):
-    """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)
 
-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
@@ -217,7 +220,44 @@
     """
     files = glob.glob(os.path.join(direc,'*.py'))
     for afile in files:
-        convertfile(afile)
+        if afile[-8:] == 'setup.py': continue
+        convertfile(afile, orig)
 
+header_re = re.compile(r'(numarray/libnumarray.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/libnumarray.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, orig=0)
+    convertsrc(dirname, ['h','c'], orig=0)
+
+def converttree(direc=os.path.curdir):
+    """Convert all .py files in the tree given
+
+    """
+    os.path.walk(direc, _func, None)
+
+
 if __name__ == '__main__':
-    fromargs(sys.argv)
+    converttree(sys.argv)

Modified: trunk/numpy/oldnumeric/alter_code1.py
===================================================================
--- trunk/numpy/oldnumeric/alter_code1.py	2006-09-19 00:20:56 UTC (rev 3185)
+++ trunk/numpy/oldnumeric/alter_code1.py	2006-09-19 07:58:41 UTC (rev 3186)
@@ -28,7 +28,7 @@
  * 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 @@
 
 import datetime
 def fromstr(filestr):
+    savestr = filestr[:]
     filestr = fixtypechars(filestr)
     filestr = fixistesting(filestr)
     filestr, fromall1 = changeimports(filestr, 'Numeric', 'numpy.oldnumeric')
@@ -151,56 +152,84 @@
     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)
         



More information about the Numpy-svn mailing list