+
+################################################################################
+
+def cc_fix_changes (changes):
+ o = changes.get("architecture", "")
+ if o != "":
+ del changes["architecture"]
+ changes["architecture"] = {}
+ for j in string.split(o):
+ changes["architecture"][j] = 1
+
+# Sort by source name, source version, 'have source', and then by filename
+def changes_compare (a, b):
+ try:
+ a_changes = parse_changes(a);
+ except:
+ return -1;
+
+ try:
+ b_changes = parse_changes(b);
+ except:
+ return 1;
+
+ cc_fix_changes (a_changes);
+ cc_fix_changes (b_changes);
+
+ # Sort by source name
+ a_source = a_changes.get("source");
+ b_source = b_changes.get("source");
+ q = cmp (a_source, b_source);
+ if q:
+ return q;
+
+ # Sort by source version
+ a_version = a_changes.get("version");
+ b_version = b_changes.get("version");
+ q = apt_pkg.VersionCompare(a_version, b_version);
+ if q:
+ return q;
+
+ # Sort by 'have source'
+ a_has_source = a_changes["architecture"].get("source");
+ b_has_source = b_changes["architecture"].get("source");
+ if a_has_source and not b_has_source:
+ return -1;
+ elif b_has_source and not a_has_source:
+ return 1;
+
+ # Fall back to sort by filename
+ return cmp(a, b);
+
+################################################################################
+
+def find_next_free (dest, too_many=100):
+ extra = 0;
+ orig_dest = dest;
+ while os.path.exists(dest) and extra < too_many:
+ dest = orig_dest + '.' + repr(extra);
+ extra = extra + 1;
+ if extra >= too_many:
+ raise tried_too_hard_exc;
+ return dest;
+
+################################################################################
+
+def result_join (original, sep = '\t'):
+ list = [];
+ for i in xrange(len(original)):
+ if original[i] == None:
+ list.append("");
+ else:
+ list.append(original[i]);
+ return string.join(list, sep);
+
+################################################################################
+
+def prefix_multi_line_string(str, prefix):
+ out = "";
+ for line in string.split(str, '\n'):
+ line = string.strip(line);
+ if line:
+ out = out + "%s%s\n" % (prefix, line);
+ # Strip trailing new line
+ if out:
+ out = out[:-1];
+ return out;
+
+################################################################################
+
+def validate_changes_file_arg(file, fatal=1):
+ error = None;
+
+ if file[-6:] == ".katie":
+ file = file[:-6]+".changes";
+
+ if file[-8:] != ".changes":
+ error = "invalid file type; not a changes file";
+ else:
+ if not os.access(file,os.R_OK):
+ if os.path.exists(file):
+ error = "permission denied";
+ else:
+ error = "file not found";
+
+ if error:
+ if fatal:
+ fubar("%s: %s." % (file, error));
+ else:
+ warn("Skipping %s - %s" % (file, error));
+ return None;
+ else:
+ return file;
+
+################################################################################
+
+def get_conf():
+ return Cnf;
+
+################################################################################
+
+apt_pkg.init()
+
+Cnf = apt_pkg.newConfiguration();
+apt_pkg.ReadConfigFileISC(Cnf,default_config);
+
+if which_conf_file() != default_config:
+ apt_pkg.ReadConfigFileISC(Cnf,which_conf_file())
+
+################################################################################