将 SConstruct 移植到 Python 3

Porting SConstruct to Python 3

2020 年使 SConstruct Python 3 兼容的最佳做法是什么?

例如,运行旧的SConstruct给出了这样的错误。

✗ python ~/scons/scripts/scons.py
scons: Reading SConscript files ...
  File "/home/techtonik/Folding@home/fah-control/SConstruct", line 17

    except Exception, e:

                    ^

SyntaxError: invalid syntax

2to3modernize 应该有所帮助。只是 Python 语法有问题。

2to3 随 Python 一起提供,也可以作为 Python 模块执行:

python -m lib2to3 -w SConstruct

这是建议的 2to3 补丁,看起来很小:

RefactoringTool: Refactored SConstruct
--- SConstruct  (original)
+++ SConstruct  (refactored)
@@ -3,8 +3,8 @@
 env = Environment(ENV = os.environ)
 try:
     env.Tool('config', toolpath = [os.environ.get('CBANG_HOME')])
-except Exception, e:
-    raise Exception, 'CBANG_HOME not set?\n' + str(e)
+except Exception as e:
+    raise Exception('CBANG_HOME not set?\n' + str(e))
 
 env.CBLoadTools('packager run_distutils osx fah-client-version')
 env.CBAddVariables(
@@ -14,7 +14,7 @@
 # Version
 try:
     version = env.FAHClientVersion()
-except Exception, e:
+except Exception as e:
     print(e)
     version = '0.0.0'
     env.Replace(PACKAGE_VERSION = version)
RefactoringTool: Files that need to be modified:
RefactoringTool: SConstruct