[Python-checkins] python/nondist/sandbox/setuptools/setuptools/command bdist_egg.py, 1.27, 1.28

pje@users.sourceforge.net pje at users.sourceforge.net
Sun Aug 14 23:17:54 CEST 2005


Update of /cvsroot/python/python/nondist/sandbox/setuptools/setuptools/command
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25708/setuptools/command

Modified Files:
	bdist_egg.py 
Log Message:
Auto-generate namespace __init__.py files for packages without them.  This
is a workaround for packages like 'll-color', which are distributed without
'll/__init__.py', to avoid overwriting ll-core's copy of ll/__init__.py.
This allows existing packages that use this sort of kludging to be treated
as a crude namespace package, as long as the "real" __init__.py also 
calls declare_namespace().


Index: bdist_egg.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setuptools/setuptools/command/bdist_egg.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- bdist_egg.py	12 Jul 2005 23:43:21 -0000	1.27
+++ bdist_egg.py	14 Aug 2005 21:17:45 -0000	1.28
@@ -26,8 +26,8 @@
     ]))
     f.close()
 
-
-
+# stub __init__.py for packages distributed without one
+NS_PKG_STUB = '__import__("pkg_resources").declare_namespace(__name__)'
 
 
 
@@ -186,7 +186,7 @@
                 write_stub(os.path.basename(ext_name), pyfile)
             to_compile.append(pyfile)
             ext_outputs[p] = ext_name.replace(os.sep,'/')
-
+        to_compile.extend(self.make_init_files())
         if to_compile:
             cmd.byte_compile(to_compile)
 
@@ -260,30 +260,30 @@
         log.warn("zip_safe flag not set; analyzing archive contents...")
         return analyze_egg(self.bdist_dir, self.stubs)
 
+    def make_init_files(self):
+        """Create missing package __init__ files"""
+        init_files = []       
+        for base,dirs,files in walk_egg(self.bdist_dir):
+            if base==self.bdist_dir:
+                # don't put an __init__ in the root
+                continue
+            for name in files:
+                if name.endswith('.py'):
+                    if '__init__.py' not in files:
+                        pkg = base[len(self.bdist_dir)+1:].replace(os.sep,'.')
+                        if self.distribution.has_contents_for(pkg):
+                            log.warn("Creating missing __init__.py for %s",pkg)
+                            filename = os.path.join(base,'__init__.py')
+                            if not self.dry_run:
+                                f = open(filename,'w'); f.write(NS_PKG_STUB)
+                                f.close()    
+                            init_files.append(filename)
+                    break
+            else:
+                # not a package, don't traverse to subdirectories
+                dirs[:] = []
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+        return init_files
 
 def walk_egg(egg_dir):
     """Walk an unpacked egg's contents, skipping the metadata directory"""



More information about the Python-checkins mailing list