normalize apiurl
[opensuse:osc.git] / osc / babysitter.py
1 # Copyright (C) 2008 Novell Inc.  All rights reserved.
2 # This program is free software; it may be used, copied, modified
3 # and distributed under the terms of the GNU General Public Licence,
4 # either version 2, or (at your option) any later version.
5
6 import os.path
7 import sys
8 import signal
9 from osc import oscerr
10 from urllib2 import URLError, HTTPError
11 from oscsslexcp import NoSecureSSLError
12 from osc.util.cpio import CpioError
13 from osc.util.packagequery import PackageError
14
15 try:
16     from M2Crypto.SSL.Checker import SSLVerificationError
17     from M2Crypto.SSL import SSLError as SSLError
18 except:
19     SSLError = None
20     SSLVerificationError = None
21
22 try:
23     # import as RPMError because the class "error" is too generic
24     from rpm import error as RPMError
25 except:
26     # if rpm-python isn't installed (we might be on a debian system):
27     RPMError = None
28
29
30 # the good things are stolen from Matt Mackall's mercurial
31
32 def catchterm(*args):
33     raise oscerr.SignalInterrupt
34
35 for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM':
36     num = getattr(signal, name, None)
37     if num: signal.signal(num, catchterm)
38
39
40 def run(prg):
41
42     try:
43
44         try:
45             if '--debugger' in sys.argv:
46                 import pdb
47                 pdb.set_trace()
48
49             # here we actually run the program:
50             return prg.main()
51
52         except:
53             # look for an option in the prg.options object and in the config dict
54             # print stack trace, if desired
55             if getattr(prg.options, 'traceback', None) or getattr(prg.conf, 'config', {}).get('traceback', None) or \
56                getattr(prg.options, 'post_mortem', None) or getattr(prg.conf, 'config', {}).get('post_mortem', None):
57                 import traceback
58                 traceback.print_exc(file=sys.stderr)
59                 # we could use http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215
60
61             # enter the debugger, if desired
62             if getattr(prg.options, 'post_mortem', None) or getattr(prg.conf, 'config', {}).get('post_mortem', None):
63                 if sys.stdout.isatty() and not hasattr(sys, 'ps1'):
64                     import pdb
65                     pdb.post_mortem(sys.exc_info()[2])
66                 else:
67                     print >>sys.stderr, 'sys.stdout is not a tty. Not jumping into pdb.'
68             raise
69
70     except oscerr.SignalInterrupt:
71         print >>sys.stderr, 'killed!'
72         return 1
73
74     except KeyboardInterrupt:
75         print >>sys.stderr, 'interrupted!'
76         return 1
77
78     except oscerr.UserAbort:
79         print >>sys.stderr, 'aborted.'
80         return 1
81
82     except oscerr.APIError, e:
83         print >>sys.stderr, 'BuildService API error:', e.msg
84         return 1
85
86     except oscerr.LinkExpandError, e:
87         print >>sys.stderr, 'Link "%s/%s" cannot be expanded:\n' % (e.prj, e.pac), e.msg
88         print >>sys.stderr, 'Use "osc repairlink" to fix merge conflicts.\n'
89         return 1
90
91     except oscerr.WorkingCopyWrongVersion, e:
92         print >>sys.stderr, e
93
94     except oscerr.NoWorkingCopy, e:
95         print >>sys.stderr, e
96         if os.path.isdir('.git'): print >>sys.stderr, "Current directory looks like git."
97         if os.path.isdir('.hg'):  print >>sys.stderr, "Current directory looks like mercurial."
98         if os.path.isdir('.svn'): print >>sys.stderr, "Current directory looks like svn."
99         if os.path.isdir('CVS'):  print >>sys.stderr, "Current directory looks like cvs."
100         return 1
101
102     except HTTPError, e:
103         print >>sys.stderr, 'Server returned an error:', e
104         if hasattr(e, 'osc_msg'):
105             print >>sys.stderr, e.osc_msg
106
107         body = e.read()
108         if getattr(prg.options, 'debug', None) or \
109            getattr(prg.conf, 'config', {}).get('debug', None):
110             print >>sys.stderr, e.hdrs
111             print >>sys.stderr, body
112
113         if e.code in [ 400, 403, 404, 500 ]:
114             if '<summary>' in body:
115                 msg = body.split('<summary>')[1]
116                 msg = msg.split('</summary>')[0]
117                 print >>sys.stderr, msg
118
119         return 1
120
121     except URLError, e:
122         print >>sys.stderr, 'Failed to reach a server:\n', e.reason
123         return 1
124
125     except (oscerr.ConfigError, oscerr.NoConfigfile), e:
126         print >>sys.stderr, e.msg
127         return 1
128
129     except oscerr.OscIOError, e:
130         print >>sys.stderr, e.msg
131         if getattr(prg.options, 'debug', None) or \
132            getattr(prg.conf, 'config', {}).get('debug', None):
133             print >>sys.stderr, e.e
134         return 1
135
136     except (oscerr.WrongOptions, oscerr.WrongArgs), e:
137         print >>sys.stderr, e
138         return 2
139
140     except oscerr.WorkingCopyOutdated, e:
141         print >>sys.stderr, e
142         return 1
143
144     except (oscerr.PackageExists, oscerr.PackageMissing), e:
145         print >>sys.stderr, e.msg
146         return 1
147
148     except AttributeError, e:
149         print >>sys.stderr, e
150         return 1
151
152     except RPMError, e:
153         print >>sys.stderr, e
154         return 1
155
156     except SSLError, e:
157         print >>sys.stderr, "SSL Error:", e
158         return 1
159
160     except SSLVerificationError, e:
161         print >>sys.stderr, "Certificate Verification Error:", e
162         return 1
163
164     except NoSecureSSLError, e:
165         print >>sys.stderr, e
166         return 1
167
168     except OSError, e:
169         print >>sys.stderr, e
170         return 1
171
172     except CpioError, e:
173         print >>sys.stderr, e
174         return 1
175
176     except PackageError, e:
177         print >>sys.stderr, e.msg
178         return 1
179
180 # vim: sw=4 et