Browse Source

Automatically invoke 2to3 on built/direct for a Python 3 build

rdb 12 years ago
parent
commit
4b9da436a0
2 changed files with 47 additions and 4 deletions
  1. 6 4
      makepanda/makepanda.py
  2. 41 0
      makepanda/makepandacore.py

+ 6 - 4
makepanda/makepanda.py

@@ -1611,12 +1611,14 @@ def RunGenPyCode(target, inputs, opts):
     if (PkgSkip("PYTHON") != 0):
         return
 
-    cmdstr = sys.executable + " -B " + os.path.join("direct", "src", "ffi", "jGenPyCode.py")
+    cmdstr = sys.executable + " -B " + os.path.join(GetOutputDir(), "direct", "ffi", "jGenPyCode.py")
     if (GENMAN): cmdstr += " -d"
     cmdstr += " -r"
     for i in inputs:
-        if (GetOrigExt(i)==".dll"):
-            cmdstr += " " + os.path.basename(os.path.splitext(i)[0].replace("_d","").replace(GetOutputDir()+"/lib/",""))
+        if (GetOrigExt(i)==".pyd"):
+            cmdstr += " panda3d." + os.path.basename(os.path.splitext(i)[0])
+        elif (GetOrigExt(i)==".dll"):
+            cmdstr += " " + os.path.basename(os.path.splitext(i)[0].replace("_d",""))
 
     oscmd(cmdstr)
 
@@ -2379,7 +2381,7 @@ CreatePandaVersionFiles()
 ##########################################################################################
 
 if (PkgSkip("DIRECT")==0):
-    CopyTree(GetOutputDir()+'/direct', 'direct/src')
+    CopyPythonTree(GetOutputDir() + '/direct', 'direct/src', lib2to3_fixers=['all'])
     ConditionalWriteFile(GetOutputDir() + '/direct/__init__.py', "")
     if (GetTarget() == 'windows'):
         CopyFile(GetOutputDir()+'/bin/panda3d.py', 'direct/src/ffi/panda3d.py')

+ 41 - 0
makepanda/makepandacore.py

@@ -2343,6 +2343,47 @@ def CopyTree(dstdir, srcdir, omitCVS=True):
         else:
             cmd = 'cp -R -f ' + srcdir + ' ' + dstdir
         oscmd(cmd)
+        if omitCVS:
+            DeleteCVS(dstdir)
+
+def CopyPythonTree(dstdir, srcdir, lib2to3_fixers=[]):
+    if (not os.path.isdir(dstdir)):
+        os.mkdir(dstdir)
+
+    lib2to3 = None
+    if len(lib2to3_fixers) > 0 and sys.version_info >= (3, 0):
+        from lib2to3.main import main as lib2to3
+        lib2to3_args = ['-w', '-n', '--no-diffs', '-x', 'buffer', '-x', 'idioms', '-x', 'set_literal', '-x', 'ws_comma']
+        if lib2to3_fixers != ['all']:
+            for fixer in lib2to3_fixers:
+                lib2to3_args += ['-f', fixer]
+
+    refactor = []
+    for entry in os.listdir(srcdir):
+        srcpth = os.path.join(srcdir, entry)
+        dstpth = os.path.join(dstdir, entry)
+        if (os.path.isfile(srcpth)):
+            base, ext = os.path.splitext(entry)
+            if (entry != ".cvsignore" and ext not in SUFFIX_INC):
+                if (NeedsBuild([dstpth], [srcpth])):
+                    WriteBinaryFile(dstpth, ReadBinaryFile(srcpth))
+
+                    if ext == '.py' and not entry.endswith('-extensions.py'):
+                        refactor.append((dstpth, srcpth))
+                    else:
+                        JustBuilt([dstpth], [srcpth])
+
+        elif (entry != "CVS"):
+            CopyPythonTree(dstpth, srcpth, lib2to3_fixers)
+
+    for dstpth, srcpth in refactor:
+        if lib2to3 is not None:
+            ret = lib2to3("lib2to3.fixes", lib2to3_args + [dstpth])
+            if ret != 0:
+                os.remove(dstpth)
+                exit("Error in lib2to3.")
+        JustBuilt([dstpth], [srcpth])
+
 
 ########################################################################
 ##