From 582b59478949d32c1a35b466e29fd783caeb7ca7 Mon Sep 17 00:00:00 2001 From: cmdr2 Date: Tue, 4 Oct 2022 13:48:56 +0530 Subject: [PATCH] Developer console which shows an activated mamba environment; A script to enable developer mode and auto-create the symlinks --- Open Developer Console.cmd | 11 ++++ installer/bootstrap/bootstrap.bat | 4 ++ installer/developer/enable_dev_mode.py | 85 ++++++++++++++++++++++++++ open_dev_console.sh | 9 +++ 4 files changed, 109 insertions(+) create mode 100644 Open Developer Console.cmd create mode 100644 installer/developer/enable_dev_mode.py create mode 100644 open_dev_console.sh diff --git a/Open Developer Console.cmd b/Open Developer Console.cmd new file mode 100644 index 00000000..783800cf --- /dev/null +++ b/Open Developer Console.cmd @@ -0,0 +1,11 @@ +@echo off + +set SD_BASE_DIR=%cd% +set MAMBA_ROOT_PREFIX=%SD_BASE_DIR%\env\mamba +set INSTALL_ENV_DIR=%SD_BASE_DIR%\env\installer_env + +call "%MAMBA_ROOT_PREFIX%\condabin\mamba_hook.bat" + +call micromamba activate "%INSTALL_ENV_DIR%" + +cmd /k \ No newline at end of file diff --git a/installer/bootstrap/bootstrap.bat b/installer/bootstrap/bootstrap.bat index c45df3d7..d52ef320 100644 --- a/installer/bootstrap/bootstrap.bat +++ b/installer/bootstrap/bootstrap.bat @@ -1,5 +1,9 @@ @echo off +@rem This file initializes micromamba and activates the env. +@rem A similar bootstrap file needs to exist for each platform (win, linux, macOS) +@rem Ready to hand-over to the platform-independent installer after this (written in python). + set MAMBA_ROOT_PREFIX=%SD_BASE_DIR%\env\mamba set INSTALL_ENV_DIR=%SD_BASE_DIR%\env\installer_env set INSTALLER_YAML_FILE=%SD_BASE_DIR%\installer\yaml\installer-environment.yaml diff --git a/installer/developer/enable_dev_mode.py b/installer/developer/enable_dev_mode.py new file mode 100644 index 00000000..5666c48d --- /dev/null +++ b/installer/developer/enable_dev_mode.py @@ -0,0 +1,85 @@ +import argparse +import subprocess +import sys +import json +import os +import platform +import shutil + +config_path = os.path.join('config.json') + +if not os.path.exists('installer') or not os.path.exists('ui') or not os.path.exists('engine'): + print('Error: This script needs to be run from the root of the stable-diffusion-ui folder! Please cd to the correct folder, and run this again.') + exit(1) + +parser = argparse.ArgumentParser() + +parser.add_argument( + "--symlink_dir", type=str, default=None, help="the absolute path to the project git repository (to link to)" +) +opt = parser.parse_args() + +def run(cmd): + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) + + for c in iter(lambda: p.stdout.read(1), b""): + sys.stdout.buffer.write(c) + sys.stdout.flush() + + p.wait() + + return p.returncode == 0 + +def get_config(): + if not os.path.exists(config_path): + return {} + + with open(config_path, "r") as f: + return json.load(f) + +def save_config(config): + with open(config_path, "w") as f: + json.dump(config, f) + +# set the `is_developer_mode` flag to `true` in the config +config = get_config() +config['is_developer_mode'] = True +save_config(config) + +print('set is_developer_mode=true in config.json') + +# make the symlink, if requested +if opt.symlink_dir is not None: + if not os.path.exists(opt.symlink_dir): + print(f'Symlink directory "{opt.symlink_dir}" was not found! Are you sure it has been escaped correctly?') + exit(1) + + installer_target_path = os.path.join(opt.symlink_dir, 'installer') + ui_target_path = os.path.join(opt.symlink_dir, 'ui') + engine_target_path = os.path.join(opt.symlink_dir, 'engine') + + shutil.rmtree('installer', ignore_errors=True) + shutil.rmtree('ui', ignore_errors=True) + shutil.rmtree('engine', ignore_errors=True) + + if not os.path.exists(ui_target_path) or not os.path.exists(installer_target_path) or not os.path.exists(engine_target_path): + print('The target symlink directory does not contain the required {ui, installer, engine} folders. Are you sure it is the correct git repo for the project?') + exit(1) + + cmds = [] + + if platform.system() == 'Windows': + cmds.append(f'mklink /J "installer" "{installer_target_path}"') + cmds.append(f'mklink /J "ui" "{ui_target_path}"') + cmds.append(f'mklink /J "engine" "{engine_target_path}"') + elif platform.system() in ('Linux', 'Darwin'): + cmds.append(f'ln -s "{installer_target_path}" "installer"') + cmds.append(f'ln -s "{ui_target_path}" "ui"') + cmds.append(f'ln -s "{engine_target_path}" "engine"') + + for cmd in cmds: + if not run(cmd): + print('Error while running', cmd) + exit(1) + + print(f'Created symlinks! Your installation will now automatically use the files present in the repository at {opt.symlink_dir}') diff --git a/open_dev_console.sh b/open_dev_console.sh new file mode 100644 index 00000000..c254b748 --- /dev/null +++ b/open_dev_console.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +export SD_BASE_DIR=`pwd` +export MAMBA_ROOT_PREFIX="$SD_BASE_DIR/env/mamba" +export INSTALL_ENV_DIR="$SD_BASE_DIR/env/installer_env" + +eval "$($MAMBA_ROOT_PREFIX/micromamba shell hook -s posix)" + +micromamba activate "$INSTALL_ENV_DIR"