<s> import os . path , sys , fnmatch <EOL> from distutils . command . build_py import build_py as _build_py <EOL> from distutils . util import convert_path <EOL> from glob import glob <EOL> try : <EOL> from distutils . util import Mixin2to3 as _Mixin2to3 <EOL> from distutils import log <EOL> from lib2to3 . refactor import RefactoringTool , get_fixers_from_package <EOL> import setuptools <EOL> class DistutilsRefactoringTool ( RefactoringTool ) : <EOL> def log_error ( self , msg , * args , ** kw ) : <EOL> log . error ( msg , * args ) <EOL> def log_message ( self , msg , * args ) : <EOL> log . info ( msg , * args ) <EOL> def log_debug ( self , msg , * args ) : <EOL> log . debug ( msg , * args ) <EOL> class Mixin2to3 ( _Mixin2to3 ) : <EOL> def run_2to3 ( self , files , doctests = False ) : <EOL> if self . distribution . use_2to3 is not True : <EOL> return <EOL> if not files : <EOL> return <EOL> log . info ( "<STR_LIT>" + "<STR_LIT:U+0020>" . join ( files ) ) <EOL> self . __build_fixer_names ( ) <EOL> self . __exclude_fixers ( ) <EOL> if doctests : <EOL> if setuptools . run_2to3_on_doctests : <EOL> r = DistutilsRefactoringTool ( self . fixer_names ) <EOL> r . refactor ( files , write = True , doctests_only = True ) <EOL> else : <EOL> _Mixin2to3 . run_2to3 ( self , files ) <EOL> def __build_fixer_names ( self ) : <EOL> if self . fixer_names : return <EOL> self . fixer_names = [ ] <EOL> for p in setuptools . lib2to3_fixer_packages : <EOL> self . fixer_names . extend ( get_fixers_from_package ( p ) ) <EOL> if self . distribution . use_2to3_fixers is not None : <EOL> for p in self . distribution . use_2to3_fixers : <EOL> self . fixer_names . extend ( get_fixers_from_package ( p ) ) <EOL> def __exclude_fixers ( self ) : <EOL> excluded_fixers = getattr ( self , '<STR_LIT>' , [ ] ) <EOL> if self . distribution . use_2to3_exclude_fixers is not None : <EOL> excluded_fixers . extend ( self . distribution . use_2to3_exclude_fixers ) <EOL> for fixer_name in excluded_fixers : <EOL> if fixer_name in self . fixer_names : <EOL> self . fixer_names . remove ( fixer_name ) <EOL> except ImportError : <EOL> class Mixin2to3 : <EOL> def run_2to3 ( self , files , doctests = True ) : <EOL> pass <EOL> class build_py ( _build_py , Mixin2to3 ) : <EOL> """<STR_LIT>""" <EOL> def finalize_options ( self ) : <EOL> _build_py . finalize_options ( self ) <EOL> self . package_data = self . distribution . package_data <EOL> self . exclude_package_data = self . distribution . exclude_package_data or { } <EOL> if '<STR_LIT>' in self . __dict__ : del self . __dict__ [ '<STR_LIT>' ] <EOL> self . __updated_files = [ ] <EOL> self . __doctests_2to3 = [ ] <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . py_modules and not self . packages : <EOL> return <EOL> if self . py_modules : <EOL> self . build_modules ( ) <EOL> if self . packages : <EOL> self . build_packages ( ) <EOL> self . build_package_data ( ) <EOL> self . run_2to3 ( self . __updated_files , False ) <EOL> self . run_2to3 ( self . __updated_files , True ) <EOL> self . run_2to3 ( self . __doctests_2to3 , True ) <EOL> self . byte_compile ( _build_py . get_outputs ( self , include_bytecode = <NUM_LIT:0> ) ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr == '<STR_LIT>' : <EOL> self . data_files = files = self . _get_data_files ( ) ; return files <EOL> return _build_py . __getattr__ ( self , attr ) <EOL> def build_module ( self , module , module_file , package ) : <EOL> outfile , copied = _build_py . build_module ( self , module , module_file , package ) <EOL> if copied : <EOL> self . __updated_files . append ( outfile ) <EOL> return outfile , copied <EOL> def _get_data_files ( self ) : <EOL> """<STR_LIT>""" <EOL> self . analyze_manifest ( ) <EOL> data = [ ] <EOL> for package in self . packages or ( ) : <EOL> src_dir = self . get_package_dir ( package ) <EOL> build_dir = os . path . join ( * ( [ self . build_lib ] + package . split ( '<STR_LIT:.>' ) ) ) <EOL> plen = len ( src_dir ) + <NUM_LIT:1> <EOL> filenames = [ <EOL> file [ plen : ] for file in self . find_data_files ( package , src_dir ) <EOL> ] <EOL> data . append ( ( package , src_dir , build_dir , filenames ) ) <EOL> return data <EOL> def find_data_files ( self , package , src_dir ) : <EOL> """<STR_LIT>""" <EOL> globs = ( self . package_data . get ( '<STR_LIT>' , [ ] ) <EOL> + self . package_data . get ( package , [ ] ) ) <EOL> files = self . manifest_files . get ( package , [ ] ) [ : ] <EOL> for pattern in globs : <EOL> files . extend ( glob ( os . path . join ( src_dir , convert_path ( pattern ) ) ) ) <EOL> return self . exclude_data_files ( package , src_dir , files ) <EOL> def build_package_data ( self ) : <EOL> """<STR_LIT>""" <EOL> lastdir = None <EOL> for package , src_dir , build_dir , filenames in self . data_files : <EOL> for filename in filenames : <EOL> target = os . path . join ( build_dir , filename ) <EOL> self . mkpath ( os . path . dirname ( target ) ) <EOL> srcfile = os . path . join ( src_dir , filename ) <EOL> outf , copied = self . copy_file ( srcfile , target ) <EOL> srcfile = os . path . abspath ( srcfile ) <EOL> if copied and srcfile in self . distribution . convert_2to3_doctests : <EOL> self . __doctests_2to3 . append ( outf ) <EOL> def analyze_manifest ( self ) : <EOL> self . manifest_files = mf = { } <EOL> if not self . distribution . include_package_data : <EOL> return <EOL> src_dirs = { } <EOL> for package in self . packages or ( ) : <EOL> src_dirs [ assert_relative ( self . get_package_dir ( package ) ) ] = package <EOL> self . run_command ( '<STR_LIT>' ) <EOL> ei_cmd = self . get_finalized_command ( '<STR_LIT>' ) <EOL> for path in ei_cmd . filelist . files : <EOL> d , f = os . path . split ( assert_relative ( path ) ) <EOL> prev = None <EOL> oldf = f <EOL> while d and d != prev and d not in src_dirs : <EOL> prev = d <EOL> d , df = os . path . split ( d ) <EOL> f = os . path . join ( df , f ) <EOL> if d in src_dirs : <EOL> if path . endswith ( '<STR_LIT>' ) and f == oldf : <EOL> continue <EOL> mf . setdefault ( src_dirs [ d ] , [ ] ) . append ( path ) <EOL> def get_data_files ( self ) : pass <EOL> if sys . version < "<STR_LIT>" : <EOL> def get_outputs ( self , include_bytecode = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return _build_py . get_outputs ( self , include_bytecode ) + [ <EOL> os . path . join ( build_dir , filename ) <EOL> for package , src_dir , build_dir , filenames in self . data_files <EOL> for filename in filenames <EOL> ] <EOL> def check_package ( self , package , package_dir ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . packages_checked [ package ] <EOL> except KeyError : <EOL> pass <EOL> init_py = _build_py . check_package ( self , package , package_dir ) <EOL> self . packages_checked [ package ] = init_py <EOL> if not init_py or not self . distribution . namespace_packages : <EOL> return init_py <EOL> for pkg in self . distribution . namespace_packages : <EOL> if pkg == package or pkg . startswith ( package + '<STR_LIT:.>' ) : <EOL> break <EOL> else : <EOL> return init_py <EOL> f = open ( init_py , '<STR_LIT>' ) <EOL> if '<STR_LIT>' . encode ( ) not in f . read ( ) : <EOL> from distutils import log <EOL> log . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" , package <EOL> ) <EOL> f . close ( ) <EOL> return init_py <EOL> def initialize_options ( self ) : <EOL> self . packages_checked = { } <EOL> _build_py . initialize_options ( self ) <EOL> def get_package_dir ( self , package ) : <EOL> res = _build_py . get_package_dir ( self , package ) <EOL> if self . distribution . src_root is not None : <EOL> return os . path . join ( self . distribution . src_root , res ) <EOL> return res <EOL> def exclude_data_files ( self , package , src_dir , files ) : <EOL> """<STR_LIT>""" <EOL> globs = ( self . exclude_package_data . get ( '<STR_LIT>' , [ ] ) <EOL> + self . exclude_package_data . get ( package , [ ] ) ) <EOL> bad = [ ] <EOL> for pattern in globs : <EOL> bad . extend ( <EOL> fnmatch . filter ( <EOL> files , os . path . join ( src_dir , convert_path ( pattern ) ) <EOL> ) <EOL> ) <EOL> bad = dict . fromkeys ( bad ) <EOL> seen = { } <EOL> return [ <EOL> f for f in files if f not in bad <EOL> and f not in seen and seen . setdefault ( f , <NUM_LIT:1> ) <EOL> ] <EOL> def assert_relative ( path ) : <EOL> if not os . path . isabs ( path ) : <EOL> return path <EOL> from distutils . errors import DistutilsSetupError <EOL> raise DistutilsSetupError ( <EOL> """<STR_LIT>""" % path <EOL> ) </s>
