# Utility functions
# Copyright (C) 2000 James Troup <james@nocrew.org>
-# $Id: utils.py,v 1.5 2000-11-30 04:19:30 troup Exp $
+# $Id: utils.py,v 1.12 2001-01-25 06:00:07 troup Exp $
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
no_files_exc = "No Files: field in .dsc file.";
cant_open_exc = "Can't read file.";
unknown_hostname_exc = "Unknown hostname";
+cant_overwrite_exc = "Permission denied; can't overwrite existent file."
######################################################################################
######################################################################################
+# What a mess. FIXME
+def extract_component_from_section(section):
+ component = "";
+
+ if string.find(section, '/') != -1:
+ component = string.split(section, '/')[0];
+ if string.lower(component) == "non-us" and string.count(section, '/') > 0:
+ s = string.split(section, '/')[1];
+ if s == "main" or s == "non-free" or s == "contrib": # Avoid e.g. non-US/libs
+ component = string.split(section, '/')[0]+ '/' + string.split(section, '/')[1];
+
+ if string.lower(section) == "non-us":
+ component = "non-US/main";
+
+ if component == "":
+ component = "main";
+ elif string.lower(component) == "non-us":
+ component = "non-US/main";
+
+ return (section, component);
+
+######################################################################################
+
def parse_changes(filename):
changes_in = open_file(filename,'r');
error = ""
if i == "":
break
s = string.split(i)
- section = priority = component = ""
- if dsc != "":
- (md5, size, name) = s
- else:
- (md5, size, section, priority, name) = s
+ section = priority = "";
+ try:
+ if dsc != "":
+ (md5, size, name) = s
+ else:
+ (md5, size, section, priority, name) = s
+ except ValueError:
+ raise changes_parse_error_exc, i
if section == "": section = "-"
if priority == "": priority = "-"
- # What a mess. FIXME
- if string.find(section, '/') != -1:
- component = string.split(section, '/')[0]
- if string.lower(component) == "non-us" and string.count(section, '/') > 1:
- s = string.split(section, '/')[1]
- if s == "main" or s == "non-free" or s == "contrib": # Avoid e.g. non-US/libs
- component = string.split(section, '/')[0]+ '/' + string.split(section, '/')[1]
-
- if string.lower(section) == "non-us":
- component = "non-US/main";
-
- if component == "":
- component = "main";
- elif string.lower(component) == "non-us":
- component = "non-US/main";
+ (section, component) = extract_component_from_section(section);
files[name] = { "md5sum" : md5,
"size" : size,
def poolify (source, component):
if component != "":
component = component + '/';
+ # FIXME: this is nasty
+ component = string.lower(component);
+ component = string.replace(component, 'non-us/', 'non-US/');
if source[:3] == "lib":
return component + source[:4] + '/' + source + '/'
else:
######################################################################################
def move (src, dest):
- if os.path.exists(dest) and stat.S_ISDIR(os.stat(dest)[stat.ST_MODE]):
+ if os.path.exists(dest) and os.path.isdir(dest):
dest_dir = dest;
else:
dest_dir = os.path.dirname(dest);
os.makedirs(dest_dir, 02775);
os.umask(umask);
#print "Moving %s to %s..." % (src, dest);
- shutil.copy2(src, dest);
- if os.path.exists(dest) and stat.S_ISDIR(os.stat(dest)[stat.ST_MODE]):
+ if os.path.exists(dest) and os.path.isdir(dest):
dest = dest + '/' + os.path.basename(src);
+ # Check for overwrite permission on existent files
+ if os.path.exists(dest) and not os.access(dest, os.W_OK):
+ raise cant_overwrite_exc
+ shutil.copy2(src, dest);
os.chmod(dest, 0664);
os.unlink(src);
def copy (src, dest):
- if os.path.exists(dest) and stat.S_ISDIR(os.stat(dest)[stat.ST_MODE]):
+ if os.path.exists(dest) and os.path.isdir(dest):
dest_dir = dest;
else:
dest_dir = os.path.dirname(dest);
os.makedirs(dest_dir, 02775);
os.umask(umask);
#print "Copying %s to %s..." % (src, dest);
- shutil.copy2(src, dest);
- if os.path.exists(dest) and stat.S_ISDIR(os.stat(dest)[stat.ST_MODE]):
+ if os.path.exists(dest) and os.path.isdir(dest):
dest = dest + '/' + os.path.basename(src);
+ if os.path.exists(dest) and not os.access(dest, os.W_OK):
+ raise cant_overwrite_exc
+ shutil.copy2(src, dest);
os.chmod(dest, 0664);
######################################################################################
######################################################################################
+# Escape characters which have meaning to SQL's regex comparison operator ('~')
+# (woefully incomplete)
+
+def regex_safe (s):
+ s = string.replace(s, '+', '\\\\+');
+ return s
+
+######################################################################################
+
+def size_type (c):
+ t = " b";
+ if c > 10000:
+ c = c / 1000;
+ t = " Kb";
+ if c > 10000:
+ c = c / 1000;
+ t = " Mb";
+ return ("%d%s" % (c, t))