#!/usr/bin/env python
# Utility functions
-# Copyright (C) 2000, 2001, 2002, 2003, 2004 James Troup <james@nocrew.org>
-# $Id: utils.py,v 1.70 2004-11-27 13:32:16 troup Exp $
+# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 James Troup <james@nocrew.org>
+# $Id: utils.py,v 1.73 2005-03-18 05:24:38 troup Exp $
################################################################################
################################################################################
-import commands, encodings.ascii, encodings.utf_8, encodings.latin_1, \
- email.Header, os, pwd, re, select, socket, shutil, string, sys, \
- tempfile, traceback;
+import codecs, commands, email.Header, os, pwd, re, select, socket, shutil, \
+ string, sys, tempfile, traceback;
import apt_pkg;
import db_access;
################################################################################
re_comments = re.compile(r"\#.*")
-re_no_epoch = re.compile(r"^\d*\:")
-re_no_revision = re.compile(r"\-[^-]*$")
+re_no_epoch = re.compile(r"^\d+\:")
+re_no_revision = re.compile(r"-[^-]+$")
re_arch_from_filename = re.compile(r"/binary-[^/]+/")
re_extract_src_version = re.compile (r"(\S+)\s*\((.*)\)")
re_isadeb = re.compile (r"(.+?)_(.+?)_(.+)\.u?deb$");
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 read 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";
"""Encodes a (header) string per RFC2047 if necessary. If the
string is neither ASCII nor UTF-8, it's assumed to be ISO-8859-1."""
try:
- encodings.ascii.Codec().decode(s);
+ codecs.lookup('ascii')[1](s)
return s;
except UnicodeError:
pass;
try:
- encodings.utf_8.Codec().decode(s);
+ codecs.lookup('utf-8')[1](s)
h = email.Header.Header(s, 'utf-8', 998);
return str(h);
except UnicodeError:
return q;
# Sort by source version
- a_version = a_changes.get("version");
- b_version = b_changes.get("version");
+ a_version = a_changes.get("version", "0");
+ b_version = b_changes.get("version", "0");
q = apt_pkg.VersionCompare(a_version, b_version);
if q:
return q;
################################################################################
-def validate_changes_file_arg(file, fatal=1):
+def validate_changes_file_arg(filename, require_changes=1):
+ """'filename' is either a .changes or .katie file. If 'filename' is a
+.katie file, it's changed to be the corresponding .changes file. The
+function then checks if the .changes file a) exists and b) is
+readable and returns the .changes filename if so. If there's a
+problem, the next action depends on the option 'require_changes'
+argument:
+
+ o If 'require_changes' == -1, errors are ignored and the .changes
+ filename is returned.
+ o If 'require_changes' == 0, a warning is given and 'None' is returned.
+ o If 'require_changes' == 1, a fatal error is raised.
+"""
error = None;
- orig_filename = file
- if file.endswith(".katie"):
- file = file[:-6]+".changes";
+ orig_filename = filename
+ if filename.endswith(".katie"):
+ filename = filename[:-6]+".changes";
- if not file.endswith(".changes"):
+ if not filename.endswith(".changes"):
error = "invalid file type; not a changes file";
else:
- if not os.access(file,os.R_OK):
- if os.path.exists(file):
+ if not os.access(filename,os.R_OK):
+ if os.path.exists(filename):
error = "permission denied";
else:
error = "file not found";
if error:
- if fatal:
+ if require_changes == 1:
fubar("%s: %s." % (orig_filename, error));
- else:
+ elif require_changes == 0:
warn("Skipping %s - %s" % (orig_filename, error));
return None;
+ else: # We only care about the .katie file
+ return filename;
else:
- return file;
+ return filename;
################################################################################
################################################################################
-def pp_dep (deps):
+def pp_deps (deps):
pp_deps = [];
for atom in deps:
(pkg, version, constraint) = atom;