3
# This module contains a set of utility functions that are used
4
# throughout the dpkg suite.
6
# Copyright 2002 Wichert Akkerman <wichert@deephackmode.org>
8
# This file is free software; you can redistribute it and/or modify it
9
# 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, but
14
# WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
# 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
import os, re, string, sys
25
# Message levels as used by inform()
26
VERB_QUIET = 0 # Quiet operation (default)
27
VERB_INFORMATIVE = 1 # Informative messages
28
VERB_DETAIL = 2 # Detailed infomration on what we're doing
29
VERB_DEBUG = 3 # Debug information
32
def inform(msg, level=VERB_INFORMATIVE):
33
"Print an informative message if the verbose-level is high enough."
35
if DpkgOptions.Options["verbose"]>=level:
39
"Print a message and exit with an error."
40
sys.stderr.write(str + "\n")
44
def SlurpFile(file, sep='\n'):
45
"Read the contents of a file."
48
return string.split(fd.read(), sep)
51
def SlurpCommand(command, sep='\n'):
52
"Run a command and return its output."
59
return string.split(data, sep)
62
def __FilterData(data, regexp):
63
"Filter the data through a regexp and return the matching groups."
66
matcher=re.compile(regexp)
70
lst.append(mo.groups())
75
def FilterFile(file, regexp, sep='\n'):
76
"Read a file return the regexp matches."
78
return __FilterData(SlurpFile(file, sep), regexp)
81
def FilterCommand(command, regexp, sep='\n'):
82
"Run a command and return the regexp matches."
84
return __FilterData(SlurpCommand(command, sep), regexp)
87
def ValidPackageName(name):
88
"Check if a package name is valid"
90
if re.match("^%s$" % DpkgOptions.PackageRegex, name):
95
def ValidPackagevVersion(version):
96
"Check if a package version is valid"
98
if re.match("^%s$" % DpkgOptions.VersionRegex, version):
103
def HandleArgOption(keyword, sopt, lopt, opt, args):
104
'''Utility function for argument parsers. Check for a specific
105
option-taking argument and processes it.'''
107
DpkgOptions.Options[keyword]=args.pop(0)
110
DpkgOptions.Options[keyword]=ol[2:]
113
DpkgOptions.Options[keyword]=args.pop(0)
115
elif lopt and opt[:len(lopt)]==lopt and opt[len(lopt)]=='=':
116
DpkgOptions.Options[keyword]=opt[len(lopt)+1:]
122
def HandleNoArgOption(keyword, sopt, lopt, opt):
123
'''Utility function for argument parsers. Check for a specific
124
no-option-taking argument and processes it.'''
125
if opt==sopt or (lopt and opt==lopt):
126
DpkgOptions.Options[keyword]=DpkgOptions.Options[keyword]+1
132
# Global initialization
133
if not DpkgOptions.Options.has_key("verbose"):
134
DpkgOptions.Options["verbose"]=VERB_QUIET