1 |
On Tue, 17 Jan 2017 19:48:16 +0200 |
2 |
Alexandru Elisei <alexandru.elisei@×××××.com> wrote: |
3 |
|
4 |
> Currently module functions return a message to emaint after |
5 |
> invocation. Emaint prints this message then exits normally (with a |
6 |
> success return code) even if the module encountered an error. This |
7 |
> patch aims to change this by having each module public function |
8 |
> return a tuple of (returncode, message). Emaint will inspect the |
9 |
> return code and will exit unsuccessfully if necessary. |
10 |
> --- |
11 |
> pym/portage/emaint/main.py | 11 +++++++++-- |
12 |
> pym/portage/emaint/modules/binhost/binhost.py | 6 ++++-- |
13 |
> pym/portage/emaint/modules/config/config.py | 8 ++++++-- |
14 |
> pym/portage/emaint/modules/logs/logs.py | 9 +++++---- |
15 |
> pym/portage/emaint/modules/merges/merges.py | 18 +++++++++++------- |
16 |
> pym/portage/emaint/modules/move/move.py | 9 +++++++-- |
17 |
> pym/portage/emaint/modules/resume/resume.py | 4 +++- |
18 |
> pym/portage/emaint/modules/sync/sync.py | 19 |
19 |
> +++++++++++++------ pym/portage/emaint/modules/world/world.py | |
20 |
> 8 ++++++-- 9 files changed, 64 insertions(+), 28 deletions(-) |
21 |
> |
22 |
> diff --git a/pym/portage/emaint/main.py b/pym/portage/emaint/main.py |
23 |
> index 65e3545..ef4383a 100644 |
24 |
> --- a/pym/portage/emaint/main.py |
25 |
> +++ b/pym/portage/emaint/main.py |
26 |
> @@ -115,6 +115,7 @@ class TaskHandler(object): |
27 |
> """Runs the module tasks""" |
28 |
> if tasks is None or func is None: |
29 |
> return |
30 |
> + returncode = os.EX_OK |
31 |
> for task in tasks: |
32 |
> inst = task() |
33 |
> show_progress = self.show_progress_bar and |
34 |
> self.isatty @@ -135,14 +136,20 @@ class TaskHandler(object): |
35 |
> # them for other tasks if there is |
36 |
> more to do. 'options': options.copy() |
37 |
> } |
38 |
> - result = getattr(inst, func)(**kwargs) |
39 |
> + rcode, msgs = getattr(inst, func)(**kwargs) |
40 |
> if show_progress: |
41 |
> # make sure the final progress is |
42 |
> displayed self.progress_bar.display() |
43 |
> print() |
44 |
> self.progress_bar.stop() |
45 |
> if self.callback: |
46 |
> - self.callback(result) |
47 |
> + self.callback(msgs) |
48 |
> + # Keep the last error code when using the |
49 |
> 'all' command. |
50 |
> + if rcode != os.EX_OK: |
51 |
> + returncode = rcode |
52 |
> + |
53 |
> + if returncode != os.EX_OK: |
54 |
> + sys.exit(returncode) |
55 |
> |
56 |
|
57 |
|
58 |
Please move this sys.exit to the main(). So this should just return |
59 |
a list of the rcodes to match the tasks it was passed. |
60 |
TaskHandler can be used easily via api, so we don't want it to exit out |
61 |
of someone else's code. That will also keep main() as the primary cli |
62 |
interface. There process the list of rcodes and exit appropriately. |
63 |
|
64 |
|
65 |
|
66 |
> |
67 |
> def print_results(results): |
68 |
> diff --git a/pym/portage/emaint/modules/binhost/binhost.py |
69 |
> b/pym/portage/emaint/modules/binhost/binhost.py |
70 |
> index cf1213e..8cf3da6 100644 |
71 |
> --- a/pym/portage/emaint/modules/binhost/binhost.py |
72 |
> +++ b/pym/portage/emaint/modules/binhost/binhost.py |
73 |
> @@ -86,7 +86,9 @@ class BinhostHandler(object): |
74 |
> stale = set(metadata).difference(cpv_all) |
75 |
> for cpv in stale: |
76 |
> errors.append("'%s' is not in the |
77 |
> repository" % cpv) |
78 |
> - return errors |
79 |
> + if errors: |
80 |
> + return (1, errors) |
81 |
> + return (os.EX_OK, None) |
82 |
> |
83 |
|
84 |
Alexandru, I tend to not prefer using numbers to indicate success/fail. |
85 |
They can be confusing at times. In this case 1 means fail, 0 means |
86 |
success. Which is the opposite of True/False. It would be different |
87 |
if there were multiple return code values. Then they would be |
88 |
meaningful. I want to keep these modules pythonic in the sense that |
89 |
they return standard True/False for API use. In this case returning |
90 |
True/False would also make it easy to process the list of rcodes |
91 |
returned to main() |
92 |
|
93 |
sys.exit(False in rcodes) |
94 |
|
95 |
which will reverse the True/False to 0/1 automatically for us and make |
96 |
it super simple to process a variable length list success/fail results. |
97 |
|
98 |
-- |
99 |
Brian Dolbec <dolsen> |