[reprotest] 01/01: WIP TODOs REMAINING main: Add a --env-build option for testing different env vars

Ximin Luo infinity0 at debian.org
Tue Sep 26 15:17:50 UTC 2017


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch env-build
in repository reprotest.

commit 01f5d0134cfb503715929d167ad94ed8d37c4733
Author: Ximin Luo <infinity0 at debian.org>
Date:   Tue Sep 26 17:16:54 2017 +0200

    WIP TODOs REMAINING main: Add a --env-build option for testing different env vars
---
 README.rst            |  14 +++++++
 debian/changelog      |   2 +
 reprotest/__init__.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++++-
 reprotest/build.py    |  27 ++++++++++++-
 4 files changed, 145 insertions(+), 2 deletions(-)

diff --git a/README.rst b/README.rst
index aebef9a..ad70d31 100644
--- a/README.rst
+++ b/README.rst
@@ -216,6 +216,20 @@ of names is given in the --help text for --variations.
 Most variations do not have parameters, and for them only the + and - operators
 are relevant. The variations that accept parameters are:
 
+environment.variables
+    A semicolon-separated ordered set, specifying environment variables that
+    reprotest should try to vary. Default is "REPROTEST_CAPTURE_ENVIRONMENT".
+    Supports regex-based syntax e.g.
+
+    - PID=\d{1,6}
+    - HOME=(/\w{3,12}){1,4}
+    - (GO|PYTHON|)PATH=(/\w{3,12}){1,4}(:(/\w{3,12}){1,4}){3,12}
+
+    Special cases:
+
+    - XDG_RUNTIME_DIR= (empty RHS) to tell reprotest to delete the variable
+    - $VARNAME=.{0} to tell reprotest to actually set an empty value
+    - \\x2c and \\x3b to match or generate , and ; respectively.
 user_group.available
     A semicolon-separated ordered set, specifying the available user+group
     combinations that reprotest can ``sudo(1)`` to. Default is empty, in which
diff --git a/debian/changelog b/debian/changelog
index e2943da..81e4a18 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -10,6 +10,8 @@ reprotest (0.7.1) UNRELEASED; urgency=medium
     this automatically in our presets.
   * Pass --exclude-directory-metadata to diffoscope(1) by default as this is
     the majority use-case. Document the other cases in README and the man page.
+  * Add a --env-build option to try to determine which (known and unknown)
+    environment variables cause reproducibility.
 
  -- Ximin Luo <infinity0 at debian.org>  Fri, 22 Sep 2017 17:57:31 +0200
 
diff --git a/reprotest/__init__.py b/reprotest/__init__.py
index a5972e2..bcf1293 100644
--- a/reprotest/__init__.py
+++ b/reprotest/__init__.py
@@ -21,7 +21,7 @@ import pkg_resources
 
 from reprotest.lib import adtlog
 from reprotest.lib import adt_testbed
-from reprotest.build import Build, VariationSpec, Variations, tool_missing
+from reprotest.build import Build, VariationSpec, Variations, tool_missing, environment_template_varname
 from reprotest import presets, shell_syn
 
 
@@ -391,6 +391,100 @@ def check_auto(test_args, testbed_args, build_variations=Variations.of(Variation
         return False
 
 
+def check_env(test_args, testbed_args, build_variations=Variations.of(VariationSpec.default())):
+    # default argument [] is safe here because we never mutate it.
+    _, _, artifact_pattern, store_dir, _, _, diffoscope_args = test_args
+    with empty_or_temp_dir(store_dir, "store_dir") as result_dir:
+        assert store_dir == result_dir or store_dir is None
+        proc = test_args._replace(result_dir=result_dir).corun_builds(testbed_args)
+
+        # Variables intended to control the behaviour of general run-time
+        # programs that include non-build and non-developer programs.
+        # TODO: might have to rm USER etc if it makes builds buggy...
+        # (or add a whitelist to EnvironmentVariation for the user to override)
+        blacklist = r"""
+            HOME LOGNAME USER USERNAME
+            _ COLORTERM EDITOR LS_COLORS OLDPWD PWD SHELL SHLVL TERM VISUAL VTE_VERSION
+            LANG LANGUAGE LC_\w+
+            DISPLAY WINDOWID XAUTHORITY XMODIFIERS
+            DBUS_SESSION_\w+ DESKTOP_SESSION GDMSESSION SESSION_MANAGER XDG_\w+ \w+_SOCKET
+            QT_\w+ GTK_\w+ \w+_IM_MODULE
+            SSH_\w+ GNUPG\w+ GPG_\w+
+            DEBEMAIL DEBFULLNAME
+        """.split()
+        blacklist_names = [environment_template_varname(t) for t in blacklist]
+        # some stuff breaks when you unset certain vars, e.g. diffoscope
+        # breaks if PATH is unset. this specific example doesn't affect us here
+        # because diffoscope runs outside of the testbed, but others might.
+        never_unset = "HOME PATH".split()
+
+        # Variables intended to control the output of build processes, or (e.g.)
+        # interpreter settings that "normal users" aren't expected to need to
+        # customise in normal situations. Notes:
+        # - "path" vars are subtle, we keep them here to avoid false-positives
+        #   and breaking builds but ideally they would be "in the blacklist if
+        #   contents differ, else in the whitelist"
+        whitelist = r"""
+            CC CPP CXX FC F GCJ LD OBJC OBJCXX RUSTC
+            CFLAGS CPPFLAGS CXXFLAGS FCFLAGS FFLAGS GCJFLAGS LDFLAGS OBJCFLAGS OBJCXXFLAGS RUSTFLAGS
+            DEB_\w+ DPKG_\w+
+            (|GO|PYTHON|MAN)PATH
+            PERL5LIB
+        """.split()
+
+        def matches(name, pp):
+            return any(re.match(p, name) for p in pp)
+
+        var_x0, var_x1 = build_variations
+        dist_x0 = proc.send(("control", var_x0))
+        is_reproducible = lambda name, var: test_args.check_reproducible(proc, dist_x0, name, var)
+
+        orig_variations = var_x1.spec.variations()
+        only_varying_env = (len(orig_variations) == 0 or
+            len(orig_variations) == 1 and "environment" in orig_variations)
+        env = set(os.environ.keys()) - set(never_unset)
+
+        # Test blacklist
+        blacklist_matches = sorted(n for n in env
+            if matches(n, blacklist_names) and n not in never_unset)
+        spec_x1 = var_x1.spec.extend("environment")
+        spec_x1 = spec_x1._replace(environment=spec_x1.environment.extend_variables(
+            # unset stuff in the current env that matches the blacklist
+            # TODO: could also generate dummy values based on the templates
+            *("%s=" % k for k in blacklist_matches),
+            # generate new stuff according to the blacklist templates
+            *blacklist))
+        var_x1 = var_x1._replace(spec=spec_x1)
+        if not is_reproducible("blacklist", var_x1):
+            print("Unreproducible even when varying blacklisted envvars: ", ", ".join(sorted(blacklist_matches + blacklist)))
+            if not only_varying_env:
+                print("This may be caused by other factors; try re-running this again with --vary=-all")
+            else:
+                print("You should (probably) make your program reproducible when varying these.")
+            return False
+
+        # Test non-whitelist
+        unrecognized = sorted(n for n in env
+            if not matches(n, blacklist_names) and not matches(n, whitelist))
+        extra_blacklist = ["REPROTEST_CAPTURE_ENVIRONMENT_UNKNOWN_\w+"]
+        spec_x2 = spec_x1._replace(environment=spec_x1.environment.extend_variables(
+            # unset stuff in the current env that doesn't match the whitelist (or blacklist, which we set earlier)
+            *("%s=" % k for k in unrecognized),
+            # TODO: could also generate some totally random var names
+            *extra_blacklist))
+        var_x2 = var_x1._replace(spec=spec_x2)
+        if not is_reproducible("non-whitelist", var_x2):
+            print("Unreproducible when varying unknown envvars: ", ", ".join(sorted(extra_blacklist + unrecognized)))
+            print("Please file a bug to reprotest to add these to the whitelist or blacklist, to be decided.")
+            print("If blacklist is chosen, then you should also make your program reproducible when varying them.")
+            return False
+
+        test_args.output_reproducible_hashes(dist_x0)
+        if orig_variations != VariationSpec.all_names():
+            print("However, other factors may still make the build unreproducible; try re-running with --vary=+all.")
+        return True
+
+
 def config_to_args(parser, filename):
     if not filename:
         return []
@@ -507,6 +601,12 @@ def cli_parser():
         'variations cause unreproducibility, potentially up to and including '
         'the ones specified by --variations and --vary. Conflicts with '
         '--extra-build.')
+    group1_0.add_argument('--env-build', default=False, action='store_true',
+        help='Automatically perform builds to try to determine which specific '
+        'environment variables cause unreproducibility, based on a hard-coded '
+        'whitelist and blacklist. You probably want to set --vary=-all as well '
+        'when setting this flag; see the man page for details. Conflicts with '
+        '--extra-build and --auto-build.')
     # TODO: remove after reprotest 0.8
     group1.add_argument('--dont-vary', default=[], action='append', help=argparse.SUPPRESS)
 
@@ -670,6 +770,8 @@ def run(argv, dry_run=None):
     specs = [spec]
     if parsed_args.auto_build:
         check_func = check_auto
+    elif parsed_args.env_build:
+        check_func = check_env
     else:
         for extra_build in parsed_args.extra_build:
             specs.append(spec.extend(extra_build))
diff --git a/reprotest/build.py b/reprotest/build.py
index 976495c..7db20d3 100644
--- a/reprotest/build.py
+++ b/reprotest/build.py
@@ -5,11 +5,13 @@ import collections
 import functools
 import getpass
 import grp
+import itertools
 import logging
 import os
 import shlex
 import shutil
 import random
+import rstr
 import time
 import types
 
@@ -174,10 +176,23 @@ fi
 # def cpu(script, env, tree):
 #     return script, env, tree
 
+def environment_template_varname(tmpl):
+    return tmpl.partition("=")[0]
+
 def environment(ctx, build, vary):
     if not vary:
         return build
-    return build.add_env('CAPTURE_ENVIRONMENT', 'i_capture_the_environment')
+    removed = []
+    for tmpl in ctx.spec.environment.variables:
+        k, sep, v = tmpl.partition("=")
+        if not v and sep:
+            removed += [k]
+        else:
+            build = build.add_env(rstr.xeger(k), rstr.xeger(v) or "i_capture_the_environment")
+    if removed:
+        command = ["env"] + list(itertools.chain.from_iterable(zip(itertools.repeat("-u"), removed)))
+        build = build.append_to_build_command(_shell_ast.SimpleCommand.make(*command))
+    return build
 
 # TODO: this requires superuser privileges.
 # def domain_host(ctx, script, env, tree):
@@ -401,6 +416,15 @@ class TimeVariation(collections.namedtuple('_TimeVariation', 'faketimes auto_fak
         return self.empty()._replace(faketimes=self.faketimes + new_faketimes)
 
 
+class EnvironmentVariation(collections.namedtuple("_EnvironmentVariation", "variables")):
+    @classmethod
+    def default(cls):
+        return cls(mdiffconf.strlist_set(";", ["REPROTEST_CAPTURE_ENVIRONMENT"]))
+
+    def extend_variables(self, *ks):
+        return self._replace(variables=self.variables + list(ks))
+
+
 class UserGroupVariation(collections.namedtuple('_UserGroupVariation', 'available')):
     @classmethod
     def default(cls):
@@ -411,6 +435,7 @@ class VariationSpec(mdiffconf.ImmutableNamespace):
     @classmethod
     def default(cls, variations=VARIATIONS):
         default_overrides = {
+            "environment": EnvironmentVariation.default(),
             "user_group": UserGroupVariation.default(),
             "time": TimeVariation.default(),
         }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/reprotest.git



More information about the Reproducible-commits mailing list