<s> import os <EOL> import sys <EOL> import signal <EOL> sys . path . append ( os . getcwd ( ) ) <EOL> import fbuild <EOL> import fbuild . db <EOL> import fbuild . target <EOL> import fbuild . path <EOL> import fbuild . context <EOL> import fbuild . options <EOL> import fbuild . builders . file <EOL> try : <EOL> import fbuildroot <EOL> except ImportError as e : <EOL> fbuildroot = e <EOL> def parse_args ( argv ) : <EOL> parser = fbuild . options . make_parser ( ) <EOL> try : <EOL> pre_options = fbuildroot . pre_options <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> parser = pre_options ( parser ) or parser <EOL> options , args = parser . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> try : <EOL> post_options = fbuildroot . post_options <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> options , args = post_options ( options , args ) or ( options , args ) <EOL> return fbuild . context . Context ( options , args ) <EOL> def install_files ( ctx ) : <EOL> for subdir , files in ctx . to_install . items ( ) : <EOL> target_root = fbuild . path . Path ( subdir ) . addroot ( ctx . install_prefix ) <EOL> for file , froot in files : <EOL> file = file . relpath ( file . getcwd ( ) ) <EOL> target = file . removeroot ( ctx . buildroot / '<STR_LIT>' ) . addroot ( target_root / <EOL> froot ) <EOL> fbuild . builders . file . copy ( ctx , file , target ) <EOL> def build ( ctx ) : <EOL> if ctx . options . dump_state : <EOL> ctx . db . dump_database ( ) <EOL> return <NUM_LIT:0> <EOL> if ctx . options . delete_function : <EOL> if not ctx . db . delete_function ( ctx . options . delete_function ) : <EOL> raise fbuild . Error ( '<STR_LIT>' % <EOL> ctx . options . delete_function ) <EOL> return <NUM_LIT:0> <EOL> if ctx . options . delete_file : <EOL> if not ctx . db . delete_file ( ctx . options . delete_file ) : <EOL> raise fbuild . Error ( '<STR_LIT>' % ctx . options . delete_file ) <EOL> return <NUM_LIT:0> <EOL> targets = ctx . args or [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in targets : <EOL> if targets [ - <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise fbuild . Error ( '<STR_LIT>' ) <EOL> if not set ( targets ) - { '<STR_LIT>' , '<STR_LIT>' } : <EOL> targets . insert ( targets . index ( '<STR_LIT>' ) - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> for target_name in targets : <EOL> if target_name == '<STR_LIT>' : <EOL> install_files ( ctx ) <EOL> else : <EOL> target = fbuild . target . find ( target_name ) <EOL> target . function ( ctx ) <EOL> return <NUM_LIT:0> <EOL> def main ( argv = None ) : <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> fbuild . target . register ( name = name ) ( getattr ( fbuildroot , name ) ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> prune_get_all = fbuildroot . prune_get_all <EOL> except : <EOL> def prune_get_all ( ctx , root = None ) : <EOL> files = set ( ) <EOL> if root is None : <EOL> root = ctx . buildroot <EOL> for dirpath , dirnames , filenames in root . walk ( ) : <EOL> files . update ( map ( dirpath . __truediv__ , filenames ) ) <EOL> return files <EOL> try : <EOL> prune_get_bad = fbuildroot . prune_get_bad <EOL> except : <EOL> def prune_get_bad ( ctx , files ) : <EOL> return files <EOL> ctx = parse_args ( sys . argv if argv is None else argv ) <EOL> old_handler = signal . getsignal ( signal . SIGINT ) <EOL> def handle_interrupt ( signum , frame ) : <EOL> ctx . scheduler . shutdown ( ) <EOL> old_handler ( signum , frame ) <EOL> signal . signal ( signal . SIGINT , handle_interrupt ) <EOL> try : <EOL> if isinstance ( fbuildroot , Exception ) : <EOL> if not os . path . exists ( '<STR_LIT>' ) : <EOL> raise fbuild . Error ( '<STR_LIT>' ) <EOL> else : <EOL> raise fbuildroot <EOL> if ctx . options . clean_buildroot : <EOL> if ctx . options . buildroot . exists ( ) : <EOL> ctx . options . buildroot . rmtree ( ) <EOL> return <EOL> ctx . create_buildroot ( ) <EOL> ctx . load_configuration ( ) <EOL> try : <EOL> result = build ( ctx ) <EOL> if ctx . options . prune : <EOL> ctx . prune ( prune_get_all , prune_get_bad ) <EOL> except fbuild . Error as e : <EOL> ctx . logger . log ( e , color = '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except KeyboardInterrupt : <EOL> print ( '<STR_LIT>' ) <EOL> raise <EOL> finally : <EOL> ctx . save_configuration ( ) <EOL> ctx . db . shutdown ( ) <EOL> finally : <EOL> ctx . scheduler . shutdown ( ) <EOL> return result </s>
