def write_version(self): if self.frozen_during_migration: raise click.UsageError( "You cannot edit the configuration if the migration is not persisted" ) makedirs(self.location) version = self.version createfile(self.version_file_name, ensure_unicode(str(version)), internal=True)
def migrate_settings_to_recipe(settings_level_file, name): if open(settings_level_file, "rb").read().decode("utf-8").strip() == "{}": return False makedirs(recipes_dir + "/" + name) enabled = not json.load(open(settings_level_file)).get("_self", {}).get("disabled", False) order = json.load(open(settings_level_file)).get("_self", {}).get("order", 100) move(settings_level_file, recipes_dir + "/" + name + "/{}.json".format(self.app_name)) createfile(recipes_dir + "/" + name + "/version.txt", str(self.version + 1)) createfile(recipes_dir + "/" + name + ".json", json.dumps( { "enabled": enabled, "order": order, } ) ) return 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 install_plugin(profile, force, plugin, login, password, develop, no_deps): """Install a plugin""" dest_dir = profile.pluginsdir def plugin_location(plugin): return os.path.join(dest_dir, "{}.py".format(plugin_file_name(plugin))) for _plugin in plugin: if not force and os.path.exists(plugin_location(_plugin)): raise click.UsageError( "{} already installed in profile {}." " If you really want to install it, use --force or" " run plugin uninstall --{} {}".format(_plugin, profile.name, profile.name, _plugin)) for _plugin in plugin: if is_local_plugin(_plugin): if _plugin.startswith("file://"): content = open(_plugin[len("file://"):], "rb").read().decode("utf-8") plugin_file = os.path.abspath(_plugin[len("file://"):]) else: content = open(_plugin, "rb").read().decode("utf-8") plugin_file = os.path.abspath(_plugin) plugin_name = os.path.basename(plugin_file)[:-3] plugin_dir = os.path.dirname(plugin_file) if os.path.basename(plugin_dir) != "{}_plugins".format( config.app_name): raise click.UsageError( "{} does not look like a plugin directory".format( plugin_dir)) user_name = os.path.basename(os.path.dirname(plugin_dir)) plugin_name = "{}/{}".format(user_name, plugin_name) else: content = get_plugin_from_remote(_plugin) plugin_name = _plugin def install_requirements(): require_prefix = "# require: " requires = [ line[len(require_prefix):] for line in content.splitlines() if line.startswith(require_prefix) ] for require in requires: LOGGER.status("Installing requirement {}".format(require)) args = ['--upgrade', require] if force: args.append("--force-reinstall") pip("install", args) plugin_require_prefix = "# plugin_require: " plugin_requires = [ line[len(plugin_require_prefix):] for line in content.splitlines() if line.startswith(plugin_require_prefix) ] for plugin_require in plugin_requires: LOGGER.status( "Installing plugin requirements {}".format(plugin_require)) install_plugin( profile=profile, force=force, login=login, password=password, plugin=[plugin_require], develop=False, no_deps=no_deps, ) if not no_deps: install_requirements() LOGGER.status("Installing plugin {} in profile {}".format( plugin_name, profile.name)) location = plugin_location(plugin_name) makedirs(os.path.dirname(location)) if is_local_plugin(_plugin) and develop: LOGGER.debug("Installing {} in develop mode".format(plugin_name)) if force and os.path.exists(location): rm(location) ln(plugin_file, location) else: createfile(location, content)
def fork(force, name): """Create a brand new project, based on clk that can be used by itself.""" output = Path(name) if output.exists(): if force: rm(output) else: raise click.UsageError(f"{output} already exist") makedirs(output) createfile( output / "setup.py", f"""#!/usr/bin/env python3 # -*- coding:utf-8 -*- from setuptools import setup, find_packages setup( name='{name}', version="0.0.0", packages=find_packages(), zip_safe=False, install_requires=[ "click-project", ], entry_points={{ 'console_scripts': [ '{name}={name}.main:main', ] }}, ) """) package = (output / name) makedirs(package) createfile( package / "main.py", f"""#!/usr/bin/env python3 # -*- coding:utf-8 -*- from click_project.setup import basic_entry_point, main from click_project.decorators import command, argument, flag, option @basic_entry_point( __name__, extra_command_packages=["{name}.commands"], exclude_core_commands=["git-sync"], ) def {name}(**kwargs): pass if __name__ == "__main__": main() """) createfile(package / "__init__.py", f"""#!/usr/bin/env python3""") commands = (package / "commands") makedirs(commands) (commands / "somecommand.py", """#!/usr/bin/env python3 # -*- coding:utf-8 -*- import click from click_project.decorators import command, argument, flag, option from click_project.log import get_logger LOGGER = get_logger(__name__) @command() @argument("some-argument", help="some argument") @flag("--some-flag/--no-some-flag", help="some flag") @option("--some-option", help="some option") def somecommand(some_argument, some_flag, some_option): "Some command" LOGGER.info(some_argument) LOGGER.info(some_flag) LOGGER.info(some_option) """) print(f"Now, install {name} with `python3 -m pip install {name}`," f" enable its completion with `{name} completion install`" f" and don't forget to have fun")