def goto_app(app): try: os.chdir(app) click.echo(f'Currently working in {os.getcwd()}') except: click.echo(f'Could not change working directory to {app}') click.exit()
def sync(): click.confirm( "This will add (and potentially overwrite) files in your" + " course repository. The changes will not be permanent and" + " you can use git to undo anything you would rather not" + " have. Would you like to continue?", abort=True, ) module_dir = os.path.split(__file__)[0].replace("cli", "") templates_src = os.path.join(module_dir, "..", "ofcourse-templates") templates_dest = os.path.join(os.getcwd(), "templates") if not os.path.isdir(templates_dest): click.echo(u"\u2717 Uhoh, {} is not a directory...") click.echo(u"If you are trying to start a new course, try" + u" `ofcourse new`") click.echo(u"If you are updating an existing course, make" + u" sure you are in the right directory") click.exit(1) dir_util.copy_tree(templates_src, templates_dest, update=True) click.echo(u"\u2714 Your templates have been updated!") static_dest = os.path.join(os.getcwd(), "static") dir_util.copy_tree(os.path.join(module_dir, "static"), static_dest, update=True) click.echo(u"\u2714 Your static content (CSS/JS) has been updated!") click.echo(u"Make sure to add the new files to your git repository") click.echo( u"Tip: To see what's new, use `git add -p` to stage changes" + u" individually, and `git status` to check for new files" )
def create_pull_request(branch): """Open pull request creation view in browser.""" provider, project = remote_data() try: url = 'https://{provider}/{pull_request_url}'.format( provider=provider, pull_request_url=GIT_PROVIDERS[provider]['pull_request'].format( project=project, branch=quote_plus(branch))) webbrowser.open(url) except KeyError: click.exit('Unable to create pull request')
def add_role(role, email): user = User.by_email(email) if not user: click.echo("The specified user does not exist.") click.exit(1) try: user.add_role(role) db.session.commit() except ValueError: click.echo("The specified role is not defined.") click.exit(1) click.echo("User {} now has role {}.".format(email, role))
def get_repos(labels): if not labels: click.echo( 'You need to specify the "labels" object in the config file', error=True) click.exit() repos = labels.get("repositories") if not isinstance(repos, list): click.echo( 'You need to specify the "repos" list in the config ' "labels object file", error=True, ) click.exit() return repos
def fetch_sample_metadata(from_, samples, all_columns, context, output, tagged, force_category, resolve_ambiguities): """Retreive sample metadata.""" if resolve_ambiguities and tagged: click.echo("Cannot resolve ambiguities and fetch tagged metadata", err=True) click.exit(1) import redbiom.util import redbiom.fetch import pandas as pd iterator = redbiom.util.from_or_nargs(from_, samples) if not force_category: force_category = None md, map_ = redbiom.fetch.sample_metadata(iterator, context=context, common=not all_columns, restrict_to=force_category, tagged=tagged) if resolve_ambiguities: md.set_index('#SampleID', inplace=True) # a temporary key to use when resolving ambiguities # that will be removed before writing the metadata key = "__@@AMBIGUITY@@__" # add ambiguity information into the frame ambigs = pd.Series(map_) ambigs = ambigs.loc[md.index] md[key] = ambigs # remove duplicated unambiguous identifiers md = md[~md[key].duplicated()] # remove our index, and replace the entries with the ambiguous names md.reset_index(inplace=True) md['#SampleID'] = md[key] # cleanup md.drop(columns=key, inplace=True) md.to_csv(output, sep='\t', header=True, index=False, encoding='utf-8') _write_ambig(map_, output)
def create( tileset, recipe, name=None, description=None, privacy=None, attribution=None, token=None, indent=None, ): """Create a new tileset with a recipe. $ tilesets create <tileset_id> <tileset_id> is in the form of username.handle - for example "mapbox.neat-tileset". The handle may only include "-" or "_" special characters and must be 32 characters or fewer. """ mapbox_api = utils._get_api() mapbox_token = utils._get_token(token) s = utils._get_session() url = "{0}/tilesets/v1/{1}?access_token={2}".format( mapbox_api, tileset, mapbox_token ) body = {} body["name"] = name or "" body["description"] = description or "" if privacy: body["private"] = True if privacy == "private" else False if not utils.validate_tileset_id(tileset): raise errors.TilesetNameError(tileset) if recipe: with open(recipe) as json_recipe: body["recipe"] = json.load(json_recipe) if attribution: try: body["attribution"] = json.loads(attribution) except: click.echo("Unable to parse attribution JSON") click.exit(1) r = s.post(url, json=body) click.echo(json.dumps(r.json(), indent=indent))
def move_paths(ctx, input, translation, output): ''' Move paths along offset vector. ''' arr, arrmd = ctx.obj.get(input, True) try: atype = arrmd['taxon'] except KeyError: click.echo(f'array "{input}" has no type') click.exit(-1) if atype != "paths": click.echo(f'array "{input}" is not of type "paths"') click.exit(-1) translation = pochoir.arrays.fromstr1(translation) from pochoir.arrays import to_like newarr = arr + to_like(translation, arr) ctx.obj.put(output, newarr, **arrmd)
def wrapper_decorator(*args, **kwargs): response = None repos = [] while True: if response: if 'nextToken' not in response: break else: kwargs['nextToken'] = response['nextToken'] try: response = func(*args, **kwargs) except ClientError as e: if e.response['Error']['Code'] == error_code: click.echo(error_message, err=True) else: click.echo(e, err=True) click.exit(error_message) sys.exit(1) repos += response[key] return repos
def build_pdf(files, hw, o, p, landscape, to_ps, chars_per_line): click.echo(chars_per_line) if not files: click.echo('please give file arguments') click.exit() ps_name = o + '.ps' if to_ps else '/tmp/hwgen.ps' title = '--center-title="csc710sbse: hw{hw}: Witschey"'.format(hw=hw) a2ps_cmd = [ 'a2ps', '-MLetter', # paper size title, # title on each page; customize to your needs '-{p}'.format(p=p), # pages per sheet '-C', # number every 5th line '-Av', # allow multiple files per sheet '--highlight-level=none', # fixes silly treatment of comments # '-q', # you can make the command quiet if you want ] if chars_per_line: a2ps_cmd.append('-l {}'.format(chars_per_line)) # set characters per line if landscape: a2ps_cmd.append('--landscape') # set to landscape a2ps_cmd.extend(['-o', ps_name]) # set output file a2ps_cmd.extend(files) print('about to execute', a2ps_cmd) p1 = subprocess.call(a2ps_cmd) if p1 != 0: exit(p1) if not to_ps: outfile = o ps2pdf_cmd = [ 'ps2pdf', ps_name, outfile + '.pdf' ] print('about to execute', ps2pdf_cmd) p2 = subprocess.call(ps2pdf_cmd)
def sync(): click.confirm('This will add (and potentially overwrite) files in your' + ' course repository. The changes will not be permanent and' + ' you can use git to undo anything you would rather not' + ' have. Would you like to continue?', abort=True) module_dir = os.path.split(__file__)[0].replace('cli', '') templates_src = os.path.join( module_dir, '..', 'ofcourse-templates', ) templates_dest = os.path.join(os.getcwd(), 'templates') if not os.path.isdir(templates_dest): click.echo(u'\u2717 Uhoh, {} is not a directory...') click.echo(u'If you are trying to start a new course, try' + u' `ofcourse new`') click.echo(u'If you are updating an existing course, make' + u' sure you are in the right directory') click.exit(1) dir_util.copy_tree(templates_src, templates_dest, update=True) click.echo(u'\u2714 Your templates have been updated!') static_dest = os.path.join(os.getcwd(), 'static') dir_util.copy_tree(os.path.join(module_dir, 'static'), static_dest, update=True) click.echo(u'\u2714 Your static content (CSS/JS) has been updated!') click.echo(u'Make sure to add the new files to your git repository') click.echo(u'Tip: To see what\'s new, use `git add -p` to stage changes' + u' individually, and `git status` to check for new files')
def update( tileset, token=None, indent=None, name=None, description=None, privacy=None, attribution=None, ): """Update a tileset's information. tilesets update <tileset_id> """ mapbox_api = utils._get_api() mapbox_token = utils._get_token(token) s = utils._get_session() url = "{0}/tilesets/v1/{1}?access_token={2}".format( mapbox_api, tileset, mapbox_token ) body = {} if name: body["name"] = name if description: body["description"] = description if privacy: body["private"] = True if privacy == "private" else False if attribution: try: body["attribution"] = json.loads(attribution) except: click.echo("Unable to parse attribution JSON") click.exit(1) r = s.patch(url, json=body) if r.status_code != 204: raise errors.TilesetsError(r.text)
def main( teams: typing.Iterable[str], organizations: typing.Iterable[str], github_auth_token: str, make_org_folders: bool, make_team_files: bool, tee_output: bool, verbosity_level: int, **kwargs: dict[typing.Any, typing.Any], ) -> None: github_api_client = RequestsHTTPTransport( verify=True, url='https://api.github.com/graphql', headers=dict(Authorization=f"bearer {github_auth_token}"), ) global_results = defaultdict(set) for org_name in sorted(organizations): if verbosity_level >= 1: click.echo(f"\nNow traversing {org_name}!", err=True) for team_name in sorted(teams): if verbosity_level >= 1: click.echo( f"\tLooking for repoes {team_name} is ADMIN for in {org_name}...", err=True) repoes_fetched = set((hashable_dict(repo) for repo in fetch_and_filter_repositories( org_name=org_name, team_name=team_name, graphql_client=github_api_client, ))) if verbosity_level >= 2: click.echo( f"\t\t{len(repoes_fetched)} found for {team_name} in {org_name}!", err=True) global_results[org_name] |= repoes_fetched global_results[team_name] |= repoes_fetched if verbosity_level >= 1: click.echo( f"{len(global_results[org_name])} repositories found in {org_name}!", err=True) # click.echo(json.dumps(global_results, indent=2, default=serialize_sets), err=True) if make_org_folders or make_team_files: # Save to files if requested! write_markdown_files( repoes_dataset=global_results, make_org_folders=make_org_folders, make_team_files=make_team_files, organizations=organizations, teams=teams, ) if tee_output is False: # Job done! No output to stdout return # click.echo(f"{len(org_results)}", err=True) if all(len(global_results[org_name]) == 0 for org_name in organizations): click.echo( f"No repositories found for teams {teams} in any of orgs {organizations}!", err=True) click.exit(1) output_results = list() for org_name in organizations: output_results += global_results[org_name] # click.echo(json.dumps(output_results, indent=2, default=serialize_sets), err=True) # Tabulate and write output # assert(len(list(output_results)) > 0) markdown_output = generate_markdown_template( repositories=output_results, orgs=organizations, teams=teams, ) click.echo(markdown_output)
def migrate(ctx, chain_name): """ Run project migrations """ project = ctx.obj['PROJECT'] if not project.migrations: raise click.ClickException(( "The project does not appear to have any migrations. You can use " "the `populus makemigration` command to generate project migrations" )) # Validate the project migrations validate_migration_classes(project.migrations) # Determine which chain should be used. if not chain_name: chain_name = select_chain(project) chain_config = project.config.chains[chain_name] # Determine if the chain is *migratable* if 'registrar' not in chain_config: # TODO: this should be a property of the chain object itself. # Something like `chain.is_ready_for_migrations`. if chain_name not in {'testrpc', 'temp'}: # ignore `testrpc` and `temp` because they lazily create their # registrar contracts. # TODO: We can present the use with the option to just initialize # the chain right here rather than throwing an error. initialize_chain_prompt = ( "The chain '{0}' is not configured with a registrar contract " "address. Would you like to deploy one now?".format( chain_name, ) ) if click.confirm(initialize_chain_prompt, default=True): from .chain_cmd import chain_init ctx.invoke(chain_init, chain_name=chain_name) else: no_registrar_message = ( "In order to use the migrations functionality, a registrar " "contract is required. You can initialize this chain with a " "registrar using the command `$ populus chain init " "{name}`".format(name=chain_name) ) click.echo(no_registrar_message, err=True) click.exit(1) with project.get_chain(chain_name) as chain: if chain_name in {'mainnet', 'morden'}: show_chain_sync_progress(chain) account = get_unlocked_deploy_from_address(chain) chain.web3.eth.defaultAccount = account # Wait for chain sync if this is a public network. if chain_name in {'mainnet', 'morden'}: show_chain_sync_progress(chain) # Determine if we have any migrations to run. migrations_to_execute = get_migration_classes_for_execution( project.migrations, chain, ) if not migrations_to_execute: click.echo("All migrations have been run.") ctx.exit(0) click.echo("Migration operations to perform:") for migration in migrations_to_execute: click.echo(''.join(( " ", migration.migration_id, " ({0} operations)".format(len(migration.operations)), ":", ))) for operation_index, operation in enumerate(migration.operations): click.echo(''.join(( " ", str(operation_index), " - ", str(operation), ))) click.echo("Executing migrations:") for migration in migrations_to_execute: click.echo(''.join(( " ", migration.migration_id, "... ", )), nl=False) migration.execute() click.echo(" DONE")
def files(obj, commit='HEAD', skip_merge_commits=False): """Check the files of the commits.""" from ..kwalitee import check_file, SUPPORTED_FILES from ..hooks import run options = obj.options repository = obj.repository if options.get('colors') is not False: colorama.init(autoreset=True) reset = colorama.Style.RESET_ALL yellow = colorama.Fore.YELLOW green = colorama.Fore.GREEN red = colorama.Fore.RED else: reset = yellow = green = red = '' try: sha = 'oid' commits = _pygit2_commits(commit, repository) except ImportError: try: sha = 'hexsha' commits = _git_commits(commit, repository) except ImportError: click.echo( 'To use this feature, please install pygit2. GitPython will ' 'also work but is not recommended (python <= 2.7 only).', file=sys.stderr) click.exit(2) template = '{0}commit {{commit.{1}}}{2}\n\n'.format(yellow, sha, reset) template += '{message}{errors}\n' error_template = '\n{0}{{filename}}\n{1}{{errors}}{0}'.format(reset, red) no_errors = ['\n{0}Everything is OK.{1}'.format(green, reset)] msg_file_excluded = '\n{0}{{filename}} excluded.{1}'.format(yellow, reset) def _get_files_modified(commit): """Get the list of modified files that are Python or Jinja2.""" cmd = "git show --no-commit-id --name-only --diff-filter=ACMRTUXB {0}" _, files_modified, _ = run(cmd.format(commit)) extensions = [re.escape(ext) for ext in list(SUPPORTED_FILES) + [".rst"]] test = "(?:{0})$".format("|".join(extensions)) return list(filter(lambda f: re.search(test, f), files_modified)) def _ensure_directory(filename): dir_ = os.path.dirname(filename) if not os.path.exists(dir_): os.makedirs(dir_) def _format_errors(args): filename, errors = args if errors is None: return msg_file_excluded.format(filename=filename) else: return error_template.format(filename=filename, errors='\n'.join( errors if len(errors) else no_errors)) count = 0 ident = ' ' re_line = re.compile('^', re.MULTILINE) for commit in commits: if skip_merge_commits and _is_merge_commit(commit): continue message = commit.message commit_sha = getattr(commit, sha) tmpdir = mkdtemp() errors = {} try: for filename in _get_files_modified(commit): cmd = "git show {commit_sha}:{filename}" _, out, _ = run(cmd.format(commit_sha=commit_sha, filename=filename), raw_output=True) destination = os.path.join(tmpdir, filename) _ensure_directory(destination) with open(destination, 'w+') as f: f.write(out) errors[filename] = check_file(destination, **options) finally: shutil.rmtree(tmpdir, ignore_errors=True) message = re.sub(re_line, ident, message) if len(errors): count += 1 errors = map(_format_errors, errors.items()) else: errors = no_errors click.echo(template.format(commit=commit, message=message.encode('utf-8'), errors='\n'.join(errors))) if min(count, 1): raise click.Abort
) @click.argument('value', type=click.INT) def pool_deposit(async, address, value): """ Deposit an amount in wei into your caller pool bond balance. """ Alarm = get_contract('Scheduler') alarm = Alarm(address, rpc_client) pool_manager = PoolManager(alarm) msg = ( "Do you want to deposit {0} into the bond balance for the CallerPool " "contract at `{1}` for the address `{2}`" ).format(value, alarm._meta.address, pool_manager.coinbase) if click.confirm(msg): txn_hash = alarm.depositBond.sendTransaction(value=value) else: click.echo("Deposit cancelled") click.exit(1) if async: rpc_client.wait_for_transaction(txn_hash) click.echo("Deposit of {0} completed with txn: {1}. Balance is now {2}".format(value, txn_hash, pool_manager.bond_balance)) else: click.echo("Deposit of {0} initiated with txn: {1}".format(value, txn_hash)) if __name__ == '__main__': main()
""" Deposit an amount in wei into your caller pool bond balance. """ Alarm = get_contract('Alarm') alarm = Alarm(address, rpc_client) pool_manager = PoolManager(alarm) msg = ( "Do you want to deposit {0} into the bond balance for the CallerPool " "contract at `{1}` for the address `{2}`").format( value, alarm._meta.address, pool_manager.coinbase) if click.confirm(msg): txn_hash = alarm.depositBond.sendTransaction(value=value) else: click.echo("Deposit cancelled") click.exit(1) if async: wait_for_transaction(rpc_client, txn_hash) click.echo( "Deposit of {0} completed with txn: {1}. Balance is now {2}". format(value, txn_hash, pool_manager.bond_balance)) else: click.echo("Deposit of {0} initiated with txn: {1}".format( value, txn_hash)) if __name__ == '__main__': main()
def start(ctx, broker, publish, usb, vibration, weather, cosmics, command_socket): """Start the acquisition process.""" debug = ctx.obj.get('DEBUG', False) events = set() if weather: events.add('temperature') if vibration: events.add('vibration') if cosmics: events.add('event') try: publisher = EventPublisher(broker) except Exception: logger.error('cosmicpi: could not connect to broker "{0}"'.format( broker )) click.exit(1) try: # usb_handler = UsbHandler(usb, 9600, 60) # usb_handler.open() class Foo(object): def readline(self): return '' def write(self, *args, **kwargs): pass def close(self, *args, **kwargs): pass usb_handler = Foo() except Exception: logger.error('cosmicpi: could not connect to USB "{0}"'.format( usb )) click.exit(1) detector = Detector(usb_handler, publisher, debug, events=events) handlers = ( detector, CommandHandler(detector, usb_handler, command_socket), ) try: threads = [threading.Thread(target=target) for target in handlers] for thread in threads: thread.daemon = True thread.start() while True: time.sleep(1) except Exception: logger.exception('cosmicpi: unexpected exception') finally: detector.stop() threads[0].join() # FIXME gracefully quit command handler # for index, thread in enumerate(threads): # handlers[index].stop() # thread.join() time.sleep(1) usb_handler.close() publisher.close()
def cli( metadata_file, metadata_url, operating_system, rootfs, resolvers, max_attempts, verbose, quiet, ): level = logging.WARNING if verbose: valid_levels = [logging.INFO, logging.DEBUG] try: level = valid_levels[verbose - 1] # Ignore urllib3 DEBUG messages urllib3_log = logging.getLogger("urllib3") urllib3_log.setLevel(logging.WARNING) except IndexError: # If 3 or more 'v', enable debug, and don't ignore urllib3 level = logging.DEBUG if not quiet: logging.basicConfig(level=level) if not (metadata_file or metadata_url): if not quiet: print("--metadata-file or --metadata-url must be specified", file=sys.stderr) click.exit(10) if metadata_file and metadata_url: log.debug( "Metadata file '{}' specified, preferring over metadata url.". format(metadata_file.name)) attempt = 1 while True: try: try_run( metadata_file, metadata_url, operating_system, rootfs, resolvers, verbose, quiet, ) break except Exception as exc: # If a metadata file has been passed, retrying won't result in a # different outcome. if metadata_file: raise if attempt == max(max_attempts, 1): raise attempt += 1 delay = 2**min(attempt, 7) log.error( "Caught unexpected exception ('{}'), retrying in {} seconds..." .format(exc, delay)) time.sleep(delay)
def exit(): click.abort() click.close() click.exit(0)