~keryx-admins/keryx/1.0

« back to all changes in this revision

Viewing changes to src/libkeryx/definitions/apt_def/minideblib/DpkgUtils.py

  • Committer: Chris Oliver
  • Date: 2010-01-01 05:23:24 UTC
  • Revision ID: excid3@gmail.com-20100101052324-a3es298620cpivcm
Added minideblib

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# DpkgUtils.py
 
2
#
 
3
# This module contains a set of utility functions that are used
 
4
# throughout the dpkg suite.
 
5
#
 
6
# Copyright 2002 Wichert Akkerman <wichert@deephackmode.org>
 
7
#
 
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.
 
12
#
 
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.
 
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
import os, re, string, sys
 
23
import DpkgOptions
 
24
 
 
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
 
30
 
 
31
 
 
32
def inform(msg, level=VERB_INFORMATIVE):
 
33
        "Print an informative message if the verbose-level is high enough."
 
34
 
 
35
        if DpkgOptions.Options["verbose"]>=level:
 
36
                print msg
 
37
 
 
38
def abort(str):
 
39
        "Print a message and exit with an error."
 
40
        sys.stderr.write(str + "\n")
 
41
        sys.exit(1)
 
42
 
 
43
 
 
44
def SlurpFile(file, sep='\n'):
 
45
        "Read the contents of a file."
 
46
 
 
47
        fd=open(file, 'r')
 
48
        return string.split(fd.read(), sep)
 
49
 
 
50
 
 
51
def SlurpCommand(command, sep='\n'):
 
52
        "Run a command and return its output."
 
53
 
 
54
        fd=os.popen(command)
 
55
        data=fd.read()
 
56
        if data=='':
 
57
                return ()
 
58
        else:
 
59
                return string.split(data, sep)
 
60
 
 
61
 
 
62
def __FilterData(data, regexp):
 
63
        "Filter the data through a regexp and return the matching groups."
 
64
 
 
65
        lst=[]
 
66
        matcher=re.compile(regexp)
 
67
        for d in data:
 
68
                mo=matcher.search(d)
 
69
                if mo:
 
70
                        lst.append(mo.groups())
 
71
        
 
72
        return lst
 
73
 
 
74
 
 
75
def FilterFile(file, regexp, sep='\n'):
 
76
        "Read a file return the regexp matches."
 
77
 
 
78
        return __FilterData(SlurpFile(file, sep), regexp)
 
79
 
 
80
 
 
81
def FilterCommand(command, regexp, sep='\n'):
 
82
        "Run a command and return the regexp matches."
 
83
 
 
84
        return __FilterData(SlurpCommand(command, sep), regexp)
 
85
 
 
86
 
 
87
def ValidPackageName(name):
 
88
        "Check if a package name is valid"
 
89
 
 
90
        if re.match("^%s$" % DpkgOptions.PackageRegex, name):
 
91
                return 1
 
92
        return 0
 
93
 
 
94
 
 
95
def ValidPackagevVersion(version):
 
96
        "Check if a package version is valid"
 
97
 
 
98
        if re.match("^%s$" % DpkgOptions.VersionRegex, version):
 
99
                return 1
 
100
        return 0
 
101
 
 
102
 
 
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.'''
 
106
        if opt==sopt:
 
107
                DpkgOptions.Options[keyword]=args.pop(0)
 
108
                return 1
 
109
        elif opt[:2]==sopt:
 
110
                DpkgOptions.Options[keyword]=ol[2:]
 
111
                return 1
 
112
        elif opt==lopt:
 
113
                DpkgOptions.Options[keyword]=args.pop(0)
 
114
                return 1
 
115
        elif lopt and opt[:len(lopt)]==lopt and opt[len(lopt)]=='=':
 
116
                DpkgOptions.Options[keyword]=opt[len(lopt)+1:]
 
117
                return 1
 
118
 
 
119
        return 0
 
120
 
 
121
 
 
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
 
127
                return 1
 
128
 
 
129
        return 0
 
130
 
 
131
 
 
132
# Global initialization
 
133
if not DpkgOptions.Options.has_key("verbose"):
 
134
        DpkgOptions.Options["verbose"]=VERB_QUIET
 
135