~ubuntu-branches/ubuntu/quantal/lsb/quantal-proposed

« back to all changes in this revision

Viewing changes to initdutils.py

  • Committer: Bazaar Package Importer
  • Author(s): Tollef Fog Heen
  • Date: 2004-09-20 09:29:54 UTC
  • Revision ID: james.westby@ubuntu.com-20040920092954-d90b1hxc5iccxfgo
Tags: 1.3-9ubuntu7
prerm, postinst: Add support for amd64. (closes: #1354)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Support for scanning init scripts for LSB info
2
2
 
3
 
import re, sys, os
4
 
 
5
 
FACILITIES = '/var/lib/lsb/facilities'
 
3
import re, sys, os, cStringIO
 
4
 
 
5
try:
 
6
    assert True
 
7
except:
 
8
    True = 1
 
9
    False = 0
 
10
 
 
11
class RFC822Parser(dict):
 
12
    "A dictionary-like object."
 
13
    __linere = re.compile(r'([^:]+):\s*(.*)$')
 
14
    
 
15
    def __init__(self, fileob=None, strob=None, startcol=0, basedict=None):
 
16
        if not fileob and not strob:
 
17
            raise ValueError, 'need a file or string'
 
18
        if not basedict:
 
19
            basedict = {}
 
20
        
 
21
        super(RFC822Parser, self).__init__(basedict)
 
22
 
 
23
        if not fileob:
 
24
            fileob = cStringIO.StringIO(strob)
 
25
 
 
26
        key = None
 
27
        for line in fileob:
 
28
            if startcol:
 
29
                line = line[startcol:]
 
30
 
 
31
            if not line.strip():
 
32
                continue
 
33
 
 
34
            # Continuation line
 
35
            if line[0].isspace():
 
36
                if not key:
 
37
                    continue
 
38
                self[key] += '\n' + line.strip()
 
39
                continue
 
40
 
 
41
            m = self.__linere.match(line)
 
42
            if not m:
 
43
                # Not a valid RFC822 header
 
44
                continue
 
45
            key, value = m.groups()
 
46
            self[key] = value.strip()
 
47
 
 
48
# End of RFC882Parser
 
49
 
 
50
LSBLIB = '/var/lib/lsb'
 
51
FACILITIES = os.path.join(LSBLIB, 'facilities')
 
52
DEPENDS = os.path.join(LSBLIB, 'depends')
6
53
 
7
54
beginre = re.compile(re.escape('### BEGIN INIT INFO'))
8
55
endre = re.compile(re.escape('### END INIT INFO'))
9
 
linere = re.compile(r'\#\s+([^:]+):\s*(.*)')
 
56
#linere = re.compile(r'\#\s+([^:]+):\s*(.*)')
10
57
 
11
58
def scan_initfile(initfile):
12
 
    headers = {'Description': []}
13
 
    scanning = 0
 
59
    headerlines = ''
 
60
    scanning = False
14
61
    
15
 
    for line in open(initfile).xreadlines():
 
62
    for line in file(initfile):
16
63
        line = line.rstrip()
17
64
        if beginre.match(line):
18
 
            scanning = 1
 
65
            scanning = True
19
66
            continue
20
67
        elif scanning and endre.match(line):
21
 
            scanning = 0
 
68
            scanning = False
22
69
            continue
23
70
        elif not scanning:
24
71
            continue
25
72
 
26
 
        if line[0] != '#':
27
 
            continue
28
 
 
29
 
        if line[1:3] == '  ' or line[1] == '\t':
30
 
            headers['Description'].append(line[1:].strip())
31
 
            continue
32
 
 
33
 
        match = linere.match(line)
34
 
        if not match:
35
 
            print >> sys.stderr, "Warning: ignoring invalid init info line"
36
 
            print >> sys.stderr, "-> %s" % line
37
 
            continue
38
 
 
39
 
        header, body = match.groups()
40
 
        if header == "Description":
41
 
            headers[header].append(body.strip())
42
 
        elif header in ('Default-Start', 'Default-Stop'):
 
73
        if line.startswith('# '):
 
74
            headerlines += line[2:] + '\n'
 
75
        elif line.startswith('#\t'):
 
76
            headerlines += line[1:] + '\n'
 
77
 
 
78
    inheaders = RFC822Parser(strob=headerlines)
 
79
    headers = {}
 
80
    for header, body in inheaders.iteritems():
 
81
        if header in ('Default-Start', 'Default-Stop'):
43
82
            headers[header] = map(int, body.split())
44
 
        elif header in ('Required-Start', 'Required-Stop', 'Provides'):
 
83
        elif header in ('Required-Start', 'Required-Stop', 'Provides',
 
84
                        'Should-Start', 'Should-Stop'):
45
85
            headers[header] = body.split()
46
86
        else:
47
87
            headers[header] = body
56
96
            pass
57
97
        return
58
98
    
59
 
    items = facilities.items()
60
 
    fh = open(FACILITIES, 'w')
61
 
    for facility, entries in items:
62
 
        if facility[0] == '$': continue
63
 
        for scriptname, pri in entries.items():
 
99
    fh = file(FACILITIES, 'w')
 
100
    for facility, entries in facilities.items():
 
101
        # Ignore system facilities
 
102
        if facility.startswith('$'): continue
 
103
        for (scriptname, pri) in entries.items():
64
104
            start, stop = pri
65
105
            print >> fh, "%(scriptname)s %(facility)s %(start)d %(stop)d" % locals()
66
106
    fh.close()
71
111
        for line in open(FACILITIES).xreadlines():
72
112
            try:
73
113
                scriptname, name, start, stop = line.strip().split()
74
 
                facilities.get(name, {})[scriptname] = (int(start), int(stop))
 
114
                facilities.setdefault(name, {})[scriptname] = (int(start),
 
115
                                                               int(stop))
75
116
            except ValueError, x:
76
117
                print >> sys.stderr, 'Invalid facility line', line
77
118
 
78
119
    return facilities
 
120
 
 
121
def load_depends():
 
122
    depends = {}
 
123
 
 
124
    if os.path.exists(DEPENDS):
 
125
        independs = RFC822Parser(fileob=file(DEPENDS))
 
126
        for initfile, facilities in independs.iteritems():
 
127
            depends[initfile] = facilities.split()
 
128
    return depends
 
129
 
 
130
def save_depends(depends):
 
131
    if not depends:
 
132
        try:
 
133
            os.unlink(DEPENDS)
 
134
        except OSError:
 
135
            pass
 
136
        return
 
137
    
 
138
    fh = file(DEPENDS, 'w')
 
139
    for initfile, facilities in depends.iteritems():
 
140
        print >> fh, '%s: %s' % (initfile, ' '.join(facilities))
 
141
    fh.close()
 
142
 
 
143
if __name__ == '__main__':
 
144
    print scan_initfile('init-fragment')