<s> import os <EOL> import sys <EOL> import shutil <EOL> import logging <EOL> import binascii <EOL> import tempfile <EOL> import ppctools <EOL> from errors import CodetypeError , UnsupportedOSError <EOL> class PyiiAsmhApp ( object ) : <EOL> def __init__ ( self ) : <EOL> self . opcodes = None <EOL> self . geckocodes = None <EOL> self . bapo = None <EOL> self . xor = None <EOL> self . chksum = None <EOL> self . codetype = None <EOL> self . log = logging . getLogger ( "<STR_LIT>" ) <EOL> try : <EOL> os . remove ( "<STR_LIT>" ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> os . rename ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> except OSError : <EOL> pass <EOL> hdlr = logging . FileHandler ( "<STR_LIT>" ) <EOL> formatter = logging . Formatter ( "<STR_LIT>" ) <EOL> hdlr . setFormatter ( formatter ) <EOL> self . log . addHandler ( hdlr ) <EOL> def assemble ( self , inputfile , outputfile = None , filetype = "<STR_LIT:text>" ) : <EOL> tmpdir = tempfile . mkdtemp ( prefix = "<STR_LIT>" ) + os . path . normcase ( "<STR_LIT:/>" ) <EOL> if filetype is None : <EOL> try : <EOL> f = open ( tmpdir + "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> except IOError : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> shutil . rmtree ( tmpdir ) <EOL> return None <EOL> else : <EOL> try : <EOL> f . seek ( <NUM_LIT:0> ) <EOL> f . write ( inputfile ) <EOL> finally : <EOL> f . close ( ) <EOL> inputfile = None <EOL> try : <EOL> toReturn = "<STR_LIT>" <EOL> geckocodes = ppctools . asm_opcodes ( tmpdir , inputfile , outputfile ) <EOL> except UnsupportedOSError : <EOL> self . log . exception ( ) <EOL> toReturn = ( "<STR_LIT>" + sys . platform + "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> except IOError as e : <EOL> self . log . exception ( e ) <EOL> toReturn = "<STR_LIT>" + str ( e ) <EOL> except RuntimeError as e : <EOL> self . log . exception ( e ) <EOL> toReturn = str ( e ) <EOL> else : <EOL> toReturn = ppctools . construct_code ( geckocodes , <EOL> self . bapo , self . xor , self . chksum , self . codetype ) <EOL> shutil . rmtree ( tmpdir ) <EOL> return toReturn <EOL> def disassemble ( self , inputfile , outputfile = None , filetype = "<STR_LIT:text>" ) : <EOL> tmpdir = tempfile . mkdtemp ( prefix = "<STR_LIT>" ) + os . path . normcase ( "<STR_LIT:/>" ) <EOL> codes = None <EOL> if filetype is None : <EOL> codes = inputfile <EOL> else : <EOL> try : <EOL> if filetype == "<STR_LIT>" : <EOL> f = open ( inputfile , "<STR_LIT:rb>" ) <EOL> else : <EOL> f = open ( inputfile , "<STR_LIT:r>" ) <EOL> except IOError : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> shutil . rmtree ( tmpdir ) <EOL> return None <EOL> else : <EOL> try : <EOL> f . seek ( <NUM_LIT:0> ) <EOL> codes = "<STR_LIT>" . join ( f . readlines ( ) ) <EOL> if filetype == "<STR_LIT>" : <EOL> codes = binascii . b2a_hex ( "<STR_LIT>" . join ( codes ) ) . upper ( ) <EOL> finally : <EOL> f . close ( ) <EOL> rawcodes = ppctools . deconstruct_code ( codes ) <EOL> try : <EOL> f = open ( tmpdir + "<STR_LIT>" , "<STR_LIT:wb>" ) <EOL> except IOError : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> f . seek ( <NUM_LIT:0> ) <EOL> rawhex = "<STR_LIT>" . join ( "<STR_LIT>" . join ( rawcodes [ <NUM_LIT:0> ] . split ( "<STR_LIT:\n>" ) ) . split ( "<STR_LIT:U+0020>" ) ) <EOL> f . write ( binascii . a2b_hex ( rawhex ) ) <EOL> except IOError : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> except TypeError as e : <EOL> self . log . exception ( e ) <EOL> finally : <EOL> f . close ( ) <EOL> try : <EOL> toReturn = ( "<STR_LIT>" , ( None , None , None , None ) ) <EOL> opcodes = ppctools . dsm_geckocodes ( tmpdir , outputfile , None ) <EOL> except UnsupportedOSError : <EOL> self . log . exception ( "<STR_LIT>" ) <EOL> toReturn = ( ( "<STR_LIT>" + sys . platform + "<STR_LIT>" + <EOL> "<STR_LIT>" ) , <EOL> ( None , None , None , None ) ) <EOL> except IOError as e : <EOL> self . log . exception ( e ) <EOL> toReturn = ( "<STR_LIT>" + str ( e ) , ( None , None , None , None ) ) <EOL> except RuntimeError as e : <EOL> self . log . exception ( e ) <EOL> toReturn = ( str ( e ) , ( None , None , None , None ) ) <EOL> else : <EOL> toReturn = ( opcodes + "<STR_LIT:\n>" , rawcodes [ <NUM_LIT:1> : ] ) <EOL> shutil . rmtree ( tmpdir ) <EOL> return toReturn <EOL> def print_usage ( self , err = None ) : <EOL> if err : <EOL> print err + "<STR_LIT:\n>" <EOL> name = "<STR_LIT>" <EOL> print ( "<STR_LIT>" + name + "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + name + "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT:U+0020>" + name + "<STR_LIT>" <EOL> print "<STR_LIT:U+0020>" + name + "<STR_LIT>" <EOL> print "<STR_LIT:U+0020>" + name + "<STR_LIT>" <EOL> print "<STR_LIT:U+0020>" + name + "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def run ( self , args ) : <EOL> commands = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> options = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> filetypes = ( "<STR_LIT:text>" , "<STR_LIT>" ) <EOL> codetypes = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> good_bapo = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> inputfile_argnum = <NUM_LIT:1> <EOL> filetype = "<STR_LIT:text>" <EOL> try : <EOL> if args [ <NUM_LIT:0> ] not in commands : <EOL> self . print_usage ( "<STR_LIT>" + args [ <NUM_LIT:0> ] + "<STR_LIT:'>" ) <EOL> elif args [ <NUM_LIT:0> ] in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . print_usage ( ) <EOL> elif args [ <NUM_LIT:1> ] != "<STR_LIT>" : <EOL> if args [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> if args [ <NUM_LIT:2> ] not in filetypes : <EOL> self . print_usage ( "<STR_LIT:'>" + args [ <NUM_LIT:2> ] + "<STR_LIT>" ) <EOL> filetype = args [ <NUM_LIT:2> ] <EOL> inputfile_argnum = <NUM_LIT:3> <EOL> open ( args [ <NUM_LIT:3> ] , "<STR_LIT:r>" ) . close ( ) <EOL> else : <EOL> open ( args [ <NUM_LIT:1> ] , "<STR_LIT:r>" ) . close ( ) <EOL> elif args [ <NUM_LIT:2> ] . upper ( ) not in codetypes : <EOL> self . print_usage ( "<STR_LIT>" + args [ <NUM_LIT:2> ] + "<STR_LIT:'>" ) <EOL> elif args [ <NUM_LIT:2> ] . upper ( ) == "<STR_LIT>" : <EOL> open ( args [ <NUM_LIT:1> ] , "<STR_LIT:r>" ) . close ( ) <EOL> else : <EOL> self . codetype = args [ <NUM_LIT:2> ] . upper ( ) <EOL> if self . codetype == "<STR_LIT>" : <EOL> inputfile_argnum = <NUM_LIT:3> <EOL> open ( args [ <NUM_LIT:3> ] , "<STR_LIT:r>" ) . close ( ) <EOL> elif args [ <NUM_LIT:3> ] != "<STR_LIT>" : <EOL> self . print_usage ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> elif len ( args [ <NUM_LIT:4> ] ) != <NUM_LIT:8> or args [ <NUM_LIT:4> ] [ : <NUM_LIT:2> ] not in good_bapo : <EOL> self . print_usage ( "<STR_LIT:'>" + args [ <NUM_LIT:4> ] + "<STR_LIT>" ) <EOL> elif self . codetype == "<STR_LIT>" : <EOL> int ( args [ <NUM_LIT:4> ] , <NUM_LIT:16> ) <EOL> self . bapo = args [ <NUM_LIT:4> ] <EOL> inputfile_argnum = <NUM_LIT:5> <EOL> open ( args [ <NUM_LIT:5> ] , "<STR_LIT:r>" ) . close ( ) <EOL> elif args [ <NUM_LIT:5> ] not in options [ <NUM_LIT:3> ] or args [ <NUM_LIT:7> ] not in options [ <NUM_LIT:3> ] : <EOL> self . print_usage ( "<STR_LIT>" <EOL> + "<STR_LIT>" + args [ <NUM_LIT:5> ] + "<STR_LIT>" + args [ <NUM_LIT:7> ] + "<STR_LIT>" ) <EOL> elif args [ <NUM_LIT:5> ] == args [ <NUM_LIT:7> ] : <EOL> self . print_usage ( "<STR_LIT>" ) <EOL> else : <EOL> xornum = <NUM_LIT:5> <EOL> chknum = <NUM_LIT:7> <EOL> int ( args [ <NUM_LIT:4> ] , <NUM_LIT:16> ) <EOL> self . bapo = args [ <NUM_LIT:4> ] <EOL> if args [ <NUM_LIT:7> ] == "<STR_LIT>" : <EOL> xornum = <NUM_LIT:7> <EOL> chknum = <NUM_LIT:5> <EOL> if len ( args [ xornum + <NUM_LIT:1> ] ) != <NUM_LIT:4> : <EOL> self . print_usage ( "<STR_LIT:'>" + args [ xornum + <NUM_LIT:1> ] + "<STR_LIT>" ) <EOL> elif len ( args [ chknum + <NUM_LIT:1> ] ) != <NUM_LIT:2> : <EOL> self . print_usage ( "<STR_LIT:'>" + args [ chknum + <NUM_LIT:1> ] + "<STR_LIT>" ) <EOL> int ( args [ <NUM_LIT:6> ] , <NUM_LIT:16> ) <EOL> int ( args [ <NUM_LIT:8> ] , <NUM_LIT:16> ) <EOL> self . xor = args [ xornum + <NUM_LIT:1> ] <EOL> self . chksum = args [ chknum + <NUM_LIT:1> ] <EOL> inputfile_argnum = <NUM_LIT:9> <EOL> open ( args [ <NUM_LIT:9> ] , "<STR_LIT:r>" ) . close ( ) <EOL> except IndexError : <EOL> self . print_usage ( ) <EOL> except ValueError : <EOL> self . print_usage ( "<STR_LIT>" ) <EOL> except IOError : <EOL> self . print_usage ( "<STR_LIT>" + args [ inputfile_argnum ] + "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> args [ inputfile_argnum + <NUM_LIT:1> ] <EOL> except IndexError : <EOL> if args [ <NUM_LIT:0> ] in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> print self . assemble ( args [ inputfile_argnum ] , None , <EOL> filetype ) <EOL> else : <EOL> print self . disassemble ( args [ inputfile_argnum ] , None , <EOL> filetype ) [ <NUM_LIT:0> ] <EOL> else : <EOL> if args [ <NUM_LIT:0> ] in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . assemble ( args [ inputfile_argnum ] , <EOL> args [ inputfile_argnum + <NUM_LIT:1> ] , filetype ) <EOL> else : <EOL> self . disassemble ( args [ inputfile_argnum ] , <EOL> args [ inputfile_argnum + <NUM_LIT:1> ] , filetype ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> app = PyiiAsmhApp ( ) <EOL> app . run ( sys . argv [ <NUM_LIT:1> : ] ) </s>
