def external_command(**kwargs): from click_project.lib import call config.merge_settings() args = ([command_path] + get_settings_for_path("parameters", path)) env = {(config.main_command.path + "___" + key).upper(): (str(value) if value else "") for key, value in kwargs.items()} ctx = click.get_current_context() env[(config.main_command.path + "___PATH").upper()] = (ctx.command_path.replace(" ", "_").upper()) while ctx: env.update({ (ctx.command_path.replace(" ", "_") + "__" + key).upper(): ((" ".join(map(quote, value)) if type(value) is tuple else str(value) if value else "")) for key, value in ctx.params.items() }) ctx = ctx.parent env[(config.main_command.path + "___CMD_OPTIND").upper()] = (str( len(config.command_line_settings["parameters"][path]))) env[(config.main_command.path + "___CMD_ARGS").upper()] = ( " ".join( quote(a) for a in config.command_line_settings["parameters"][path])) env[(config.main_command.path + "___OPTIND").upper()] = (str( len(args[1:]))) env[(config.main_command.path + "___ARGS").upper()] = (" ".join( quote(a) for a in args[1:])) with updated_env(**env): call(args)
def push(remote, name, force): """Send the file to the given remote""" args = [git(), "push", remote] if force: args.append("--force") for ref_ in name: call(args + [ref_])
def docker_compose(args, internal=False): with updated_env(**extra_env()): call( ["docker-compose"] + (extra_options() if callable(extra_options) else extra_options) + args, internal=internal, cwd=abs_directory())
def tesseract(input, language, remove_after): """Extract the text from a pdf and create a searchable pdf""" ocr_base = os.path.splitext(input)[0] + "_ocr" with temporary_file(suffix=".tiff") as tiff: call(split(f"convert -density 300 '{input}' -depth 8 '{tiff.name}'")) call(split(f"tesseract '{tiff.name}' '{ocr_base}' -l '{language}' pdf")) if remove_after: rm(input)
def cat(name): """Display the file content If it is a file, show its content. If it is a directory, show its files listing. """ call([git(), "cat-file", "-p", name])
def fetch(remote, name): """Fetch those files from the remote Once fetched, you can cat or restore them """ for ref_ in name: call([git(), "fetch", remote, "{}:{}".format(ref_, ref_)])
def remove(remote, name): """Remove the file The file is removed locally. If --from is used, the file is removed from this remote instead (i.e. without removing locally) """ call([git()] + (["push", "-d", remote] if remote else ["update-ref", "-d"]) + [name]) LOGGER.status("Removed {}".format(name))
def exec_(launcher_command, launcher, shell, command, stdout, stderr): """Run a command in the CoSMo environment""" if launcher: launcher_command = config.settings2["launchers"][launcher] if shell: command = [ ' '.join([command[0]] + [double_quote(arg) for arg in command[1:]]) ] out = open(stdout, "wb") if stdout else None err = open(stderr, "wb") if stderr else None call(command, shell=shell, stdout=out, stderr=err, launcher_command=launcher_command)
def external_command(**kwargs): from click_project.lib import call ctx = click.get_current_context() config.merge_settings() args = ([command_path] + list(ctx.params.get("args", []))) def value_to_string(value): return (" ".join(map(quote, value)) if type(value) is tuple else str(value) if value else "") env = {("CLK___" + key).upper(): (value_to_string(value)) for key, value in kwargs.items()} env[("CLK___PATH").upper()] = (ctx.command_path.replace( " ", "_").upper()) if "args" in ctx.params: env[("CLK___ARGS").upper()] = " ".join( map(quote, ctx.params["args"])) while ctx: env.update({ (ctx.command_path.replace(" ", "_") + "__" + key).upper(): (value_to_string(value)) for key, value in ctx.params.items() }) ctx = ctx.parent for path, parameters in config.get_settings2("parameters").items(): env[(f"CLK_P_" + path.replace("-", "__").replace( ".", "_")).upper()] = " ".join(map(quote, parameters)) env[("CLK___CMD_OPTIND").upper()] = (str( len( config.commandline_profile.get_settings("parameters") [path]))) env[("CLK___CMD_ARGS").upper()] = (" ".join( quote(a) for a in config.commandline_profile.get_settings( "parameters")[path])) env[("CLK___OPTIND").upper()] = (str(len(args[1:]))) env[("CLK___ALL").upper()] = (" ".join(quote(a) for a in args[1:])) with updated_env(**env): call( args, internal=True, )
def _save(name, file, force, create_hash_file=False): full = os.path.abspath(file) hash_file = full + ".hash" dir = os.path.dirname(full) file = os.path.basename(full) tl = toplevel() prefixfile = os.path.relpath(full, tl) if os.path.isdir(full): call([git(), "add", "-f", "--", file], cwd=dir) blob = check_output([git(), "write-tree", "--prefix", prefixfile]).strip() check_output([git(), "reset", "--", file], nostderr=True) else: blob = check_output([git(), "hash-object", "-w", "--", full]).strip() name = name or (file + "-" + blob[:8]) name = name.replace(".", "_").replace(" ", "_") if create_hash_file: createfile(hash_file, name) refs_dict = get_refs_dict() if not force and name in refs_dict: other_blob = refs_dict[name] if blob == other_blob: LOGGER.warning("The git name {} already exists" " and is associated to the same content".format( name, blob, )) return name else: raise click.UsageError( "The git name {} already exists with hash {}." " You are willing to associate it with the hash {}." " Either choose another name or use --force" " if you know what you are doing".format( name, other_blob, blob, )) ref = "refs/git-store/{}".format(name) check_output([git(), "update-ref", ref, blob]) return name
def svn_sync(revision, username, password, url, auth_cache, interactive, trust_server_cert, directory, args): """Retrieve and/or update a svn repository""" directory = directory or re.split('[:/]', url)[-1] args = list(args) if username is not None: args += ['--username', username] if password is not None: args += ['--password', password] if not auth_cache: args += ['--no-auth-cache'] if not interactive: args += ['--non-interactive'] if trust_server_cert: args += ['--trust-server-cert'] if os.path.exists(directory): with cd(directory): call(['svn', 'up', '--revision', revision] + args, env=config.old_env) else: call(['svn', 'checkout', '--revision', revision] + args + [url, directory], env=config.old_env)
def restore(file, name): """Put the content of name into file By default, put the content of name on a file/directory with the same name. You can provide a different location with --file """ file = file or name[len("refs/git-store/"):] file = os.path.abspath(file) tl = toplevel() prefixfile = os.path.relpath(file, tl) if check_output([git(), "cat-file", "-t", name]).strip() == "tree": call([git(), "read-tree", name, "--prefix", prefixfile]) call([git(), "checkout", "--", file]) check_output([git(), "reset", "--", file]) else: call([git(), "cat-file", "blob", name], stdout=open(file, "w")) LOGGER.info("Restored git name {} in location {}".format( name[len("refs/git-store/"):], prefixfile))
def pip(command, args): return call([command] + args)
def _fetch(remote): """Fetch all the notes from the git remote""" call(["git", "fetch", remote, "{}:{}".format(note_ref(), note_ref())])
def _push(remote): """Send all the notes to the git remote""" call(["git", "push", remote, "{}:{}".format(note_ref(), note_ref())])
def _show(name): """Show the note associated to the file""" call(notes_command() + ["show", name])
def _remove(name): """Remove the note associated to the file""" call(notes_command() + ["remove", name])
def edit(name): """Edit the note of the file""" call(notes_command() + ["edit", name])
def append(message, name): """Append the message to the note associated to the file""" call(notes_command() + ["append", "-m", message, name])
def _set(message, name): """Set a message as note of the file""" call(notes_command() + ["add", "-f", "-m", message, name])
def pip(args): """Run pip in the context of this installation of click-project""" call([sys.executable, "-m", "pip"] + list(args))
def fix_up(): """Add the current user to the docker group. Calling this will result in changing /etc/group using sudo and relogging using the 'login' command""" call(['sudo', 'adduser', getpass.getuser(), 'docker']) call(['sudo', 'login'])
def open(profile, opener): """Open the directory containing the profile""" call([opener, profile.location])
def clone(profile, url, name): """Clone a recipe stored in github in the given profile""" profile = profile or config.global_profile recipe_path = Path(profile.location) / "recipes" / name call(["git", "clone", url, str(recipe_path)])