Skip to content

Commit

Permalink
Merge pull request #1733 from emlys/task/1713
Browse files Browse the repository at this point in the history
Plugin install: use micromamba to make sure git is available
  • Loading branch information
dcdenu4 authored Jan 27, 2025
2 parents 595684b + cfa5f15 commit 2b38221
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions workbench/src/main/setupAddRemovePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,26 @@ export function setupAddPlugin() {
try {
logger.info('adding plugin at', pluginURL);
const micromamba = settingsStore.get('micromamba');
const rootPrefix = upath.join(process.resourcesPath, 'micromamba_envs');
const baseEnvPrefix = upath.join(rootPrefix, 'invest_base');
// Create invest_base environment, if it doesn't already exist
// The purpose of this environment is just to ensure that git is available
if (!fs.existsSync(baseEnvPrefix)) {
await spawnWithLogging(
micromamba,
['create', '--yes', '--prefix', `"${baseEnvPrefix}"`, '-c', 'conda-forge', 'git']
);
}
// Create a temporary directory and check out the plugin's pyproject.toml
const tmpPluginDir = fs.mkdtempSync(upath.join(tmpdir(), 'natcap-invest-'));
await spawnWithLogging(
'git',
['clone', '--depth', '1', '--no-checkout', pluginURL, tmpPluginDir]
micromamba,
['run', '--prefix', `"${baseEnvPrefix}"`,
'git', 'clone', '--depth', '1', '--no-checkout', pluginURL, tmpPluginDir]
);
await spawnWithLogging(
'git',
['checkout', 'HEAD', 'pyproject.toml'],
micromamba,
['run', '--prefix', `"${baseEnvPrefix}"`, 'git', 'checkout', 'HEAD', 'pyproject.toml'],
{ cwd: tmpPluginDir }
);
// Read in the plugin's pyproject.toml, then delete it
Expand All @@ -77,25 +88,25 @@ export function setupAddPlugin() {
const pluginID = pyprojectTOML.tool.natcap.invest.model_id;
const pluginName = pyprojectTOML.tool.natcap.invest.model_name;
const pluginPyName = pyprojectTOML.tool.natcap.invest.pyname;
const condaDeps = pyprojectTOML.tool.natcap.invest.conda_dependencies;

// Create a conda env containing the plugin and its dependencies
const envName = `invest_plugin_${pluginID}`;
await spawnWithLogging(
micromamba,
['create', '--yes', '--name', envName, '-c', 'conda-forge', '"python<3.12"', '"gdal<3.6"']
);
const pluginEnvPrefix = upath.join(rootPrefix, envName);
const createCommand = [
'create', '--yes', '--prefix', `"${pluginEnvPrefix}"`,
'-c', 'conda-forge', 'python'];
if (condaDeps) { // include dependencies read from pyproject.toml
condaDeps.forEach((dep) => createCommand.push(`"${dep}"`));
}
await spawnWithLogging(micromamba, createCommand);
logger.info('created micromamba env for plugin');
await spawnWithLogging(
micromamba,
['run', '--name', envName, 'pip', 'install', `git+${pluginURL}`]
['run', '--prefix', `"${pluginEnvPrefix}"`, 'pip', 'install', `git+${pluginURL}`]
);
logger.info('installed plugin into its env');
// Write plugin metadata to the workbench's config.json
const envInfo = execSync(`${micromamba} info --name ${envName}`, { windowsHide: true }).toString();
logger.info(`env info:\n${envInfo}`);
const regex = /env location : (.+)/;
const envPath = envInfo.match(regex)[1];
logger.info(`env path:\n${envPath}`);
logger.info('writing plugin info to settings store');
settingsStore.set(
`plugins.${pluginID}`,
Expand All @@ -104,7 +115,7 @@ export function setupAddPlugin() {
pyname: pluginPyName,
type: 'plugin',
source: pluginURL,
env: envPath,
env: pluginEnvPrefix,
}
);
logger.info('successfully added plugin');
Expand Down

0 comments on commit 2b38221

Please sign in to comment.