提交 1d79ca7f 编写于 作者: J Joerg Jaspert

Exception handling

上级 f86fb53a
2008-05-05 Joerg Jaspert <joerg@debian.org>
* dak/queue_report.py: Use the exception class
* dak/process_unchecked.py: dito
* daklib/dak_exceptions.py: New file, central place for all those
own exceptions dak may raise.
* daklib/utils.py: Use dak_exceptions and delete all those string
exception raising stuff, which is depcreated.
During that delete the unknown_hostname_exc, as it wasnt used.
* dak/import_archive.py: use the new Exception class
* dak/rm.py: dito
* dak/generate_releases.py: dito
* dak/queue_report.py: dito
* daklib/queue.py: dito
2008-05-04 Joerg Jaspert <joerg@debian.org>
......
......@@ -132,7 +132,7 @@ def check_dscs():
f = line[:-1]
try:
utils.parse_changes(f, signing_rules=1)
except utils.invalid_dsc_format_exc, line:
except InvalidDscError, line:
utils.warn("syntax error in .dsc file '%s', line %s." % (f, line))
count += 1
......
......@@ -25,6 +25,7 @@
import sys, os, popen2, tempfile, stat, time, pg
import apt_pkg
import daklib.utils as utils
from daklib.exceptions import *
################################################################################
......@@ -107,7 +108,7 @@ def print_md5sha_files (tree, files, hashop):
else:
size = os.stat(path + name)[stat.ST_SIZE]
file_handle = utils.open_file(path + name)
except utils.cant_open_exc:
except CantOpenError:
print "ALERT: Couldn't open " + path + name
else:
hash = hashop(file_handle)
......
......@@ -40,6 +40,7 @@ import commands, os, pg, re, sys, time
import apt_pkg
import daklib.database as database
import daklib.utils as utils
from daklib.exceptions import *
###############################################################################
......@@ -323,7 +324,7 @@ def process_sources (filename, suite, component, archive):
suite_id = database.get_suite_id(suite)
try:
file = utils.open_file (filename)
except utils.cant_open_exc:
except CantOpenError:
utils.warn("can't open '%s'" % (filename))
return
Scanner = apt_pkg.ParseTagFile(file)
......@@ -406,7 +407,7 @@ def process_packages (filename, suite, component, archive):
suite_id = database.get_suite_id(suite)
try:
file = utils.open_file (filename)
except utils.cant_open_exc:
except CantOpenError:
utils.warn("can't open '%s'" % (filename))
return
Scanner = apt_pkg.ParseTagFile(file)
......
......@@ -182,19 +182,19 @@ def check_changes():
# Parse the .changes field into a dictionary
try:
changes.update(utils.parse_changes(filename))
except utils.cant_open_exc:
except CantOpenError:
reject("%s: can't read file." % (filename))
return 0
except utils.changes_parse_error_exc, line:
except ParseChangesError, line:
reject("%s: parse error, can't grok: %s." % (filename, line))
return 0
# Parse the Files field from the .changes into another dictionary
try:
files.update(utils.build_file_list(changes))
except utils.changes_parse_error_exc, line:
except ParseChangesError, line:
reject("%s: parse error, can't grok: %s." % (filename, line))
except utils.nk_format_exc, format:
except UnknownFormatError, format:
reject("%s: unknown format '%s'." % (filename, format))
return 0
......@@ -686,21 +686,21 @@ def check_dsc():
# Parse the .dsc file
try:
dsc.update(utils.parse_changes(dsc_filename, signing_rules=1))
except utils.cant_open_exc:
except CantOpenError:
# if not -n copy_to_holding() will have done this for us...
if Options["No-Action"]:
reject("%s: can't read file." % (dsc_filename))
except utils.changes_parse_error_exc, line:
except ParseChangesError, line:
reject("%s: parse error, can't grok: %s." % (dsc_filename, line))
except utils.invalid_dsc_format_exc, line:
except InvalidDscError, line:
reject("%s: syntax error on line %s." % (dsc_filename, line))
# Build up the file list of files mentioned by the .dsc
try:
dsc_files.update(utils.build_file_list(dsc, is_a_dsc=1))
except utils.no_files_exc:
except NoFilesFieldError:
reject("%s: no Files: field." % (dsc_filename))
return 0
except utils.changes_parse_error_exc, line:
except ParseChangesError, line:
reject("%s: parse error, can't grok: %s." % (dsc_filename, line))
return 0
......@@ -946,9 +946,9 @@ def check_hashes ():
try:
fs = utils.build_file_list(changes, 0, "checksums-%s" % h, h)
check_hash(".changes %s" % (h), fs, h, f, files)
except utils.no_files_exc:
except NoFilesFieldError:
reject("No Checksums-%s: field in .changes" % (h))
except utils.changes_parse_error_exc, line:
except ParseChangesError, line:
reject("parse error for Checksums-%s in .changes, can't grok: %s." % (h, line))
if "source" not in changes["architecture"]: continue
......@@ -956,9 +956,9 @@ def check_hashes ():
try:
fs = utils.build_file_list(dsc, 1, "checksums-%s" % h, h)
check_hash(".dsc %s" % (h), fs, h, f, dsc_files)
except utils.no_files_exc:
except NoFilesFieldError:
reject("No Checksums-%s: field in .dsc" % (h))
except utils.changes_parse_error_exc, line:
except ParseChangesError, line:
reject("parse error for Checksums-%s in .dsc, can't grok: %s." % (h, line))
################################################################################
......@@ -975,7 +975,7 @@ def check_hash (where, lfiles, key, testfn, basedict = None):
try:
file_handle = utils.open_file(f)
except utils.cant_open_exc:
except CantOpenError:
continue
# Check hash
......
......@@ -43,6 +43,7 @@ import commands, os, pg, re, sys
import apt_pkg, apt_inst
import daklib.database as database
import daklib.utils as utils
from daklib.exceptions import *
################################################################################
......@@ -364,7 +365,7 @@ def main ():
filename = "/".join(source_packages[i])
try:
dsc = utils.parse_changes(filename)
except utils.cant_open_exc:
except CantOpenError:
utils.warn("couldn't open '%s'." % (filename))
continue
for package in dsc.get("binary").split(','):
......
......@@ -22,6 +22,7 @@
import cPickle, errno, os, pg, re, stat, sys, time
import apt_inst, apt_pkg
import utils, database
from dak_exceptions import *
from types import *
......@@ -615,7 +616,7 @@ distribution."""
morgue_file = os.path.join(Cnf["Dir::Morgue"],Cnf["Dir::MorgueReject"],file_entry)
try:
morgue_file = utils.find_next_free(morgue_file)
except utils.tried_too_hard_exc:
except NoFreeFilenameError:
# Something's either gone badly Pete Tong, or
# someone is trying to exploit us.
utils.warn("**WARNING** failed to move %s from the reject directory to the morgue." % (file_entry))
......
......@@ -49,17 +49,6 @@ re_verwithext = re.compile(r"^(\d+)(?:\.(\d+))(?:\s+\((\S+)\))?$")
re_srchasver = re.compile(r"^(\S+)\s+\((\S+)\)$")
changes_parse_error_exc = "Can't parse line in .changes file"
invalid_dsc_format_exc = "Invalid .dsc file"
nk_format_exc = "Unknown Format: in .changes file"
no_files_exc = "No Files: field in .dsc or .changes file."
cant_open_exc = "Can't open file"
unknown_hostname_exc = "Unknown hostname"
cant_overwrite_exc = "Permission denied; can't overwrite existent file."
file_exists_exc = "Destination file exists"
sendmail_failed_exc = "Sendmail invocation failed"
tried_too_hard_exc = "Tried too hard to find a free filename."
default_config = "/etc/dak/dak.conf"
default_apt_config = "/etc/dak/apt.conf"
......@@ -72,7 +61,7 @@ def open_file(filename, mode='r'):
try:
f = open(filename, mode)
except IOError:
raise cant_open_exc, filename
raise CantOpenError, filename
return f
################################################################################
......@@ -135,7 +124,7 @@ The rules for (signing_rules == 1)-mode are:
lines = changes_in.readlines()
if not lines:
raise changes_parse_error_exc, "[Empty changes file]"
raise ParseChangesError, "[Empty changes file]"
# Reindex by line number so we can easily verify the format of
# .dsc files...
......@@ -157,10 +146,10 @@ The rules for (signing_rules == 1)-mode are:
if signing_rules == 1:
index += 1
if index > num_of_lines:
raise invalid_dsc_format_exc, index
raise InvalidDscError, index
line = indexed_lines[index]
if not line.startswith("-----BEGIN PGP SIGNATURE"):
raise invalid_dsc_format_exc, index
raise InvalidDscError, index
inside_signature = 0
break
else:
......@@ -189,7 +178,7 @@ The rules for (signing_rules == 1)-mode are:
mlf = re_multi_line_field.match(line)
if mlf:
if first == -1:
raise changes_parse_error_exc, "'%s'\n [Multi-line field continuing on from nothing?]" % (line)
raise ParseChangesError, "'%s'\n [Multi-line field continuing on from nothing?]" % (line)
if first == 1 and changes[field] != "":
changes[field] += '\n'
first = 0
......@@ -198,7 +187,7 @@ The rules for (signing_rules == 1)-mode are:
error += line
if signing_rules == 1 and inside_signature:
raise invalid_dsc_format_exc, index
raise InvalidDscError, index
changes_in.close()
changes["filecontents"] = "".join(lines)
......@@ -212,7 +201,7 @@ The rules for (signing_rules == 1)-mode are:
changes["source-version"] = srcver.group(2)
if error:
raise changes_parse_error_exc, error
raise ParseChangesError, error
return changes
......@@ -225,12 +214,12 @@ def build_file_list(changes, is_a_dsc=0, field="files", hashname="md5sum"):
# Make sure we have a Files: field to parse...
if not changes.has_key(field):
raise no_files_exc
raise NoFilesFieldError
# Make sure we recognise the format of the Files: field
format = re_verwithext.search(changes.get("format", "0.0"))
if not format:
raise nk_format_exc, "%s" % (changes.get("format","0.0"))
raise UnknownFormatError, "%s" % (changes.get("format","0.0"))
format = format.groups()
if format[1] == None:
......@@ -242,12 +231,12 @@ def build_file_list(changes, is_a_dsc=0, field="files", hashname="md5sum"):
if is_a_dsc:
if format != (1,0):
raise nk_format_exc, "%s" % (changes.get("format","0.0"))
raise UnknownFormatError, "%s" % (changes.get("format","0.0"))
else:
if (format < (1,5) or format > (1,8)):
raise nk_format_exc, "%s" % (changes.get("format","0.0"))
raise UnknownFormatError, "%s" % (changes.get("format","0.0"))
if field != "files" and format < (1,8):
raise nk_format_exc, "%s" % (changes.get("format","0.0"))
raise UnknownFormatError, "%s" % (changes.get("format","0.0"))
includes_section = (not is_a_dsc) and field == "files"
......@@ -263,7 +252,7 @@ def build_file_list(changes, is_a_dsc=0, field="files", hashname="md5sum"):
else:
(md5, size, name) = s
except ValueError:
raise changes_parse_error_exc, i
raise ParseChangesError, i
if section == "":
section = "-"
......@@ -371,7 +360,7 @@ def send_mail (message, filename=""):
# Invoke sendmail
(result, output) = commands.getstatusoutput("%s < %s" % (Cnf["Dinstall::SendmailCommand"], filename))
if (result != 0):
raise sendmail_failed_exc, output
raise SendmailFailedError, output
# Clean up any temporary files
if message:
......@@ -427,10 +416,10 @@ def copy (src, dest, overwrite = 0, perms = 0664):
# Don't overwrite unless forced to
if os.path.exists(dest):
if not overwrite:
raise file_exists_exc
raise FileExistsError
else:
if not os.access(dest, os.W_OK):
raise cant_overwrite_exc
raise CantOverwriteError
shutil.copy2(src, dest)
os.chmod(dest, perms)
......@@ -574,7 +563,7 @@ def find_next_free (dest, too_many=100):
dest = orig_dest + '.' + repr(extra)
extra += 1
if extra >= too_many:
raise tried_too_hard_exc
raise NoFreeFilenameError
return dest
################################################################################
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册