Skip to content

Commit 7d9b7a7

Browse files
authored
Merge pull request #8 from NordicHPC/rkdarst/less-verbosity
Add -v option, reduce default logging
2 parents 1bc0e4e + eaf41ac commit 7d9b7a7

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ These options directly map to normal Jupyter kernel install options:
7575

7676
These are envkernel-specific options:
7777

78+
* `--verbose`, `-v`: Print more debugging information when installing
79+
the kernel. It is always in verbose mode when actually running the
80+
kernel.
7881
* `--python`: Python interpreter to use when invoking inside the
7982
environment. (Default `python`. Unlike other kernels, this defaults
8083
to a relative path because the point of envkernel is to set up PATH

envkernel.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import tempfile
1616

1717
LOG = logging.getLogger('envkernel')
18-
LOG.setLevel(logging.DEBUG)
18+
LOG.setLevel(logging.INFO)
1919
logging.lastResort.setLevel(logging.DEBUG)
2020

2121

@@ -127,9 +127,14 @@ def setup(self):
127127
parser.add_argument('--env', action='append', default=[],
128128
help="Environment to add, format NAME=VAL. Can be given multiple times. "
129129
"These are statically embedded in the kernel.json file")
130+
parser.add_argument('--verbose', '-v', action='store_true',
131+
help="Print more debugging information")
130132
args, unknown_args = parser.parse_known_args(self.argv)
131-
LOG.debug('setup: args: %s', args)
132-
LOG.debug('setup: unknown_args: %s', unknown_args)
133+
if args.verbose:
134+
LOG.setLevel(logging.DEBUG)
135+
136+
LOG.debug('setup: envkernel setup args: %s', args)
137+
LOG.debug('setup: kernel-specific args: %s', unknown_args)
133138
self.setup_args = args
134139
self.name = args.name
135140
self.user = args.user
@@ -243,6 +248,13 @@ def install_kernel(self, kernel, name, user=False, replace=None, prefix=None, lo
243248
LOG.info(" Note: Kernel not detected with current search path.")
244249
LOG.info(" Command line: %s", kernel['argv'])
245250

251+
def run(self):
252+
"""Hook that gets run before kernel invoked"""
253+
# User does not directly see this (except interleaved in
254+
# normal jupyter logging output), so we can set it to debug
255+
# by default.
256+
LOG.setLevel(logging.DEBUG)
257+
246258

247259

248260
class lmod(envkernel):
@@ -266,6 +278,7 @@ def run(self):
266278
267279
before '--': the modules to load
268280
after '--': the Python command to run after loading"""
281+
super().run()
269282
argv, rest = split_doubledash(self.argv, 1)
270283
parser = argparse.ArgumentParser()
271284
parser.add_argument('--purge', action='store_true', default=False, help="Purge existing modules first")
@@ -330,6 +343,7 @@ def run(self):
330343
331344
before '--': the modules to load
332345
after '--': the Python command to run after loading"""
346+
super().run()
333347
argv, rest = split_doubledash(self.argv, 1)
334348
parser = argparse.ArgumentParser()
335349
#parser.add_argument('--purge', action='store_true', default=False, help="Purge existing modules first")
@@ -399,6 +413,7 @@ def setup(self):
399413
replace=self.replace, prefix=self.prefix)
400414

401415
def run(self):
416+
super().run()
402417
argv, rest = split_doubledash(self.argv, 1)
403418
parser = argparse.ArgumentParser()
404419
parser.add_argument('image', help='Docker image name')
@@ -534,6 +549,7 @@ def setup(self):
534549
replace=self.replace, prefix=self.prefix)
535550

536551
def run(self):
552+
super().run()
537553
argv, rest = split_doubledash(self.argv, 1)
538554
parser = argparse.ArgumentParser()
539555
parser.add_argument('image', help='image name')

test_envkernel.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
import os
34
from os.path import join as pjoin
45
import pytest
@@ -141,6 +142,35 @@ def test_help():
141142
assert mod in stdout, "%s not found in --help output"%mod
142143
assert p.returncode == 0
143144

145+
def test_logging(d, caplog):
146+
"""Test that the global -v option works and increases debugging
147+
148+
Run first without -v and make sure some stuff isn't printed.
149+
Then, run with -v and ensure that the argument processing is output.
150+
"""
151+
cmd = "python3 -m envkernel lmod --name=ABC --display-name=AAA MOD1 --prefix=%s"%d
152+
print(d)
153+
env = os.environ.copy()
154+
env['JUPYTER_PATH'] = pjoin(d, 'share/jupyter')
155+
# First, test non-verbose (should have minimal output)
156+
p = subprocess.Popen(cmd, env=env,
157+
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
158+
stdout = p.stdout.read().decode()
159+
p.wait()
160+
print(stdout)
161+
assert 'Namespace' not in stdout
162+
assert 'kernel-specific' not in stdout
163+
assert 'Command line:' in stdout
164+
# Now test verbose (should have some debugging info)
165+
p = subprocess.Popen(cmd+' -v', env=env,
166+
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
167+
stdout = p.stdout.read().decode()
168+
p.wait()
169+
#print(stdout)
170+
assert 'Namespace' in stdout
171+
assert 'kernel-specific' in stdout
172+
assert 'Command line:' in stdout
173+
144174
def test_umask(d):
145175
orig_umask = os.umask(0)
146176
# Test multiple umasks, and kerneldir + kernel.json

0 commit comments

Comments
 (0)