~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/ftpmaster-tools/dak_utils.py

  • Committer: Steve Kowalik
  • Date: 2011-08-07 04:05:52 UTC
  • mto: This revision was merged to the branch mainline in revision 13626.
  • Revision ID: stevenk@ubuntu.com-20110807040552-mwnxo0flmhvl35e8
Correct the notification based on review comments, and remove request{,ed}
from the function names, switching to create{,d}.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# Utility functions from the dak suite
 
4
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006  James Troup <james@nocrew.org>
 
5
 
 
6
################################################################################
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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
 
21
 
 
22
################################################################################
 
23
 
 
24
import os
 
25
import pwd
 
26
import re
 
27
import sys
 
28
import tempfile
 
29
 
 
30
################################################################################
 
31
 
 
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*\((.*)\)")
 
36
 
 
37
################################################################################
 
38
 
 
39
def fubar(msg, exit_code=1):
 
40
    sys.stderr.write("E: %s\n" % (msg))
 
41
    sys.exit(exit_code)
 
42
 
 
43
def warn(msg):
 
44
    sys.stderr.write("W: %s\n" % (msg))
 
45
 
 
46
################################################################################
 
47
 
 
48
def prefix_multi_line_string(str, prefix, include_blank_lines=0):
 
49
    out = ""
 
50
    for line in str.split('\n'):
 
51
        line = line.strip()
 
52
        if line or include_blank_lines:
 
53
            out += "%s%s\n" % (prefix, line)
 
54
    # Strip trailing new line
 
55
    if out:
 
56
        out = out[:-1]
 
57
    return out
 
58
 
 
59
################################################################################
 
60
 
 
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.
 
66
 
 
67
def split_args (s, dwim=1):
 
68
    if not s:
 
69
        return []
 
70
 
 
71
    if s.find(",") == -1:
 
72
        return s.split();
 
73
    else:
 
74
        if s[-1:] == "," and dwim:
 
75
            fubar("split_args: found trailing comma, spurious space maybe?");
 
76
        return s.split(",");
 
77
 
 
78
################################################################################
 
79
 
 
80
def extract_component_from_section(section):
 
81
    component = "";
 
82
 
 
83
    if section.find('/') != -1:
 
84
        component = section.split('/')[0];
 
85
 
 
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" ]
 
89
 
 
90
    # Expand default component
 
91
    if component == "":
 
92
        if section in valid_components:
 
93
            component = section;
 
94
        else:
 
95
            component = "main";
 
96
 
 
97
    return (section, component);
 
98
 
 
99
################################################################################
 
100
 
 
101
def Dict(**dict): return dict
 
102
 
 
103
################################################################################
 
104
 
 
105
def our_raw_input(prompt=""):
 
106
    if prompt:
 
107
        sys.stdout.write(prompt);
 
108
    sys.stdout.flush();
 
109
    try:
 
110
        ret = raw_input();
 
111
        return ret;
 
112
    except EOFError:
 
113
        sys.stderr.write("\nUser interrupt (^D).\n");
 
114
        raise SystemExit;
 
115
 
 
116
################################################################################
 
117
 
 
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 '.'."""
 
122
 
 
123
    if directory:
 
124
        old_tempdir = tempfile.tempdir;
 
125
        tempfile.tempdir = directory;
 
126
 
 
127
    filename = tempfile.mktemp();
 
128
 
 
129
    if dotprefix:
 
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);
 
132
    os.close(fd);
 
133
 
 
134
    if directory:
 
135
        tempfile.tempdir = old_tempdir;
 
136
 
 
137
    return filename;
 
138
 
 
139
################################################################################
 
140
 
 
141
def pp_deps (deps):
 
142
    pp_deps = [];
 
143
    for atom in deps:
 
144
        (pkg, version, constraint) = atom;
 
145
        if constraint:
 
146
            pp_dep = "%s (%s %s)" % (pkg, constraint, version);
 
147
        else:
 
148
            pp_dep = pkg;
 
149
        pp_deps.append(pp_dep);
 
150
    return " |".join(pp_deps);
 
151
 
 
152
################################################################################
 
153
 
 
154
# Returns the user name with a laughable attempt at rfc822 conformancy
 
155
# (read: removing stray periods).
 
156
def whoami ():
 
157
    return pwd.getpwuid(os.getuid())[4].split(',')[0].replace('.', '');
 
158
 
 
159
################################################################################
 
160
 
 
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];
 
165
 
 
166
################################################################################
 
167
 
 
168
# Function for use in sorting lists of architectures.
 
169
# Sorts normally except that 'source' dominates all others.
 
170
 
 
171
def arch_compare_sw (a, b):
 
172
    if a == "source" and b == "source":
 
173
        return 0;
 
174
    elif a == "source":
 
175
        return -1;
 
176
    elif b == "source":
 
177
        return 1;
 
178
 
 
179
    return cmp (a, b);
 
180
 
 
181
################################################################################