~ubuntu-branches/ubuntu/raring/lsb/raring-proposed

« back to all changes in this revision

Viewing changes to initdutils.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lawrence
  • Date: 2005-03-27 21:42:24 UTC
  • mto: (1.1.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050327214224-ptr2vsz557dpai0d
Tags: 2.0-7
Fix Replaces line to use the correct version in lsb-base.
(Closes: #301694, RC; Closes: #301747)

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
        # Ignore empty headers
 
82
        if not body.strip():
 
83
            break
 
84
        
 
85
        if header in ('Default-Start', 'Default-Stop'):
43
86
            headers[header] = map(int, body.split())
44
 
        elif header in ('Required-Start', 'Required-Stop', 'Provides'):
 
87
        elif header in ('Required-Start', 'Required-Stop', 'Provides',
 
88
                        'Should-Start', 'Should-Stop'):
45
89
            headers[header] = body.split()
46
90
        else:
47
91
            headers[header] = body
56
100
            pass
57
101
        return
58
102
    
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():
 
103
    fh = file(FACILITIES, 'w')
 
104
    for facility, entries in facilities.items():
 
105
        # Ignore system facilities
 
106
        if facility.startswith('$'): continue
 
107
        for (scriptname, pri) in entries.items():
64
108
            start, stop = pri
65
109
            print >> fh, "%(scriptname)s %(facility)s %(start)d %(stop)d" % locals()
66
110
    fh.close()
71
115
        for line in open(FACILITIES).xreadlines():
72
116
            try:
73
117
                scriptname, name, start, stop = line.strip().split()
74
 
                facilities.get(name, {})[scriptname] = (int(start), int(stop))
 
118
                facilities.setdefault(name, {})[scriptname] = (int(start),
 
119
                                                               int(stop))
75
120
            except ValueError, x:
76
121
                print >> sys.stderr, 'Invalid facility line', line
77
122
 
78
123
    return facilities
 
124
 
 
125
def load_depends():
 
126
    depends = {}
 
127
 
 
128
    if os.path.exists(DEPENDS):
 
129
        independs = RFC822Parser(fileob=file(DEPENDS))
 
130
        for initfile, facilities in independs.iteritems():
 
131
            depends[initfile] = facilities.split()
 
132
    return depends
 
133
 
 
134
def save_depends(depends):
 
135
    if not depends:
 
136
        try:
 
137
            os.unlink(DEPENDS)
 
138
        except OSError:
 
139
            pass
 
140
        return
 
141
    
 
142
    fh = file(DEPENDS, 'w')
 
143
    for initfile, facilities in depends.iteritems():
 
144
        print >> fh, '%s: %s' % (initfile, ' '.join(facilities))
 
145
    fh.close()
 
146
 
 
147
if __name__ == '__main__':
 
148
    print scan_initfile('init-fragment')