<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import os <EOL> from warnings import warn <EOL> import sys <EOL> from distutils . errors import DistutilsExecError <EOL> from distutils . spawn import spawn <EOL> from distutils . dir_util import mkpath <EOL> from distutils import log <EOL> try : <EOL> from pwd import getpwnam <EOL> except ImportError : <EOL> getpwnam = None <EOL> try : <EOL> from grp import getgrnam <EOL> except ImportError : <EOL> getgrnam = None <EOL> def _get_gid ( name ) : <EOL> """<STR_LIT>""" <EOL> if getgrnam is None or name is None : <EOL> return None <EOL> try : <EOL> result = getgrnam ( name ) <EOL> except KeyError : <EOL> result = None <EOL> if result is not None : <EOL> return result [ <NUM_LIT:2> ] <EOL> return None <EOL> def _get_uid ( name ) : <EOL> """<STR_LIT>""" <EOL> if getpwnam is None or name is None : <EOL> return None <EOL> try : <EOL> result = getpwnam ( name ) <EOL> except KeyError : <EOL> result = None <EOL> if result is not None : <EOL> return result [ <NUM_LIT:2> ] <EOL> return None <EOL> def make_tarball ( base_name , base_dir , compress = "<STR_LIT>" , verbose = <NUM_LIT:0> , dry_run = <NUM_LIT:0> , <EOL> owner = None , group = None ) : <EOL> """<STR_LIT>""" <EOL> tar_compression = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , None : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> compress_ext = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> if compress is not None and compress not in compress_ext . keys ( ) : <EOL> raise ValueError , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> archive_name = base_name + '<STR_LIT>' <EOL> if compress != '<STR_LIT>' : <EOL> archive_name += compress_ext . get ( compress , '<STR_LIT>' ) <EOL> mkpath ( os . path . dirname ( archive_name ) , dry_run = dry_run ) <EOL> import tarfile <EOL> log . info ( '<STR_LIT>' ) <EOL> uid = _get_uid ( owner ) <EOL> gid = _get_gid ( group ) <EOL> def _set_uid_gid ( tarinfo ) : <EOL> if gid is not None : <EOL> tarinfo . gid = gid <EOL> tarinfo . gname = group <EOL> if uid is not None : <EOL> tarinfo . uid = uid <EOL> tarinfo . uname = owner <EOL> return tarinfo <EOL> if not dry_run : <EOL> tar = tarfile . open ( archive_name , '<STR_LIT>' % tar_compression [ compress ] ) <EOL> try : <EOL> tar . add ( base_dir , filter = _set_uid_gid ) <EOL> finally : <EOL> tar . close ( ) <EOL> if compress == '<STR_LIT>' : <EOL> warn ( "<STR_LIT>" , PendingDeprecationWarning ) <EOL> compressed_name = archive_name + compress_ext [ compress ] <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> cmd = [ compress , archive_name , compressed_name ] <EOL> else : <EOL> cmd = [ compress , '<STR_LIT>' , archive_name ] <EOL> spawn ( cmd , dry_run = dry_run ) <EOL> return compressed_name <EOL> return archive_name <EOL> def make_zipfile ( base_name , base_dir , verbose = <NUM_LIT:0> , dry_run = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import zipfile <EOL> except ImportError : <EOL> zipfile = None <EOL> zip_filename = base_name + "<STR_LIT>" <EOL> mkpath ( os . path . dirname ( zip_filename ) , dry_run = dry_run ) <EOL> if zipfile is None : <EOL> if verbose : <EOL> zipoptions = "<STR_LIT>" <EOL> else : <EOL> zipoptions = "<STR_LIT>" <EOL> try : <EOL> spawn ( [ "<STR_LIT>" , zipoptions , zip_filename , base_dir ] , <EOL> dry_run = dry_run ) <EOL> except DistutilsExecError : <EOL> raise DistutilsExecError , ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % zip_filename <EOL> else : <EOL> log . info ( "<STR_LIT>" , <EOL> zip_filename , base_dir ) <EOL> if not dry_run : <EOL> zip = zipfile . ZipFile ( zip_filename , "<STR_LIT:w>" , <EOL> compression = zipfile . ZIP_DEFLATED ) <EOL> for dirpath , dirnames , filenames in os . walk ( base_dir ) : <EOL> for name in filenames : <EOL> path = os . path . normpath ( os . path . join ( dirpath , name ) ) <EOL> if os . path . isfile ( path ) : <EOL> zip . write ( path , path ) <EOL> log . info ( "<STR_LIT>" % path ) <EOL> zip . close ( ) <EOL> return zip_filename <EOL> ARCHIVE_FORMATS = { <EOL> '<STR_LIT>' : ( make_tarball , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( make_tarball , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( make_tarball , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( make_tarball , [ ( '<STR_LIT>' , None ) ] , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( make_zipfile , [ ] , "<STR_LIT>" ) <EOL> } <EOL> def check_archive_formats ( formats ) : <EOL> """<STR_LIT>""" <EOL> for format in formats : <EOL> if format not in ARCHIVE_FORMATS : <EOL> return format <EOL> return None <EOL> def make_archive ( base_name , format , root_dir = None , base_dir = None , verbose = <NUM_LIT:0> , <EOL> dry_run = <NUM_LIT:0> , owner = None , group = None ) : <EOL> """<STR_LIT>""" <EOL> save_cwd = os . getcwd ( ) <EOL> if root_dir is not None : <EOL> log . debug ( "<STR_LIT>" , root_dir ) <EOL> base_name = os . path . abspath ( base_name ) <EOL> if not dry_run : <EOL> os . chdir ( root_dir ) <EOL> if base_dir is None : <EOL> base_dir = os . curdir <EOL> kwargs = { '<STR_LIT>' : dry_run } <EOL> try : <EOL> format_info = ARCHIVE_FORMATS [ format ] <EOL> except KeyError : <EOL> raise ValueError , "<STR_LIT>" % format <EOL> func = format_info [ <NUM_LIT:0> ] <EOL> for arg , val in format_info [ <NUM_LIT:1> ] : <EOL> kwargs [ arg ] = val <EOL> if format != '<STR_LIT>' : <EOL> kwargs [ '<STR_LIT>' ] = owner <EOL> kwargs [ '<STR_LIT>' ] = group <EOL> try : <EOL> filename = func ( base_name , base_dir , ** kwargs ) <EOL> finally : <EOL> if root_dir is not None : <EOL> log . debug ( "<STR_LIT>" , save_cwd ) <EOL> os . chdir ( save_cwd ) <EOL> return filename </s>
