| 1 |
""" |
|---|
| 2 |
Post setup operations |
|---|
| 3 |
""" |
|---|
| 4 |
|
|---|
| 5 |
import sys, os, os.path, shutil |
|---|
| 6 |
import pkg_resources as pr |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
class PostSetup(object): |
|---|
| 10 |
""" |
|---|
| 11 |
I do post-setup file copying with equivalent user and group access |
|---|
| 12 |
permissions. |
|---|
| 13 |
""" |
|---|
| 14 |
def __init__(self, srcDir, user, group): |
|---|
| 15 |
self.srcDir = srcDir |
|---|
| 16 |
self.user, self.group = user, group |
|---|
| 17 |
|
|---|
| 18 |
def _get_UID(self): |
|---|
| 19 |
import pwd |
|---|
| 20 |
return pwd.getpwnam(self.user)[2] |
|---|
| 21 |
uid = property(_get_UID) |
|---|
| 22 |
|
|---|
| 23 |
def _get_GID(self): |
|---|
| 24 |
import grp |
|---|
| 25 |
return grp.getgrnam(self.group)[2] |
|---|
| 26 |
gid = property(_get_GID) |
|---|
| 27 |
|
|---|
| 28 |
def prepareDirectory(self, dirParts): |
|---|
| 29 |
for k in xrange(len(dirParts)-1): |
|---|
| 30 |
thisDir = os.path.join(*dirParts[:k+1]) |
|---|
| 31 |
if not os.path.exists(thisDir): |
|---|
| 32 |
os.makedirs(thisDir) |
|---|
| 33 |
# Ensure correct permissions of the created directory (don't |
|---|
| 34 |
# touch existing ones) |
|---|
| 35 |
os.chown(thisDir, self.uid, self.gid) |
|---|
| 36 |
os.chmod(thisDir, 0770) |
|---|
| 37 |
|
|---|
| 38 |
def copy(self, srcPath, *destPathParts): |
|---|
| 39 |
self.prepareDirectory(destPathParts[:-1]) |
|---|
| 40 |
destPath = os.path.join(*destPathParts) |
|---|
| 41 |
doCopy = False |
|---|
| 42 |
if os.path.exists(destPath): |
|---|
| 43 |
# Exists, copy only if dest time less (older) than source |
|---|
| 44 |
# time |
|---|
| 45 |
times = [os.path.getmtime(x) for x in (srcPath, destPath)] |
|---|
| 46 |
if times[1] <= times[0]: |
|---|
| 47 |
doCopy = True |
|---|
| 48 |
else: |
|---|
| 49 |
# Doesn't exist, always copy |
|---|
| 50 |
doCopy = True |
|---|
| 51 |
if doCopy: |
|---|
| 52 |
shutil.copy(srcPath, destPath) |
|---|
| 53 |
print "%s -> %s" % (srcPath, destPath) |
|---|
| 54 |
# Ensure correct ownership of files, newly copied or not |
|---|
| 55 |
os.chown(destPath, self.uid, self.gid) |
|---|
| 56 |
|
|---|
| 57 |
def setup(self): |
|---|
| 58 |
for mungedPath in os.listdir(self.srcDir): |
|---|
| 59 |
if mungedPath.startswith('.'): |
|---|
| 60 |
continue |
|---|
| 61 |
srcPath = os.path.join(self.srcDir, mungedPath) |
|---|
| 62 |
self.copy(srcPath, '/', *mungedPath.split('_')) |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
def run(projectName, user, group): |
|---|
| 66 |
""" |
|---|
| 67 |
When a C{setup.py} script is called with one of the C{install*} commands |
|---|
| 68 |
and includes a call this method, the files in the C{misc} subdirectory are |
|---|
| 69 |
processed via a L{PostSetup} object. |
|---|
| 70 |
""" |
|---|
| 71 |
ignore = sys.platform.startswith('win') |
|---|
| 72 |
for arg in sys.argv: |
|---|
| 73 |
if arg in ('-h', '--help'): |
|---|
| 74 |
ignore = True |
|---|
| 75 |
continue |
|---|
| 76 |
if not ignore and arg.startswith('install'): |
|---|
| 77 |
requirement = pr.Requirement.parse(projectName) |
|---|
| 78 |
srcDir = pr.resource_filename(requirement, 'misc') |
|---|
| 79 |
ps = PostSetup(srcDir, user, group) |
|---|
| 80 |
ps.setup() |
|---|
| 81 |
pr.cleanup_resources() |
|---|
| 82 |
return |
|---|
| 83 |
|
|---|
| 84 |
|
|---|