From 6c5df3650a90ca2567e6f865de237c5fb1fadf5d Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Sat, 5 Sep 2015 17:57:45 +0200 Subject: Try to fix `python setup.py bdist_dumb' on Mac OS X (paylogic/pip-accel#2) Additions based on /usr/lib/python2.7/distutils/archive_util.py from the Ubuntu 12.04 package python2.7 (2.7.3-0ubuntu3.8). I have not looked into the compatibility of the software licenses of Paramiko vs distutils however the original setup_helper.py code in Paramiko was clearly also copied from distutils so to be honest it's not like I'm changing the status quo. --- setup_helper.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) (limited to 'setup_helper.py') diff --git a/setup_helper.py b/setup_helper.py index ff6b0e16..5d23137b 100644 --- a/setup_helper.py +++ b/setup_helper.py @@ -30,9 +30,42 @@ import distutils.archive_util from distutils.dir_util import mkpath from distutils.spawn import spawn - -def make_tarball(base_name, base_dir, compress='gzip', - verbose=False, dry_run=False): +try: + from pwd import getpwnam +except ImportError: + getpwnam = None + +try: + from grp import getgrnam +except ImportError: + getgrnam = None + +def _get_gid(name): + """Returns a gid, given a group name.""" + if getgrnam is None or name is None: + return None + try: + result = getgrnam(name) + except KeyError: + result = None + if result is not None: + return result[2] + return None + +def _get_uid(name): + """Returns an uid, given a user name.""" + if getpwnam is None or name is None: + return None + try: + result = getpwnam(name) + except KeyError: + result = None + if result is not None: + return result[2] + return None + +def make_tarball(base_name, base_dir, compress='gzip', verbose=0, dry_run=0, + owner=None, group=None): """Create a tar file from all the files under 'base_dir'. This file may be compressed. @@ -75,11 +108,25 @@ def make_tarball(base_name, base_dir, compress='gzip', mkpath(os.path.dirname(archive_name), dry_run=dry_run) log.info('Creating tar file %s with mode %s' % (archive_name, mode)) + uid = _get_uid(owner) + gid = _get_gid(group) + + def _set_uid_gid(tarinfo): + if gid is not None: + tarinfo.gid = gid + tarinfo.gname = group + if uid is not None: + tarinfo.uid = uid + tarinfo.uname = owner + return tarinfo + if not dry_run: tar = tarfile.open(archive_name, mode=mode) # This recursively adds everything underneath base_dir - tar.add(base_dir) - tar.close() + try: + tar.add(base_dir, filter=_set_uid_gid) + finally: + tar.close() if compress and compress not in tarfile_compress_flag: spawn([compress] + compress_flags[compress] + [archive_name], -- cgit v1.2.3 From a7c8266fe784dffa2a5fdd1526437c6ba7ba1aab Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Wed, 9 Sep 2015 22:18:24 +0200 Subject: Restore Python 2.6 compatibility for `python setup.py {s,b}dist' --- setup_helper.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'setup_helper.py') diff --git a/setup_helper.py b/setup_helper.py index 5d23137b..9e3834b3 100644 --- a/setup_helper.py +++ b/setup_helper.py @@ -124,7 +124,12 @@ def make_tarball(base_name, base_dir, compress='gzip', verbose=0, dry_run=0, tar = tarfile.open(archive_name, mode=mode) # This recursively adds everything underneath base_dir try: - tar.add(base_dir, filter=_set_uid_gid) + try: + # Support for the `filter' parameter was added in Python 2.7, + # earlier versions will raise TypeError. + tar.add(base_dir, filter=_set_uid_gid) + except TypeError: + tar.add(base_dir) finally: tar.close() -- cgit v1.2.3