[py-svn] r17504 - py/dist/py/misc

hpk at codespeak.net hpk at codespeak.net
Mon Sep 12 17:38:49 CEST 2005


Author: hpk
Date: Mon Sep 12 17:38:48 2005
New Revision: 17504

Added:
   py/dist/py/misc/dynpkg.py
Log:
experimental code (not expoerted to the outside world) 
to support building setup.py-style packages on the fly 
from an __init__.py file representing the resulting
package on a project. 



Added: py/dist/py/misc/dynpkg.py
==============================================================================
--- (empty file)
+++ py/dist/py/misc/dynpkg.py	Mon Sep 12 17:38:48 2005
@@ -0,0 +1,79 @@
+"""
+
+"""
+
+import py
+import sys
+
+log = py.log.get("dynpkg", 
+                 info=py.log.STDOUT, 
+                 debug=py.log.STDOUT,
+                 command=None) # py.log.STDOUT)
+
+class DistPython: 
+    def __init__(self, location=None, python=None): 
+        if python is None:
+            python = py.std.sys.executable
+        self.python = python
+        if location is None:
+            location = py.path.local()
+        self.location = location 
+
+    def clean(self):
+        out = self._exec("clean -a")
+        #print out
+
+    def build(self):
+        out = self._exec("build")
+        #print out
+
+    def _exec(self, cmd):
+        python = self.python 
+        old = self.location.chdir()
+        try:
+            cmd = "%(python)s setup.py %(cmd)s" % locals()
+            log.command(cmd)
+            out = py.process.cmdexec(cmd)
+        finally:
+            old.chdir()
+        return out 
+
+    def get_package_path(self, pkgname): 
+        pkg = self._get_package_path(pkgname) 
+        if pkg is None:
+            #self.clean()
+            self.build()
+            pkg = self._get_package_path(pkgname)
+        assert pkg is not None 
+        return pkg 
+
+    def _get_package_path(self, pkgname):
+        major, minor = py.std.sys.version_info[:2]
+        #assert major >=2 and minor in (3,4,5)
+        suffix = "%s.%s" %(major, minor)
+        location = self.location
+        base = location.join('build')
+        if base.check(dir=1):
+            # XXX check if we need to rebuild
+            for pkg in base.visit(lambda x: x.check(dir=1)): 
+                if pkg.basename == pkgname:
+                    if pkg.dirpath().basename.endswith(suffix): 
+                        return pkg
+
+def setpkg(distdir, finalpkgname):
+    assert distdir.check(dir=1)
+    dist = DistPython(distdir) 
+    pkg = dist.get_package_path(finalpkgname) 
+    assert pkg.check(dir=1)
+    sys.path.insert(0, str(pkg.dirpath()))
+    try:
+        modname = pkg.purebasename
+        if modname in sys.modules:
+            log.debug("removing from sys.modules:", modname)
+            del sys.modules[modname]
+        sys.modules[modname] = mod = __import__(modname) 
+    finally: 
+        sys.path[0] # XXX
+    log.info("module is at", mod.__file__) 
+    return mod 
+ 



More information about the pytest-commit mailing list