changeset 87:3571417ef92e

interpolate file permissions
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Jan 2011 14:39:21 -0800
parents 2c1310e94645
children 712a6d358083
files examples/doctest.txt makeitso/makeitso.py
diffstat 2 files changed, 23 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/examples/doctest.txt	Mon Jan 10 12:39:26 2011 -0800
+++ b/examples/doctest.txt	Mon Jan 10 14:39:21 2011 -0800
@@ -9,7 +9,7 @@
     >>> import tempfile
     >>> from StringIO import StringIO
 
-Basic functionality:
+Basic functionality::
 
     >>> example = os.path.join(here, 'example.txt')
     >>> template = makeitso.PolyTemplate([example], interactive=False)
@@ -18,7 +18,7 @@
     >>> template.substitute(name='foo')
     Hello foo
     
-Substitute to a buffer:
+Substitute to a buffer::
 
     >>> buffer = StringIO()
     >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False)
@@ -26,7 +26,7 @@
     >>> buffer.getvalue().strip()
     'Hello bar'
 
-Substitute to a file:
+Substitute to a file::
 
     >>> buffer = tempfile.mktemp()
     >>> template = makeitso.PolyTemplate([example], output=buffer, interactive=False)
@@ -91,3 +91,16 @@
     >>> file(buffer).read().strip()
     'Hello bar'
     >>> os.remove(buffer)
+
+Test to make sure permissions are preserved. This won't work on windows::
+
+    >>> buffer = tempfile.mktemp()
+    >>> f = file(buffer, 'w')
+    >>> print >> f, '#!/bin/bash\necho foo'
+    >>> f.close()
+    >>> os.chmod(buffer, 0755)
+    >>> uritemplate = makeitso.URITemplate(example, output=buffer, interactive=False)
+    >>> uritemplate.substitute(name='bar')
+    >>> ('%o' % os.stat(buffer).st_mode).endswith('755')
+    True
+    >>> os.remove(buffer)
--- a/makeitso/makeitso.py	Mon Jan 10 12:39:26 2011 -0800
+++ b/makeitso/makeitso.py	Mon Jan 10 14:39:21 2011 -0800
@@ -216,18 +216,18 @@
         f = self.output
         
         if isinstance(f, basestring):
+            path = f
             if os.path.isdir(f):
-                f = os.path.join(f, basename(self.name))
-            f = file(f, 'w')
+                path = os.path.join(path, basename(self.name))
+            f = file(path, 'w')
             print >> f, output
             f.close()
+            try:
+                os.chmod(f, os.stat(self.name).st_mode)
+            except:
+                pass
         else:
             print >> f, output
-        try:
-            os.chmod(self.output, os.stat(self.name).st_mode)
-        except:
-            pass
-
 
 class DirectoryTemplate(ContentTemplate):
     """template for a directory structure"""