Exception handling improvements

* Replace some prints with proper logging messages
* Make VCSException as verbose as BuildException, including error output
This commit is contained in:
Daniel Martí 2014-07-02 15:30:05 +02:00
parent c64aafaf42
commit d132adf63c
4 changed files with 47 additions and 46 deletions

View file

@ -1050,20 +1050,23 @@ def main():
logfile.write(str(be)) logfile.write(str(be))
logfile.close() logfile.close()
reason = str(be).split('\n', 1)[0] if options.verbose else str(be) reason = str(be).split('\n', 1)[0] if options.verbose else str(be)
print("Could not build app %s due to BuildException: %s" % ( logging.error("Could not build app %s due to BuildException: %s" % (
app['id'], reason)) app['id'], reason))
if options.stop: if options.stop:
sys.exit(1) sys.exit(1)
failed_apps[app['id']] = be failed_apps[app['id']] = be
wikilog = be.get_wikitext() wikilog = be.get_wikitext()
except VCSException as vcse: except VCSException as vcse:
print("VCS error while building app %s: %s" % (app['id'], vcse)) reason = str(vcse).split('\n', 1)[0] if options.verbose else str(vcse)
logging.error("VCS error while building app %s: %s" % (
app['id'], reason))
if options.stop: if options.stop:
sys.exit(1) sys.exit(1)
failed_apps[app['id']] = vcse failed_apps[app['id']] = vcse
wikilog = str(vcse) wikilog = str(vcse)
except Exception as e: except Exception as e:
print("Could not build app %s due to unknown error: %s" % (app['id'], traceback.format_exc())) logging.error("Could not build app %s due to unknown error: %s" % (
app['id'], traceback.format_exc()))
if options.stop: if options.stop:
sys.exit(1) sys.exit(1)
failed_apps[app['id']] = e failed_apps[app['id']] = e

View file

@ -32,8 +32,7 @@ import logging
import common import common
import metadata import metadata
from common import BuildException from common import BuildException, VCSException
from common import VCSException
from metadata import MetaDataException from metadata import MetaDataException

View file

