From 28e681689fdbed84535c8e5826cf41cf29bdc128 Mon Sep 17 00:00:00 2001 From: DaizyModi <54097382+DaizyModi@users.noreply.github.com> Date: Fri, 18 Feb 2022 17:34:39 +0530 Subject: [PATCH] feat: Setup Requirements for specific apps (#1265) * feat: flag for Setup Requirements * chore: Moved __get_installed_app method in Bench * fix: improve help message * fix: allow muliple apps to be specified Co-authored-by: Sagar Vora Co-authored-by: gavin --- bench/app.py | 8 ++++++-- bench/bench.py | 38 ++++++++++++++++++++------------------ bench/commands/setup.py | 18 ++++++++++++------ bench/utils/bench.py | 22 ++++++++++++++-------- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/bench/app.py b/bench/app.py index 0dc2fba7..259b6429 100755 --- a/bench/app.py +++ b/bench/app.py @@ -173,7 +173,7 @@ class App(AppMeta): shutil.move(active_app_path, archived_app_path) @step(title="Installing App {repo}", success="App {repo} Installed") - def install(self, skip_assets=False, verbose=False): + def install(self, skip_assets=False, verbose=False, restart_bench=True): import bench.cli from bench.utils.app import get_app_name @@ -190,7 +190,11 @@ class App(AppMeta): ) install_app( - app=app_name, bench_path=self.bench.name, verbose=verbose, skip_assets=skip_assets, + app=app_name, + bench_path=self.bench.name, + verbose=verbose, + skip_assets=skip_assets, + restart_bench=restart_bench ) @step(title="Uninstalling App {repo}", success="App {repo} Uninstalled") diff --git a/bench/bench.py b/bench/bench.py index db04fd34..9cce57b4 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -143,6 +143,14 @@ class Bench(Base, Validator): if systemd and conf.get("restart_systemd_on_update"): restart_systemd_processes(bench_path=self.name, web_workers=web) + def get_installed_apps(self) -> List: + """Returns list of installed apps on bench, not in excluded_apps.txt + """ + apps = [app for app in self.apps if app not in self.excluded_apps] + apps.remove("frappe") + apps.insert(0, "frappe") + return apps + class BenchApps(MutableSequence): def __init__(self, bench: Bench): @@ -317,35 +325,29 @@ class BenchSetup(Base): logger.log("backups were set up") - def __get_installed_apps(self) -> List: - """Returns list of installed apps on bench, not in excluded_apps.txt - """ - apps = [app for app in self.bench.apps if app not in self.bench.excluded_apps] - apps.remove("frappe") - apps.insert(0, "frappe") - return apps - @job(title="Setting Up Bench Dependencies", success="Bench Dependencies Set Up") - def requirements(self): - """Install and upgrade all installed apps on given Bench + def requirements(self, apps=None): + """Install and upgrade all installed apps on given Bench if apps not specified """ from bench.app import App - apps = self.__get_installed_apps() + if not apps: + apps = self.bench.get_installed_apps() self.pip() print(f"Installing {len(apps)} applications...") for app in apps: - App(app, bench=self.bench, to_clone=False).install() + App(app, bench=self.bench, to_clone=False).install( skip_assets=True, restart_bench=False) - def python(self): - """Install and upgrade Python dependencies for installed apps on given Bench + def python(self, apps=None): + """Install and upgrade Python dependencies for installed apps on given Bench if app not specified """ import bench.cli - apps = self.__get_installed_apps() + if not apps: + apps = self.bench.get_installed_apps() quiet_flag = "" if bench.cli.verbose else "--quiet" @@ -356,12 +358,12 @@ class BenchSetup(Base): log(f"\nInstalling python dependencies for {app}", level=3, no_log=True) self.run(f"{self.bench.python} -m pip install {quiet_flag} --upgrade -e {app_path}") - def node(self): - """Install and upgrade Node dependencies for all apps on given Bench + def node(self, apps=None): + """Install and upgrade Node dependencies for all apps on given Bench if app not specified """ from bench.utils.bench import update_node_packages - return update_node_packages(bench_path=self.bench.name) + return update_node_packages(bench_path=self.bench.name, apps=apps) class BenchTearDown: diff --git a/bench/commands/setup.py b/bench/commands/setup.py index 006eba74..b8d4bee3 100755 --- a/bench/commands/setup.py +++ b/bench/commands/setup.py @@ -131,27 +131,33 @@ def setup_procfile(): def setup_socketio(): return -@click.command("requirements", help="Setup Python and Node dependencies") +@click.command("requirements") @click.option("--node", help="Update only Node packages", default=False, is_flag=True) @click.option("--python", help="Update only Python packages", default=False, is_flag=True) @click.option("--dev", help="Install optional python development dependencies", default=False, is_flag=True) -def setup_requirements(node=False, python=False, dev=False): +@click.argument("apps", nargs=-1) +def setup_requirements(node=False, python=False, dev=False, apps=None): + """ + Setup Python and Node dependencies. + + You can optionally specify one or more apps to specify dependencies for. + """ from bench.bench import Bench bench = Bench(".") if not (node or python or dev): - bench.setup.requirements() + bench.setup.requirements(apps=apps) elif not node and not dev: - bench.setup.python() + bench.setup.python(apps=apps) elif not python and not dev: - bench.setup.node() + bench.setup.node(apps=apps) else: from bench.utils.bench import install_python_dev_dependencies - install_python_dev_dependencies() + install_python_dev_dependencies(apps=apps) if node: click.secho("--dev flag only supports python dependencies. All node development dependencies are installed by default.", fg="yellow") diff --git a/bench/utils/bench.py b/bench/utils/bench.py index aba4b943..82a63bef 100644 --- a/bench/utils/bench.py +++ b/bench/utils/bench.py @@ -48,7 +48,7 @@ def get_venv_path(): return venv or log("virtualenv cannot be found", level=2) -def update_node_packages(bench_path="."): +def update_node_packages(bench_path=".", apps=None): print("Updating node packages...") from bench.utils.app import get_develop_version from distutils.version import LooseVersion @@ -58,9 +58,9 @@ def update_node_packages(bench_path="."): # After rollup was merged, frappe_version = 10.1 # if develop_verion is 11 and up, only then install yarn if v < LooseVersion("11.x.x-develop"): - update_npm_packages(bench_path) + update_npm_packages(bench_path, apps=apps) else: - update_yarn_packages(bench_path) + update_yarn_packages(bench_path, apps=apps) def install_python_dev_dependencies(bench_path=".", apps=None, verbose=False): @@ -75,7 +75,7 @@ def install_python_dev_dependencies(bench_path=".", apps=None, verbose=False): if isinstance(apps, str): apps = [apps] elif apps is None: - apps = [app for app in bench.apps if app not in bench.excluded_apps] + apps = bench.get_installed_apps() for app in apps: app_path = os.path.join(bench_path, "apps", app) @@ -86,11 +86,14 @@ def install_python_dev_dependencies(bench_path=".", apps=None, verbose=False): bench.run(f"{bench.python} -m pip install {quiet_flag} --upgrade -r {dev_requirements_path}") -def update_yarn_packages(bench_path="."): +def update_yarn_packages(bench_path=".", apps=None): from bench.bench import Bench bench = Bench(bench_path) - apps = [app for app in bench.apps if app not in bench.excluded_apps] + + if not apps: + apps = bench.get_installed_apps() + apps_dir = os.path.join(bench.name, "apps") # TODO: Check for stuff like this early on only?? @@ -106,11 +109,14 @@ def update_yarn_packages(bench_path="."): bench.run("yarn install", cwd=app_path) -def update_npm_packages(bench_path="."): +def update_npm_packages(bench_path=".", apps=None): apps_dir = os.path.join(bench_path, "apps") package_json = {} - for app in os.listdir(apps_dir): + if not apps: + apps = os.listdir(apps_dir) + + for app in apps: package_json_path = os.path.join(apps_dir, app, "package.json") if os.path.exists(package_json_path):