Skip to content

Commit

Permalink
Add log-level option (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashaag authored Jun 13, 2022
1 parent 4aceecb commit d77ce72
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
test_enviroment_file_and_enviroment_name:
name: Test options
name: Test environment-file, environment-name, extra-specs, log-level
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -31,6 +31,7 @@ jobs:
extra-specs: |
pytest=${{ matrix.pytest }}
python=3.8.8
log-level: debug

- name: run python in powershell
shell: powershell
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ The environment.yml or .lock file for the conda environment. If 'false', no envi

(Optional) Custom environment cache key used with 'cache-env: true'. With the default environment cache key, separate caches will be created for each operating system (eg., Linux) and platform (eg., x64) and day (eg., 2022-01-31), and the cache will be invalidated whenever the contents of 'environment-file' or 'extra-specs' change.

### `log-level`

(Optional) Micromamba log level to use. One of "trace", "debug", "info", "warning", "error", "critical", "off".

<!-- end generated -->

## Example usage
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ inputs:
# If 'true', run 'micromamba update' even if the environment was restored from cache
# to make sure your environment is always up-to-date.
# default: false
log-level:
description: >-
Micromamba log level to use.
One of "trace", "debug", "info", "warning", "error", "critical", "off".
default: info

runs:
using: "node16"
Expand Down
41 changes: 24 additions & 17 deletions dist/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 24 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function executePwsh (command) {
return executeShell('powershell', '-command', `${command}; exit $LASTEXITCODE`)
}

function micromambaCmd (command, logLevel, micromambaExe = 'micromamba') {
return `${micromambaExe} ${command}` + (logLevel ? ` --log-level ${logLevel}` : '')
}

function sha256 (s) {
const h = crypto.createHash('sha256')
h.update(s)
Expand Down Expand Up @@ -117,7 +121,7 @@ async function retry (callback, backoffTimes = [2000, 5000, 10000]) {
}
}

async function installMicromambaPosix (micromambaUrl) {
async function installMicromambaPosix (micromambaUrl, logLevel) {
const posixDownloader = `curl ${micromambaUrl} -Ls --retry 5 --retry-delay 1 \
| tar --strip-components=1 -vxjC ${PATHS.micromambaBinFolder} bin/micromamba`
const cacheKey = `micromamba-bin ${micromambaUrl} ${today()}`
Expand All @@ -131,10 +135,10 @@ async function installMicromambaPosix (micromambaUrl) {
await executeBash(`chmod u+x ${PATHS.micromambaExe}`)
if (MAMBA_PLATFORM === 'osx') {
// macos
await executeBash(`${PATHS.micromambaExe} shell init -s bash -p ~/micromamba -y`)
await executeBash(micromambaCmd('shell init -s bash -p ~/micromamba -y', logLevel, PATHS.micromambaExe))
// TODO need to fix a check in micromamba so that this works
// https://github.com/mamba-org/mamba/issues/925
// await executeBash(`${micromambaExe} shell init -s zsh -p ~/micromamba -y`)
// await executeBash(micromambaCmd('shell init -s zsh -p ~/micromamba -y', logLevel, PATHS.micromambaExe))
} else {
// linux
// on linux we move the bashrc to a backup and then restore
Expand All @@ -145,8 +149,8 @@ async function installMicromambaPosix (micromambaUrl) {
}
touch(PATHS.bashrc)
try {
await executeBash(`${PATHS.micromambaExe} shell init -s bash -p ~/micromamba -y`)
await executeBash(`${PATHS.micromambaExe} shell init -s zsh -p ~/micromamba -y`)
await executeBash(micromambaCmd('shell init -s bash -p ~/micromamba -y', logLevel, PATHS.micromambaExe))
await executeBash(micromambaCmd('shell init -s zsh -p ~/micromamba -y', logLevel, PATHS.micromambaExe))
fs.appendFileSync(PATHS.bashprofile, '\n' + fs.readFileSync(PATHS.bashrc, 'utf8'), 'utf8')
if (haveBashrcBackup) {
fs.renameSync(PATHS.bashrcBak, PATHS.bashrc)
Expand All @@ -160,7 +164,7 @@ async function installMicromambaPosix (micromambaUrl) {
}
}

async function installMicromambaWindows (micromambaUrl) {
async function installMicromambaWindows (micromambaUrl, logLevel) {
const powershellDownloader = `$count = 0
do{
try
Expand Down Expand Up @@ -188,12 +192,12 @@ if(-not($success)){exit}`
saveCacheOnPost(...cacheArgs)
}

await executePwsh(`${PATHS.micromambaExe} shell init -s powershell -p $HOME\\micromamba`)
await executePwsh(micromambaCmd('shell init -s powershell -p $HOME\\micromamba', logLevel, PATHS.micromambaExe))
await executePwsh(
'$env:Path = (get-item (get-command git).Path).Directory.parent.FullName + "\\usr\\bin;" + $env:Path;' +
`${PATHS.micromambaExe} shell init -s bash -p ~\\micromamba -y`
micromambaCmd('shell init -s bash -p ~\\micromamba -y', logLevel, PATHS.micromambaExe)
)
await executePwsh(`${PATHS.micromambaExe} shell init -s cmd.exe -p ~\\micromamba -y`)
await executePwsh(micromambaCmd('shell init -s cmd.exe -p ~\\micromamba -y', logLevel, PATHS.micromambaExe))
}

async function installMicromamba (inputs, extraChannels) {
Expand Down Expand Up @@ -223,7 +227,7 @@ channel_priority: strict
osx: installMicromambaPosix
}[MAMBA_PLATFORM]
const micromambaUrl = MICROMAMBA_PLATFORM_URL + inputs.micromambaVersion
await installer(micromambaUrl)
await installer(micromambaUrl, inputs.logLevel)
core.exportVariable('MAMBA_ROOT_PREFIX', PATHS.micromambaRoot)
core.exportVariable('MAMBA_EXE', PATHS.micromambaExe)
core.addPath(PATHS.micromambaBinFolder)
Expand Down Expand Up @@ -252,12 +256,12 @@ function selectSelectors (extraSpecs) {
return ret
}

async function createOrUpdateEnv (envName, envFilePath, extraSpecs) {
async function createOrUpdateEnv (envName, envFilePath, extraSpecs, logLevel) {
const envFolder = path.join(PATHS.micromambaEnvs, envName)
const action = fs.existsSync(envFolder) ? 'update' : 'create'
const selectedExtraSpecs = selectSelectors(extraSpecs)
core.info(`${action} env ${envName}`)
let cmd = `micromamba ${action} -n ${envName} --strict-channel-priority -y`
let cmd = micromambaCmd(`${action} -n ${envName} --strict-channel-priority -y`, logLevel)
if (selectedExtraSpecs.length) {
cmd += ' ' + selectedExtraSpecs.map(e => `"${e}"`).join(' ')
}
Expand Down Expand Up @@ -334,7 +338,7 @@ async function installEnvironment (inputs, envFilePath, envYaml) {
downloadCacheArgs = [PATHS.micromambaPkgs, `micromamba-pkgs ${key}`]
downloadCacheHit = await tryRestoreCache(...downloadCacheArgs)
}
await createOrUpdateEnv(envName, envFilePath, inputs.extraSpecs)
await createOrUpdateEnv(envName, envFilePath, inputs.extraSpecs, inputs.logLevel)
}

// Add micromamba activate to profile
Expand Down Expand Up @@ -382,7 +386,8 @@ async function main () {
cacheEnvKey: core.getInput('cache-env-key'),
// Not implemented
// cacheEnvAlwaysUpdate: core.getBooleanInput('cache-env-always-update')
cacheEnvAlwaysUpdate: false
cacheEnvAlwaysUpdate: false,
logLevel: core.getInput('log-level')
}

// Read environment file
Expand All @@ -399,11 +404,13 @@ async function main () {

// Show environment info
core.startGroup('Environment info')
const infoCmd = micromambaCmd('info', inputs.logLevel)
const listCmd = micromambaCmd('list', inputs.logLevel)
if (MAMBA_PLATFORM === 'win') {
await executePwsh('micromamba info')
await executePwsh('micromamba list')
await executePwsh(infoCmd)
await executePwsh(listCmd)
} else {
await executeBash(`source ${PATHS.bashprofile} && micromamba info && micromamba list`)
await executeBash(`source ${PATHS.bashprofile} && ${infoCmd} && ${listCmd}`)
}
core.endGroup()

Expand Down

0 comments on commit d77ce72

Please sign in to comment.