def main(pid=None, verbose=False): level = logging.DEBUG if verbose else logging.INFO configure_logging(level=level) sock = None if pid is None: pid, sock = connect_first_available_server() if pid is None: raise click.BadOptionUsage('pid', "Server PID is required!") shell = BackdoorShell(pid, sock=sock) shell.interact()
def __validate_cpu_core(ctx, param, value): p = psutil.Process(os.getpid()) available_cores = p.cpu_affinity() for v in value: if v not in available_cores: raise click.BadOptionUsage( f'Target core ({v}) is not one of the available cores: ' f'{available_cores}') return value
def create(config, group, type): """Create an LDAP group.""" if type not in ('user', 'service'): raise click.BadOptionUsage( # pragma: no cover "--grouptype must be 'user' or 'service'") client = Client() client.prepare_connection() group_api = API(client) group_api.create(group, type)
def exec_stage(cli_args, commits, artifacts_store): if cli_args['algorithm'] in algorithms: algorithm = algorithms[cli_args['algorithm']]() return algorithm.compute_graph(cli_args, commits, artifacts_store) else: raise click.BadOptionUsage( option_name='-al', message= f"Specified '--algorithm' not found. Possible values are: {', '.join(list(algorithms.keys()))}" )
def clean(ctx, parts, unprime, step, **kwargs): """Remove a part's assets. \b Examples: snapcraft clean snapcraft clean my-part """ # This option is only valid in legacy. if step: option = "--step" if "--step" in ctx.obj["argv"] else "-s" raise click.BadOptionUsage(option, "no such option: {}".format(option)) build_provider = get_build_provider(**kwargs) build_provider_flags = get_build_provider_flags(build_provider, **kwargs) apply_host_provider_flags(build_provider_flags) is_managed_host = build_provider == "managed-host" try: project = get_project(is_managed_host=is_managed_host) except errors.ProjectNotFoundError: # Fresh environment, nothing to clean. return if unprime and not is_managed_host: raise click.BadOptionUsage("--unprime", "no such option: --unprime") if build_provider in ["host", "managed-host"]: step = steps.PRIME if unprime else None lifecycle.clean(project, parts, step) else: build_provider_class = build_providers.get_provider_for(build_provider) if parts: with build_provider_class( project=project, echoer=echo, build_provider_flags=build_provider_flags) as instance: instance.clean(part_names=parts) else: build_provider_class(project=project, echoer=echo).clean_project() # Clear the prime directory on the host lifecycle.clean(project, parts, steps.PRIME)
def get_schedule_at(at: Optional[str]) -> Optional[datetime.datetime]: if at is None: return None try: dt = utils.parse_datetime(at) except ValueError: raise click.BadOptionUsage("--at", f"Cannot parse datetime {at}") return dt
def exec_stage(cli_args, graph): formatter = get_formatter(cli_args) if formatter: formatter.export_file(cli_args, graph) else: raise click.BadOptionUsage( option_name='-of', message= f"Specified '--outputformat' not found. Possible values are: {', '.join(get_valid_formatters())}" )
def precision(precision, id_, hwid, type_): """Change the precision for the sensor and persist it in the sensor's EEPROM""" if id_ and (hwid or type_): raise click.BadOptionUsage( "If --id is given --hwid and --type are not allowed." ) if id_: try: sensor = W1ThermSensor.get_available_sensors()[id_ - 1] except IndexError: raise click.BadOptionUsage( "No sensor with id {0} available. " "Use the ls command to show all available sensors.".format(id_) ) else: sensor = W1ThermSensor(type_, hwid) sensor.set_precision(precision, persist=True)
def search( state, format, begin, end, advanced_query, use_checkpoint, saved_search, or_query, columns, include_all, **kwargs, ): """Search for file events.""" if format == FileEventsOutputFormat.CEF and columns: raise click.BadOptionUsage( "columns", "--columns option can't be used with CEF format.") # set default table columns if format == OutputFormat.TABLE: if not columns and not include_all: columns = [ "fileName", "filePath", "eventType", "eventTimestamp", "fileCategory", "fileSize", "fileOwner", "md5Checksum", "sha256Checksum", "riskIndicators", "riskSeverity", ] if use_checkpoint: cursor = _get_file_event_cursor_store(state.profile.name) checkpoint = _handle_timestamp_checkpoint(cursor.get(use_checkpoint), state) def checkpoint_func(event): cursor.replace(use_checkpoint, event["eventId"]) else: checkpoint = checkpoint_func = None query = _construct_query(state, begin, end, saved_search, advanced_query, or_query) dfs = _get_all_file_events(state, query, checkpoint) formatter = FileEventsOutputFormatter(format, checkpoint_func=checkpoint_func) # sending to pager when checkpointing can be inaccurate due to pager buffering, so disallow pager force_no_pager = use_checkpoint formatter.echo_formatted_dataframes(dfs, columns=columns, force_no_pager=force_no_pager)
def configuration_callback( cmd_name, option_name, config_file_name, saved_callback, provider, implicit, ctx, param, value, ): """Callback for reading the config file. Also takes care of calling user specified custom callback afterwards. Args: cmd_name (str): The command name. This is used to determine the configuration directory. option_name (str): The name of the option. This is used for error messages. config_file_name (str): The name of the configuration file. saved_callback (callable): User-specified callback to be called later. provider (callable): A callable that parses the configuration file and returns a dictionary of the configuration parameters. Will be called as `provider(file_path, cmd_name)`. Default: `configobj_provider()` implicit (bool): Whether a implicit value should be applied if no configuration option value was provided. Default: `False` ctx: Click context. param: Click Option value (str or None): The value passed to the option """ logger = logging.getLogger(__name__) ctx.default_map = ctx.default_map or {} cmd_name = cmd_name or ctx.info_name if implicit: default_value = config_file_name if value is None: if Path(default_value).is_file(): logger.debug("Using implicit default %s", default_value) value = default_value if value: try: config = provider(value, cmd_name) except IOError as e: raise click.BadOptionUsage( option_name, "Cannot read configuration file: {}".format(e), ctx, ) ctx.default_map.update(config) return saved_callback(ctx, param, value) if saved_callback else value
def nucypher_cli(click_config, verbose, mock_networking, json_ipc, no_logs, quiet, debug, no_registry): # Session Emitter for pre and post character control engagement. if json_ipc: emitter = IPCStdoutEmitter( quiet=quiet, capture_stdout=NucypherClickConfig.capture_stdout) else: emitter = StdoutEmitter( quiet=quiet, capture_stdout=NucypherClickConfig.capture_stdout) click_config.attach_emitter(emitter) click_config.emit(message=NUCYPHER_BANNER) if debug and quiet: raise click.BadOptionUsage( option_name="quiet", message="--debug and --quiet cannot be used at the same time.") if debug: click_config.log_to_sentry = False click_config.log_to_file = True # File Logging globalLogPublisher.addObserver(SimpleObserver()) # Console Logging globalLogPublisher.removeObserver(logToSentry) # No Sentry GlobalConsoleLogger.set_log_level(log_level_name='debug') elif quiet: # Disable Logging globalLogPublisher.removeObserver(logToSentry) globalLogPublisher.removeObserver(SimpleObserver) globalLogPublisher.removeObserver(getJsonFileObserver()) # Logging if not no_logs: GlobalConsoleLogger.start_if_not_started() # CLI Session Configuration click_config.verbose = verbose click_config.mock_networking = mock_networking click_config.json_ipc = json_ipc click_config.no_logs = no_logs click_config.quiet = quiet click_config.no_registry = no_registry click_config.debug = debug # Only used for testing outputs; # Redirects outputs to in-memory python containers. if mock_networking: click_config.emit(message="WARNING: Mock networking is enabled") click_config.middleware = MockRestMiddleware() else: click_config.middleware = RestMiddleware() # Global Warnings if click_config.verbose: click_config.emit(message="Verbose mode is enabled", color='blue')
def get(id_, hwid, type_, unit, resolution, as_json, offset): """Get temperature of a specific sensor""" if id_ and (hwid or type_): raise click.BadArgumentUsage( "If --id is given --hwid and --type are not allowed.") if id_: try: sensor = W1ThermSensor.get_available_sensors()[id_ - 1] except IndexError: error_msg = ("No sensor with id {0} available. ".format(id_) + "Use the ls command to show all available sensors.") if CLICK_MAJOR_VERSION >= 7: # pragma: no cover raise click.BadOptionUsage("--id", error_msg) else: # pragma: no cover raise click.BadOptionUsage(error_msg) else: sensor = W1ThermSensor(type_, hwid) if resolution: sensor.set_resolution(resolution, persist=False) if offset: sensor.set_offset(offset, unit) temperature = sensor.get_temperature(unit) if as_json: data = { "hwid": sensor.id, "offset": offset, "type": sensor.name, "temperature": temperature, "unit": unit, } click.echo(json.dumps(data, indent=4, sort_keys=True)) else: click.echo("Sensor {0} measured temperature: {1} {2}".format( click.style(sensor.id, bold=True), click.style(str(temperature), bold=True), click.style(unit, bold=True), ))
def grant(general_config, bob_encrypting_key, bob_verifying_key, label, value, rate, expiration, m, n, character_options, config_file): """Create and enact an access policy for some Bob. """ # Setup emitter = setup_emitter(general_config) ALICE = character_options.create_character(emitter, config_file, general_config.json_ipc) # Input validation if ALICE.federated_only: if any((value, rate)): raise click.BadOptionUsage(option_name="--value, --rate", message="Can't use --value or --rate with a federated Alice.") elif bool(value) and bool(rate): raise click.BadOptionUsage(option_name="--rate", message="Can't use --value if using --rate") elif not (bool(value) or bool(rate)): rate = ALICE.default_rate # TODO #1709 click.confirm(f"Confirm default rate {rate}?", abort=True) # Request grant_request = { 'bob_encrypting_key': bob_encrypting_key, 'bob_verifying_key': bob_verifying_key, 'label': label, 'm': m, 'n': n, 'expiration': expiration, } if not ALICE.federated_only: if value: grant_request['value'] = value elif rate: grant_request['rate'] = rate return ALICE.controller.grant(request=grant_request)
def __init__(self, geth, provider_uri, staker_address, worker_address, federated_only, rest_host, rest_port, db_filepath, network, registry_filepath, dev, poa, light): if federated_only: # TODO: consider rephrasing in a more universal voice. if geth: raise click.BadOptionUsage( option_name="--geth", message="--geth cannot be used in federated mode.") if staker_address: raise click.BadOptionUsage( option_name='--staker-address', message="--staker-address cannot be used in federated mode." ) if registry_filepath: raise click.BadOptionUsage( option_name="--registry-filepath", message= f"--registry-filepath cannot be used in federated mode.") eth_node = NO_BLOCKCHAIN_CONNECTION provider_uri = provider_uri if geth: eth_node = actions.get_provider_process() provider_uri = eth_node.provider_uri(scheme='file') self.eth_node = eth_node self.provider_uri = provider_uri self.staker_address = staker_address self.worker_address = worker_address self.federated_only = federated_only self.rest_host = rest_host self.rest_port = rest_port # FIXME: not used in generate() self.db_filepath = db_filepath self.domains = {network} if network else None # TODO: #1580 self.registry_filepath = registry_filepath self.dev = dev self.poa = poa self.light = light
def parse_entity_types(entity_types): entity_types = [c.strip() for c in entity_types.split(',')] # validate passed types for entity_type in entity_types: if entity_type not in EntityType.ALL_FOR_STREAMING: raise click.BadOptionUsage( '--entity-type', '{} is not an available entity type. Supply a comma separated list of types from {}' .format(entity_type, ','.join(EntityType.ALL_FOR_STREAMING))) return entity_types
def get_sites_dir(base_dir): if base_dir is None: base_dir = find_base_path_in_config() if base_dir is None: base_dir = os.getcwd() sites_dir = os.path.join(base_dir, "mirror") # NOTE: fixed as mirror if not os.path.isdir(sites_dir): raise click.BadOptionUsage("--base-dir") return sites_dir
def debug_type(s): # I am certain there's a better way to get click to do this... global DEBUGS debugs = [sp.strip() for sp in re.split(r"[ ,;]+", s)] debugs = [d for d in debugs if d] for d in debugs: if d not in VALID_DEBUGS: raise click.BadOptionUsage( f"--debug={d}?? Choose from {', '.join(VALID_DEBUGS)}") DEBUGS = debugs return debugs
def create_config(self, emitter, config_file): if self.dev: # Can be None as well, meaning it is unset - no error in this case if self.federated_only is False: raise click.BadOptionUsage( option_name="--federated-only", message=click.style( "--federated-only cannot be explicitly set to False when --dev is set", fg="red")) return AliceConfiguration(emitter=emitter, dev_mode=True, network_middleware=self.middleware, domain=TEMPORARY_DOMAIN, eth_provider_uri=self.eth_provider_uri, signer_uri=self.signer_uri, gas_strategy=self.gas_strategy, max_gas_price=self.max_gas_price, federated_only=True, lonely=self.lonely, payment_method=self.payment_method, payment_provider=self.payment_provider, payment_network=self.payment_network) else: if not config_file: config_file = select_config_file( emitter=emitter, checksum_address=self.pay_with, config_class=AliceConfiguration) try: return AliceConfiguration.from_configuration_file( emitter=emitter, dev_mode=False, network_middleware=self.middleware, domain=self.domain, eth_provider_uri=self.eth_provider_uri, signer_uri=self.signer_uri, gas_strategy=self.gas_strategy, max_gas_price=self.max_gas_price, filepath=config_file, rest_port=self.discovery_port, checksum_address=self.pay_with, registry_filepath=self.registry_filepath, lonely=self.lonely, payment_method=self.payment_method, payment_provider=self.payment_provider, payment_network=self.payment_network) except FileNotFoundError: return handle_missing_configuration_file( character_config_class=AliceConfiguration, config_file=config_file)
def import_file(name, **kwargs): ''' Import an Excel spreadsheet into LoRa ''' if '.' not in name: module_name = 'mora.importing.' + name.replace('-', '_') try: module = importlib.import_module(module_name) except ImportError: if flask.current_app.debug or kwargs['verbose']: traceback.print_exc() raise click.BadOptionUsage('{} is not a known importer'.format(name)) if not hasattr(module, 'run'): raise click.BadOptionUsage('{} is not a valid importer'.format(name)) module.run(**kwargs)
def resolution(resolution, id_, hwid, type_): """Change the resolution for the sensor and persist it in the sensor's EEPROM""" if id_ and (hwid or type_): raise click.BadArgumentUsage( "If --id is given --hwid and --type are not allowed.") if id_: try: sensor = W1ThermSensor.get_available_sensors()[id_ - 1] except IndexError: error_msg = ("No sensor with id {0} available. ".format(id_) + "Use the ls command to show all available sensors.") if CLICK_MAJOR_VERSION >= 7: # pragma: no cover raise click.BadOptionUsage("--id", error_msg) else: # pragma: no cover raise click.BadOptionUsage(error_msg) else: sensor = W1ThermSensor(type_, hwid) sensor.set_resolution(resolution, persist=True)
def cli(file, manual, compare_with_extracted_clauses, prf_extracted_annotated): key = "clauses-manual" if manual else "clauses" # key = "clauses-predicted" if manual else "clauses" if compare_with_extracted_clauses and not manual: raise click.BadOptionUsage( "Can't use --compare-with-extracted-clauses without --manual" ) for line in file: data = json.loads(line) if manual and key not in data: continue if prf_extracted_annotated: if "clauses-manual" in data: add_ftpn( data["dataset"], data["clauses-manual"], data["clauses"] if "clauses-predicted" not in data else data["clauses-predicted"], ) continue if compare_with_extracted_clauses: clauses = change_IOB_from_clauses( data["clauses-manual"], data["clauses"] if "clauses-predicted" not in data else data["clauses-predicted"], ) else: clauses = data[key] add_counts_of_clauses(counts[data["dataset"]], clauses) if prf_extracted_annotated: for dataset in ytrue: p, r, f, _ = precision_recall_fscore_support(ytrue[dataset], ypred[dataset], average="macro") print(dataset, f"P: {p:.2f}, R: {r:.2f}, F: {f:.2f}") return for dataset in counts: for type in counts[dataset]: print( dataset, type, "{:.2f}".format( counts[dataset][type]["correct"] / ( counts[dataset][type]["correct"] + counts[dataset][type]["incorrect"] ) ), )
def callback(ctx, param, value): if value and not ctx.resilient_parsing: state = ctx.ensure_object(State) if not state.api_config.username: raise click.BadOptionUsage( param, 'Using --name to refer to a workspace requires a ' 'the --username options to be set because the workspace ' 'is uniquely identified by the name and username. ' 'Alternatively, use the --id option to set the workspace.', ctx=ctx) return value
def config_callback( cmd_name, option_name, config_file, saved_callback, provider, implicit, ctx, param, value, ): """ Callback for reading the config file. Also takes care of calling user specified custom callback afterwards. Parameters ---------- cmd_name : str The command name. option_name : str The name of the option. config_file : str The name of the config file. saved_callback: callable User-specified callback to be called later. provider : callable A callable that parses the configuration file and returns a dictionary of the configuration parameters. Will be called as `provider(file_path, cmd_name)`. Default: `configparse_provider()` implicit : bool Whether a implicit value should be applied if no configuration option value was provided. ctx : object Click context. """ ctx.default_map = ctx.default_map or {} cmd_name = cmd_name or ctx.info_name ctx.config = None if implicit: default_value = os.path.join(os.getcwd(), config_file) param.default = default_value value = value or default_value if value: try: config = provider(value, cmd_name) except Exception as e: raise click.BadOptionUsage(option_name, f"Error reading config: {e}", ctx) ctx.config = config ctx.default_map.update(config) return saved_callback(ctx, param, value) if saved_callback else value
def resolve_cluster_id(aws_ctx, cluster_id, cluster_id_file): if cluster_id is None: if cluster_id_file is not None: aws_ctx.echo("Loading cluster info from: %s" % cluster_id_file) with open(cluster_id_file, "r") as input: cluster_info = json.load(input) aws_ctx.debug("Cluster info is: %s" % str(cluster_info)) cluster_id = cluster_info['ClusterId'] else: raise click.BadOptionUsage( '--cluster-id or --cluster-id-file is required') return cluster_id
def _get_output(ctx): """Return the output dataset stored in the given context. If the output dataset doesn't exist, create it. """ if "ds_out" not in ctx.obj: dsin = _get_input(ctx) ctx.obj["ds_out"] = xr.Dataset(attrs=dsin.attrs) if ctx.obj["output"] is None: raise click.BadOptionUsage("output", "No output file name given.", ctx.parent) return ctx.obj["ds_out"]
def remove(clean, purge, subject, note, notebook, path): """Remove a note from the database""" if os.path.exists(path): os.chdir(path) else: raise click.UsageError("No such file or directory") if subject: subject_name = os.path.basename(subject.rstrip('/')) abs_path = os.path.join(os.getcwd(), subject) subj = Subject(path=abs_path, name=subject_name) removed_subject = subj.remove() click.echo("Removed: " + removed_subject) elif note: # Invalid file for removal operation if '.nbdb' not in notebook: raise click.UsageError("NoteBook not found") note_book = NoteBook(path=path, file_name=notebook) note_book.remove_note(note) elif clean: if '.nbdb' not in notebook: raise click.UsageError("NoteBook not found") note_book = NoteBook(path=path, file_name=notebook) if click.confirm('\nRemove all notes in the provided notebook?'): note_book.clean_notes() # Only removes .nbdb files elif purge: if click.confirm('\nRemove all notebooks in the current subject?'): click.echo() for file in os.listdir(path): if file.endswith('.nbdb'): note_book = NoteBook(path=path, file_name=file) note_book.remove() click.echo() elif notebook: if '.nbdb' not in notebook: raise click.UsageError("NoteBook not found") note_book = NoteBook(path=path, file_name=notebook) click.echo() note_book.remove() click.echo() else: raise click.BadOptionUsage('Missing option')
def scan(paths, recursive, size, min_size, max_size, hash_function): """Scan files in directories and report duplication.""" if min_size is None: min_size = size // 4 if max_size is None: max_size = size * 8 bytes_total = 0 bytes_dupe = 0 fingerprints = set() supported = supported_hashes() if hash_function not in supported: msg = "'{}' is not a supported hash.\nTry one of these:\n{}".format( hash_function, ", ".join(supported)) raise click.BadOptionUsage("hf", msg) hf = getattr(hashlib, hash_function) files = [] for path in paths: files += list(iter_files(path, recursive)) t = Timer("scan", logger=None) t.start() with click.progressbar(files) as pgbar: for entry in pgbar: try: chunker = fastcdc.fastcdc(entry.path, min_size, size, max_size, hf=hf) except Exception as e: click.echo("\n for {}".format(entry.path)) click.echo(repr(e)) continue for chunk in chunker: bytes_total += chunk.length if chunk.hash in fingerprints: bytes_dupe += chunk.length fingerprints.add(chunk.hash) t.stop() if bytes_total: data_per_s = bytes_total / Timer.timers.mean("scan") dd_ratio = bytes_dupe / bytes_total * 100 click.echo("Files: {}".format(intcomma(len(files)))) click.echo("Chunk Sizes: min {} - avg {} - max {}".format( min_size, size, max_size)) click.echo("Unique Chunks: {}".format(intcomma(len(fingerprints)))) click.echo("Total Data: {}".format(naturalsize(bytes_total))) click.echo("Dupe Data: {}".format(naturalsize(bytes_dupe))) click.echo("DeDupe Ratio: {:.2f} %".format(dd_ratio)) click.echo("Throughput: {}/s".format(naturalsize(data_per_s))) else: click.echo("No data.")
def _query_photos(photosdb: PhotosDB, query_options: QueryOptions) -> List: """Query photos given a QueryOptions instance""" try: photos = photosdb.query(query_options) except ValueError as e: if "Invalid query_eval CRITERIA:" not in str(e): raise ValueError(e) from e msg = str(e).split(":")[1] raise click.BadOptionUsage( "query_eval", f"Invalid query-eval CRITERIA: {msg}") from e return photos
def _validate_args(geth, federated_only, staker_address, registry_filepath): # # Validate # if federated_only: # TODO: consider rephrasing in a more universal voice. if geth: raise click.BadOptionUsage( option_name="--geth", message="--geth cannot be used in federated mode.") if staker_address: raise click.BadOptionUsage( option_name='--staker-address', message="--staker-address cannot be used in federated mode.") if registry_filepath: raise click.BadOptionUsage( option_name="--registry-filepath", message=f"--registry-filepath cannot be used in federated mode." )
def generate_config(self, config_root): if self.provider_uri is None: raise click.BadOptionUsage( option_name="--provider", message="--provider must be specified to create a new stakeholder") if self.network is None: raise click.BadOptionUsage( option_name="--network", message="--network must be specified to create a new stakeholder") return StakeHolderConfiguration.generate( config_root=config_root, provider_uri=self.provider_uri, poa=self.poa, light=self.light, sync=False, registry_filepath=self.registry_filepath, domains={self.network} # TODO: #1580 )