Commit 1e69e4aa authored by Xavier Thompson's avatar Xavier Thompson

stack/macros: Simplify pythonpath macro

See merge request nexedi/slapos!1398
parents 7b5b1967 43e5bde0
...@@ -3,22 +3,14 @@ parts = ...@@ -3,22 +3,14 @@ parts =
[macro.pythonpath.eggs] [macro.pythonpath.eggs]
recipe = slapos.recipe.build recipe = slapos.recipe.build
_name_ = ${:_buildout_section_name_}
init = init =
prerequisite = """ self.eggs = [e.strip() for e in options['eggs'].splitlines() if e.strip()]
[.%(_name_)s.prerequisite] update =
recipe = slapos.recipe.build
init =
section = self.buildout['%(_name_)s']
self.eggs = [e.strip() for e in section['eggs'].splitlines() if e.strip()]
update =
from zc.buildout.easy_install import working_set from zc.buildout.easy_install import working_set
buildout = self.buildout['buildout'] buildout = self.buildout['buildout']
eggs_directory = buildout['eggs-directory'] eggs_directory = buildout['eggs-directory']
develop_eggs_directory = buildout['develop-eggs-directory'] develop_eggs_directory = buildout['develop-eggs-directory']
dists = working_set(self.eggs, [develop_eggs_directory, eggs_directory]) dists = working_set(self.eggs, [develop_eggs_directory, eggs_directory])
paths = ':'.join(dist.location for dist in dists) paths = ':'.join(dist.location for dist in dists)
self.buildout['%(environment)s']['PYTHONPATH'] = paths self.buildout[options['environment']]['PYTHONPATH'] = paths
print("PYTHONPATH=" + paths) print("PYTHONPATH=" + paths)
""" % options
self.buildout.parse(prerequisite)
...@@ -32,6 +32,7 @@ import os ...@@ -32,6 +32,7 @@ import os
import requests import requests
import sqlite3 import sqlite3
import subprocess import subprocess
import tempfile
from slapos.proxy.db_version import DB_VERSION from slapos.proxy.db_version import DB_VERSION
from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass from slapos.testing.testcase import makeModuleSetUpAndTestCaseClass
...@@ -271,65 +272,75 @@ class TestJupyterCustomAdditional(SelectMixin, InstanceTestCase): ...@@ -271,65 +272,75 @@ class TestJupyterCustomAdditional(SelectMixin, InstanceTestCase):
r.destroyed() r.destroyed()
class TestIPython(InstanceTestCase): class IPythonNotebook(object):
def __init__(self, name, binary):
converted_notebook = 'test.nbconvert.ipynb' self.tempdir = tempdir = tempfile.TemporaryDirectory()
notebook_filename = 'test.ipynb' path = os.path.join(tempdir.name, name)
test_sentence = 'test' self.path = path + '.ipynb'# input notebook
self.output_path = path + '.nbconvert.ipynb' # output notebook
self.binary = binary
def setUp(self): def write(self, code):
super().setUp() content = {
notebook_source = {
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": None, "execution_count": None,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": code.splitlines(keepends=True)
"import sys\n",
"print('" + self.test_sentence + "')"
]
} }
], ],
"metadata": {}, "metadata": {},
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 4 "nbformat_minor": 4
} }
with open(self.notebook_filename, 'w') as notebook: with open(self.path, 'w') as notebook:
notebook.write(json.dumps(notebook_source)) notebook.write(json.dumps(content))
def run(self):
return subprocess.check_output(
(self.binary,'--execute', '--to', 'notebook', self.path),
stderr=subprocess.STDOUT, text=True)
def readResult(self):
with open(self.output_path) as result:
return json.loads(result.read())['cells'][0]['outputs'][0]['text'][0]
def __enter__(self):
return self
def __exit__(self, *args):
if self.tempdir:
self.tempdir.cleanup()
del self.tempdir
def tearDown(self): class TestIPython(InstanceTestCase):
os.remove(self.notebook_filename) message = 'test_sys'
if os.path.exists(self.converted_notebook): module = 'sys'
os.remove(self.converted_notebook)
super().tearDown()
def test(self): def test(self):
conversion_output = subprocess.check_output([ binary = os.path.join(
os.path.join(
self.computer_partition_root_path, self.computer_partition_root_path,
'software_release', 'software_release', 'bin', 'jupyter-nbconvert')
'bin', with IPythonNotebook('test', binary) as notebook:
'jupyter-nbconvert', notebook.write("import %s\nprint(%r)" % (self.module, self.message))
), out = notebook.run()
'--execute',
'--to',
'notebook',
self.notebook_filename,
], stderr=subprocess.STDOUT, text=True)
self.assertIn( self.assertIn(
'[NbConvertApp] Converting notebook %s to notebook' % self.notebook_filename, "[NbConvertApp] Converting notebook %s to notebook" % notebook.path,
conversion_output, out,
) )
self.assertRegex( self.assertRegex(
conversion_output, out,
r'\[NbConvertApp\] Writing \d+ bytes to %s' % self.converted_notebook r"\[NbConvertApp\] Writing \d+ bytes to %s" % notebook.output_path
) )
self.assertTrue(os.path.exists(self.converted_notebook)) self.assertTrue(os.path.exists(notebook.output_path))
with open(self.converted_notebook) as json_result: self.assertEqual(notebook.readResult(), self.message + '\n')
self.assertEqual(
json.loads(json_result.read())['cells'][0]['outputs'][0]['text'][0],
self.test_sentence + '\n', class TestIPythonNumpy(TestIPython):
) message = 'test_numpy'
module = 'numpy'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment