Commits

Ronny Pfannschmidt committed 24596ff

first working version of generating a enriched module

Comments (0)

Files changed (2)

genscript/module_maker.py

+import os
+from genscript.utils import distribution_metadata
+
 def metadata_lines(metadata):
     return ['%s = %r\n'%item for item in metadata.items()]
 
-
 def update_script(source, metadata):
     lines = source.splitlines(True)
     for index, line in enumerate(lines):
 class SdistModuleMixin:
 
     user_options = [
-        ('script', None, 'the script to enrich')
+        ('module', None, 'the module to enrich')
     ]
 
 
     def initialize_options(self):
-        self.script = None
+        self.module = None
 
     def finalize_options(self):
+        #XXX: read py_modules
         pass
 
     def run(self):
-        self.distribution.dist_files.append(('sdist_module', '', self.script))
+        basename =os.path.basename(self.module)
+        base, ext = os.path.splitext(basename)
+        new_name = '%s-%s%s'%(base, self.distribution.version, ext)
+        self.distribution.dist_files.append(('sdist_module', '', new_name))
+        sdist = self.distribution.get_command_obj('sdist')
+        sdist.ensure_finalized()
+        dist_dir = sdist.dist_dir
+
+        infile = open(self.module)
+        try:
+            unprocessed_source = infile.read()
+        finally:
+            infile.close()
+        outpath = os.path.join(dist_dir, new_name)
+
+        metadata = distribution_metadata(self.distribution.metadata)
+        processed_source = update_script(unprocessed_source, metadata)
+
+        outfile = open(outpath, 'w')
+        try:
+            outfile.write(processed_source)
+        finally:
+            outfile.close()
+

testing/test_sdist_module.py

+from distutils.dist import Distribution
+from genscript.module_maker import SdistModuleMixin
 
 
+def test_sdist_module(tmpdir):
+    script = tmpdir.join('test.py')
+    script.write('# Genscript Metadata')
 
+    cmd = SdistModuleMixin()
+    dist = cmd.distribution=Distribution({'version': '1.0'})
+
+    sdist = dist.get_command_obj('sdist')
+    sdist.ensure_finalized()
+    sdist.dist_dir = str(tmpdir)
+
+    dist.dist_files=[]
+    dist.version=1.0
+    cmd.initialize_options()
+    cmd.module = str(script)
+    cmd.finalize_options()
+    cmd.run()
+    assert dist.dist_files ==[('sdist_module', '', 'test-1.0.py')] #XXX
+    
+    assert tmpdir.join('test-1.0.py').check()
+
+    new_content = tmpdir.join('test-1.0.py').read()
+    assert 'version' in new_content
+
+
+