~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: 2002-04-15 02:25:51 UTC
  • Revision ID: james.westby@ubuntu.com-20020415022551-don2tfe0tawo0g44
Tags: 1.1.0-11
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
violation of the LSB specification.  See #118646.
(If necessary, run-parts may be diverted by this package to conform.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Support for scanning init scripts for LSB info
 
2
 
 
3
import re, sys, os
 
4
 
 
5
FACILITIES = '/var/lib/lsb/facilities'
 
6
 
 
7
beginre = re.compile(re.escape('### BEGIN INIT INFO'))
 
8
endre = re.compile(re.escape('### END INIT INFO'))
 
9
linere = re.compile(r'\#\s+([^:]+):\s*(.*)')
 
10
 
 
11
def scan_initfile(initfile):
 
12
    headers = {'Description': []}
 
13
    scanning = 0
 
14
    
 
15
    for line in open(initfile).xreadlines():
 
16
        line = line.rstrip()
 
17
        if beginre.match(line):
 
18
            scanning = 1
 
19
            continue
 
20
        elif scanning and endre.match(line):
 
21
            scanning = 0
 
22
            continue
 
23
        elif not scanning:
 
24
            continue
 
25
 
 
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'):
 
43
            headers[header] = map(int, body.split())
 
44
        elif header in ('Required-Start', 'Required-Stop', 'Provides'):
 
45
            headers[header] = body.split()
 
46
        else:
 
47
            headers[header] = body
 
48
 
 
49
    return headers
 
50
 
 
51
def save_facilities(facilities):
 
52
    if not facilities:
 
53
        try:
 
54
            os.unlink(FACILITIES)
 
55
        except OSError:
 
56
            pass
 
57
        return
 
58
    
 
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():
 
64
            start, stop = pri
 
65
            print >> fh, "%(scriptname)s %(facility)s %(start)d %(stop)d" % locals()
 
66
    fh.close()
 
67
 
 
68
def load_facilities():
 
69
    facilities = {}
 
70
    if os.path.exists(FACILITIES):
 
71
        for line in open(FACILITIES).xreadlines():
 
72
            try:
 
73
                scriptname, name, start, stop = line.strip().split()
 
74
                facilities.get(name, {})[scriptname] = (int(start), int(stop))
 
75
            except ValueError, x:
 
76
                print >> sys.stderr, 'Invalid facility line', line
 
77
 
 
78
    return facilities