Merge lp:~steve-mcintyre/linaro-chromiumos/error_checking into lp:linaro-chromiumos

Proposed by Steve McIntyre
Status: Needs review
Proposed branch: lp:~steve-mcintyre/linaro-chromiumos/error_checking
Merge into: lp:linaro-chromiumos
Diff against target: 69 lines (+9/-7)
2 files modified
remote/run_build (+2/-1)
remote/setup_build (+7/-6)
To merge this branch: bzr merge lp:~steve-mcintyre/linaro-chromiumos/error_checking
Reviewer Review Type Date Requested Status
Loïc Minier Pending
Review via email: mp+55578@code.launchpad.net

Description of the change

Should have pushed this ages ago...

More error handling; make sure that we abort properly on errors.

To post a comment you must log in.
Revision history for this message
Loïc Minier (lool) wrote :

Remove the "print" before the exception throwing; you can either deal with the error in higher level logic, or print the exception message from the main script, or just let the exception go out to the default handler which will include the error message.

Usually in scripts people prefer the subprocess module rather than os.system() which is very limited; subprocess.Popen() is very flexible allowing you to do pipes or capture stdout / stderr for instance, and if you only need to run a command and throw an exception if it fails it provides a wrapper around Popen called check_call() which does that. There are other utilities to get command output as well.

I suggest you use check_call instead of os.system; this will allow closing stderr or stdout in the future when we deem sensible, or piping them to a log file etc.

Unmerged revisions

18. By Steve McIntyre

Better error-handling

Check for errors from os.system() calls and throw exceptions on
failure.
Throw exceptions rather than return errors from functions.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'remote/run_build'
2--- remote/run_build 2011-03-23 15:48:31 +0000
3+++ remote/run_build 2011-03-30 15:41:27 +0000
4@@ -13,7 +13,8 @@
5 def run(command, as_root=False):
6 if as_root:
7 command = "sudo -- %s" % command
8- os.system(command)
9+ if os.system(command) != 0:
10+ raise Exception("command failed")
11
12 def main(argv):
13 options = {}
14
15=== modified file 'remote/setup_build'
16--- remote/setup_build 2011-03-24 17:52:47 +0000
17+++ remote/setup_build 2011-03-30 15:41:27 +0000
18@@ -63,7 +63,8 @@
19 def run(command, as_root=False):
20 if as_root:
21 command = "sudo -- %s" % command
22- os.system(command)
23+ if os.system(command) != 0:
24+ raise Exception("command failed")
25
26 def logged_run(log_file, command):
27 # XXX needs to escape command properly
28@@ -81,7 +82,7 @@
29 def cmd_create_tree(options, tree_dir):
30 if os.path.exists(tree_dir):
31 print 'cmd_create_tree: tree %s already exists. Abort.' % tree_dir
32- return errno.EEXIST
33+ raise Exception("Tree directory %s already exists" % tree_dir)
34
35 os.makedirs(tree_dir, 0755)
36 run('apt-get update', as_root=True)
37@@ -92,7 +93,7 @@
38 def cmd_get_source(options, tree_dir):
39 if not os.path.exists(tree_dir):
40 print 'cmd_get_source: tree %s does not exist. Abort.' % tree_dir
41- return errno.ENOENT
42+ raise Exception("Tree directory %s does not exist" % tree_dir)
43
44 basedir = os.path.dirname(os.path.realpath(sys.argv[0]))
45
46@@ -114,7 +115,7 @@
47 def cmd_create_chroot(options, tree_dir):
48 if not os.path.exists(tree_dir):
49 print 'cmd_create_chroot: tree %s does not exist. Abort.' % tree_dir
50- return errno.ENOENT
51+ raise Exception("Tree directory %s does not exist" % tree_dir)
52
53 create_logdir(tree_dir)
54 os.chdir(tree_dir)
55@@ -126,12 +127,12 @@
56 def cmd_build(options, tree_dir):
57 if not os.path.exists(tree_dir):
58 print 'cmd_build: tree %s does not exist. Abort.' % tree_dir
59- return errno.ENOENT
60+ raise Exception("Tree directory %s does not exist" % tree_dir)
61
62 chroot_path = os.path.join(tree_dir, 'chroot')
63 if not os.path.exists(chroot_path):
64 print 'cmd_build: chroot %s does not exist. Abort.' % chroot_path
65- return errno.ENOENT
66+ raise Exception("Chroot %s does not exist" % chroot_path)
67
68 create_logdir(tree_dir)
69 os.chdir(tree_dir)

Subscribers

People subscribed via source and target branches