def read_cmd_options(): from optparse import OptionParser, Values tornado.options.define('distribution', default=None, help='distribution of the time between events') tornado.options.define('limit', default=0, type=int, help='number of events to generate') tornado.options.define('eventlog', default=False, help='dump event log', type=bool) tornado.options.define('timestamp', default=False, help='add an X-Float-Timestamp header to events', type=bool) tornado.options.define('bayeux', default=False, help='use the Bayeux protocol for publishing', type=bool) tornado.options.define('timescale', default=1.0, help='accelerate time by this factor', type=float) tornado.options.define('delay', default=2.0, help='initial delay, in seconds', type=float) remaining = tornado.options.parse_command_line() options = Values() if len(remaining) >= 2: options.filename = remaining[0] options.server_urls = remaining[1:] else: OptionParser().error('At least one file name and one ' 'server URL required') return options
def minify_script(patches=None, keep_report=True, show_diff=False): """minifies createstubs.py Args: patches ([PathLike], optional): List of paths to patches to apply. Defaults to None. keep_report (bool, optional): Keeps single report line in createstubs Defautls to True. show_diff (bool, optional): Print diff from edits. Defaults to False. Returns: str: minified source text """ patches = patches or [] edits = [ ("comment", "print"), ("comment", "import logging"), ("comment", "self._log ="), ("comment", "self._log.debug"), ("comment", "self._log.warning"), ("comment", "self._log.info"), ("comment", "self._log.error"), ] if keep_report: report = ('rprint', ('self._log.info("Stub module: {:<20} to file:' ' {:<55} mem:{:>5}".' 'format(module_name, file_name, m1))')) edits.insert(0, report) minopts = Values({'tabs': False}) with SCRIPT.open('r') as f: content = f.read() for path in patches: path = Path(path) content = apply_patch(content, path.read_text()) content = edit_lines(content, edits, show_diff=show_diff) tokens = token_utils.listified_tokenizer(content) source = minification.minify(tokens, minopts) return source
def setUp(self): # patch to avoid touching sys and socket self.socket_patch = patch('hl7.client.socket.socket') self.mock_socket = self.socket_patch.start() self.mock_socket().recv.return_value = 'thanks' self.stdout_patch = patch('hl7.client.stdout') self.mock_stdout = self.stdout_patch.start() self.stdin_patch = patch('hl7.client.stdin') self.mock_stdin = self.stdin_patch.start() self.stderr_patch = patch('hl7.client.stderr') self.mock_stderr = self.stderr_patch.start() # we need a temporary directory self.dir = mkdtemp() self.write(SB + 'foobar' + EB + CR) self.option_values = Values({ 'port': 6661, 'filename': os.path.join(self.dir, 'test.hl7'), 'verbose': True, 'loose': False, 'version': False, }) self.options_patch = patch('hl7.client.OptionParser') option_parser = self.options_patch.start() self.mock_options = Mock() option_parser.return_value = self.mock_options self.mock_options.parse_args.return_value = (self.option_values, ['localhost'])
def _register_attributes(self, app, args): # FIXME: there is no better lib function than this snippet schema_file = app.get_share_file('schema') if os.path.exists(schema_file): self.log('Registering schema %s' % schema_file) lo, pos = self._get_ldap_connection(args) with self._get_password_file(args) as password_file: create_recursive_container('cn=ldapschema,cn=univention,%s' % ucr_get('ldap/base'), lo, pos) schema_obj = UniventionLDAPSchema(ucr_instance()) userdn = self._get_userdn(args) udm_passthrough_options = ['--binddn', userdn, '--bindpwdfile', password_file] opts = Values() opts.packagename = 'appcenter-app-%s' % app.id opts.packageversion = app.version opts.ucsversionstart = None opts.ucsversionend = None os.environ['UNIVENTION_APP_IDENTIFIER'] = app.id try: schema_obj.register(schema_file, opts, udm_passthrough_options) except SystemExit as exc: if exc.code == 4: self.warn('A newer version of %s has already been registered. Skipping...' % schema_file) else: raise RegisterSchemaFailed(exc.code) else: if not schema_obj.wait_for_activation(): raise RegisterSchemaFileFailed(schema_file) finally: if 'UNIVENTION_APP_IDENTIFIER' in os.environ: del os.environ['UNIVENTION_APP_IDENTIFIER'] # and this is what should be there after one line of lib.register_schema(schema_file) app = app.get_app_cache_obj().copy(locale='en').find_by_component_id(app.component_id) attributes, __ = get_extended_attributes(app) if attributes: for i, attribute in enumerate(attributes): self.log('Registering attribute %s' % attribute.name) create_extended_attribute(attribute, app, i + 1, lo, pos)
def setUp(self): # patch to avoid touching sys and socket self.socket_patch = patch("hl7.client.socket.socket") self.mock_socket = self.socket_patch.start() self.mock_socket().recv.return_value = "thanks" self.stdout_patch = patch("hl7.client.stdout") self.mock_stdout = self.stdout_patch.start() self.stdin_patch = patch("hl7.client.stdin") self.mock_stdin = self.stdin_patch.start() self.stderr_patch = patch("hl7.client.stderr") self.mock_stderr = self.stderr_patch.start() # we need a temporary directory self.dir = mkdtemp() self.write(SB + b"foobar" + EB + CR) self.option_values = Values({ "port": 6661, "filename": os.path.join(self.dir, "test.hl7"), "verbose": True, "loose": False, "version": False, }) self.options_patch = patch("hl7.client.OptionParser") option_parser = self.options_patch.start() self.mock_options = Mock() option_parser.return_value = self.mock_options self.mock_options.parse_args.return_value = (self.option_values, ["localhost"])
def get_parsed_args(): parser = OptionParser() parser.add_option('-d', '--dd_url', action='store', default=None, dest='dd_url') parser.add_option('-c', '--clean', action='store_true', default=False, dest='clean') parser.add_option('-u', '--use-local-forwarder', action='store_true', default=False, dest='use_forwarder') parser.add_option('-n', '--disable-dd', action='store_true', default=False, dest="disable_dd") parser.add_option('-v', '--verbose', action='store_true', default=False, dest='verbose', help='Print out stacktraces for errors in checks') try: options, args = parser.parse_args() except SystemExit: # Ignore parse errors options, args = Values({'dd_url': None, 'clean': False, 'disable_dd':False, 'use_forwarder': False}), [] return options, args
def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) config = get_config(parse_args=False) # Setup the correct options so the agent will use the forwarder opts, args = Values({ 'clean': False, 'disabled_dd': False }), [] agentConfig = get_config(parse_args=False, options=opts) self.restart_interval = \ int(agentConfig.get('autorestart_interval', RESTART_INTERVAL)) log.info("Autorestarting the collector ever %s seconds" % self.restart_interval) # Keep a list of running processes so we can start/end as needed. # Processes will start started in order and stopped in reverse order. self.procs = { 'forwarder': DDForwarder(config), 'collector': DDAgent(agentConfig), 'dogstatsd': DogstatsdProcess(config), 'pup': PupProcess(config), }
def run(conf, analyser, plugin=None, format='osmose'): from optparse import Values options = Values({ 'verbose': False, 'plugin': plugin and [plugin] or [], 'change': False, }) LOG = StringIO() logger = OsmoseLog.logger(LOG, True) osmosis_manager = modules.OsmOsisManager.OsmOsisManager( conf, conf.db_host, conf.db_user, conf.db_password, conf.db_base, conf.db_schema or conf.country, conf.db_persistent, logger) analyser_conf = analyser_config(conf, options, osmosis_manager) output = StringIO() analyser_conf.error_file = issues_file_from_fromat(output, format) with analyser(analyser_conf, logger.sub()) as analyser_obj: analyser_obj.analyser() return output.getvalue()
def get_platform_from_task_def(flow: str, task: str) -> Dict[str, Any]: """Return the platform dictionary for a particular task. Uses the flow definition - designed to be used with tasks with unsubmitted jobs. Args: flow: The name of the Cylc flow to be queried. task: The name of the task to be queried. Returns: Platform Dictionary. """ _, _, flow_file = parse_id(flow, constraint='workflows', src=True) config = WorkflowConfig(flow, flow_file, Values()) # Get entire task spec to allow Cylc 7 platform from host guessing. task_spec = config.pcfg.get(['runtime', task]) platform = get_platform(task_spec) if platform is None: raise PlatformLookupError( 'Platform lookup failed; platform is a subshell to be evaluated: ' f' Task: {task}, platform: {task_spec["platform"]}.') return platform
def test_parse_file_no_errors(): target_path = os.path.join(tempfile.gettempdir(), str(uuid4())) os.makedirs(target_path) cwd = os.getcwd() os.chdir(target_path) try: rp = ReportSVGBadge( Values({ 'output_file': None, 'image': '123.svg', 'format': 'svg' })) with open('lala.py', 'w') as f: f.write('print(123)\n') f.write('print(123)\n') with open('lulu.py', 'w') as f: f.write('print(123)\n') f.write('print(123)\n') rp.beginning('lala.py') rp.finished('lala.py') rp.beginning('lulu.py') rp.finished('lulu.py') rp.stop() with open("123.svg") as f: el = xmltodict.parse(f.read()) assert el['svg']['g'][1]['text'][3]['#text'] == '100%' finally: os.chdir(cwd) rmtree(target_path)
def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) config = get_config(parse_args=False) # Setup the correct options so the agent will use the forwarder opts, args = Values({ 'autorestart': False, 'dd_url': None, 'clean': False, 'use_forwarder': True, 'disabled_dd': False }), [] agentConfig = get_config(parse_args=False, options=opts) self.hostname = get_hostname(agentConfig) # Watchdog for Windows self._collector_heartbeat, self._collector_send_heartbeat = multiprocessing.Pipe( False) self._collector_failed_heartbeats = 0 self._max_failed_heartbeats = \ MAX_FAILED_HEARTBEATS * agentConfig['check_freq'] / SERVICE_SLEEP_INTERVAL # Keep a list of running processes so we can start/end as needed. # Processes will start started in order and stopped in reverse order. self.procs = { 'forwarder': DDForwarder(config, self.hostname), 'collector': DDAgent(agentConfig, self.hostname, heartbeat=self._collector_send_heartbeat), 'dogstatsd': DogstatsdProcess(config, self.hostname), 'jmxfetch': JMXFetchProcess(config, self.hostname), }
def test_main(self, mock_get_options, mock_clean, mock_chdir, mock_git_clone, mock_set_origin, mock_git_checkout, mock_build_modules, mock_build_manifest): parsed_options = { 'working_dir': '/tmp/working', 'output_dir': '/tmp/output', 'path': None } options = Values(defaults=parsed_options) mock_get_options.return_value = options # test builder.main() # validation mock_get_options.assert_called_with() mock_clean.assert_called_with(options) mock_chdir.assert_any_call(options.working_dir) mock_git_clone.assert_called_with(options) mock_set_origin.assert_called_with(options) mock_git_checkout.assert_called_with(options) mock_build_modules.assert_called_with(options) mock_build_manifest.assert_called_with(options) self.assertEqual(mock_clean.call_count, 2)
def test_build_modules(self, mock_shell, mock_find, mock_publish): parsed_options = { 'output_dir': '/tmp/output', } options = Values(defaults=parsed_options) module_paths = ['path_1', 'path_2'] mock_find.return_value = module_paths # test builder.build_puppet_modules(options) # validation command = 'puppet module build %s' mock_find.assert_called_with() mock_shell.assert_any_call(command % module_paths[0]) mock_shell.assert_any_call(command % module_paths[1]) mock_publish.assert_any_call('%s/pkg' % module_paths[0], parsed_options['output_dir']) mock_publish.assert_any_call('%s/pkg' % module_paths[1], parsed_options['output_dir'])
def get_parsed_args(): parser = OptionParser() parser.add_option('-A', '--autorestart', action='store_true', default=False, dest='autorestart') parser.add_option('-d', '--dd_url', action='store', default=None, dest='dd_url') parser.add_option('-u', '--use-local-forwarder', action='store_true', default=False, dest='use_forwarder') parser.add_option('-v', '--verbose', action='store_true', default=False, dest='verbose', help='Print out stacktraces for errors in checks') parser.add_option('-p', '--profile', action='store_true', default=False, dest='profile', help='Enable Developer Mode') try: options, args = parser.parse_args() except SystemExit: # Ignore parse errors options, args = Values({'autorestart': False, 'dd_url': None, 'use_forwarder': False, 'verbose': False, 'profile': False}), [] return options, args
def runCAS(self,options,cfg,rebuild): if not "run" in self.available.split(';'): return # ~~> prepare options as if run from command line specs = Values() setattr(specs,'configName',options.configName) setattr(specs,'configFile', options.configFile) setattr(specs,'sortieFile',True) setattr(specs,'tmpdirectory',True) setattr(specs,'rootDir', options.rootDir) setattr(specs,'version', options.version) setattr(specs,'wDir', options.wDir) setattr(specs,'compileonly', False) if options.hosts != '': setattr(specs,'hosts', options.hosts) else: setattr(specs,'hosts', gethostname().split('.')[0]) setattr(specs,'split', options.split) setattr(specs,'run', options.run) setattr(specs,'merge', options.merge) if options.ncsize != '': self.active["ncsize"] = options.ncsize setattr(specs,'ncsize', self.active["ncsize"]) setattr(specs,'nctile', '1') # default but should not be used for validation setattr(specs,'bypass',self.bypass) # ~~> check on sorties and run casFile = path.join(self.active['path'],self.active["target"]) sacFile = path.join(self.active['safe'],self.active["target"]) sortieFiles = getLatestSortieFiles(sacFile) outputs = self.dids[self.active["xref"]][self.active['cfg']]['output'] if matchSafe(casFile,self.active["target"]+'_*??h??min??s*.sortie',self.active['safe'],rebuild): print ' +> running cas file: ' + self.active["target"] for k in outputs: matchSafe('',path.basename(k[1][0]),self.active['safe'],2) try: sortieFiles = runCAS(self.active['cfg'],cfg,self.active["code"],sacFile,specs) except Exception as e: raise Exception([filterMessage({'name':'ACTION::runCAS'},e,self.bypass)]) # only one item here if sortieFiles != []: self.updateCFG({ 'sortie': sortieFiles })
def __init__(self, values=None): if values: self.defaults.update(values.__dict__) # Ensure that context has default values for all unset variables super(self.__class__, self).__init__(vars(Values(self.defaults))) if self.masterDataDirectory: self.master_datadir = self.masterDataDirectory else: self.master_datadir = gp.get_masterdatadir() self.master_port = self.get_master_port() if self.local_dump_prefix: self.dump_prefix = self.local_dump_prefix + "_" else: self.dump_prefix = "" if not self.include_dump_tables: self.include_dump_tables = [] if not self.exclude_dump_tables: self.exclude_dump_tables = [] if not self.output_options: self.output_options = [] if not self.dump_schema: self.dump_schema = [] if not self.exclude_dump_schema: self.exclude_dump_schema = [] self.gparray = GpArray.initFromCatalog(dbconn.DbURL(dbname="template1", port=self.master_port), utility=True) self.use_old_filename_format = False # Use new filename format by default self.content_map = self.setup_content_map()
def main(args): parser = OptionParser(usage='%prog [ options ... ] URI [ FILES ]', description='Analyze repository modifications', version=VERSION) parser.disable_interspersed_args() parser.add_option('-g', '--debug', dest='debug', action="store_true", default=False, help="Run in debug mode") parser.add_option('-c', '--config-file', dest='config_file', metavar='FILE', help="Use a custom configuration file") parser.add_option('-r', '--revision', dest='revision', metavar='REV', help='Revision to analyze (HEAD)') parser.add_option('-f', '--fast', dest='fast', action="store_true", default=False, help="Run faster but moves and copies are not detected") parser.add_option('-o', '--output', dest='output', default='text', help='Output type [text|db|xml|csv] (%default)') add_outputs_options(parser) # Save default values and pass an emtpy Values object to # parser_args, so that default values are not set. We need it # to know whether a value has been provided by the user or not # After parsing the command line we complete the config options # with the default values for the options that have not been set # by the parser or by a config file defaults = parser.get_default_values() options, args = parser.parse_args(args, values=Values()) try: config = Config(options.config_file) except AttributeError: config = Config() config.update(options.__dict__) config.add(defaults.__dict__) if not args: parser.error("missing required repository URI") return 1 parser.destroy() if config.debug: import repositoryhandler.backends repositoryhandler.backends.DEBUG = True uri = args[0] files = args[1:] files_from_stdin = (files and files[0] == '-') # Create repository path = uri_to_filename(uri) if path is not None: try: repo = create_repository_from_path(path) except RepositoryUnknownError: printerr( "Path %s doesn't seem to point to a repository supported by guilty", (path, )) return 1 except Exception, e: printerr("Unknown error creating repository for path %s (%s)", (path, str(e))) return 1 uri = repo.get_uri_for_path(path)
def process_include_files(infile, outfile, inpath=''): options = Values({ 'force': False, }) prep_schema_doc(infile, outfile, inpath, options)
cmd_options.append('--commoncause') if trial['options'].get('Cluster start',False) == True: cmd_options.append('--clusterstart') cmd = format("./run_coe3 {0}"," ".join(cmd_options)) print cmd if rungame: os.system(cmd) default_options = { 'version': '0.1', 'debug': False, 'mapdir': './coe3.app/Contents/Resources/maps', 'rungame': False, 'mapgen': True, 'list_trials': False } options = Values(default_options) def print_columns(ncols, keyL): rows = len(keyL) // ncols if len(keyL) % ncols: rows += 1 fmts = [ "%2d) %-" + str(max([ len(keyL[j]) for j in range(i*rows,(i+1)*rows) if j < len(keyL)])) +"s " for i in range(ncols) ] for cols in zip(*[range(i*rows,(i+1)*rows) for i in range(ncols)]): for i, k in enumerate(cols): if k < len(keyL) : print fmts[i] % (1+k, keyL[k]), print "" def trials_main(): from optparse import OptionParser, OptionGroup
def _makeValues(self, **kw): from optparse import Values return Values(kw.copy())
def test_hosts_no_topology_raises_error(self, conf_mock): conf_mock.side_effect = ConfigFileNotFoundError self.assertRaisesRegexp( ConfigurationError, "Hosts defined in --hosts/-H must be in the " "topology file", main._update_env, Values(), Values({'hosts': 'master'}))
def test_get_default_options(self): options = Values({'k1': 'dv1', 'k2': 'dv2'}) non_default_options = Values({'k2': 'V2', 'k3': 'V3'}) default_options = main.get_default_options(options, non_default_options) self.assertEqual(default_options, Values({'k1': 'dv1'}))
def Calc(calc, outfile=None, NoDataValue=None, type=None, format=None, creation_options=None, allBands='', overwrite=False, hideNoData=False, projectionCheck=False, color_table=None, extent=None, user_namespace=None, debug=False, quiet=False, **input_files): """ Perform raster calculations with numpy syntax. Use any basic arithmetic supported by numpy arrays such as +-* along with logical operators such as >. Note that all files must have the same dimensions, but no projection checking is performed. Keyword arguments: [A-Z]: input files [A_band - Z_band]: band to use for respective input file Examples: add two files together: Calc("A+B", A="input1.tif", B="input2.tif", outfile="result.tif") average of two layers: Calc(calc="(A+B)/2", A="input1.tif", B="input2.tif", outfile="result.tif") set values of zero and below to null: Calc(calc="A*(A>0)", A="input.tif", A_band=2, outfile="result.tif", NoDataValue=0) work with two bands: Calc(["(A+B)/2", "A*(A>0)"], A="input.tif", A_band=1, B="input.tif", B_band=2, outfile="result.tif", NoDataValue=0) sum all files with hidden noDataValue Calc(calc="sum(a,axis=0)", a=['0.tif','1.tif','2.tif'], outfile="sum.tif", hideNoData=True) """ opts = Values() opts.input_files = input_files # Single calc value compatibility # (type is overridden in the parameter list) if isinstance(calc, list): opts.calc = calc else: opts.calc = [calc] opts.outF = outfile opts.NoDataValue = NoDataValue opts.type = type opts.format = format opts.creation_options = [] if creation_options is None else creation_options opts.allBands = allBands opts.overwrite = overwrite opts.hideNoData = hideNoData opts.projectionCheck = projectionCheck opts.color_table = color_table opts.extent = extent opts.user_namespace = user_namespace opts.debug = debug opts.quiet = quiet return doit(opts, None)
def parse_options(): parser = OptionParser(usage='usage: %prog [options] url...') default_options = OptionGroup(parser, "Default", "") default_options.add_option('--verbose', '-v', default=None, dest="verbose", action="store_true", help="Print the current targets, etc.") default_options.add_option('--dont-filter', default=False, dest="do_print", action="store_true", help="Write output directly to the command" "line, don't filter it.") default_options.add_option('--vulnerabilities-only', default=False, dest="vuln_only", action="store_true", help="Print only vulnerabilities " "(i.e. no warnings)") default_options.add_option('--abort-early', '-a', default=False, dest="abort_early", action="store_true", help="Exit on first found vulnerability.") default_options.add_option('--import-cookies', default=None, dest="import_cookies", help="Given a file, it will import it." "(Hint: Useful to avoid Captchas...)") parser.add_option_group(default_options) crawling_options = OptionGroup( parser, "Crawling", "This section provides information" "about the different crawling options.") crawling_options.add_option('--no-crawl', action='store_true', dest='no_crawl', help="DO NOT search for links on the target") crawling_options.add_option('--whitelist', default=[], dest="whitelist", help="Hosts which are allowed to be crawled.") crawling_options.add_option('--blacklist', default=[], dest="blacklist", action="append", help="Specify sites which shouldn't be" "visited or attacked. (Hint: logout)") parser.add_option_group(crawling_options) authentification_options = OptionGroup( parser, "Authentification", "Authentification to a specific" " post site.") authentification_options.add_option('--auth', default=None, dest="auth_url", help="Post target for " "authentification") authentification_options.add_option('--auth-data', dest='auth_data', action='append', type='str', default=[], help="A post parameter in the " "form of targetname=targetvalue") authentification_options.add_option('--form-page', dest='form_page', default=None, help="The site of the form you want " "to use to sign in") authentification_options.add_option('--form-id', dest='form_id', default=None, help="The id of the form you want " "to use to sign in.") authentification_options.add_option('--form-data', dest='form_data', action='append', type='str', default=[], help="A field you want to set " "manually.") parser.add_option_group(authentification_options) configuration_options = OptionGroup( parser, "Configuration", "You are also able to write your" " specified parameters in a file" " for easier usage.") configuration_options.add_option('--config', '-c', metavar='FILE', dest="read_config", help="Read the parameters from FILE") configuration_options.add_option('--write-config', metavar='FILE', dest="write_config", help="Insted of running the options," ' write them to the specified file ("-" ' 'for standard output).') parser.add_option_group(configuration_options) billion_laughs_configuration_options = OptionGroup( parser, "Billion Laughs configuration", "You are able to specify" "some options for the" "Billion Laughs test.") billion_laughs_configuration_options.add_option( '--net', dest='bl_config', action='store', default=None, nargs=2, help='This option will run' 'the Billion Laughs test' 'on a network application.' 'You need to pass your IP and an unused port.' 'If this option is not used' 'you can just test local applications.') # Options for scanning for specific vulnerabilities. attack_options = OptionGroup( parser, "Attacks", "If you specify own or several of the " "options _only_ this/these will be run. " "If you don't specify any, all will be " "run.") for attack in all_attacks(): attack_options.add_option('--' + attack.__name__, dest=attack.__name__, action="store_true", default=False) attack_options.add_option('--except-' + attack.__name__, dest=attack.__name__ + "_except", action="store_true", default=False) parser.add_option_group(attack_options) # Get default values options, arguments = parser.parse_args([]) # Parse command line cli_options = Values() _, cli_arguments = parser.parse_args(values=cli_options) # Update default values with configuration file config_fn = cli_options.__dict__.get('read_config') if config_fn is not None: read_options, read_arguments = read_config(config_fn, parser) options.__dict__.update(read_options) arguments += read_arguments # Update actual CLI options options.__dict__.update(cli_options.__dict__) arguments += cli_arguments if not arguments and not options.write_config: parser.error(u'Need at least one target') return (options, arguments)
class TestCliListTargets(utils.CliTestCase): def setUp(self): self.original_timezone = os.environ.get('TZ') os.environ['TZ'] = 'US/Eastern' time.tzset() def tearDown(self): if self.original_timezone is None: del os.environ['TZ'] else: os.environ['TZ'] = self.original_timezone time.tzset() @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_list_targets_error_args(self, ensure_connection_mock, stderr): session = mock.MagicMock(getAPIVersion=lambda: koji.API_VERSION, getBuildTargets=lambda n: []) options = mock.MagicMock(quiet=False) with self.assertRaises(SystemExit) as ex: anon_handle_list_targets(options, session, ['aaa']) self.assertExitCode(ex, 2) @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_list_targets_error_all_not_found(self, ensure_connection_mock, stderr): session = mock.MagicMock(getAPIVersion=lambda: koji.API_VERSION, getBuildTargets=lambda n: []) options = mock.MagicMock(quiet=False) with self.assertRaises(SystemExit) as ex: anon_handle_list_targets(options, session, []) self.assertExitCode(ex, 2) self.assertTrue('No targets were found' in stderr.getvalue()) @mock.patch('optparse.OptionParser.parse_args', return_value=(Values({'quiet': False, 'name': 'f50'}), [])) @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_list_targets_error_name_not_found(self, ensure_connection_mock, stderr, opt): session = mock.MagicMock(getAPIVersion=lambda: koji.API_VERSION, getBuildTargets=lambda n: []) options = mock.MagicMock(quiet=False) with self.assertRaises(SystemExit) as ex: anon_handle_list_targets(options, session, []) self.assertExitCode(ex, 2) self.assertTrue('No such build target:' in stderr.getvalue()) @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_list_targets_all(self, ensure_connection_mock, stdout): session = mock.MagicMock(getAPIVersion=lambda: koji.API_VERSION, getBuildTargets=lambda n: _mock_targets) options = mock.MagicMock(quiet=False) anon_handle_list_targets(options, session, []) expected = [ 'Name|Buildroot|Destination|', '---------------------------------------------------------------------------------------------', 'f33|f33-build|f33-updates-candidate|', '' ] self.assertEqual(expected, [re.sub(' +', '|', l) for l in stdout.getvalue().split('\n')]) @mock.patch('optparse.OptionParser.parse_args', return_value=(Values({'quiet': False, 'name': 'f50'}), [])) @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_list_targets_one(self, ensure_connection_mock, stdout, opt): session = mock.MagicMock(getAPIVersion=lambda: koji.API_VERSION, getBuildTargets=lambda n: _mock_targets) options = mock.MagicMock(quiet=False) anon_handle_list_targets(options, session, []) expected = [ 'Name|Buildroot|Destination|', '---------------------------------------------------------------------------------------------', 'f33|f33-build|f33-updates-candidate|', '' ] self.assertEqual(expected, [re.sub(' +', '|', l) for l in stdout.getvalue().split('\n')])
def test_get_cmd_params_from_parser_should_return_dict(self): mocked_parser.parse_args.return_value = (Values(default_params), None) returned_params = _get_cmd_params_from_parser(mocked_parser) self.assertEqual(type(returned_params), dict)
def parse_and_validate_commands(args=sys.argv[1:]): # Find local fabfile path or abort fabfile = "prestoadmin" # Store absolute path to fabfile in case anyone needs it state.env.real_fabfile = fabfile # Load fabfile (which calls its module-level code, including # tweaks to env values) and put its commands in the shared commands # dict docstring, callables = load_fabfile(fabfile) state.commands.update(callables) # Parse command line options parser = parser_for_options() # Unless you pass in values, optparse fills in the default values for all # of the options. We want to save the version of the options without # default values, because that takes precedence over all other env vars. non_default_options, arguments = parser.parse_args(args, values=Values()) options, arguments = parser.parse_args(args) default_options = get_default_options(options, non_default_options) # Handle regular args vs -- args arguments = parser.largs if len(parser.rargs) > 0: warn("Arbitrary remote shell commands not supported.") show_commands(None, 'normal', 2) if options.extended_help: parser.print_extended_help() sys.exit(0) # If user didn't specify any commands to run, show help if not arguments: parser.print_help() sys.exit(0) # don't consider this an error # Parse arguments into commands to run (plus args/kwargs/hosts) commands_to_run = None try: commands_to_run = parse_arguments(arguments, state.commands) except NameError as e: warn(e.message) _LOGGER.warn("Unable to parse arguments", exc_info=True) parser.print_help() sys.exit(2) # Handle show (command-specific help) option if options.display: display_command(commands_to_run[0][0]) load_config_callback = _get_config_callback(commands_to_run) _update_env(default_options, non_default_options, load_config_callback) if not options.serial: state.env.parallel = True state.env.warn_only = False # Initial password prompt, if requested if options.initial_password_prompt: prompt = "Initial value for env.password: " state.env.password = getpass.getpass(prompt) state.env['tasks'] = [x[0] for x in commands_to_run] return commands_to_run
def parseSelf(obj): opt = Values() for attr in dir(obj): if isinstance(getattr(obj, attr), Option): opt.ensure_value(attr, getattr(obj, attr).value) return opt
def main(): """ Main funtion that runs the module. """ module = build() config = {'server': module.params['server']} archs = module.params['archs'] channels = module.params['channels'] host_name = module.params['host'] if 'ssl_auth' in module.params: config.update(**ssl_config(module)) else: module.fail_json(changed=False, skipped=False, failed=True, error='Missing authentication config') options = Values(config) session_opts = koji.grab_session_options(options) session = koji.ClientSession(options.server, session_opts) try: session.ssl_login(options.cert, None, options.serverca) except Exception as e: module.fail_json(changed=False, skipped=False, failed=True, error=str(e)) if not session.getHost(host_name): try: session.addHost(host_name, archs) except Exception as e: module.fail_json(changed=False, skipped=False, failed=True, msg=str(e)) channel_errs = [] added_channels = [] for channel in channels: try: session.addHostToChannel(host_name, channel, create=True) added_channels.append(channel) except Exception as e: if not str(e).endswith( 'is already subscribed to the %s channel' % channel): channel_errs.append(str(e)) if len(channel_errs) > 0: module.fail_json(changed=False, skipped=False, failed=True, msg=dir(channel_errs[0]), error='Add Channel Error: ' + str(channel_errs)) module.exit_json(changed=True, skipped=False, failed=False, result={ 'name': host_name, 'archs': archs, 'channels': added_channels })
import time from optparse import Values from module.jira import ( execute_command, ) from test import settings password = settings.get('PASSWORD') username = settings.get('USERNAME') summary = settings.get('TEST_SUMMARY') description = settings.get('TEST_DESCRIPTION') reporter = settings.get('USERNAME') project = settings.get('PROJECT') system_type = settings.get('ISSUE_MNT') occurred_time = 'customfield_10200:' + time.strftime("%d/%m/%y") if (__name__ == "__main__"): contents = { 'loglevel': 20, 'password': password, 'user': username, 'server': 'http://bug.xingshulin.com' } options = Values(contents) args = [ 'create', '-s', summary, '-d', description, '-p', project, '-t', system_type, '-a', reporter, '-f', occurred_time ] execute_command(options, args)