@ -433,42 +433,42 @@ class vcs_git(vcs):
# Brand new checkout # Brand new checkout
p = FDroidPopen(['git', 'clone', self.remote, self.local]) p = FDroidPopen(['git', 'clone', self.remote, self.local])
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git clone failed") raise VCSException("Git clone failed", p.output)
self.checkrepo() self.checkrepo()
else: else:
self.checkrepo() self.checkrepo()
# Discard any working tree changes # Discard any working tree changes
p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local) p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git reset failed") raise VCSException("Git reset failed", p.output)
# Remove untracked files now, in case they're tracked in the target # Remove untracked files now, in case they're tracked in the target
# revision (it happens!) # revision (it happens!)
p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local) p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git clean failed") raise VCSException("Git clean failed", p.output)
if not self.refreshed: if not self.refreshed:
# Get latest commits and tags from remote # Get latest commits and tags from remote
p = FDroidPopen(['git', 'fetch', 'origin'], cwd=self.local) p = FDroidPopen(['git', 'fetch', 'origin'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git fetch failed") raise VCSException("Git fetch failed", p.output)
p = SilentPopen(['git', 'fetch', '--prune', '--tags', 'origin'], cwd=self.local) p = SilentPopen(['git', 'fetch', '--prune', '--tags', 'origin'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git fetch failed") raise VCSException("Git fetch failed", p.output)
# Recreate origin/HEAD as git clone would do it, in case it disappeared # Recreate origin/HEAD as git clone would do it, in case it disappeared
p = SilentPopen(['git', 'remote', 'set-head', 'origin', '--auto'], cwd=self.local) p = SilentPopen(['git', 'remote', 'set-head', 'origin', '--auto'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git remote set-head failed") raise VCSException("Git remote set-head failed", p.output)
self.refreshed = True self.refreshed = True
# origin/HEAD is the HEAD of the remote, e.g. the "default branch" on # origin/HEAD is the HEAD of the remote, e.g. the "default branch" on
# a github repo. Most of the time this is the same as origin/master. # a github repo. Most of the time this is the same as origin/master.
rev = str(rev if rev else 'origin/HEAD') rev = str(rev if rev else 'origin/HEAD')
p = SilentPopen(['git', 'checkout', '-f', rev], cwd=self.local) p = SilentPopen(['git', 'checkout', '-f', rev], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git checkout of '%s' failed" % rev) raise VCSException("Git checkout of '%s' failed" % rev, p.output)
# Get rid of any uncontrolled files left behind # Get rid of any uncontrolled files left behind
p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local) p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git clean failed") raise VCSException("Git clean failed", p.output)
def initsubmodules(self): def initsubmodules(self):
self.checkrepo() self.checkrepo()
@ -491,13 +491,13 @@ class vcs_git(vcs):
]: ]:
p = SilentPopen(['git', 'submodule', 'foreach', '--recursive'] + cmd, cwd=self.local) p = SilentPopen(['git', 'submodule', 'foreach', '--recursive'] + cmd, cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git submodule reset failed") raise VCSException("Git submodule reset failed", p.output)
p = FDroidPopen(['git', 'submodule', 'sync'], cwd=self.local) p = FDroidPopen(['git', 'submodule', 'sync'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git submodule sync failed") raise VCSException("Git submodule sync failed", p.output)
p = FDroidPopen(['git', 'submodule', 'update', '--init', '--force', '--recursive'], cwd=self.local) p = FDroidPopen(['git', 'submodule', 'update', '--init', '--force', '--recursive'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git submodule update failed") raise VCSException("Git submodule update failed", p.output)
def gettags(self): def gettags(self):
self.checkrepo() self.checkrepo()
@ -550,23 +550,23 @@ class vcs_gitsvn(vcs):
gitsvn_cmd += ' -b %s' % i[9:] gitsvn_cmd += ' -b %s' % i[9:]
p = SilentPopen([gitsvn_cmd + " %s %s" % (remote_split[0], self.local)], shell=True) p = SilentPopen([gitsvn_cmd + " %s %s" % (remote_split[0], self.local)], shell=True)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git clone failed") raise VCSException("Git clone failed", p.output)
else: else:
p = SilentPopen([gitsvn_cmd + " %s %s" % (self.remote, self.local)], shell=True) p = SilentPopen([gitsvn_cmd + " %s %s" % (self.remote, self.local)], shell=True)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git clone failed") raise VCSException("Git clone failed", p.output)
self.checkrepo() self.checkrepo()
else: else:
self.checkrepo() self.checkrepo()
# Discard any working tree changes # Discard any working tree changes
p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local) p = SilentPopen(['git', 'reset', '--hard'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git reset failed") raise VCSException("Git reset failed", p.output)
# Remove untracked files now, in case they're tracked in the target # Remove untracked files now, in case they're tracked in the target
# revision (it happens!) # revision (it happens!)
p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local) p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git clean failed") raise VCSException("Git clean failed", p.output)
if not self.refreshed: if not self.refreshed:
# Get new commits, branches and tags from repo # Get new commits, branches and tags from repo
p = SilentPopen(['%sgit svn fetch %s' % self.userargs()], cwd=self.local, shell=True) p = SilentPopen(['%sgit svn fetch %s' % self.userargs()], cwd=self.local, shell=True)
@ -574,7 +574,7 @@ class vcs_gitsvn(vcs):
raise VCSException("Git svn fetch failed") raise VCSException("Git svn fetch failed")
p = SilentPopen(['%sgit svn rebase %s' % self.userargs()], cwd=self.local, shell=True) p = SilentPopen(['%sgit svn rebase %s' % self.userargs()], cwd=self.local, shell=True)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git svn rebase failed") raise VCSException("Git svn rebase failed", p.output)
self.refreshed = True self.refreshed = True
rev = str(rev if rev else 'master') rev = str(rev if rev else 'master')
@ -603,17 +603,17 @@ class vcs_gitsvn(vcs):
# Try a plain git checkout as a last resort # Try a plain git checkout as a last resort
p = SilentPopen(['git', 'checkout', rev], cwd=self.local) p = SilentPopen(['git', 'checkout', rev], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("No git treeish found and direct git checkout of '%s' failed" % rev) raise VCSException("No git treeish found and direct git checkout of '%s' failed" % rev, p.output)
else: else:
# Check out the git rev equivalent to the svn rev # Check out the git rev equivalent to the svn rev
p = SilentPopen(['git', 'checkout', git_rev], cwd=self.local) p = SilentPopen(['git', 'checkout', git_rev], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git svn checkout of '%s' failed" % rev) raise VCSException("Git svn checkout of '%s' failed" % rev, p.output)
# Get rid of any uncontrolled files left behind # Get rid of any uncontrolled files left behind
p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local) p = SilentPopen(['git', 'clean', '-dffx'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Git clean failed") raise VCSException("Git clean failed", p.output)
def gettags(self): def gettags(self):
self.checkrepo() self.checkrepo()
@ -643,24 +643,24 @@ class vcs_svn(vcs):
if not os.path.exists(self.local): if not os.path.exists(self.local):
p = SilentPopen(['svn', 'checkout', self.remote, self.local] + self.userargs()) p = SilentPopen(['svn', 'checkout', self.remote, self.local] + self.userargs())
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Svn checkout of '%s' failed" % rev) raise VCSException("Svn checkout of '%s' failed" % rev, p.output)
else: else:
for svncommand in ( for svncommand in (
'svn revert -R .', 'svn revert -R .',
r"svn status | awk '/\?/ {print $2}' | xargs rm -rf"): r"svn status | awk '/\?/ {print $2}' | xargs rm -rf"):
p = SilentPopen([svncommand], cwd=self.local, shell=True) p = SilentPopen([svncommand], cwd=self.local, shell=True)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Svn reset ({0}) failed in {1}".format(svncommand, self.local)) raise VCSException("Svn reset ({0}) failed in {1}".format(svncommand, self.local), p.output)
if not self.refreshed: if not self.refreshed:
p = SilentPopen(['svn', 'update'] + self.userargs(), cwd=self.local) p = SilentPopen(['svn', 'update'] + self.userargs(), cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Svn update failed") raise VCSException("Svn update failed", p.output)
self.refreshed = True self.refreshed = True
revargs = list(['-r', rev] if rev else []) revargs = list(['-r', rev] if rev else [])
p = SilentPopen(['svn', 'update', '--force'] + revargs + self.userargs(), cwd=self.local) p = SilentPopen(['svn', 'update', '--force'] + revargs + self.userargs(), cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Svn update failed") raise VCSException("Svn update failed", p.output)
def getref(self): def getref(self):
p = SilentPopen(['svn', 'info'], cwd=self.local) p = SilentPopen(['svn', 'info'], cwd=self.local)
@ -679,15 +679,15 @@ class vcs_hg(vcs):
if not os.path.exists(self.local): if not os.path.exists(self.local):
p = SilentPopen(['hg', 'clone', self.remote, self.local]) p = SilentPopen(['hg', 'clone', self.remote, self.local])
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Hg clone failed") raise VCSException("Hg clone failed", p.output)
else: else:
p = SilentPopen(['hg status -uS | xargs rm -rf'], cwd=self.local, shell=True) p = SilentPopen(['hg status -uS | xargs rm -rf'], cwd=self.local, shell=True)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Hg clean failed") raise VCSException("Hg clean failed", p.output)
if not self.refreshed: if not self.refreshed:
p = SilentPopen(['hg', 'pull'], cwd=self.local) p = SilentPopen(['hg', 'pull'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Hg pull failed") raise VCSException("Hg pull failed", p.output)
self.refreshed = True self.refreshed = True
rev = str(rev if rev else 'default') rev = str(rev if rev else 'default')
@ -695,7 +695,7 @@ class vcs_hg(vcs):
return return
p = SilentPopen(['hg', 'update', '-C', rev], cwd=self.local) p = SilentPopen(['hg', 'update', '-C', rev], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Hg checkout of '%s' failed" % rev) raise VCSException("Hg checkout of '%s' failed" % rev, p.output)
p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local) p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
# Also delete untracked files, we have to enable purge extension for that: # Also delete untracked files, we have to enable purge extension for that:
if "'purge' is provided by the following extension" in p.output: if "'purge' is provided by the following extension" in p.output:
@ -703,9 +703,9 @@ class vcs_hg(vcs):
myfile.write("\n[extensions]\nhgext.purge=\n") myfile.write("\n[extensions]\nhgext.purge=\n")
p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local) p = SilentPopen(['hg', 'purge', '--all'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("HG purge failed") raise VCSException("HG purge failed", p.output)
elif p.returncode != 0: elif p.returncode != 0:
raise VCSException("HG purge failed") raise VCSException("HG purge failed", p.output)
def gettags(self): def gettags(self):
p = SilentPopen(['hg', 'tags', '-q'], cwd=self.local) p = SilentPopen(['hg', 'tags', '-q'], cwd=self.local)
@ -721,21 +721,21 @@ class vcs_bzr(vcs):
if not os.path.exists(self.local): if not os.path.exists(self.local):
p = SilentPopen(['bzr', 'branch', self.remote, self.local]) p = SilentPopen(['bzr', 'branch', self.remote, self.local])
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Bzr branch failed") raise VCSException("Bzr branch failed", p.output)
else: else:
p = SilentPopen(['bzr', 'clean-tree', '--force', '--unknown', '--ignored'], cwd=self.local) p = SilentPopen(['bzr', 'clean-tree', '--force', '--unknown', '--ignored'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Bzr revert failed") raise VCSException("Bzr revert failed", p.output)
if not self.refreshed: if not self.refreshed:
p = SilentPopen(['bzr', 'pull'], cwd=self.local) p = SilentPopen(['bzr', 'pull'], cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Bzr update failed") raise VCSException("Bzr update failed", p.output)
self.refreshed = True self.refreshed = True
revargs = list(['-r', rev] if rev else []) revargs = list(['-r', rev] if rev else [])
p = SilentPopen(['bzr', 'revert'] + revargs, cwd=self.local) p = SilentPopen(['bzr', 'revert'] + revargs, cwd=self.local)
if p.returncode != 0: if p.returncode != 0:
raise VCSException("Bzr revert of '%s' failed" % rev) raise VCSException("Bzr revert of '%s' failed" % rev, p.output)
def gettags(self): def gettags(self):
p = SilentPopen(['bzr', 'tags'], cwd=self.local) p = SilentPopen(['bzr', 'tags'], cwd=self.local)
@ -943,7 +943,7 @@ def parse_androidmanifests(paths, ignoreversions=None):
return (max_version, max_vercode, max_package) return (max_version, max_vercode, max_package)
class BuildException(Exception): class _FDroidException(Exception):
def __init__(self, value, detail=None): def __init__(self, value, detail=None):
self.value = value self.value = value
self.detail = detail self.detail = detail
@ -965,12 +965,12 @@ class BuildException(Exception):
return ret return ret
class VCSException(Exception): class VCSException(_FDroidException):
def __init__(self, value): pass
self.value = value
def __str__(self):
return self.value class BuildException(_FDroidException):
pass
# Get the specified source library. # Get the specified source library.

View file

@ -24,8 +24,7 @@ import logging
import common import common
import metadata import metadata
from common import BuildException from common import BuildException, VCSException
from common import VCSException
config = None config = None
options = None options = None