Commits

Ronny Pfannschmidt  committed 682b6b6

add the basic package extraction machinery for genscript

  • Participants
  • Parent commits fca30d1

Comments (0)

Files changed (2)

File genscript/pkgpacker.py

+import py
+
+def find_toplevel(name):
+    for syspath in py.std.sys.path:
+        base = py.path.local(syspath)
+        lib = base/name
+        if lib.check(dir=1):
+            return lib
+    raise LookupError(name)
+
+def pkgname(toplevel, rootpath, path):
+    parts = path.parts()[len(rootpath.parts()):]
+    return '.'.join([toplevel] + [x.purebasename for x in parts])
+
+def pkg_to_mapping(name):
+    toplevel = find_toplevel(name)
+    name2src = {}
+    for pyfile in toplevel.visit('*.py'):
+        pkg = pkgname(name, toplevel, pyfile)
+        name2src[pkg] = pyfile.read()
+    return name2src

File testing/test_walkpkg.py

+import py
+from genscript.pkgpacker import find_toplevel, pkgname, pkg_to_mapping
+from genscript import pkgpacker
+
+py_pkg = py.path.local(py.__file__).pypkgpath()
+
+def test_find_toplevel():
+    pylib = find_toplevel('py')
+    assert pylib == py_pkg
+
+
+def test_pkgname(tmpdir):
+    result = pkgname('test', tmpdir, tmpdir/'foo.py')
+    assert result == 'test.foo'
+
+
+def test_pkg_to_mapping(tmpdir, monkeypatch):
+    monkeypatch.setattr(pkgpacker, 'find_toplevel', lambda x:tmpdir)
+    tmpdir.join('test.py').write('#test\n')
+    tmpdir.join('__init__.py').write('#!/bin/python')
+
+    mapping = pkg_to_mapping('test')
+    expected = {
+        'test.__init__': '#!/bin/python',
+        'test.test': '#test\n',
+    }
+    assert mapping == expected
+