setup: add command to generate index of extensions
authorYuya Nishihara <yuya@tcha.org>
Sat, 04 Jun 2011 20:11:10 +0900
changeset 14538 3818c67a501e
parent 14537 3c7907dc95ca
child 14539 558ec14ba6be
setup: add command to generate index of extensions It generates prebuilt index of all extensions, which will be used by frozen exe when running 'hg help extensions'. Now py2exe invokes this command automatically.
.hgignore
setup.py
--- a/.hgignore	Mon Jun 06 21:02:48 2011 +0200
+++ b/.hgignore	Sat Jun 04 20:11:10 2011 +0900
@@ -35,6 +35,7 @@
 cscope.*
 i18n/hg.pot
 locale/*/LC_MESSAGES/hg.mo
+hgext/__index__.py
 
 # files installed with a local --pure build
 mercurial/base85.py
--- a/setup.py	Mon Jun 06 21:02:48 2011 +0200
+++ b/setup.py	Sat Jun 04 20:11:10 2011 +0900
@@ -56,7 +56,7 @@
 import shutil
 import tempfile
 from distutils import log
-from distutils.core import setup, Extension
+from distutils.core import setup, Command, Extension
 from distutils.dist import Distribution
 from distutils.command.build import build
 from distutils.command.build_ext import build_ext
@@ -64,7 +64,7 @@
 from distutils.command.install_scripts import install_scripts
 from distutils.spawn import spawn, find_executable
 from distutils.ccompiler import new_compiler
-from distutils.errors import CCompilerError
+from distutils.errors import CCompilerError, DistutilsExecError
 from distutils.sysconfig import get_python_inc
 from distutils.version import StrictVersion
 
@@ -260,6 +260,34 @@
             else:
                 yield module
 
+class buildhgextindex(Command):
+    description = 'generate prebuilt index of hgext (for frozen package)'
+    user_options = []
+    _indexfilename = 'hgext/__index__.py'
+
+    def initialize_options(self):
+        pass
+
+    def finalize_options(self):
+        pass
+
+    def run(self):
+        if os.path.exists(self._indexfilename):
+            os.unlink(self._indexfilename)
+
+        # here no extension enabled, disabled() lists up everything
+        code = ('import pprint; from mercurial import extensions; '
+                'pprint.pprint(extensions.disabled())')
+        out, err = runcmd([sys.executable, '-c', code], env)
+        if err:
+            raise DistutilsExecError(err)
+
+        f = open(self._indexfilename, 'w')
+        f.write('# this file is autogenerated by setup.py\n')
+        f.write('docs = ')
+        f.write(out)
+        f.close()
+
 class hginstallscripts(install_scripts):
     '''
     This is a specialization of install_scripts that replaces the @LIBDIR@ with
@@ -309,6 +337,7 @@
 cmdclass = {'build_mo': hgbuildmo,
             'build_ext': hgbuildext,
             'build_py': hgbuildpy,
+            'build_hgextindex': buildhgextindex,
             'install_scripts': hginstallscripts}
 
 packages = ['mercurial', 'mercurial.hgweb',
@@ -373,6 +402,8 @@
         {'script':'hg',
          'copyright':'Copyright (C) 2005-2010 Matt Mackall and others',
          'product_version':version}]
+    # sub command of 'build' because 'py2exe' does not handle sub_commands
+    build.sub_commands.insert(0, ('build_hgextindex', None))
 
 if os.name == 'nt':
     # Windows binary file versions for exe/dll files must have the