Skip to content
This repository has been archived by the owner on Apr 24, 2019. It is now read-only.

Commit

Permalink
- Added a way to change the owner of created directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Zagrodnick committed Oct 20, 2008
1 parent 7969d12 commit 2c9fe2a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/lovely/recipe/fs/README.txt
Expand Up @@ -107,6 +107,53 @@ But we need to activate this function explicitely.
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
==============

Expand Down
37 changes: 37 additions & 0 deletions src/lovely/recipe/fs/mkdir-root.txt
@@ -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

23 changes: 21 additions & 2 deletions src/lovely/recipe/fs/mkdir.py
@@ -1,5 +1,6 @@
import os
import logging
import os
import pwd
import zc.buildout


Expand All @@ -14,7 +15,21 @@ def __init__(self, buildout, name, options):
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']
Expand All @@ -35,6 +50,10 @@ def install(self):
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):
Expand Down
8 changes: 7 additions & 1 deletion src/lovely/recipe/fs/tests.py
Expand Up @@ -16,6 +16,8 @@
"""
__docformat__ = 'restructuredtext'

import os

from zc.buildout import testing
import doctest, unittest
from zope.testing import doctest, renormalizing
Expand All @@ -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,
Expand Down

0 comments on commit 2c9fe2a

Please sign in to comment.