From 3e725162ec70b591344cf6e896d7f2e31c3551e2 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Mon, 28 Aug 2017 18:18:31 -0700 Subject: [PATCH] Reduce up() cyclomatic complexity Signed-off-by: Joffrey F --- compose/cli/main.py | 62 +++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index 21bf1f308..face38e6d 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -969,33 +969,10 @@ class TopLevelCommand(object): if cascade_stop: print("Aborting on container exit...") - - exit_code = 0 - if exit_value_from: - candidates = list(filter( - lambda c: c.service == exit_value_from, - attached_containers)) - if not candidates: - log.error( - 'No containers matching the spec "{0}" ' - 'were run.'.format(exit_value_from) - ) - exit_code = 2 - elif len(candidates) > 1: - exit_values = filter( - lambda e: e != 0, - [c.inspect()['State']['ExitCode'] for c in candidates] - ) - - exit_code = exit_values[0] - else: - exit_code = candidates[0].inspect()['State']['ExitCode'] - else: - for e in self.project.containers(service_names=options['SERVICE'], stopped=True): - if (not e.is_running and cascade_starter == e.name): - if not e.exit_code == 0: - exit_code = e.exit_code - break + all_containers = self.project.containers(service_names=options['SERVICE'], stopped=True) + exit_code = compute_exit_code( + exit_value_from, attached_containers, cascade_starter, all_containers + ) self.project.stop(service_names=service_names, timeout=timeout) sys.exit(exit_code) @@ -1016,6 +993,37 @@ class TopLevelCommand(object): print(get_version_info('full')) +def compute_exit_code(exit_value_from, attached_containers, cascade_starter, all_containers): + exit_code = 0 + if exit_value_from: + candidates = list(filter( + lambda c: c.service == exit_value_from, + attached_containers)) + if not candidates: + log.error( + 'No containers matching the spec "{0}" ' + 'were run.'.format(exit_value_from) + ) + exit_code = 2 + elif len(candidates) > 1: + exit_values = filter( + lambda e: e != 0, + [c.inspect()['State']['ExitCode'] for c in candidates] + ) + + exit_code = exit_values[0] + else: + exit_code = candidates[0].inspect()['State']['ExitCode'] + else: + for e in all_containers: + if (not e.is_running and cascade_starter == e.name): + if not e.exit_code == 0: + exit_code = e.exit_code + break + + return exit_code + + def convergence_strategy_from_opts(options): no_recreate = options['--no-recreate'] force_recreate = options['--force-recreate']