def parse_commandline(): usage = "%prog [options] <fasta>" version = "0.1" description = \ "%prog reads rooster and writes an ICS calendar file for it." epilog = \ "Copyright (c) 2010 K. Anton Feenstra -- "\ "[email protected] -- www.few.vu.nl/~feenstra" parser = OptionParser(usage=usage, description=description, version="%prog "+version, epilog=epilog) parser.add_option("-r", "--rooster", dest="roosterfile", metavar="FILE", help="rooster file") parser.set_defaults(roosterfile="rooster.txt") parser.add_option("-i", "--ics", dest="icsfile", metavar="FILE", help="ics file") parser.set_defaults(icsfile="rooster.ics") # get the options: (options, args) = parser.parse_args() # if we have an option left, use it as rooster file if we don't have that if len(args) and options.roosterfile==None: options.roosterfile = args.pop(0) # if we still have an option left, use as ics file if we don't have that if len(args) and options.icsfile==None: options.icsfile = args.pop(0) # check if we have an option left (which we shouldn't): if len(args): parser.print_help() print "" print "ERROR: too many argument, or unknown option(s)" sys.exit(-1) # check if we have dist and sel files: if options.roosterfile == None: parser.print_help() print "" print "ERROR: no input file given" sys.exit(-1) # we also want to return our version, for use in other output version=parser.get_version() # clean up (recommended): del(parser) return options, args, version
(options, args) = parser.parse_args() # If we don't have 2 arguments then exit. if len(args) < 1 or len(args) > 2: parser.print_help() parser.error("incorrect number of arguments") # Set log level loglevel = 'WARNING' if options.debug: loglevel = 'DEBUG' elif options.verbose: loglevel = 'INFO' log.info(parser.get_version()) log.setLevel(level=loglevel) log.info('loglevel=%s' % loglevel) ev, qml = setup_event2qml(options=options, database=args[0]) DB_PATH = os.path.abspath(args[0]) assert os.path.exists(DB_PATH), 'provide correct path to db dir.' outdir_arg = args[1] if len(args) > 1 else 'outdir' outdir = os.path.join(os.getcwd(), outdir_arg) if not os.path.exists(outdir): os.mkdir(outdir) else:
class ProgramArguments(object): """ Class for holding the command line options @author: Roy Nielsen """ def __init__(self): """ Initialization routine for our program options Acquiring command line arguments with OptionParser """ self.parser = OptionParser(usage=" %prog [options]\n\n Use \"%prog -h\" to get a list of options", version="%prog " + STONIXVERSION) self.parser.add_option("-m", "--module", dest="mod", help="Module to run.", default="", action="store") self.parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=0, \ help="Print status messages") self.parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="Print debug messages") self.parser.add_option("-X", "--rollback", action="store_true", dest="rollback", default=False, help="Roll back fixes if possible.") self.parser.add_option("-r", "--report", action="store_true", dest="report", default=False, help="Report the status of rules.") self.parser.add_option("-f", "--fix", action="store_true", dest="fix", default=False, help="Set the status of rules according to the config file, or if unavailable - to default settings.") self.parser.add_option("-u", "--update", action="store_true", dest="update", default=False, help="Update this software package, if necessary.") self.parser.add_option("-i", "--install", action="store_true", dest="install", default=False, help="Install") self.parser.add_option("-G", "--gui", action="store_true", dest="gui", default=False, help="Start the GUI.") self.parser.add_option("-c", "--cli", action="store_true", dest="cli", default=False, help="Start the Command Line Interface.") self.parser.add_option("-p", "--printconfigsimple", action="store_true", dest="pcs", default=False, help="Generate a new config file with current options in the simple format.") self.parser.add_option("-P", "--printconfigfull", action="store_true", dest="pcf", default=False, help="Generate a new config file with the current options in the full format (shows all options).") ##### # How verbose to be self.parser.add_option("--message-level", type='choice', \ action='store', dest="message_level", \ choices=['normal', 'quiet', 'verbose', 'debug'], \ default="normal", help=SUPPRESS_HELP, \ metavar="LEVEL") ##### # The Self Update test will look to a development/test environment # to test Self Update rather than testing self update self.parser.add_option("-S", "--selfupdatetest", action="store_true", dest="selfupdatetest", default=False, help="SUPPRESS_HELP") (self.opts, self.args) = self.parser.parse_args() if self.opts.fix and self.opts.report and self.opts.rollback: self.parser.error("Fix, Report and Rollback functions are mutually exclusive.") if self.opts.fix and self.opts.report: self.parser.error("Fix and Report options are mutually exclusive.") if self.opts.fix and self.opts.rollback: self.parser.error("Fix and Rollback options are mutually exclusive.") if self.opts.report and self.opts.rollback: self.parser.error("Report and Rollback options are mutually exclusive.") if self.opts.pcf and (self.opts.fix or self.opts.report or self.opts.rollback or self.opts.pcs or self.opts.update): self.parser.error('The -P --printconfigfull option may not be used with the fix, report, rollback, update or GUI options') if self.opts.pcs and (self.opts.fix or self.opts.report or self.opts.rollback or self.opts.pcf or self.opts.update): self.parser.error('The -p --printconfigsimple option may not be used with the fix, report, rollback, update or GUI options') if self.opts.debug: print "Selected options: " print self.opts print self.args def get_gui(self): """ Return whether or not to start the GUI. @author: Roy Nielsen """ return self.opts.gui def get_install(self): """ Return the install option (True/False) @author: Roy Nielsen """ return self.opts.install def get_update(self): """ Return whether or not to update this software package. @author: Roy Nielsen """ return self.opts.update def get_fix(self): """ Return whether or not to run in fix mode. @author: Roy Nielsen """ return self.opts.fix def get_report(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.report def get_rollback(self): """ Return whether or not to run in rollback mode. @author: Roy Nielsen """ return self.opts.rollback def get_verbose(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.verbose def get_debug(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.debug def get_module(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.mod def get_num_of_args(self): """ Return the number of arguments passed in. @author: Roy Nielsen """ return len(self.args) def get_cli(self): """ Return whether or not to run in Command Line Mode @author: Roy Nielsen """ return self.opts.cli def getPrintConfigFull(self): """ Return the bool for whether or not the CLI has requested the generation of a full config file. @author: Roy Nielsen """ return self.opts.pcf def getPrintConfigSimple(self): """ Return the bool for whether or not the CLI has requested the generation of a simple config file. @author: Roy Nielsen """ return self.opts.pcs def getProgramVersion(self): """ Return the version of stonix. @author: Roy Nielsen """ programVersion = self.parser.get_version() programVersion = programVersion.split(' ') programVersion = programVersion[1] return programVersion def get_msg_lvl(self): """ Return the message level passed in, or the default value @author: Roy Nielsen """ if self.opts.message_level and \ not re.match("^normal$", self.opts.message_level): msg_lvl = self.opts.message_level elif self.opts.verbose: msg_lvl = "verbose" elif self.opts.debug: msg_lvl = "debug" else : msg_lvl = "normal" return msg_lvl def getArgs(self): ''' Get the list of arguments - from the optparse webpage: "the list of arguments to process (default: sys.argv[1:])" @author: Roy Nielsen ''' return sys.argv[1:]
def parse_commandline(): usage = "%prog [options]" version = "0.1" description = \ "%prog reads rooster and writes an ICS calendar file for it." epilog = \ "Copyright (c) 2010 K. Anton Feenstra -- "\ "[email protected] -- www.few.vu.nl/~feenstra" parser = OptionParser(usage=usage, description=description, version="%prog " + version, epilog=epilog) parser.add_option("-r", "--rooster", dest="roosterfile", metavar="FILE", help="rooster file") parser.set_defaults(roosterfile=None) parser.add_option("-o", "--ics", dest="icsfile", metavar="FILE", help="ics file") parser.set_defaults(icsfile=None) parser.add_option("-v", "--verbose", dest="debug", action="store_true", help="Output verbose debugging info (%default)") parser.set_defaults(debug=False) # get the options: (options, args) = parser.parse_args() # if we have an option left, use it as rooster file if we don't have that if len(args) and options.roosterfile == None: options.roosterfile = args.pop(0) # if we still have an option left, use as ics file if we don't have that if len(args) and options.icsfile == None: options.icsfile = args.pop(0) # check if we have an option left (which we shouldn't): if len(args): parser.print_help() print "" print "ERROR: too many argument, or unknown option(s)" print args sys.exit(-1) # check if we have rooster file: if options.roosterfile == None: parser.print_help() print "" print "ERROR: no input file given" sys.exit(-1) # check if we have ics file: if options.icsfile == None: # create one from roosterfile name: filename, extension = os.path.splitext(options.roosterfile) options.icsfile = filename + ".ics" print "No output file given, writing output to:", options.icsfile if os.path.isfile(options.icsfile): print "ERROR: output file exists; specify explicitly to overwrite." sys.exit(-1) # we also want to return our version, for use in other output version = parser.get_version() # clean up (recommended): del (parser) return options, args, version
def main(): global example_dict global options global zmqSock defaultvar_configparser = '''\ [PMGRPCD] topic = some.thing.is.topic-avro bsservers = kafka.some.thing.net:9093 urlscreg = https://schema-registry.some.thing.net:443 calocation = /some/thing/to/schema/registry/ssl/something_root_ca.crt secproto = ssl sslcertloc = /some/thing/to/ssl/certificate/location/something.crt sslkeyloc = /some/thing/to/ssl/key/location/something.key gpbmapfile = /etc/pmacct/telemetry/gpbmapfile.map avscmapfile = /etc/pmacct/telemetry/schema_id_map_file.json mitigation = True debug = False pmgrpcdlogfile = /var/log/pmgrpcd.log serializelogfile = /var/log/pmgrpcd_avro.log workers = 20 cisco = True huawei = True example = True examplepath = /tmp/stexamples jsondatadumpfile = /tmp/stexamples/jsondatadumpfile.json rawdatadumpfile = /tmp/stexamples/rawdatadumpfile.json zmq = False zmqipport = tcp://127.0.0.1:50000 kafkaavro = True onlyopenconfig = False ''' usage_str = "%prog [options]" version_str = "%prog " + SCRIPTVERSION parser = OptionParser(usage=usage_str, version=version_str) config = configparser.ConfigParser() if os.path.isfile(CONFIGFILE): config.read(CONFIGFILE) if not 'PMGRPCD' in config.sections(): #add Section GRPCD to configfile print("Add Section PMGRPCD to the Configfile %s" % CONFIGFILE) with open(CONFIGFILE, 'a') as configf: configf.write(defaultvar_configparser) config.read(CONFIGFILE) else: with open(CONFIGFILE, 'w') as configf: configf.write(defaultvar_configparser) config.read(CONFIGFILE) parser.add_option("-T", "--topic", default=config.get("PMGRPCD", 'topic'), dest="topic", help="the json data are serialized to this topic") parser.add_option("-B", "--bsservers", default=config.get("PMGRPCD", 'bsservers'), dest="bsservers", help="bootstrap servers url with port to reach kafka") parser.add_option("-S", "--secproto", default=config.get("PMGRPCD", 'secproto'), dest="secproto", help="security protocol (is normaly ssl)") parser.add_option("-O", "--sslcertloc", default=config.get("PMGRPCD", 'sslcertloc'), dest="sslcertloc", help="path/file to ssl certification location") parser.add_option("-K", "--sslkeyloc", default=config.get("PMGRPCD", 'sslkeyloc'), dest="sslkeyloc", help="path/file to ssl key location") parser.add_option("-U", "--urlscreg", default=config.get("PMGRPCD", 'urlscreg'), dest="urlscreg", help="the url to the schema-registry") parser.add_option( "-L", "--calocation", default=config.get("PMGRPCD", 'calocation'), dest="calocation", help="the ca_location used to connect to schema-registry") parser.add_option( "-G", "--gpbmapfile", default=config.get("PMGRPCD", 'gpbmapfile'), dest="gpbmapfile", help="change path/name of gpbmapfile [default: %default]") parser.add_option("-M", "--avscmapfile", default=config.get("PMGRPCD", 'avscmapfile'), dest="avscmapfile", help="path/name to the avscmapfile") parser.add_option( "-m", "--mitigation", action="store_true", default=config.getboolean("PMGRPCD", 'mitigation'), dest="mitigation", help= "enable plugin mitigation mod_result_dict from python module mitigation.py" ) parser.add_option("-d", "--debug", action="store_true", default=config.getboolean("PMGRPCD", 'debug'), dest="debug", help="enable debug messages on the logfile") parser.add_option( "-l", "--pmgrpcdlogfile", default=config.get("PMGRPCD", 'pmgrpcdlogfile'), dest='pmgrpcdlogfile', help= "pmgrpcdlogfile the logfile on the collector face with path/name [default: %default]" ) parser.add_option( "-a", "--serializelogfile", default=config.get("PMGRPCD", 'serializelogfile'), dest="serializelogfile", help= "serializelogfile with path/name for kafka avro and zmq messages [default: %default]" ) parser.add_option( "-w", "--workers", action="store", type='int', default=config.get("PMGRPCD", 'workers'), dest="workers", help="change the nr of paralell working processes [default: %default]") parser.add_option( "-C", "--cisco", action="store_true", default=config.getboolean("PMGRPCD", 'cisco'), dest="cisco", help="enable the grpc messages comming from Cisco [default: %default]") parser.add_option( "-H", "--huawei", action="store_true", default=config.getboolean("PMGRPCD", 'huawei'), dest="huawei", help="enable the grpc messages comming from Huawei [default: %default]" ) parser.add_option( "-e", "--example", action="store_true", default=config.getboolean("PMGRPCD", 'example'), dest="example", help="Enable writing Example Json-Data-Files [default: %default]") parser.add_option( "-E", "--examplepath", default=config.get("PMGRPCD", 'examplepath'), dest="examplepath", help="dump a json example of each proto/path to this examplepath") parser.add_option( "-j", "--jsondatadumpfile", dest="jsondatadumpfile", help="writing the output to the jsondatadumpfile path/name") parser.add_option( "-r", "--rawdatafile", dest="rawdatafile", help= "writing the raw data from the routers to the rowdatafile path/name") parser.add_option("-z", "--zmq", action="store_true", default=config.getboolean("PMGRPCD", 'zmq'), dest="zmq", help="enable forwarding to ZMQ [default: %default]") parser.add_option( "-p", "--zmqipport", default=config.get("PMGRPCD", 'zmqipport'), dest="zmqipport", help="define proto://ip:port of zmq socket bind [default: %default]") parser.add_option( "-k", "--kafkaavro", action="store_true", default=config.getboolean("PMGRPCD", 'kafkaavro'), dest="kafkaavro", help= "enable forwarding to Kafka kafkaavro (with schema-registry) [default: %default]" ) parser.add_option("-o", "--onlyopenconfig", action="store_true", default=config.getboolean("PMGRPCD", 'onlyopenconfig'), dest="onlyopenconfig", help="only accept pakets of openconfig") parser.add_option("-i", "--ip", dest="ip", help="only accept pakets of this single ip") parser.add_option( "-A", "--avscid", dest="avscid", help= "this is to serialize manually with avscid and jsondatafile (for development)" ) parser.add_option( "-J", "--jsondatafile", dest="jsondatafile", help= "this is to serialize manually with avscid and jsondatafile (for development)" ) parser.add_option( "-c", "--console", action="store_true", dest="console", help= "this is to display all log-messages also on console (for development)" ) parser.add_option("-v", action="store_true", dest="version", help="print version of this script") (options, args) = parser.parse_args() init_pmgrpcdlog() init_serializelog() if options.version: print(parser.get_version()) raise SystemExit pmgrpcdlog.info("startoptions of this script: %s" % str(options)) #Test-Statements Logging #----------------------- #pmgrpcdlog.debug('debug message') #pmgrpcdlog.info('info message') #pmgrpcdlog.warning('warn message') #pmgrpcdlog.error('error message') #pmgrpcdlog.critical('critical message') #serializelog.debug('debug message') #serializelog.info('info message') #serializelog.warning('warn message') #serializelog.error('error message') #serializelog.critical('critical message') if options.zmq: zmqContext = zmq.Context() zmqSock = zmqContext.socket(zmq.PUSH) zmqSock.bind(options.zmqipport) pmgrpcdlog.info("enable listening to SIGNAL USR1 with Sinalhandler") signal.signal(signal.SIGUSR1, signalhandler) pmgrpcdlog.info("enable listening to SIGNAL USR2 with Sinalhandler") signal.signal(signal.SIGUSR2, signalhandler) if (options.avscid and options.jsondatafile): pmgrpcdlog.info( "manually serialize with avscid (%s) and jsondatafile (%s)" % (options.avscid, options.jsondatafile)) avscid = int(options.avscid) avsc = getavroschema(avscid) avroinstance = getavro_schid_instance(avscid) with open(options.jsondatafile, 'r') as jsondatahandler: jsondata = json.load(jsondatahandler) #serialize(json.dumps(avsc), jsondata, topic, avscid, avroinstance) serialize(jsondata, options.topic, avscid, avroinstance) elif (options.avscid or options.jsondatafile): pmgrpcdlog.info( "manually serialize need both options avscid and jsondatafile") parser.print_help() else: pmgrpcdlog.info('pmgrpsd.py is started at %s' % (str(datetime.now()))) serve()
class ProgramArguments(object): """ Class for holding the command line options @author: Roy Nielsen """ def __init__(self): """ Initialization routine for our program options Acquiring command line arguments with OptionParser """ self.parser = OptionParser( usage= " %prog [options]\n\n Use \"%prog -h\" to get a list of options", version="%prog " + STONIXVERSION) self.parser.add_option("-m", "--module", dest="mod", help="Module to run.", default="", action="store") self.parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=0, \ help="Print status messages") self.parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="Print debug messages") self.parser.add_option("-X", "--rollback", action="store_true", dest="rollback", default=False, help="Roll back fixes if possible.") self.parser.add_option("-r", "--report", action="store_true", dest="report", default=False, help="Report the status of rules.") self.parser.add_option( "-f", "--fix", action="store_true", dest="fix", default=False, help= "Set the status of rules according to the config file, or if unavailable - to default settings." ) self.parser.add_option( "-u", "--update", action="store_true", dest="update", default=False, help="Update this software package, if necessary.") self.parser.add_option("-i", "--install", action="store_true", dest="install", default=False, help="Install") self.parser.add_option("-G", "--gui", action="store_true", dest="gui", default=False, help="Start the GUI.") self.parser.add_option("-c", "--cli", action="store_true", dest="cli", default=False, help="Start the Command Line Interface.") self.parser.add_option( "-p", "--printconfigsimple", action="store_true", dest="pcs", default=False, help= "Generate a new config file with current options in the simple format." ) self.parser.add_option( "-P", "--printconfigfull", action="store_true", dest="pcf", default=False, help= "Generate a new config file with the current options in the full format (shows all options)." ) ##### # How verbose to be self.parser.add_option("--message-level", type='choice', \ action='store', dest="message_level", \ choices=['normal', 'quiet', 'verbose', 'debug'], \ default="normal", help=SUPPRESS_HELP, \ metavar="LEVEL") ##### # The Self Update test will look to a development/test environment # to test Self Update rather than testing self update self.parser.add_option("-S", "--selfupdatetest", action="store_true", dest="selfupdatetest", default=False, help="SUPPRESS_HELP") (self.opts, self.args) = self.parser.parse_args() if self.opts.fix and self.opts.report and self.opts.rollback: self.parser.error( "Fix, Report and Rollback functions are mutually exclusive.") if self.opts.fix and self.opts.report: self.parser.error("Fix and Report options are mutually exclusive.") if self.opts.fix and self.opts.rollback: self.parser.error( "Fix and Rollback options are mutually exclusive.") if self.opts.report and self.opts.rollback: self.parser.error( "Report and Rollback options are mutually exclusive.") if self.opts.pcf and (self.opts.fix or self.opts.report or self.opts.rollback or self.opts.pcs or self.opts.update): self.parser.error( 'The -P --printconfigfull option may not be used with the fix, report, rollback, update or GUI options' ) if self.opts.pcs and (self.opts.fix or self.opts.report or self.opts.rollback or self.opts.pcf or self.opts.update): self.parser.error( 'The -p --printconfigsimple option may not be used with the fix, report, rollback, update or GUI options' ) if self.opts.debug: print "Selected options: " print self.opts print self.args def get_gui(self): """ Return whether or not to start the GUI. @author: Roy Nielsen """ return self.opts.gui def get_install(self): """ Return the install option (True/False) @author: Roy Nielsen """ return self.opts.install def get_update(self): """ Return whether or not to update this software package. @author: Roy Nielsen """ return self.opts.update def get_fix(self): """ Return whether or not to run in fix mode. @author: Roy Nielsen """ return self.opts.fix def get_report(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.report def get_rollback(self): """ Return whether or not to run in rollback mode. @author: Roy Nielsen """ return self.opts.rollback def get_verbose(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.verbose def get_debug(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.debug def get_module(self): """ Return the filename passed in, or the default value @author: Roy Nielsen """ return self.opts.mod def get_num_of_args(self): """ Return the number of arguments passed in. @author: Roy Nielsen """ return len(self.args) def get_cli(self): """ Return whether or not to run in Command Line Mode @author: Roy Nielsen """ return self.opts.cli def getPrintConfigFull(self): """ Return the bool for whether or not the CLI has requested the generation of a full config file. @author: Roy Nielsen """ return self.opts.pcf def getPrintConfigSimple(self): """ Return the bool for whether or not the CLI has requested the generation of a simple config file. @author: Roy Nielsen """ return self.opts.pcs def getProgramVersion(self): """ Return the version of stonix. @author: Roy Nielsen """ programVersion = self.parser.get_version() programVersion = programVersion.split(' ') programVersion = programVersion[1] return programVersion def get_msg_lvl(self): """ Return the message level passed in, or the default value @author: Roy Nielsen """ if self.opts.message_level and \ not re.match("^normal$", self.opts.message_level): msg_lvl = self.opts.message_level elif self.opts.verbose: msg_lvl = "verbose" elif self.opts.debug: msg_lvl = "debug" else: msg_lvl = "normal" return msg_lvl def getArgs(self): ''' Get the list of arguments - from the optparse webpage: "the list of arguments to process (default: sys.argv[1:])" @author: Roy Nielsen ''' return sys.argv[1:]
(options, args) = parser.parse_args() filt = options.filtQ sort_s = options.sortS pred = options.pred # prediction of the number of repeat units, when reference file contains the 5'- and 3'-flanking sequences seq = options.seq # input file contains a genomic DNA sequence swift = options.swift # swift mode, default: off outdir = options.outdir # output directory output = options.output # basename of output files detail = options.detail # detail mode q_fasta = options.q_fasta # catalogue file for repeat units cutoff = options.cutoff # threshold for the presence of each unit, based on the estimated read depth genome_size = options.genome_size # if Mtb genome size, 4500000 setlength = 50 * genome_size # base input for swift mode ver = parser.get_version() print("version = " + ver) ## Check input fastq files narg = len(args) print("no. args = " + str(narg)) if narg == 0: print( "usage: python RepUnitTyping.py [options] FASTQ_1/FASTA FASTQ_2(optional)" ) print(" -h, --help for further help message") print(" --version for this program's version") sys.exit() elif narg == 1: input1 = args[0] # Input fastq file 1 if not os.path.isfile(input1): print("ERROR: Invalid FASTQ_1/FASTA file!")
default=config['utm5']['hours']) parser.add_option('-c', '--ignore-config', action='store_true', dest='ignore_cfg', help='игнорировать сохраненные логин и пароль', default=False) opt, args = parser.parse_args() if opt.debug: logging.basicConfig(level=logging.DEBUG, format="%(funcName)s: %(message)s") elif opt.verbose: logging.basicConfig(level=logging.INFO, format="%(message)s") else: logging.basicConfig(level=logging.WARNING, format="%(message)s") logging.debug('%s %s\n\n== Args ==\n%s\n\n== Opts ==\n%s\n\n' % ( os.path.basename(sys.argv[0]), parser.get_version(), args, "\n".join([': '.join(map(str, i)) for i in opt.__dict__.items()]))) if opt.login is None and len(args) == 1: opt.login = args[0] if opt.ignore_cfg == False and opt.login is None: opt.login = config['utm5']['login'] opt.passwd = config['utm5']['passwd'] if opt.login is None: opt.login = input('Login: ') if opt.passwd is None: opt.passwd = getpass()
class ApplicationStarter(): ''' Handles all command line options/arguments and start the oand server. ''' parser = None def __init__(self): ''' Used to control the command line options, and the execution of the script. First function called when using the script. ''' self._parser = OptionParser( usage = self._get_usage(), version = "oand " + __version__, add_help_option = True, option_list = self.get_options(), formatter = IndentedHelpFormatter(max_help_position=40, width=80)) (options, args) = self._parser.parse_args() print self._parser.get_version() if len(args) != 1: self._parser.print_help() else: self._handle_positional_argument(options, args[0]) def get_options(self): return [ make_option( "-v", "--verbose", action="store_const", const=2, dest="verbose", help="Show more output."), make_option( "-q", "--quiet", action="store_const", const=0, dest="verbose", help="Show no output."), make_option( "-a", "--server-name", metavar="NAME", help="The server name."), make_option( "-s", "--server-domain-name", metavar="NAME", help="The server domain name or ip."), make_option( "-d", "--server-port", metavar="PORT", type="int", help="The server port number."), make_option( "-z", "--bff-name", metavar="NAME", help="The bff server name."), make_option( "-x", "--bff-domain-name", metavar="NAME", help="The bff server domain name or ip."), make_option( "-c", "--bff-port", metavar="PORT", type="int", help="The bff server port number."), make_option( "--defaults-extra-file", metavar="FILE", dest="config", default="oand.cfg", help="The name of the config file."), make_option( "--pid-file", metavar="FILE", help="The pid-file path."), make_option( "--log-file", metavar="FILE", help="The log-file path.") ] def handle_network(self, action): config = {} app = {} daemon = {} for x in range(10): x = int(x) print "%s daemon %d" % (action, x) if x == 0: config[x] = Config( "server-" + str(x), "localhost", str(4000 + x) ) else: config[x] = Config( "server-" + str(x), "localhost", str(4000 + x), "server-" + str(x-1), "localhost", str(4000 + x - 1) ) config[x].pid_file = "/tmp/oand-%d.pid" % x config[x].log_file = "../log/oand-network.log" app[x] = OANApplication.create_twisted_circular_node(config[x]) daemon[x] = OANDaemon(app[x]) if (action == "start"): daemon[x].start() time.sleep(1) elif (action == "stop"): daemon[x].stop() def _handle_positional_argument(self, options, argument): ''' Handle the positional arguments from the commandline. ''' if argument == 'start-network': self.handle_network("start") return elif argument == 'stop-network': self.handle_network("stop") return elif argument == 'restart-network': self.handle_network("stop") self.handle_network("start") return config = Config() config.set_from_file(options.config) config.set_from_cmd_line(options) config.print_options() app = OANApplication.create_twisted_circular_node(config) daemon = OANDaemon(app) if argument == 'start': daemon.start() elif argument == 'stop': daemon.stop() elif argument == 'restart': daemon.restart() elif argument == 'native': app.run_server() else: self._parser.print_help() def _get_usage(self): ''' Display information about how to start the daemon-server. ''' return """usage: %prog [-vqf] {start|stop|restart|native|status}
def __init__(self): """ Initialization routine for our program options Acquiring command line arguments with OptionParser """ self.version = "" self.environ = Environment() ##### # Define the default log directory, and make sure it # it exists as a directory. self.resources = getResourcesDir() defaultLogDir = os.path.join(self.resources, "logs") """ if not os.path.isdir(defaultLogDir): if os.path.exists(defaultLogDir): os.unlink(defaultLogDir) os.mkdir(defaultLogDir) """ ##### # Collect the passed in parameters, or define them as default. parser = OptionParser(usage=" %prog [options]\n\n" + \ "Only options are to change the logging level of the application.", \ version="%prog 0.7.0.25") ##### # Logging level lp.VERBOSE parser.add_option("-v", "--verbose", action="store_true", \ dest="verbose", default=False, \ help="Print status messages") ##### # Logging level lp.DEBUG parser.add_option("-d", "--debug", action="store_true", dest="debug", \ default=False, help="Print debug messages") ##### # Where to put the logs. parser.add_option("-l", "--log-path", action="store", dest="logPath", \ default="/tmp/", help="Path to put the logs") ##### # General proxy - set all proxy values to this value parser.add_option("-p", "--proxy", action="store", dest="proxy", \ default="", help="Sets HTTP_PROXY, HTTPS_PROXY and FTP_PROXY to the value passed in.") ##### # To set the HTTP_PROXY setting parser.add_option("--http-proxy", action="store", dest="httpProxy", \ default="", help="Sets the http_proxy.") ##### # To set the HTTPS_PROXY setting parser.add_option("--https-proxy", action="store", dest="httpsProxy", \ default="", help="Sets the https_proxy.") ##### # To set the FTP_PROXY setting parser.add_option("--ftp-proxy", action="store", dest="ftpProxy", \ default="", help="Sets the ftp_proxy.") ##### # To set the RSYNC_PROXY setting parser.add_option("--rsync-proxy", action="store", dest="rsyncProxy", \ default="", help="Sets the rsync_proxy.") ##### # To set the NO_PROXY setting parser.add_option("--no-proxy", action="store", dest="noProxy", \ default="", help="Sets the no_proxy.") ##### # Where to put the logs. parser.add_option("--repo-root", action="store", dest="repoRoot", \ default="/opt/tools/src/boxcutter", help="Path to put the logs") (self.options, self.args) = parser.parse_args() programVersion = parser.get_version() programVersion = programVersion.split(' ') self.version = programVersion[1] if self.getDebugMode(): loglevel = lp.DEBUG elif self.getVerboseMode(): loglevel = lp.VERBOSE else: loglevel = lp.WARNING # self.logger = CyLogger(level=loglevel) self.logger = CyLogger(debug_mode=lp.DEBUG) ##### # Instanciate a configuration object and initialize its # variables. This object is what will be passed to all # of the objects in this program for shared knowledge, # much like the Environment in Stonix. self.conf = Conf() #programLogDir = self.get_log_path() self.logger.initializeLogs(logdir=self.options.logPath) self.conf.setLogger(self.logger) self.conf.setVersion(self.getVersion()) self.conf.setEnviron(self.getEnviron()) self.conf.setHttpProxy(self.getHttpProxy()) self.conf.setHttpsProxy(self.getHttpsProxy()) self.conf.setFtpProxy(self.getFtpProxy()) self.conf.setRsyncProxy(self.getRsyncProxy()) self.conf.setNoProxy(self.getNoProxy()) if self.options.proxy and isinstance(self.options.proxy, basestring): ##### # Need better input validation... self.conf.setHttpProxy(self.getProxy()) self.conf.setHttpsProxy(self.getProxy()) self.conf.setFtpProxy(self.getProxy()) self.conf.setProxy(self.getProxy()) self.conf.setRepoRoot(self.getRepoRoot())