<s> """<STR_LIT>""" <EOL> import errno <EOL> import logging <EOL> import optparse <EOL> import os <EOL> import sys <EOL> from protorpc import descriptor <EOL> from protorpc import generate_python <EOL> from protorpc import protobuf <EOL> from protorpc import registry <EOL> from protorpc import transport <EOL> from protorpc import util <EOL> EXCLUDED_PACKAGES = frozenset ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> commands = { } <EOL> def usage ( ) : <EOL> """<STR_LIT>""" <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def fatal_error ( message ) : <EOL> """<STR_LIT>""" <EOL> sys . stderr . write ( message ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def open_input_file ( filename ) : <EOL> """<STR_LIT>""" <EOL> if filename is None : <EOL> return sys . stdin <EOL> else : <EOL> try : <EOL> return open ( filename , '<STR_LIT:rb>' ) <EOL> except IOError , err : <EOL> fatal_error ( str ( err ) ) <EOL> @ util . positional ( <NUM_LIT:1> ) <EOL> def generate_file_descriptor ( dest_dir , file_descriptor , force_overwrite ) : <EOL> """<STR_LIT>""" <EOL> package = file_descriptor . package <EOL> if not package : <EOL> logging . warn ( '<STR_LIT>' ) <EOL> return <EOL> if package in EXCLUDED_PACKAGES : <EOL> logging . warn ( '<STR_LIT>' % package ) <EOL> return <EOL> package_path = package . split ( '<STR_LIT:.>' ) <EOL> directory = package_path [ : - <NUM_LIT:1> ] <EOL> package_file_name = package_path [ - <NUM_LIT:1> ] <EOL> directory_name = os . path . join ( dest_dir , * directory ) <EOL> output_file_name = os . path . join ( directory_name , <EOL> '<STR_LIT>' % ( package_file_name , ) ) <EOL> try : <EOL> os . makedirs ( directory_name ) <EOL> except OSError , err : <EOL> if err . errno != errno . EEXIST : <EOL> raise <EOL> if not force_overwrite and os . path . exists ( output_file_name ) : <EOL> logging . warn ( '<STR_LIT>' , <EOL> output_file_name , package ) <EOL> return <EOL> output_file = open ( output_file_name , '<STR_LIT:w>' ) <EOL> logging . info ( '<STR_LIT>' , <EOL> file_descriptor . package , output_file_name ) <EOL> generate_python . format_python_file ( file_descriptor , output_file ) <EOL> @ util . positional ( <NUM_LIT:1> ) <EOL> def command ( name , required = ( ) , optional = ( ) ) : <EOL> """<STR_LIT>""" <EOL> def check_params_decorator ( function ) : <EOL> def check_params_wrapper ( options , * args ) : <EOL> if not ( len ( required ) <= len ( args ) <= len ( required ) + len ( optional ) ) : <EOL> sys . stderr . write ( "<STR_LIT>" % name ) <EOL> usage ( ) <EOL> function ( options , * args ) <EOL> check_params_wrapper . required = required <EOL> check_params_wrapper . optional = optional <EOL> commands [ name ] = check_params_wrapper <EOL> return check_params_wrapper <EOL> return check_params_decorator <EOL> @ command ( '<STR_LIT:file>' , optional = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def file_command ( options , input_filename = None , output_filename = None ) : <EOL> """<STR_LIT>""" <EOL> with open_input_file ( input_filename ) as input_file : <EOL> descriptor_content = input_file . read ( ) <EOL> if output_filename : <EOL> output_file = open ( output_filename , '<STR_LIT:w>' ) <EOL> else : <EOL> output_file = sys . stdout <EOL> file_descriptor = protobuf . decode_message ( descriptor . FileDescriptor , <EOL> descriptor_content ) <EOL> generate_python . format_python_file ( file_descriptor , output_file ) <EOL> @ command ( '<STR_LIT>' , optional = [ '<STR_LIT:filename>' ] ) <EOL> def fileset_command ( options , input_filename = None ) : <EOL> """<STR_LIT>""" <EOL> with open_input_file ( input_filename ) as input_file : <EOL> descriptor_content = input_file . read ( ) <EOL> dest_dir = os . path . expanduser ( options . dest_dir ) <EOL> if not os . path . isdir ( dest_dir ) and os . path . exists ( dest_dir ) : <EOL> fatal_error ( "<STR_LIT>" % dest_dir ) <EOL> file_set = protobuf . decode_message ( descriptor . FileSet , <EOL> descriptor_content ) <EOL> for file_descriptor in file_set . files : <EOL> generate_file_descriptor ( dest_dir , file_descriptor = file_descriptor , <EOL> force_overwrite = options . force ) <EOL> @ command ( '<STR_LIT>' , <EOL> required = [ '<STR_LIT:host>' ] , <EOL> optional = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def registry_command ( options , <EOL> host , <EOL> service_name = None , <EOL> registry_path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> dest_dir = os . path . expanduser ( options . dest_dir ) <EOL> url = '<STR_LIT>' % ( host , registry_path ) <EOL> reg = registry . RegistryService . Stub ( transport . HttpTransport ( url ) ) <EOL> if service_name is None : <EOL> service_names = [ service . name for service in reg . services ( ) . services ] <EOL> else : <EOL> service_names = [ service_name ] <EOL> file_set = reg . get_file_set ( names = service_names ) . file_set <EOL> for file_descriptor in file_set . files : <EOL> generate_file_descriptor ( dest_dir , file_descriptor = file_descriptor , <EOL> force_overwrite = options . force ) <EOL> def make_opt_parser ( ) : <EOL> """<STR_LIT>""" <EOL> command_descriptions = [ ] <EOL> for name in sorted ( commands . iterkeys ( ) ) : <EOL> command = commands [ name ] <EOL> params = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % param for param in command . required ] + <EOL> [ '<STR_LIT>' % param for param in command . optional ] ) <EOL> command_descriptions . append ( '<STR_LIT>' % ( name , params ) ) <EOL> command_usage = '<STR_LIT>' % '<STR_LIT>' . join ( command_descriptions ) <EOL> parser = optparse . OptionParser ( usage = command_usage ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> default = os . getcwd ( ) , <EOL> help = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) <EOL> return parser <EOL> parser = make_opt_parser ( ) <EOL> def main ( ) : <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> options , positional = parser . parse_args ( ) <EOL> if not positional : <EOL> usage ( ) <EOL> command_name = positional [ <NUM_LIT:0> ] <EOL> command = commands . get ( command_name ) <EOL> if not command : <EOL> sys . stderr . write ( "<STR_LIT>" % command_name ) <EOL> usage ( ) <EOL> parameters = positional [ <NUM_LIT:1> : ] <EOL> command ( options , * parameters ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
