<s> """<STR_LIT>""" <EOL> import os <EOL> import tarfile <EOL> import unittest <EOL> import warnings <EOL> import zipfile <EOL> from os . path import join <EOL> from textwrap import dedent <EOL> from test . test_support import captured_stdout , check_warnings , run_unittest <EOL> try : <EOL> import zlib <EOL> except ImportError : <EOL> zlib = None <EOL> try : <EOL> import grp <EOL> import pwd <EOL> UID_GID_SUPPORT = True <EOL> except ImportError : <EOL> UID_GID_SUPPORT = False <EOL> from distutils . command . sdist import sdist , show_formats <EOL> from distutils . core import Distribution <EOL> from distutils . tests . test_config import PyPIRCCommandTestCase <EOL> from distutils . errors import DistutilsOptionError <EOL> from distutils . spawn import find_executable <EOL> from distutils . log import WARN <EOL> from distutils . filelist import FileList <EOL> from distutils . archive_util import ARCHIVE_FORMATS <EOL> SETUP_PY = """<STR_LIT>""" <EOL> MANIFEST = """<STR_LIT>""" <EOL> class SDistTestCase ( PyPIRCCommandTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SDistTestCase , self ) . setUp ( ) <EOL> self . old_path = os . getcwd ( ) <EOL> os . mkdir ( join ( self . tmp_dir , '<STR_LIT>' ) ) <EOL> os . mkdir ( join ( self . tmp_dir , '<STR_LIT>' ) ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , SETUP_PY ) <EOL> os . chdir ( self . tmp_dir ) <EOL> def tearDown ( self ) : <EOL> os . chdir ( self . old_path ) <EOL> super ( SDistTestCase , self ) . tearDown ( ) <EOL> def get_cmd ( self , metadata = None ) : <EOL> """<STR_LIT>""" <EOL> if metadata is None : <EOL> metadata = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:version>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> dist = Distribution ( metadata ) <EOL> dist . script_name = '<STR_LIT>' <EOL> dist . packages = [ '<STR_LIT>' ] <EOL> dist . include_package_data = True <EOL> cmd = sdist ( dist ) <EOL> cmd . dist_dir = '<STR_LIT>' <EOL> return dist , cmd <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_prune_file_list ( self ) : <EOL> os . mkdir ( join ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> os . mkdir ( join ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> os . mkdir ( join ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> dist_folder = join ( self . tmp_dir , '<STR_LIT>' ) <EOL> files = os . listdir ( dist_folder ) <EOL> self . assertEqual ( files , [ '<STR_LIT>' ] ) <EOL> zip_file = zipfile . ZipFile ( join ( dist_folder , '<STR_LIT>' ) ) <EOL> try : <EOL> content = zip_file . namelist ( ) <EOL> finally : <EOL> zip_file . close ( ) <EOL> self . assertEqual ( len ( content ) , <NUM_LIT:4> ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_make_distribution ( self ) : <EOL> if ( find_executable ( '<STR_LIT>' ) is None or <EOL> find_executable ( '<STR_LIT>' ) is None ) : <EOL> return <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . formats = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> dist_folder = join ( self . tmp_dir , '<STR_LIT>' ) <EOL> result = os . listdir ( dist_folder ) <EOL> result . sort ( ) <EOL> self . assertEqual ( result , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> os . remove ( join ( dist_folder , '<STR_LIT>' ) ) <EOL> os . remove ( join ( dist_folder , '<STR_LIT>' ) ) <EOL> cmd . formats = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> result = os . listdir ( dist_folder ) <EOL> result . sort ( ) <EOL> self . assertEqual ( result , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_unicode_metadata_tgz ( self ) : <EOL> """<STR_LIT>""" <EOL> dist , cmd = self . get_cmd ( { '<STR_LIT:name>' : u'<STR_LIT>' , '<STR_LIT:version>' : u'<STR_LIT:1.0>' } ) <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> dist_folder = join ( self . tmp_dir , '<STR_LIT>' ) <EOL> result = os . listdir ( dist_folder ) <EOL> self . assertEqual ( result , [ '<STR_LIT>' ] ) <EOL> os . remove ( join ( dist_folder , '<STR_LIT>' ) ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_add_defaults ( self ) : <EOL> dist , cmd = self . get_cmd ( ) <EOL> dist . package_data = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> data_dir = join ( self . tmp_dir , '<STR_LIT:data>' ) <EOL> os . mkdir ( data_dir ) <EOL> self . write_file ( ( data_dir , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> some_dir = join ( self . tmp_dir , '<STR_LIT>' ) <EOL> os . mkdir ( some_dir ) <EOL> hg_dir = join ( self . tmp_dir , '<STR_LIT>' ) <EOL> os . mkdir ( hg_dir ) <EOL> self . write_file ( ( hg_dir , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> self . write_file ( ( some_dir , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> self . write_file ( ( some_dir , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> dist . data_files = [ ( '<STR_LIT:data>' , [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> script_dir = join ( self . tmp_dir , '<STR_LIT>' ) <EOL> os . mkdir ( script_dir ) <EOL> self . write_file ( ( script_dir , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> dist . scripts = [ join ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . use_defaults = True <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> dist_folder = join ( self . tmp_dir , '<STR_LIT>' ) <EOL> files = os . listdir ( dist_folder ) <EOL> self . assertEqual ( files , [ '<STR_LIT>' ] ) <EOL> zip_file = zipfile . ZipFile ( join ( dist_folder , '<STR_LIT>' ) ) <EOL> try : <EOL> content = zip_file . namelist ( ) <EOL> finally : <EOL> zip_file . close ( ) <EOL> self . assertEqual ( len ( content ) , <NUM_LIT:12> ) <EOL> f = open ( join ( self . tmp_dir , '<STR_LIT>' ) ) <EOL> try : <EOL> manifest = f . read ( ) <EOL> finally : <EOL> f . close ( ) <EOL> self . assertEqual ( manifest , MANIFEST % { '<STR_LIT>' : os . sep } ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_metadata_check_option ( self ) : <EOL> dist , cmd = self . get_cmd ( metadata = { } ) <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> warnings = [ msg for msg in self . get_logs ( WARN ) if <EOL> msg . startswith ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( len ( warnings ) , <NUM_LIT:2> ) <EOL> self . clear_logs ( ) <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . ensure_finalized ( ) <EOL> cmd . metadata_check = <NUM_LIT:0> <EOL> cmd . run ( ) <EOL> warnings = [ msg for msg in self . get_logs ( WARN ) if <EOL> msg . startswith ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( len ( warnings ) , <NUM_LIT:0> ) <EOL> def test_check_metadata_deprecated ( self ) : <EOL> dist , cmd = self . get_cmd ( ) <EOL> with check_warnings ( ) as w : <EOL> warnings . simplefilter ( "<STR_LIT>" ) <EOL> cmd . check_metadata ( ) <EOL> self . assertEqual ( len ( w . warnings ) , <NUM_LIT:1> ) <EOL> def test_show_formats ( self ) : <EOL> with captured_stdout ( ) as stdout : <EOL> show_formats ( ) <EOL> num_formats = len ( ARCHIVE_FORMATS . keys ( ) ) <EOL> output = [ line for line in stdout . getvalue ( ) . split ( '<STR_LIT:\n>' ) <EOL> if line . strip ( ) . startswith ( '<STR_LIT>' ) ] <EOL> self . assertEqual ( len ( output ) , num_formats ) <EOL> def test_finalize_options ( self ) : <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . finalize_options ( ) <EOL> self . assertEqual ( cmd . manifest , '<STR_LIT>' ) <EOL> self . assertEqual ( cmd . template , '<STR_LIT>' ) <EOL> self . assertEqual ( cmd . dist_dir , '<STR_LIT>' ) <EOL> cmd . formats = <NUM_LIT:1> <EOL> self . assertRaises ( DistutilsOptionError , cmd . finalize_options ) <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . finalize_options ( ) <EOL> cmd . formats = '<STR_LIT>' <EOL> self . assertRaises ( DistutilsOptionError , cmd . finalize_options ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> @ unittest . skipUnless ( UID_GID_SUPPORT , "<STR_LIT>" ) <EOL> def test_make_distribution_owner_group ( self ) : <EOL> if ( find_executable ( '<STR_LIT>' ) is None or <EOL> find_executable ( '<STR_LIT>' ) is None ) : <EOL> return <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . owner = pwd . getpwuid ( <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> cmd . group = grp . getgrgid ( <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> archive_name = join ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> archive = tarfile . open ( archive_name ) <EOL> try : <EOL> for member in archive . getmembers ( ) : <EOL> self . assertEqual ( member . uid , <NUM_LIT:0> ) <EOL> self . assertEqual ( member . gid , <NUM_LIT:0> ) <EOL> finally : <EOL> archive . close ( ) <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> archive_name = join ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> archive = tarfile . open ( archive_name ) <EOL> try : <EOL> for member in archive . getmembers ( ) : <EOL> self . assertEqual ( member . uid , os . getuid ( ) ) <EOL> finally : <EOL> archive . close ( ) <EOL> def _check_template ( self , content ) : <EOL> dist , cmd = self . get_cmd ( ) <EOL> os . chdir ( self . tmp_dir ) <EOL> self . write_file ( '<STR_LIT>' , content ) <EOL> cmd . ensure_finalized ( ) <EOL> cmd . filelist = FileList ( ) <EOL> cmd . read_template ( ) <EOL> warnings = self . get_logs ( WARN ) <EOL> self . assertEqual ( len ( warnings ) , <NUM_LIT:1> ) <EOL> def test_invalid_template_unknown_command ( self ) : <EOL> self . _check_template ( '<STR_LIT>' ) <EOL> def test_invalid_template_wrong_arguments ( self ) : <EOL> self . _check_template ( '<STR_LIT>' ) <EOL> @ unittest . skipIf ( os . name != '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_invalid_template_wrong_path ( self ) : <EOL> self . _check_template ( '<STR_LIT>' ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_get_file_list ( self ) : <EOL> dist , cmd = self . get_cmd ( ) <EOL> dist . package_data = { '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> f = open ( cmd . manifest ) <EOL> try : <EOL> manifest = [ line . strip ( ) for line in f . read ( ) . split ( '<STR_LIT:\n>' ) <EOL> if line . strip ( ) != '<STR_LIT>' ] <EOL> finally : <EOL> f . close ( ) <EOL> self . assertEqual ( len ( manifest ) , <NUM_LIT:5> ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> build_py = dist . get_command_obj ( '<STR_LIT>' ) <EOL> build_py . finalized = False <EOL> build_py . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> f = open ( cmd . manifest ) <EOL> try : <EOL> manifest2 = [ line . strip ( ) for line in f . read ( ) . split ( '<STR_LIT:\n>' ) <EOL> if line . strip ( ) != '<STR_LIT>' ] <EOL> finally : <EOL> f . close ( ) <EOL> self . assertEqual ( len ( manifest2 ) , <NUM_LIT:6> ) <EOL> self . assertIn ( '<STR_LIT>' , manifest2 [ - <NUM_LIT:1> ] ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_manifest_marker ( self ) : <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . ensure_finalized ( ) <EOL> cmd . run ( ) <EOL> f = open ( cmd . manifest ) <EOL> try : <EOL> manifest = [ line . strip ( ) for line in f . read ( ) . split ( '<STR_LIT:\n>' ) <EOL> if line . strip ( ) != '<STR_LIT>' ] <EOL> finally : <EOL> f . close ( ) <EOL> self . assertEqual ( manifest [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' ) <EOL> @ unittest . skipUnless ( zlib , '<STR_LIT>' ) <EOL> def test_manifest_comments ( self ) : <EOL> contents = dedent ( """<STR_LIT>""" ) <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . ensure_finalized ( ) <EOL> self . write_file ( ( self . tmp_dir , cmd . manifest ) , contents ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> cmd . run ( ) <EOL> self . assertEqual ( cmd . filelist . files , [ '<STR_LIT>' ] ) <EOL> @ unittest . skipUnless ( zlib , "<STR_LIT>" ) <EOL> def test_manual_manifest ( self ) : <EOL> dist , cmd = self . get_cmd ( ) <EOL> cmd . formats = [ '<STR_LIT>' ] <EOL> cmd . ensure_finalized ( ) <EOL> self . write_file ( ( self . tmp_dir , cmd . manifest ) , '<STR_LIT>' ) <EOL> self . write_file ( ( self . tmp_dir , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) <EOL> cmd . run ( ) <EOL> self . assertEqual ( cmd . filelist . files , [ '<STR_LIT>' ] ) <EOL> f = open ( cmd . manifest ) <EOL> try : <EOL> manifest = [ line . strip ( ) for line in f . read ( ) . split ( '<STR_LIT:\n>' ) <EOL> if line . strip ( ) != '<STR_LIT>' ] <EOL> finally : <EOL> f . close ( ) <EOL> self . assertEqual ( manifest , [ '<STR_LIT>' ] ) <EOL> archive_name = join ( self . tmp_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> archive = tarfile . open ( archive_name ) <EOL> try : <EOL> filenames = [ tarinfo . name for tarinfo in archive ] <EOL> finally : <EOL> archive . close ( ) <EOL> self . assertEqual ( sorted ( filenames ) , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_suite ( ) : <EOL> return unittest . makeSuite ( SDistTestCase ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_unittest ( test_suite ( ) ) </s>
