+def exec_scripts(profile_path, script_name, meta_information=None):
+ """"Run userscripts
+
+ This will run all executables from the profile folder, and global per-user
+ and system-wide configuration folders, named script_name or residing in
+ subdirectories named script_name.d.
+
+ meta_information is expected to be an dictionary. It will be passed to the block scripts
+ in the environment, as variables called AUTORANDR_<CAPITALIZED_KEY_HERE>.
+
+ Returns True unless any of the scripts exited with non-zero exit status.
+ """
+ all_ok = True
+ 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()
+
+ # If there are multiple candidates, the XDG spec tells to only use the first one.
+ ran_scripts = set()
+
+ user_profile_path = os.path.expanduser("~/.autorandr")
+ if not os.path.isdir(user_profile_path):
+ user_profile_path = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "autorandr")
+
+ for folder in chain((profile_path, os.path.dirname(profile_path), user_profile_path),
+ (os.path.join(x, "autorandr") for x in os.environ.get("XDG_CONFIG_DIRS", "").split(":"))):
+
+ if script_name not in ran_scripts:
+ script = os.path.join(folder, script_name)
+ if os.access(script, os.X_OK | os.F_OK):
+ all_ok &= subprocess.call(script, env=env) != 0
+ ran_scripts.add(script_name)
+
+ script_folder = os.path.join(folder, "%s.d" % script_name)
+ if os.access(script_folder, os.R_OK | os.X_OK) and os.path.isdir(script_folder):
+ for file_name in os.listdir(script_folder):
+ check_name = "d/%s" % (file_name,)
+ if check_name not in ran_scripts:
+ script = os.path.join(script_folder, file_name)
+ if os.access(script, os.X_OK | os.F_OK):
+ all_ok &= subprocess.call(script, env=env) != 0
+ ran_scripts.add(check_name)
+
+ return all_ok