From: Phillip Berndt Date: Sun, 3 Jan 2016 11:27:59 +0000 (+0100) Subject: Pass meta-information to block scripts X-Git-Tag: 1.0~34 X-Git-Url: https://git.donarmstrong.com/?p=deb_pkgs%2Fautorandr.git;a=commitdiff_plain;h=83a911dc2b06c1e2b8c765259fb0359509194df8;ds=sidebyside Pass meta-information to block scripts Currently, $AUTORANDR_CURRENT_PROFILES and $AUTORANDR_CURRENT_PROFILE are supported, containing a colon-separated list (or the first, respectively) of active profiles. If no profile was detected as active/current, both variables will be empty. This fixes #42 --- diff --git a/autorandr.py b/autorandr.py index c2fdb9a..2fac25d 100755 --- a/autorandr.py +++ b/autorandr.py @@ -484,12 +484,21 @@ def find_profiles(current_config, profiles): detected_profiles.append(profile_name) return detected_profiles -def profile_blocked(profile_path): - "Check if a profile is blocked" +def profile_blocked(profile_path, meta_information=None): + """Check if a profile is blocked. + + meta_information is expected to be an dictionary. It will be passed to the block scripts + in the environment, as variables called AUTORANDR_. + """ script = os.path.join(profile_path, "block") if not os.access(script, os.X_OK | os.F_OK): return False - return subprocess.call(script) == 0 + if meta_information: + env = os.environ.copy() + env.update({ "AUTORANDR_%s" % str(key).upper(): str(value) for (key, value) in meta_information.items() }) + else: + env = os.environ.copy() + return subprocess.call(script, env=env) == 0 def output_configuration(configuration, config): "Write a configuration file" @@ -757,8 +766,19 @@ def main(argv): if "--load" in options: load_profile = options["--load"] else: + # Find the active profile(s) first, for the block script (See #42) + current_profiles = [] for profile_name in profiles.keys(): - if profile_blocked(os.path.join(profile_path, profile_name)): + configs_are_equal = is_equal_configuration(config, profiles[profile_name]["config"]) + if configs_are_equal: + current_profiles.append(profile_name) + block_script_metadata = { + "CURRENT_PROFILE": "".join(current_profiles[:1]), + "CURRENT_PROFILES": ":".join(current_profiles) + } + + for profile_name in profiles.keys(): + if profile_blocked(os.path.join(profile_path, profile_name), block_script_metadata): print("%s (blocked)" % profile_name, file=sys.stderr) continue props = [] @@ -766,8 +786,7 @@ def main(argv): props.append("(detected)") if ("-c" in options or "--change" in options) and not load_profile: load_profile = profile_name - configs_are_equal = is_equal_configuration(config, profiles[profile_name]["config"]) - if configs_are_equal: + if profile_name in current_profiles: props.append("(current)") print("%s%s%s" % (profile_name, " " if props else "", " ".join(props)), file=sys.stderr) if not configs_are_equal and "--debug" in options and profile_name in detected_profiles: