<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import sys , string , os <EOL> from types import * <EOL> from glob import glob <EOL> from distutils . core import Command <EOL> from distutils . errors import * <EOL> from distutils . util import convert_path <EOL> from distutils import log <EOL> class build_py ( Command ) : <EOL> description = "<STR_LIT>" <EOL> user_options = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:d>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:c>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , None , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:O>' , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:f>' , "<STR_LIT>" ) , <EOL> ] <EOL> boolean_options = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> negative_opt = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def initialize_options ( self ) : <EOL> self . build_lib = None <EOL> self . py_modules = None <EOL> self . package = None <EOL> self . package_data = None <EOL> self . package_dir = None <EOL> self . compile = <NUM_LIT:0> <EOL> self . optimize = <NUM_LIT:0> <EOL> self . force = None <EOL> def finalize_options ( self ) : <EOL> self . set_undefined_options ( '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . packages = self . distribution . packages <EOL> self . py_modules = self . distribution . py_modules <EOL> self . package_data = self . distribution . package_data <EOL> self . package_dir = { } <EOL> if self . distribution . package_dir : <EOL> for name , path in self . distribution . package_dir . items ( ) : <EOL> self . package_dir [ name ] = convert_path ( path ) <EOL> self . data_files = self . get_data_files ( ) <EOL> if type ( self . optimize ) is not IntType : <EOL> try : <EOL> self . optimize = int ( self . optimize ) <EOL> assert <NUM_LIT:0> <= self . optimize <= <NUM_LIT:2> <EOL> except ( ValueError , AssertionError ) : <EOL> raise DistutilsOptionError , "<STR_LIT>" <EOL> def run ( self ) : <EOL> if self . py_modules : <EOL> self . build_modules ( ) <EOL> if self . packages : <EOL> self . build_packages ( ) <EOL> self . build_package_data ( ) <EOL> self . byte_compile ( self . get_outputs ( include_bytecode = <NUM_LIT:0> ) ) <EOL> def get_data_files ( self ) : <EOL> """<STR_LIT>""" <EOL> data = [ ] <EOL> if not self . packages : <EOL> return data <EOL> for package in self . packages : <EOL> src_dir = self . get_package_dir ( package ) <EOL> build_dir = os . path . join ( * ( [ self . build_lib ] + package . split ( '<STR_LIT:.>' ) ) ) <EOL> plen = <NUM_LIT:0> <EOL> if src_dir : <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 = [ ] <EOL> for pattern in globs : <EOL> filelist = glob ( os . path . join ( src_dir , convert_path ( pattern ) ) ) <EOL> files . extend ( [ fn for fn in filelist if fn not in files ] ) <EOL> return 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> self . copy_file ( os . path . join ( src_dir , filename ) , target , <EOL> preserve_mode = False ) <EOL> def get_package_dir ( self , package ) : <EOL> """<STR_LIT>""" <EOL> path = string . split ( package , '<STR_LIT:.>' ) <EOL> if not self . package_dir : <EOL> if path : <EOL> return apply ( os . path . join , path ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> else : <EOL> tail = [ ] <EOL> while path : <EOL> try : <EOL> pdir = self . package_dir [ string . join ( path , '<STR_LIT:.>' ) ] <EOL> except KeyError : <EOL> tail . insert ( <NUM_LIT:0> , path [ - <NUM_LIT:1> ] ) <EOL> del path [ - <NUM_LIT:1> ] <EOL> else : <EOL> tail . insert ( <NUM_LIT:0> , pdir ) <EOL> return apply ( os . path . join , tail ) <EOL> else : <EOL> pdir = self . package_dir . get ( '<STR_LIT>' ) <EOL> if pdir is not None : <EOL> tail . insert ( <NUM_LIT:0> , pdir ) <EOL> if tail : <EOL> return apply ( os . path . join , tail ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def check_package ( self , package , package_dir ) : <EOL> if package_dir != "<STR_LIT>" : <EOL> if not os . path . exists ( package_dir ) : <EOL> raise DistutilsFileError , "<STR_LIT>" % package_dir <EOL> if not os . path . isdir ( package_dir ) : <EOL> raise DistutilsFileError , ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) % package_dir <EOL> if package : <EOL> init_py = os . path . join ( package_dir , "<STR_LIT>" ) <EOL> if os . path . isfile ( init_py ) : <EOL> return init_py <EOL> else : <EOL> log . warn ( ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) , init_py ) <EOL> return None <EOL> def check_module ( self , module , module_file ) : <EOL> if not os . path . isfile ( module_file ) : <EOL> log . warn ( "<STR_LIT>" , module_file , module ) <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def find_package_modules ( self , package , package_dir ) : <EOL> self . check_package ( package , package_dir ) <EOL> module_files = glob ( os . path . join ( package_dir , "<STR_LIT>" ) ) <EOL> modules = [ ] <EOL> setup_script = os . path . abspath ( self . distribution . script_name ) <EOL> for f in module_files : <EOL> abs_f = os . path . abspath ( f ) <EOL> if abs_f != setup_script : <EOL> module = os . path . splitext ( os . path . basename ( f ) ) [ <NUM_LIT:0> ] <EOL> modules . append ( ( package , module , f ) ) <EOL> else : <EOL> self . debug_print ( "<STR_LIT>" % setup_script ) <EOL> return modules <EOL> def find_modules ( self ) : <EOL> """<STR_LIT>""" <EOL> packages = { } <EOL> modules = [ ] <EOL> for module in self . py_modules : <EOL> path = string . split ( module , '<STR_LIT:.>' ) <EOL> package = string . join ( path [ <NUM_LIT:0> : - <NUM_LIT:1> ] , '<STR_LIT:.>' ) <EOL> module_base = path [ - <NUM_LIT:1> ] <EOL> try : <EOL> ( package_dir , checked ) = packages [ package ] <EOL> except KeyError : <EOL> package_dir = self . get_package_dir ( package ) <EOL> checked = <NUM_LIT:0> <EOL> if not checked : <EOL> init_py = self . check_package ( package , package_dir ) <EOL> packages [ package ] = ( package_dir , <NUM_LIT:1> ) <EOL> if init_py : <EOL> modules . append ( ( package , "<STR_LIT>" , init_py ) ) <EOL> module_file = os . path . join ( package_dir , module_base + "<STR_LIT>" ) <EOL> if not self . check_module ( module , module_file ) : <EOL> continue <EOL> modules . append ( ( package , module_base , module_file ) ) <EOL> return modules <EOL> def find_all_modules ( self ) : <EOL> """<STR_LIT>""" <EOL> modules = [ ] <EOL> if self . py_modules : <EOL> modules . extend ( self . find_modules ( ) ) <EOL> if self . packages : <EOL> for package in self . packages : <EOL> package_dir = self . get_package_dir ( package ) <EOL> m = self . find_package_modules ( package , package_dir ) <EOL> modules . extend ( m ) <EOL> return modules <EOL> def get_source_files ( self ) : <EOL> modules = self . find_all_modules ( ) <EOL> filenames = [ ] <EOL> for module in modules : <EOL> filenames . append ( module [ - <NUM_LIT:1> ] ) <EOL> return filenames <EOL> def get_module_outfile ( self , build_dir , package , module ) : <EOL> outfile_path = [ build_dir ] + list ( package ) + [ module + "<STR_LIT>" ] <EOL> return apply ( os . path . join , outfile_path ) <EOL> def get_outputs ( self , include_bytecode = <NUM_LIT:1> ) : <EOL> modules = self . find_all_modules ( ) <EOL> outputs = [ ] <EOL> for ( package , module , module_file ) in modules : <EOL> package = string . split ( package , '<STR_LIT:.>' ) <EOL> filename = self . get_module_outfile ( self . build_lib , package , module ) <EOL> outputs . append ( filename ) <EOL> if include_bytecode : <EOL> if self . compile : <EOL> outputs . append ( filename + "<STR_LIT:c>" ) <EOL> if self . optimize > <NUM_LIT:0> : <EOL> outputs . append ( filename + "<STR_LIT:o>" ) <EOL> outputs += [ <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> return outputs <EOL> def build_module ( self , module , module_file , package ) : <EOL> if type ( package ) is StringType : <EOL> package = string . split ( package , '<STR_LIT:.>' ) <EOL> elif type ( package ) not in ( ListType , TupleType ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> outfile = self . get_module_outfile ( self . build_lib , package , module ) <EOL> dir = os . path . dirname ( outfile ) <EOL> self . mkpath ( dir ) <EOL> return self . copy_file ( module_file , outfile , preserve_mode = <NUM_LIT:0> ) <EOL> def build_modules ( self ) : <EOL> modules = self . find_modules ( ) <EOL> for ( package , module , module_file ) in modules : <EOL> self . build_module ( module , module_file , package ) <EOL> def build_packages ( self ) : <EOL> for package in self . packages : <EOL> package_dir = self . get_package_dir ( package ) <EOL> modules = self . find_package_modules ( package , package_dir ) <EOL> for ( package_ , module , module_file ) in modules : <EOL> assert package == package_ <EOL> self . build_module ( module , module_file , package ) <EOL> def byte_compile ( self , files ) : <EOL> from distutils . util import byte_compile <EOL> prefix = self . build_lib <EOL> if prefix [ - <NUM_LIT:1> ] != os . sep : <EOL> prefix = prefix + os . sep <EOL> if self . compile : <EOL> byte_compile ( files , optimize = <NUM_LIT:0> , <EOL> force = self . force , prefix = prefix , dry_run = self . dry_run ) <EOL> if self . optimize > <NUM_LIT:0> : <EOL> byte_compile ( files , optimize = self . optimize , <EOL> force = self . force , prefix = prefix , dry_run = self . dry_run ) </s>
