<s> import collections <EOL> import six <EOL> from anvil import log as logging <EOL> from anvil import settings <EOL> from anvil import shell as sh <EOL> from anvil import utils <EOL> from anvil . packaging . helpers import pip_helper <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def _fetch_missing_extra ( python_names_in , python_names_out ) : <EOL> python_missing_names = set ( ) <EOL> python_extra_names = set ( ) <EOL> python_reqs_in = set ( pip_helper . extract_requirement ( n ) <EOL> for n in python_names_in ) <EOL> python_reqs_out = set ( pip_helper . extract_requirement ( n ) <EOL> for n in python_names_out ) <EOL> for req in python_reqs_in : <EOL> if req not in python_reqs_out : <EOL> python_missing_names . add ( str ( req ) ) <EOL> for req in python_reqs_out : <EOL> if req not in python_reqs_in : <EOL> python_extra_names . add ( str ( req ) ) <EOL> return ( python_missing_names , python_extra_names ) <EOL> class Helper ( object ) : <EOL> def __init__ ( self , epoch_map , package_map , arch_dependent , <EOL> rpmbuild_dir , download_dir , deps_dir , log_dir , <EOL> build_options ) : <EOL> self . _py2rpm_executable = sh . which ( "<STR_LIT>" , [ "<STR_LIT>" ] ) <EOL> self . _epoch_map = epoch_map <EOL> self . _package_map = package_map <EOL> self . _arch_dependent = arch_dependent <EOL> self . _rpmbuild_dir = rpmbuild_dir <EOL> self . _download_dir = download_dir <EOL> self . _deps_dir = deps_dir <EOL> self . _log_dir = log_dir <EOL> self . _build_options = build_options <EOL> @ staticmethod <EOL> def _make_value_escape ( value ) : <EOL> value = value . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) <EOL> value = value . replace ( "<STR_LIT:$>" , "<STR_LIT>" ) <EOL> value = value . replace ( "<STR_LIT:#>" , "<STR_LIT>" ) <EOL> return value <EOL> def _start_cmdline ( self , escape_values = False ) : <EOL> if escape_values : <EOL> escape_func = self . _make_value_escape <EOL> else : <EOL> escape_func = lambda value : value <EOL> cmdline = [ <EOL> self . _py2rpm_executable , <EOL> "<STR_LIT>" , <EOL> self . _rpmbuild_dir <EOL> ] <EOL> if self . _epoch_map : <EOL> cmdline += [ <EOL> "<STR_LIT>" <EOL> ] + [ "<STR_LIT>" % ( key , escape_func ( value ) ) <EOL> for key , value in self . _epoch_map . iteritems ( ) ] <EOL> if self . _package_map : <EOL> cmdline += [ <EOL> "<STR_LIT>" , <EOL> ] + [ "<STR_LIT>" % ( key , escape_func ( value ) ) <EOL> for key , value in self . _package_map . iteritems ( ) ] <EOL> if self . _build_options : <EOL> build_options = [ ] <EOL> for key , values in self . _build_options . iteritems ( ) : <EOL> if values : <EOL> for value in values : <EOL> build_options . append ( "<STR_LIT>" % ( key , <EOL> escape_func ( value ) ) ) <EOL> if build_options : <EOL> cmdline . append ( "<STR_LIT>" ) <EOL> cmdline . extend ( build_options ) <EOL> if self . _arch_dependent : <EOL> cmdline += [ <EOL> "<STR_LIT>" , <EOL> ] + list ( self . _arch_dependent ) <EOL> return cmdline <EOL> def _execute_make ( self , filename , marks_dir , jobs ) : <EOL> cmdline = [ "<STR_LIT>" , "<STR_LIT>" , filename , "<STR_LIT>" , str ( jobs ) ] <EOL> out_filename = sh . joinpths ( self . _log_dir , "<STR_LIT>" % sh . basename ( filename ) ) <EOL> sh . execute_save_output ( cmdline , out_filename , cwd = marks_dir ) <EOL> def _convert_names_to_rpm ( self , python_names , only_name ) : <EOL> if not python_names : <EOL> return { } , { } <EOL> cmdline = self . _start_cmdline ( ) + [ "<STR_LIT>" ] + python_names <EOL> result = collections . defaultdict ( set ) <EOL> conflicts = collections . defaultdict ( set ) <EOL> current_source = None <EOL> for line in sh . execute ( cmdline ) [ <NUM_LIT:0> ] . splitlines ( ) : <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> line = line [ len ( "<STR_LIT>" ) : ] <EOL> if only_name : <EOL> positions = [ line . find ( "<STR_LIT:>>" ) , line . find ( "<STR_LIT:<>" ) , line . find ( "<STR_LIT:=>" ) ] <EOL> positions = sorted ( [ p for p in positions if p != - <NUM_LIT:1> ] ) <EOL> if positions : <EOL> line = line [ <NUM_LIT:0> : positions [ <NUM_LIT:0> ] ] <EOL> result [ current_source ] . add ( line . strip ( ) ) <EOL> elif line . startswith ( "<STR_LIT>" ) : <EOL> line = line [ len ( "<STR_LIT>" ) : ] <EOL> conflicts [ current_source ] . add ( line . strip ( ) ) <EOL> elif line . startswith ( "<STR_LIT>" ) : <EOL> current_source = line [ len ( "<STR_LIT>" ) : ] . strip ( ) <EOL> found_names = set ( result . keys ( ) ) <EOL> found_names . update ( conflicts . keys ( ) ) <EOL> missing_names , extra_names = _fetch_missing_extra ( python_names , <EOL> found_names ) <EOL> if missing_names : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % '<STR_LIT:U+002CU+0020>' . join ( sorted ( missing_names ) ) ) <EOL> if extra_names : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % '<STR_LIT:U+002CU+0020>' . join ( sorted ( extra_names ) ) ) <EOL> return result , conflicts <EOL> def names_to_rpm_names ( self , python_names ) : <EOL> mapping = self . _convert_names_to_rpm ( python_names , only_name = True ) [ <NUM_LIT:0> ] <EOL> result = { } <EOL> for k , v in six . iteritems ( mapping ) : <EOL> assert len ( v ) == <NUM_LIT:1> , ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( k , sorted ( v ) ) ) <EOL> result [ k ] = v . pop ( ) <EOL> return result <EOL> def names_to_rpm_deps ( self , python_names ) : <EOL> requires , conflicts = self . _convert_names_to_rpm ( python_names , only_name = False ) <EOL> requires_list = [ req for value in six . itervalues ( requires ) for req in value ] <EOL> conflicts_list = [ req for value in six . itervalues ( conflicts ) for req in value ] <EOL> return requires_list , conflicts_list <EOL> def build_all_srpms ( self , package_files , tracewriter , jobs ) : <EOL> ( _fn , content ) = utils . load_template ( sh . joinpths ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> scripts_dir = sh . abspth ( sh . joinpths ( settings . TEMPLATE_DIR , "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> cmdline = self . _start_cmdline ( escape_values = True ) [ <NUM_LIT:1> : ] + [ <EOL> "<STR_LIT>" , scripts_dir , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , self . _rpmbuild_dir , <EOL> "<STR_LIT>" , <EOL> ] <EOL> executable = "<STR_LIT:U+0020>" . join ( self . _start_cmdline ( ) [ <NUM_LIT:0> : <NUM_LIT:1> ] ) <EOL> params = { <EOL> "<STR_LIT>" : self . _download_dir , <EOL> "<STR_LIT>" : self . _log_dir , <EOL> "<STR_LIT>" : executable , <EOL> "<STR_LIT>" : "<STR_LIT:U+0020>" . join ( cmdline ) <EOL> } <EOL> marks_dir = sh . joinpths ( self . _deps_dir , "<STR_LIT>" ) <EOL> if not sh . isdir ( marks_dir ) : <EOL> sh . mkdirslist ( marks_dir , tracewriter = tracewriter ) <EOL> makefile_path = sh . joinpths ( self . _deps_dir , "<STR_LIT>" ) <EOL> sh . write_file ( makefile_path , utils . expand_template ( content , params ) , <EOL> tracewriter = tracewriter ) <EOL> utils . log_iterable ( package_files , <EOL> header = "<STR_LIT>" % <EOL> ( len ( package_files ) , jobs ) , <EOL> logger = LOG ) <EOL> self . _execute_make ( makefile_path , marks_dir , jobs ) <EOL> def build_srpm ( self , source , log_filename , <EOL> release = None , with_tests = False ) : <EOL> cmdline = self . _start_cmdline ( ) + [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> if release is not None : <EOL> cmdline . extend ( [ "<STR_LIT>" , release ] ) <EOL> if with_tests : <EOL> cmdline . append ( "<STR_LIT>" ) <EOL> cmdline . extend ( [ "<STR_LIT>" , source ] ) <EOL> out_filename = sh . joinpths ( self . _log_dir , <EOL> "<STR_LIT>" % log_filename ) <EOL> sh . execute_save_output ( cmdline , out_filename , cwd = source ) <EOL> def build_all_binaries ( self , repo_name , src_repo_dir , rpmbuild_flags , <EOL> tracewriter , jobs ) : <EOL> makefile_path = sh . joinpths ( self . _deps_dir , "<STR_LIT>" % repo_name ) <EOL> marks_dir = sh . joinpths ( self . _deps_dir , "<STR_LIT>" ) <EOL> if not sh . isdir ( marks_dir ) : <EOL> sh . mkdirslist ( marks_dir , tracewriter = tracewriter ) <EOL> params = { <EOL> "<STR_LIT>" : src_repo_dir , <EOL> "<STR_LIT>" : rpmbuild_flags , <EOL> "<STR_LIT>" : self . _log_dir , <EOL> "<STR_LIT>" : self . _rpmbuild_dir , <EOL> } <EOL> ( _fn , content ) = utils . load_template ( sh . joinpths ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> sh . write_file ( makefile_path , utils . expand_template ( content , params ) , <EOL> tracewriter = tracewriter ) <EOL> self . _execute_make ( makefile_path , marks_dir , jobs ) </s>
