[Checkins] SVN: lovely.recipe/trunk/src/lovely/recipe/fs/ - Added a way to change the owner of created directories.

Christian Zagrodnick cz at gocept.com
Mon Oct 20 08:24:04 EDT 2008


Log message for revision 92398:
  - Added a way to change the owner of created directories.
  
  

Changed:
  U   lovely.recipe/trunk/src/lovely/recipe/fs/README.txt
  A   lovely.recipe/trunk/src/lovely/recipe/fs/mkdir-root.txt
  U   lovely.recipe/trunk/src/lovely/recipe/fs/mkdir.py
  U   lovely.recipe/trunk/src/lovely/recipe/fs/tests.py

-=-
Modified: lovely.recipe/trunk/src/lovely/recipe/fs/README.txt
===================================================================
--- lovely.recipe/trunk/src/lovely/recipe/fs/README.txt	2008-10-20 11:33:54 UTC (rev 92397)
+++ lovely.recipe/trunk/src/lovely/recipe/fs/README.txt	2008-10-20 12:24:02 UTC (rev 92398)
@@ -107,6 +107,53 @@
     d  subdir
 
 
+We can change the owner of the created directory if run as root. This is tested
+in mkdir-root.txt.
+
+If not run as root, setting the owner is an error:
+
+    >>> write(sample_buildout, 'buildout.cfg',
+    ... """
+    ... [buildout]
+    ... parts = data-dir
+    ... find-links = http://download.zope.org/distribution
+    ...
+    ... [data-dir]
+    ... recipe = lovely.recipe:mkdir
+    ... createpath = True
+    ... path = another/with/subdir
+    ... owner = nobody
+    ... """)
+    >>> print system(buildout),
+    While:
+      Installing.
+      Getting section data-dir.
+      Initializing part data-dir.
+    Error: Only root can change the owner to nobody.
+
+
+It is an error when the user does not exist:
+
+    >>> write(sample_buildout, 'buildout.cfg',
+    ... """
+    ... [buildout]
+    ... parts = data-dir
+    ... find-links = http://download.zope.org/distribution
+    ...
+    ... [data-dir]
+    ... recipe = lovely.recipe:mkdir
+    ... createpath = True
+    ... path = another/with/subdir
+    ... owner = someuser
+    ... """)
+    >>> print system(buildout),
+    While:
+      Installing.
+      Getting section data-dir.
+      Initializing part data-dir.
+    Error: The user someuser does not exist.
+
+
 Creating Files
 ==============
 

Added: lovely.recipe/trunk/src/lovely/recipe/fs/mkdir-root.txt
===================================================================
--- lovely.recipe/trunk/src/lovely/recipe/fs/mkdir-root.txt	                        (rev 0)
+++ lovely.recipe/trunk/src/lovely/recipe/fs/mkdir-root.txt	2008-10-20 12:24:02 UTC (rev 92398)
@@ -0,0 +1,37 @@
+Creating Directories with owner change
+======================================
+
+We can change the owner of the created directory if run as root:
+
+    >>> write(sample_buildout, 'buildout.cfg',
+    ... """
+    ... [buildout]
+    ... parts = data-dir
+    ... find-links = http://download.zope.org/distribution
+    ...
+    ... [data-dir]
+    ... recipe = lovely.recipe:mkdir
+    ... createpath = True
+    ... path = with/subdir
+    ... owner = nobody
+    ... """)
+    >>> import os
+    >>> import pwd
+    >>> nobody_uid = pwd.getpwnam('nobody')[2]
+    >>> print system(buildout),
+    Installing data-dir.
+    data-dir: Creating parent directory .../_TEST_/sample-buildout/with
+    data-dir: Creating directory with/subdir
+   
+The owner of the subdir is changed:
+
+    >>> path = os.path.join(sample_buildout, 'with/subdir')
+    >>> os.stat(path).st_uid == nobody_uid
+    True
+
+But not the owner of the parent dir:
+
+    >>> path = os.path.join(sample_buildout, 'with')
+    >>> os.stat(path).st_uid == nobody_uid
+    False
+


Property changes on: lovely.recipe/trunk/src/lovely/recipe/fs/mkdir-root.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: lovely.recipe/trunk/src/lovely/recipe/fs/mkdir.py
===================================================================
--- lovely.recipe/trunk/src/lovely/recipe/fs/mkdir.py	2008-10-20 11:33:54 UTC (rev 92397)
+++ lovely.recipe/trunk/src/lovely/recipe/fs/mkdir.py	2008-10-20 12:24:02 UTC (rev 92398)
@@ -1,5 +1,6 @@
+import logging
 import os
-import logging
+import pwd
 import zc.buildout
 
 
@@ -14,8 +15,22 @@
                               buildout['buildout']['directory'],
                               self.originalPath,
                               )
-        self.createPath = options.get('createpath', 'False').lower() in ['true', 'on', '1']
+        owner = options.get('owner')
+        if owner:
+            try:
+                uid = pwd.getpwnam(owner)[2]
+            except KeyError:
+                raise zc.buildout.UserError(
+                    'The user %s does not exist.' % owner)
+            if os.getuid() != 0:
+                raise zc.buildout.UserError(
+                    'Only root can change the owner to %s.' % owner)
 
+            options['owner-uid'] = str(uid)
+
+        self.createPath = options.get('createpath', 'False').lower() in [
+            'true', 'on', '1']
+
     def install(self):
         path = self.options['path']
         dirname = os.path.dirname(self.options['path'])
@@ -35,6 +50,10 @@
             logging.getLogger(self.name).info(
                 'Creating directory %s', self.originalPath)
             os.mkdir(path)
+        uid = self.options.get('owner-uid')
+        if uid is not None:
+            uid = int(uid)
+            os.chown(path, uid, -1)
         return ()
 
     def update(self):

Modified: lovely.recipe/trunk/src/lovely/recipe/fs/tests.py
===================================================================
--- lovely.recipe/trunk/src/lovely/recipe/fs/tests.py	2008-10-20 11:33:54 UTC (rev 92397)
+++ lovely.recipe/trunk/src/lovely/recipe/fs/tests.py	2008-10-20 12:24:02 UTC (rev 92398)
@@ -16,6 +16,8 @@
 """
 __docformat__ = 'restructuredtext'
 
+import os
+
 from zc.buildout import testing
 import doctest, unittest
 from zope.testing import doctest, renormalizing
@@ -25,9 +27,13 @@
 
 def test_suite():
 
+    test_file = 'README.txt'
+    if os.getuid() == 0:
+        test_file = 'mkdir-root.txt'
+
     return unittest.TestSuite((
         doctest.DocFileSuite(
-            'README.txt',
+            test_file,
             setUp=setUpBuildout,
             optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
             tearDown=testing.buildoutTearDown,



More information about the Checkins mailing list