From fc7a529058c075d9bc8e054791e28ace05521597 Mon Sep 17 00:00:00 2001 From: Mark Seaborn Date: Sun, 2 Nov 2008 17:56:43 +0000 Subject: [PATCH] Unify run.py's two execution paths, for Policy and Selections * Always go via a Selections object. Convert a Policy if necessary. * This means the two pieces of code for setting environment variables can be combined, although they are slightly different: one tests for "package:" IDs, the other tests for ZeroInstallImplementation. --- tests/testendtoend.py | 5 +++-- zeroinstall/injector/run.py | 30 +++++++++++------------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/tests/testendtoend.py b/tests/testendtoend.py index 5a8addc..a6884ce 100644 --- a/tests/testendtoend.py +++ b/tests/testendtoend.py @@ -10,6 +10,7 @@ from basetest import BaseTest from basetest import write_file from zeroinstall.injector import autopolicy from zeroinstall.injector import run +from zeroinstall.injector import selections from zeroinstall.injector.iface_cache import iface_cache from zeroinstall.zerostore import manifest @@ -113,7 +114,7 @@ cat $LIBFOO/share/useful main_feed, download_only=True, dry_run=False) policy.download_and_execute([]) env = {} - run.add_env_bindings(env, policy) + run.add_env_bindings(env, selections.Selections(policy).selections) self.assertEquals(env.keys(), ["LIBFOO"]) cmd_path = command_for_policy(policy) proc = subprocess.Popen([cmd_path], stdout=subprocess.PIPE, env=env) @@ -142,7 +143,7 @@ cat $MY_DATA_DIR/share/useful feed_file, download_only=True, dry_run=False) policy.download_and_execute([]) env = {} - run.add_env_bindings(env, policy) + run.add_env_bindings(env, selections.Selections(policy).selections) self.assertEquals(env.keys(), ["MY_DATA_DIR"]) cmd_path = command_for_policy(policy) proc = subprocess.Popen([cmd_path], stdout=subprocess.PIPE, env=env) diff --git a/zeroinstall/injector/run.py b/zeroinstall/injector/run.py index 7d5f8ee..1b64a64 100644 --- a/zeroinstall/injector/run.py +++ b/zeroinstall/injector/run.py @@ -8,6 +8,7 @@ Executes a set of implementations as a program. import os, sys from logging import debug, info +from zeroinstall.injector import selections from zeroinstall.injector.model import SafeException, EnvironmentBinding, ZeroInstallImplementation from zeroinstall.injector.iface_cache import iface_cache @@ -21,14 +22,12 @@ def do_env_binding(environ, binding, path): environ.get(binding.name, None)) info("%s=%s", binding.name, environ[binding.name]) -def add_env_bindings(environ, policy): - for impl in policy.implementation.values(): - assert impl - _do_bindings(environ, impl, impl.bindings) - for dep in impl.requires: - dep_iface = iface_cache.get_interface(dep.interface) - dep_impl = policy.get_implementation(dep_iface) - if isinstance(dep_impl, ZeroInstallImplementation): +def add_env_bindings(environ, sels): + for selection in sels.values(): + _do_bindings(environ, selection, selection.bindings) + for dep in selection.dependencies: + dep_impl = sels[dep.interface] + if not dep_impl.id.startswith('package:'): _do_bindings(environ, dep_impl, dep.bindings) else: debug("Implementation %s is native; no bindings needed", dep_impl) @@ -48,10 +47,9 @@ def execute(policy, prog_args, dry_run = False, main = None, wrapper = None): @type wrapper: str @precondition: C{policy.ready and policy.get_uncached_implementations() == []} """ - iface = iface_cache.get_interface(policy.root) - add_env_bindings(os.environ, policy) - root_impl = policy.get_implementation(iface) - _execute(root_impl, prog_args, dry_run, main, wrapper) + selns = selections.Selections(policy) + execute_selections(selns, prog_args, dry_run=dry_run, main=main, + wrapper=wrapper) def _do_bindings(environ, impl, bindings): for b in bindings: @@ -79,13 +77,7 @@ def execute_selections(selections, prog_args, dry_run = False, main = None, wrap @precondition: All implementations are in the cache. """ sels = selections.selections - for selection in sels.values(): - _do_bindings(os.environ, selection, selection.bindings) - for dep in selection.dependencies: - dep_impl = sels[dep.interface] - if not dep_impl.id.startswith('package:'): - _do_bindings(os.environ, dep_impl, dep.bindings) - + add_env_bindings(os.environ, sels) root_impl = sels[selections.interface] _execute(root_impl, prog_args, dry_run, main, wrapper) -- 2.11.4.GIT