浏览代码

replace console entry points with just importing the module

importing pkg_resources and looking up entry points wastes 100ms+
of time, which is totally unnecessary
qubesuser 6 年之前
父节点
当前提交
b731ef3885
共有 1 个文件被更改,包括 25 次插入4 次删除
  1. 25 4
      setup.py

+ 25 - 4
setup.py

@@ -2,6 +2,7 @@
 
 import os
 import setuptools
+import setuptools.command.install
 import sys
 
 exclude=[]
@@ -19,9 +20,29 @@ def get_console_scripts():
             basename, ext = os.path.splitext(os.path.basename(filename))
             if basename in ['__init__', 'dochelpers'] or ext != '.py':
                 continue
-            yield '{} = qubesadmin.tools.{}:main'.format(
-                basename.replace('_', '-'), basename)
+            yield basename.replace('_', '-'), 'qubesadmin.tools.{}'.format(basename)
 
+# create simple scripts that run much faster than "console entry points"
+class CustomInstall(setuptools.command.install.install):
+    def run(self):
+        bin = os.path.join(self.root, "usr/bin")
+        try:
+            os.makedirs(bin)
+        except:
+            pass
+        for file, pkg in get_console_scripts():
+           path = os.path.join(bin, file)
+           with open(path, "w") as f:
+               f.write(
+"""#!/usr/bin/python3
+from {} import main
+import sys
+if __name__ == '__main__':
+	sys.exit(main())
+""".format(pkg))
+
+           os.chmod(path, 0o755)
+        setuptools.command.install.install.run(self)
 
 if __name__ == '__main__':
     setuptools.setup(
@@ -36,8 +57,8 @@ if __name__ == '__main__':
         package_data={
             'qubesadmin.tests.backup': ['*.xml'],
         },
-        entry_points={
-            'console_scripts': list(get_console_scripts()),
+        cmdclass={
+           'install': CustomInstall
         },
 
         )