Run unittests in make_dist.py; update doc v1.4
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 12 Feb 2019 09:20:54 +0000 (10:20 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 12 Feb 2019 09:20:54 +0000 (10:20 +0100)
make_dist.py

index cf4fb48..6b99299 100755 (executable)
@@ -3,17 +3,20 @@
 """
 Create rpm packages of pyi2ncommon for various python versions.
 
-Per default try building for autotest (py3.6) and i2n system (py 3.3). Can
-deviate from this by calling `./make_dist.py 3.4` (for example). All python
-versions used must be installed on local machine.
+Usage::
 
-Calls setup.py with different args. Adds %check section to .spec file
+    make_dist.py [RELEASE [PY_VERSION_1 [PY_VERSION_2 ...]]]
 
-This script relies on one feature I accidentally stumbled over: it appears that
-setting install-lib to /usr/lib/pythonVERSION/site-packages is translated by
-setup.py to installation requirement python(abi) = VERSION
+RELEASE defaults to current git commit hash, default versions to build for are
+3.6 for avocado and 3.3 for i2n system. All python versions used must be
+installed on local machine.
+
+Calls setup.py with different args. Adds %check section to .spec file. Runs
+unittests first.
 
-TODO: allow modification of release string from cmd line args (e.g. "testing")
+This script relies on one feature I accidentally stumbled over: it appears that
+setting install-lib to /usr/lib/python{VERSION}/site-packages is translated by
+setup.py to installation requirement python(abi) = {VERSION}
 
 .. codeauthor:: Intra2net AG <info@intra2net.com>
 """
@@ -47,6 +50,37 @@ PYTHONPATH=./src:$PYTHONPATH && python3 -m unittest discover test
 
 """
 
+
+def check_py_version(py_version):
+    """Test that python version is installed."""
+    try:
+        result = call(['python{}'.format(py_version), '--version'])
+    except Exception:
+        result = 1
+    if result != 0:
+        raise RuntimeError('Python version {} not installed, '
+                           'run {} VERSION'.format(py_version, sys.argv[0]))
+
+
+def run_unittests(py_version):
+    """Run unittests with given python version. Re-Run with LANG=C."""
+    # Run twice: first with environment copied from call (env=None) and one
+    # in empoverished environment without unicode capability
+    for env in None, dict(LANG='C', PATH=os.environ['PATH']):
+        try:
+            print('Running unittests with python {} and env={}'
+                  .format(py_version, env))
+            result = call(['python{}'.format(py_version), '-m', 'unittest',
+                           'discover'], env=env)
+        except Exception as exc:
+            raise RuntimeError('Unittests with python {} and env={} failed. {}'
+                               .format(py_version, env, exc))
+        if result != 0:
+            raise RuntimeError('Unittests with python {} and env={} failed '
+                               '(command returned {}).'
+                               .format(py_version, env, result))
+
+
 def run_setup(command, cmd_line_args=None, install_options=None,
               need_zip35=False):
     """
@@ -85,15 +119,6 @@ def create_rpm(py_version, release):
     """Create rpm that will install pyi2ncommon for given python version."""
     print('Creating RPM for python version {}'.format(py_version))
 
-    # test that python version is installed
-    try:
-        result = call(['python{}'.format(py_version), '--version'])
-    except Exception:
-        result = 1
-    if result != 0:
-        raise RuntimeError('Python version {} not installed, '
-                           'run {} VERSION'.format(py_version, sys.argv[0]))
-
     # define options for where to install library;
     # It appears this is automatically translated into requirement
     # python(abi) = version   !
@@ -195,6 +220,8 @@ def main():
         py_versions = sys.argv[2:]
     files_created = []
     for py_version in py_versions:
+        check_py_version(py_version)
+        run_unittests(py_version)
         files_created.append(create_rpm(py_version, release))
     files_created.append(create_spec(release))