2 # Copyright (C) 2000 James Troup <james@nocrew.org>
3 # $Id: utils.py,v 1.10 2000-12-19 17:23:03 troup Exp $
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 import commands, os, re, socket, shutil, stat, string, sys, tempfile
21 re_comments = re.compile(r"\#.*")
22 re_no_epoch = re.compile(r"^\d*\:")
23 re_no_revision = re.compile(r"\-[^-]*$")
24 re_arch_from_filename = re.compile(r"/binary-[^/]+/")
25 re_extract_src_version = re.compile (r"(\S+)\s*\((.*)\)")
27 changes_parse_error_exc = "Can't parse line in .changes file";
28 nk_format_exc = "Unknown Format: in .changes file";
29 no_files_exc = "No Files: field in .dsc file.";
30 cant_open_exc = "Can't read file.";
31 unknown_hostname_exc = "Unknown hostname";
32 cant_overwrite_exc = "Permission denied; can't overwrite existent file."
34 ######################################################################################
36 def open_file(filename, mode):
38 f = open(filename, mode);
40 raise cant_open_exc, filename
43 ######################################################################################
52 sys.stderr.write('\nUser interrupt (^D).\n')
55 ######################################################################################
57 def parse_changes(filename):
58 changes_in = open_file(filename,'r');
61 lines = changes_in.readlines();
63 if re.match('^-----BEGIN PGP SIGNATURE', line):
65 if re.match(r'^\s*$|^-----BEGIN PGP SIGNED MESSAGE', line):
67 slf = re.match(r'^(\S*)\s*:\s*(.*)', line);
69 field = string.lower(slf.groups()[0]);
70 changes[field] = slf.groups()[1];
72 mld = re.match(r'^ \.$', line);
74 changes[field] = changes[field] + '\n';
76 mlf = re.match(r'^\s(.*)', line);
78 changes[field] = changes[field] + mlf.groups()[0] + '\n';
82 changes["filecontents"] = string.join (lines, "");
84 raise changes_parse_error_exc, error;
87 ######################################################################################
89 # Dropped support for 1.4 and ``buggy dchanges 3.4'' (?!) compared to di.pl
91 def build_file_list(changes, dsc):
93 format = changes.get("format", "")
95 format = float(format)
96 if dsc == "" and (format < 1.5 or format > 2.0):
97 raise nk_format_exc, changes["format"];
99 # No really, this has happened. Think 0 length .dsc file.
100 if not changes.has_key("files"):
103 for i in string.split(changes["files"], "\n"):
107 section = priority = component = ""
109 (md5, size, name) = s
111 (md5, size, section, priority, name) = s
113 if section == "": section = "-"
114 if priority == "": priority = "-"
117 if string.find(section, '/') != -1:
118 component = string.split(section, '/')[0]
119 if string.lower(component) == "non-us" and string.count(section, '/') > 0:
120 s = string.split(section, '/')[1]
121 if s == "main" or s == "non-free" or s == "contrib": # Avoid e.g. non-US/libs
122 component = string.split(section, '/')[0]+ '/' + string.split(section, '/')[1]
124 if string.lower(section) == "non-us":
125 component = "non-US/main";
129 elif string.lower(component) == "non-us":
130 component = "non-US/main";
132 files[name] = { "md5sum" : md5,
135 "priority": priority,
136 "component": component }
140 ######################################################################################
142 # Fix the `Maintainer:' field to be an RFC822 compatible address.
143 # cf. Packaging Manual (4.2.4)
145 # 06:28|<Culus> 'The standard sucks, but my tool is supposed to
146 # interoperate with it. I know - I'll fix the suckage
147 # and make things incompatible!'
149 def fix_maintainer (maintainer):
150 m = re.match(r"^\s*(\S.*\S)\s*\<([^\> \t]+)\>", maintainer)
154 if m != None and len(m.groups()) == 2:
157 if re.search(r'[,.]', name) != None:
158 rfc822 = re.sub(r"^\s*(\S.*\S)\s*\<([^\> \t]+)\>", r"\2 (\1)", maintainer)
159 return (rfc822, name, email)
161 ######################################################################################
163 # sendmail wrapper, takes _either_ a message string or a file as arguments
164 def send_mail (message, filename):
165 #### FIXME, how do I get this out of Cnf in katie?
166 sendmail_command = "/usr/sbin/sendmail -odq -oi -t";
168 # Sanity check arguments
169 if message != "" and filename != "":
170 sys.stderr.write ("send_mail() can't be called with both arguments as non-null! (`%s' and `%s')\n%s" % (message, filename))
172 # If we've been passed a string dump it into a temporary file
174 filename = tempfile.mktemp()
175 fd = os.open(filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700)
176 os.write (fd, message)
179 (result, output) = commands.getstatusoutput("%s < %s" % (sendmail_command, filename))
181 sys.stderr.write ("Sendmail invocation (`%s') failed for `%s'!\n%s" % (sendmail_command, filename, output))
183 # Clean up any temporary files
187 ######################################################################################
189 def poolify (source, component):
191 component = component + '/';
192 # FIXME: this is nasty
193 component = string.lower(component);
194 component = string.replace(component, 'non-us/', 'non-US/');
195 if source[:3] == "lib":
196 return component + source[:4] + '/' + source + '/'
198 return component + source[:1] + '/' + source + '/'
200 ######################################################################################
202 def move (src, dest):
203 if os.path.exists(dest) and os.path.isdir(dest):
206 dest_dir = os.path.dirname(dest);
207 if not os.path.exists(dest_dir):
208 umask = os.umask(00000);
209 os.makedirs(dest_dir, 02775);
211 #print "Moving %s to %s..." % (src, dest);
212 if os.path.exists(dest) and os.path.isdir(dest):
213 dest = dest + '/' + os.path.basename(src);
214 # Check for overwrite permission on existent files
215 if os.path.exists(dest) and not os.access(dest, os.W_OK):
216 raise cant_overwrite_exc
217 shutil.copy2(src, dest);
218 os.chmod(dest, 0664);
221 def copy (src, dest):
222 if os.path.exists(dest) and os.path.isdir(dest):
225 dest_dir = os.path.dirname(dest);
226 if not os.path.exists(dest_dir):
227 umask = os.umask(00000);
228 os.makedirs(dest_dir, 02775);
230 #print "Copying %s to %s..." % (src, dest);
231 if os.path.exists(dest) and os.path.isdir(dest):
232 dest = dest + '/' + os.path.basename(src);
233 if os.path.exists(dest) and not os.access(dest, os.W_OK):
234 raise cant_overwrite_exc
235 shutil.copy2(src, dest);
236 os.chmod(dest, 0664);
238 ######################################################################################
240 # FIXME: this is inherently nasty. Can't put this mapping in a conf
241 # file because the conf file depends on the archive.. doh. Maybe an
242 # archive independent conf file is needed.
245 res = socket.gethostbyaddr(socket.gethostname());
246 if res[0] == 'pandora.debian.org':
248 elif res[0] == 'auric.debian.org':
251 raise unknown_hostname_exc, res;
253 ######################################################################################
255 # FIXME: this isn't great either.
257 def which_conf_file ():
258 archive = where_am_i ();
259 if archive == 'non-US':
260 return '/org/non-us.debian.org/katie/katie.conf-non-US';
261 elif archive == 'ftp-master':
262 return '/org/ftp.debian.org/katie/katie.conf';
264 raise unknown_hostname_exc, archive
266 ######################################################################################
268 # Escape characters which have meaning to SQL's regex comparison operator ('~')
269 # (woefully incomplete)
272 s = string.replace(s, '+', '\\\\+');
275 ######################################################################################
285 return ("%d%s" % (c, t))