Skip to content

Commit

Permalink
Attempt to cleanup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
MSeal committed Nov 18, 2022
1 parent dae55de commit 1cb4729
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
34 changes: 24 additions & 10 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function findNestedObj(obj, keys) {
keys.reduce((o, key) => o && typeof o[key] !== 'undefined' ? o[key] : undefined, obj);
}
// These are added run actions using "env:"
let runner = JSON.parse(process.env.RUNNER || "");
let secrets = JSON.parse(process.env.SECRETS || "");
Expand All @@ -47,7 +50,8 @@ const secretsPath = path.join(runner.temp, "secrets.json");
const papermillOutput = path.join(github.workspace, "papermill-nb-runner.out");
function run() {
return __awaiter(this, void 0, void 0, function* () {
github;
var parsedNotebookFile;
var papermillEnvs;
try {
const notebookFile = core.getInput('notebook');
const paramsFile = core.getInput('params');
Expand All @@ -62,19 +66,19 @@ function run() {
fs.writeFileSync(secretsPath, JSON.stringify(secrets));
const domain = secrets.NOTEABLE_DOMAIN;
const token = secrets.NOTEABLE_TOKEN;
var papermill_envs = {};
papermillEnvs = {};
if (typeof domain !== 'undefined') {
papermill_envs['NOTEABLE_DOMAIN'] = domain;
papermillEnvs['NOTEABLE_DOMAIN'] = domain;
}
if (typeof token !== 'undefined') {
papermill_envs['NOTEABLE_TOKEN'] = token;
papermillEnvs['NOTEABLE_TOKEN'] = token;
// TODO Fail here if undefined as the papermill command will fail later
}
const githubString = JSON.stringify(JSON.parse(process.env.GITHUB || ""));
const parsedNotebookFile = path.join(outputDir, path.basename(notebookFile));
parsedNotebookFile = path.join(outputDir, path.basename(notebookFile));
// Install dependencies
yield exec.exec('python3 -m pip install papermill-origami papermill>=2.4.0 nbformat>=5.4.0 nbconvert>=7.0.0'); // TODO: Remove these when fixed to not require in papermill origami
// TODO: Remove these when fixed to not require in papermill origami
yield exec.exec('python3 -m pip install papermill-origami papermill>=2.4.0 nbformat>=5.4.0 nbconvert>=7.0.0');
// Just in case the user want's to execute a local notebook
yield exec.exec('python3 -m pip install ipykernel');
yield exec.exec('python3 -m ipykernel install --user');
// Execute notebook
Expand All @@ -87,8 +91,7 @@ import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from time import sleep
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO, format="%(message)s")
params = {}
paramsPath = '${paramsFile}'
Expand Down Expand Up @@ -135,14 +138,25 @@ for task in as_completed(results):
sys.exit(1)
`;
fs.writeFileSync(executeScriptPath, pythonCode);
}
catch (error) {
core.setFailed(error.message);
}
// Not do the actual execution
try {
yield exec.exec(`cat ${executeScriptPath}`);
yield exec.exec(`python3 ${executeScriptPath}`, [], { env: Object.assign(Object.assign({}, process.env), papermill_envs) });
yield exec.exec(`python3 ${executeScriptPath}`, [], { env: Object.assign(Object.assign({}, process.env), papermillEnvs) });
// Convert to HTML
yield exec.exec(`jupyter nbconvert "${parsedNotebookFile}" --to html`);
}
catch (error) {
core.setFailed(error.message);
}
finally {
const notebookObj = JSON.parse(fs.readFileSync(parsedNotebookFile, 'utf8'));
const executionURL = findNestedObj(notebookObj, ["metadata", "executed_notebook_url"]);
yield exec.exec(`echo "Notebook run can be found at ${executionURL}"`);
}
});
}
run();
28 changes: 21 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ interface IGithubContext {
workspace: string;
}

function findNestedObj(obj, keys) {
keys.reduce((o, key) => o && typeof o[key] !== 'undefined' ? o[key] : undefined, obj)
}

// These are added run actions using "env:"
let runner: IRunnerContext = JSON.parse(process.env.RUNNER || "");
let secrets: any = JSON.parse(process.env.SECRETS || "");
Expand All @@ -25,7 +29,10 @@ const executeScriptPath = path.join(scriptsDir, "nb-runner.py");
const secretsPath = path.join(runner.temp, "secrets.json");
const papermillOutput = path.join(github.workspace, "papermill-nb-runner.out");

async function run() {github
async function run() {
var parsedNotebookFile;
var papermillEnvs;

try {
const notebookFile = core.getInput('notebook');
const paramsFile = core.getInput('params');
Expand All @@ -42,7 +49,7 @@ async function run() {github
fs.writeFileSync(secretsPath, JSON.stringify(secrets));
const domain = secrets.NOTEABLE_DOMAIN;
const token = secrets.NOTEABLE_TOKEN;
var papermillEnvs = {};
papermillEnvs = {};
if (typeof domain !== 'undefined') {
papermillEnvs['NOTEABLE_DOMAIN'] = domain;
}
Expand All @@ -52,10 +59,10 @@ async function run() {github
}
const githubString = JSON.stringify(JSON.parse(process.env.GITHUB || ""));

const parsedNotebookFile = path.join(outputDir, path.basename(notebookFile));
parsedNotebookFile = path.join(outputDir, path.basename(notebookFile));
// Install dependencies
await exec.exec('python3 -m pip install papermill-origami papermill>=2.4.0 nbformat>=5.4.0 nbconvert>=7.0.0');
// TODO: Remove these when fixed to not require in papermill origami
// Just in case the user want's to execute a local notebook
await exec.exec('python3 -m pip install ipykernel');
await exec.exec('python3 -m ipykernel install --user');

Expand All @@ -69,8 +76,7 @@ import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from time import sleep
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO, format="%(message)s")
params = {}
paramsPath = '${paramsFile}'
Expand Down Expand Up @@ -118,15 +124,23 @@ for task in as_completed(results):
`;

fs.writeFileSync(executeScriptPath, pythonCode);
} catch (error) {
core.setFailed((error as any).message);
}

// Not do the actual execution
try {
await exec.exec(`cat ${executeScriptPath}`)
await exec.exec(`python3 ${executeScriptPath}`, [], { env: { ...process.env, ...papermillEnvs } });

// Convert to HTML
await exec.exec(`jupyter nbconvert "${parsedNotebookFile}" --to html`);

} catch (error) {
core.setFailed((error as any).message);
} finally {
const notebookObj = JSON.parse(fs.readFileSync(parsedNotebookFile, 'utf8'));
const executionURL = findNestedObj(notebookObj, ["metadata", "executed_notebook_url"])
await exec.exec(`echo "Notebook run can be found at ${executionURL}"`);
}
}

Expand Down

0 comments on commit 1cb4729

Please sign in to comment.