3
# Utility functions from the dak suite
4
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 James Troup <james@nocrew.org>
6
################################################################################
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
20
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
################################################################################
30
################################################################################
32
re_single_line_field = re.compile(r"^(\S*)\s*:\s*(.*)")
33
re_multi_line_field = re.compile(r"^\s(.*)")
34
re_no_epoch = re.compile(r"^\d+\:")
35
re_extract_src_version = re.compile(r"(\S+)\s*\((.*)\)")
37
################################################################################
39
def fubar(msg, exit_code=1):
40
sys.stderr.write("E: %s\n" % (msg))
44
sys.stderr.write("W: %s\n" % (msg))
46
################################################################################
48
def prefix_multi_line_string(str, prefix, include_blank_lines=0):
50
for line in str.split('\n'):
52
if line or include_blank_lines:
53
out += "%s%s\n" % (prefix, line)
54
# Strip trailing new line
59
################################################################################
61
# Split command line arguments which can be separated by either commas
62
# or whitespace. If dwim is set, it will complain about string ending
63
# in comma since this usually means someone did 'madison -a i386, m68k
64
# foo' or something and the inevitable confusion resulting from 'm68k'
65
# being treated as an argument is undesirable.
67
def split_args (s, dwim=1):
74
if s[-1:] == "," and dwim:
75
fubar("split_args: found trailing comma, spurious space maybe?");
78
################################################################################
80
def extract_component_from_section(section):
83
if section.find('/') != -1:
84
component = section.split('/')[0];
86
# XXX James Troup 2006-01-30:
87
# We don't have Cnf, don't want to use DB particularly, so...
88
valid_components = [ "main", "restricted", "universe", "multiverse", "contrib", "non-free" ]
90
# Expand default component
92
if section in valid_components:
97
return (section, component);
99
################################################################################
101
def Dict(**dict): return dict
103
################################################################################
105
def our_raw_input(prompt=""):
107
sys.stdout.write(prompt);
113
sys.stderr.write("\nUser interrupt (^D).\n");
116
################################################################################
118
def temp_filename(directory=None, dotprefix=None, perms=0700):
119
"""Return a secure and unique filename by pre-creating it.
120
If 'directory' is non-null, it will be the directory the file is pre-created in.
121
If 'dotprefix' is non-null, the filename will be prefixed with a '.'."""
124
old_tempdir = tempfile.tempdir;
125
tempfile.tempdir = directory;
127
filename = tempfile.mktemp();
130
filename = "%s/.%s" % (os.path.dirname(filename), os.path.basename(filename));
131
fd = os.open(filename, os.O_RDWR|os.O_CREAT|os.O_EXCL, perms);
135
tempfile.tempdir = old_tempdir;
139
################################################################################
144
(pkg, version, constraint) = atom;
146
pp_dep = "%s (%s %s)" % (pkg, constraint, version);
149
pp_deps.append(pp_dep);
150
return " |".join(pp_deps);
152
################################################################################
154
# Returns the user name with a laughable attempt at rfc822 conformancy
155
# (read: removing stray periods).
157
return pwd.getpwuid(os.getuid())[4].split(',')[0].replace('.', '');
159
################################################################################
161
def join_with_commas_and(list):
162
if len(list) == 0: return "nothing";
163
if len(list) == 1: return list[0];
164
return ", ".join(list[:-1]) + " and " + list[-1];
166
################################################################################
168
# Function for use in sorting lists of architectures.
169
# Sorts normally except that 'source' dominates all others.
171
def arch_compare_sw (a, b):
172
if a == "source" and b == "source":
181
################################################################################