| 1 |
""" |
|---|
| 2 |
Post setup operations |
|---|
| 3 |
|
|---|
| 4 |
Copyright (C) 2006-2007 by Edwin A. Suominen, http://www.eepatents.com |
|---|
| 5 |
|
|---|
| 6 |
This program is free software; you can redistribute it and/or modify it under |
|---|
| 7 |
the terms of the GNU General Public License as published by the Free Software |
|---|
| 8 |
Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 9 |
version. |
|---|
| 10 |
|
|---|
| 11 |
This program is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
|---|
| 13 |
PARTICULAR PURPOSE. See the file COPYING for more details. |
|---|
| 14 |
|
|---|
| 15 |
You should have received a copy of the GNU General Public License along with |
|---|
| 16 |
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin |
|---|
| 17 |
Street, Fifth Floor, Boston, MA 02110-1301, USA |
|---|
| 18 |
|
|---|
| 19 |
""" |
|---|
| 20 |
|
|---|
| 21 |
import sys, os, os.path, shutil |
|---|
| 22 |
import pkg_resources as pr |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
class PostSetup(object): |
|---|
| 26 |
""" |
|---|
| 27 |
I do post-setup file copying with equivalent user and group access |
|---|
| 28 |
permissions. |
|---|
| 29 |
""" |
|---|
| 30 |
def __init__(self, srcDir, user, group): |
|---|
| 31 |
self.srcDir = srcDir |
|---|
| 32 |
self.user, self.group = user, group |
|---|
| 33 |
|
|---|
| 34 |
def _get_UID(self): |
|---|
| 35 |
import pwd |
|---|
| 36 |
return pwd.getpwnam(self.user)[2] |
|---|
| 37 |
uid = property(_get_UID) |
|---|
| 38 |
|
|---|
| 39 |
def _get_GID(self): |
|---|
| 40 |
import grp |
|---|
| 41 |
return grp.getgrnam(self.group)[2] |
|---|
| 42 |
gid = property(_get_GID) |
|---|
| 43 |
|
|---|
| 44 |
def prepareDirectory(self, dirParts): |
|---|
| 45 |
for k in xrange(len(dirParts)): |
|---|
| 46 |
thisDir = os.path.join(*dirParts[:k+1]) |
|---|
| 47 |
if not os.path.exists(thisDir): |
|---|
| 48 |
os.makedirs(thisDir) |
|---|
| 49 |
# Ensure correct permissions of the created directory (don't |
|---|
| 50 |
# touch existing ones) |
|---|
| 51 |
os.chown(thisDir, self.uid, self.gid) |
|---|
| 52 |
os.chmod(thisDir, 0770) |
|---|
| 53 |
|
|---|
| 54 |
def copy(self, srcPath, *destPathParts): |
|---|
| 55 |
self.prepareDirectory(destPathParts[:-1]) |
|---|
| 56 |
destPath = os.path.join(*destPathParts) |
|---|
| 57 |
doCopy = False |
|---|
| 58 |
if os.path.exists(destPath): |
|---|
| 59 |
# Exists, copy only if dest time less (older) than source |
|---|
| 60 |
# time |
|---|
| 61 |
times = [os.path.getmtime(x) for x in (srcPath, destPath)] |
|---|
| 62 |
if times[1] <= times[0]: |
|---|
| 63 |
doCopy = True |
|---|
| 64 |
else: |
|---|
| 65 |
# Doesn't exist, always copy |
|---|
| 66 |
doCopy = True |
|---|
| 67 |
if doCopy: |
|---|
| 68 |
shutil.copy(srcPath, destPath) |
|---|
| 69 |
print "%s -> %s" % (srcPath, destPath) |
|---|
| 70 |
# Ensure correct ownership of files, newly copied or not |
|---|
| 71 |
os.chown(destPath, self.uid, self.gid) |
|---|
| 72 |
|
|---|
| 73 |
def setup(self): |
|---|
| 74 |
for mungedPath in os.listdir(self.srcDir): |
|---|
| 75 |
if mungedPath.startswith('.'): |
|---|
| 76 |
continue |
|---|
| 77 |
srcPath = os.path.join(self.srcDir, mungedPath) |
|---|
| 78 |
self.copy(srcPath, '/', *mungedPath.split('_')) |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
def run(projectName, user, group): |
|---|
| 82 |
""" |
|---|
| 83 |
When a C{setup.py} script is called with one of the C{install*} commands |
|---|
| 84 |
and includes a call this method, the files in the C{misc} subdirectory are |
|---|
| 85 |
processed via a L{PostSetup} object. |
|---|
| 86 |
""" |
|---|
| 87 |
ignore = sys.platform.startswith('win') |
|---|
| 88 |
for arg in sys.argv: |
|---|
| 89 |
if arg in ('-h', '--help'): |
|---|
| 90 |
ignore = True |
|---|
| 91 |
continue |
|---|
| 92 |
if not ignore and arg.startswith('install'): |
|---|
| 93 |
requirement = pr.Requirement.parse(projectName) |
|---|
| 94 |
srcDir = pr.resource_filename(requirement, 'misc') |
|---|
| 95 |
ps = PostSetup(srcDir, user, group) |
|---|
| 96 |
ps.setup() |
|---|
| 97 |
pr.cleanup_resources() |
|---|
| 98 |
return |
|---|
| 99 |
|
|---|
| 100 |
|
|---|