<s> import os <EOL> import shutil <EOL> import subprocess <EOL> import errno <EOL> from bento . _config import BUILD_MANIFEST_PATH <EOL> from bento . installed_package_description import BuildManifest , iter_files <EOL> from bento . commands . core import Command , Option <EOL> from bento . utils . utils import pprint , extract_exception , MODE_755 , MODE_777 <EOL> def _rollback_operation ( line ) : <EOL> operation , arg = line . split ( ) <EOL> if operation == "<STR_LIT>" : <EOL> try : <EOL> os . rmdir ( arg ) <EOL> except OSError : <EOL> e = extract_exception ( ) <EOL> if e . errno != <NUM_LIT> : <EOL> raise <EOL> elif operation == "<STR_LIT>" : <EOL> os . remove ( arg ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % operation ) <EOL> def rollback_transaction ( f ) : <EOL> fid = open ( f ) <EOL> try : <EOL> lines = fid . readlines ( ) <EOL> for i in range ( len ( lines ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> _rollback_operation ( lines [ i ] ) <EOL> lines . pop ( i ) <EOL> finally : <EOL> fid . close ( ) <EOL> if len ( lines ) < <NUM_LIT:1> : <EOL> os . remove ( f ) <EOL> else : <EOL> fid = open ( f , "<STR_LIT:w>" ) <EOL> try : <EOL> fid . writelines ( lines ) <EOL> finally : <EOL> fid . close ( ) <EOL> class TransactionLog ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , journal_filename ) : <EOL> if os . path . exists ( journal_filename ) : <EOL> raise IOError ( "<STR_LIT>" % journal_filename ) <EOL> open ( journal_filename , "<STR_LIT:w>" ) . close ( ) <EOL> self . f = open ( journal_filename , "<STR_LIT:w>" ) <EOL> self . journal_filename = journal_filename <EOL> def copy ( self , source , target , category ) : <EOL> if os . path . exists ( target ) : <EOL> self . rollback ( ) <EOL> raise ValueError ( "<STR_LIT>" % target ) <EOL> d = os . path . dirname ( target ) <EOL> if not os . path . exists ( d ) : <EOL> self . makedirs ( d ) <EOL> self . f . write ( "<STR_LIT>" % target ) <EOL> shutil . copy ( source , target ) <EOL> if category == "<STR_LIT>" : <EOL> os . chmod ( target , MODE_755 ) <EOL> def makedirs ( self , name , mode = MODE_777 ) : <EOL> head , tail = os . path . split ( name ) <EOL> if not tail : <EOL> head , tail = os . path . split ( head ) <EOL> if head and tail and not os . path . exists ( head ) : <EOL> try : <EOL> self . makedirs ( head , mode ) <EOL> except OSError : <EOL> e = extract_exception ( ) <EOL> if e . errno != errno . EEXIST : <EOL> raise <EOL> if tail == os . curdir : <EOL> return <EOL> self . mkdir ( name , mode ) <EOL> def mkdir ( self , name , mode = MODE_777 ) : <EOL> self . f . write ( "<STR_LIT>" % name ) <EOL> self . f . flush ( ) <EOL> os . mkdir ( name , mode ) <EOL> def close ( self ) : <EOL> if self . f is not None : <EOL> self . f . close ( ) <EOL> def rollback ( self ) : <EOL> self . f . close ( ) <EOL> self . f = open ( self . journal_filename , "<STR_LIT:r>" ) <EOL> lines = self . f . readlines ( ) <EOL> for line in lines [ : : - <NUM_LIT:1> ] : <EOL> _rollback_operation ( line . strip ( ) ) <EOL> self . f = None <EOL> def copy_installer ( source , target , kind ) : <EOL> dtarget = os . path . dirname ( target ) <EOL> if not os . path . exists ( dtarget ) : <EOL> os . makedirs ( dtarget ) <EOL> shutil . copy ( source , target ) <EOL> if kind == "<STR_LIT>" : <EOL> os . chmod ( target , MODE_755 ) <EOL> def unix_installer ( source , target , kind ) : <EOL> if kind in [ "<STR_LIT>" ] : <EOL> mode = "<STR_LIT>" <EOL> else : <EOL> mode = "<STR_LIT>" <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" , mode , source , target ] <EOL> strcmd = "<STR_LIT>" % ( source , target ) <EOL> pprint ( '<STR_LIT>' , strcmd ) <EOL> if not os . path . exists ( os . path . dirname ( target ) ) : <EOL> os . makedirs ( os . path . dirname ( target ) ) <EOL> subprocess . check_call ( cmd ) <EOL> class InstallCommand ( Command ) : <EOL> long_descr = """<STR_LIT>""" <EOL> short_descr = "<STR_LIT>" <EOL> common_options = Command . common_options + [ Option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) , <EOL> Option ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" ) ] <EOL> def run ( self , ctx ) : <EOL> argv = ctx . command_argv <EOL> p = ctx . options_context . parser <EOL> o , a = p . parse_args ( argv ) <EOL> if o . help : <EOL> p . print_help ( ) <EOL> return <EOL> n = ctx . build_node . make_node ( BUILD_MANIFEST_PATH ) <EOL> build_manifest = BuildManifest . from_file ( n . abspath ( ) ) <EOL> scheme = ctx . retrieve_configured_scheme ( ) <EOL> build_manifest . update_paths ( scheme ) <EOL> node_sections = build_manifest . resolve_paths_with_destdir ( ctx . build_node ) <EOL> if o . list_files : <EOL> for kind , source , target in iter_files ( node_sections ) : <EOL> print ( target . abspath ( ) ) <EOL> return <EOL> if o . transaction : <EOL> trans = TransactionLog ( "<STR_LIT>" ) <EOL> try : <EOL> for kind , source , target in iter_files ( node_sections ) : <EOL> trans . copy ( source . abspath ( ) , target . abspath ( ) , kind ) <EOL> finally : <EOL> trans . close ( ) <EOL> else : <EOL> for kind , source , target in iter_files ( node_sections ) : <EOL> copy_installer ( source . abspath ( ) , target . abspath ( ) , kind ) </s>
