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

1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
1
#!/usr/bin/python
2
3
import sys, re, os, initdutils
4
5
if len(sys.argv) > 1:
6
    initfile = sys.argv[1]
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
7
    # If the absolute path isn't specified, assume it's relative to
8
    # cwd; if that doesn't exist, try /etc/init.d
9
    ap = os.path.abspath(initfile)
10
    if os.path.exists(ap):
11
        initfile = ap
12
    else:
13
        initfile = os.path.join('/etc/init.d', initfile)
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
14
else:
15
    print >> sys.stderr, 'Usage: %s /etc/init.d/<init-script>' % sys.argv[0]
16
    sys.exit(1)
17
18
# Default priorities
19
startpri = stoppri = 20
20
defstart = [2, 3, 4, 5]
21
defstop = [0, 1, 6]
22
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
23
# Estimated priorities of these facilities in Debian
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
24
os_facilities = {
25
    "$local_fs" : {'lsb' : (0, 100)},
26
    "$network" : {'lsb' : (10, 50)},
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
27
    "$remote_fs" : {'lsb': (19, 20)},
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
28
    "$named" : {'lsb': (19, 19)},
29
    "$syslog" : {'lsb' : (10, 89)},
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
30
    # No longer present in gLSB 1.2; however, required for gLSB 1.1
31
    # compat.  Note that these are looser than $portmap and $time;
32
    # anything specifying $netdaemons will be run later, which may not
33
    # be what you want...
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
34
    "$netdaemons" : {'lsb': (80, 19)},
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
35
    # gLSB 1.2
36
    "$portmap" : {'lsb' : (19, 34)},
37
    "$time" : {'lsb' : (24, 21)},
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
38
    }
39
40
facilities = initdutils.load_facilities()
41
facilities.update(os_facilities)
42
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
43
depends = initdutils.load_depends()
44
depends[initfile] = []
45
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
46
headers = initdutils.scan_initfile(initfile)
47
48
reqstart = headers.get('Required-Start', [])
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
49
shouldstart = headers.get('Should-Start', [])
50
if reqstart or shouldstart:
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
51
    startpri = 5
52
    for facility in reqstart:
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
53
        if facility not in facilities:
54
            print >> sys.stderr, 'Missing required start facility', facility
55
            sys.exit(1)
56
        else:
57
            for script, pri in facilities[facility].iteritems():
58
                if script != initfile:
59
                    start, stop = pri
60
                    startpri = max(startpri, start+1)
61
            if facility not in depends[initfile]:
62
                depends[initfile].append(facility)
63
    
64
    for facility in shouldstart:
65
        if facility not in facilities:
66
            print >> sys.stderr, 'Missing should-start facility', facility, '(ignored)'
67
        else:
68
            for script, pri in facilities[facility].iteritems():
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
69
                if script != initfile:
70
                    start, stop = pri
71
                    startpri = max(startpri, start+1)
72
    startpri = min(max(startpri, 1), 99)
73
74
reqstop = headers.get('Required-Stop', [])
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
75
shouldstop = headers.get('Should-Stop', [])
76
if reqstop or shouldstop:
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
77
    stoppri = 95
78
    for facility in reqstop:
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
79
        if facility not in facilities:
80
            print >> sys.stderr, 'Missing required stop facility', facility
81
            sys.exit(1)
82
        else:
83
            for script, pri in facilities[facility].iteritems():
84
                if script != initfile:
85
                    start, stop = pri
86
                    stoppri = min(stoppri, stop-1)
87
            if facility not in depends[initfile]:
88
                depends[initfile].append(facility)
89
90
    for facility in shouldstop:
91
        if facility not in facilities:
92
            print >> sys.stderr, 'Missing should-stop facility', facility, '(ignored)'
93
        else:
94
            for script, pri in facilities[facility].iteritems():
95
                if script != initfile:
96
                    start, stop = pri
97
                    stoppri = min(stoppri, stop-1)
98
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
99
    stoppri = min(max(stoppri, 1), 99)
100
2 by Tollef Fog Heen
prerm, postinst: Add support for amd64. (closes: #1354)
101
if depends[initfile]:
102
    initdutils.save_depends(depends)
103
1 by Chris Lawrence
Note that Debian's run-parts ignores certain lsb cron.* scripts, in
104
provides = headers.get('Provides', [])
105
if provides:
106
    for facility in provides:
107
        if facility[0] == '$':
108
            print >> sys.stderr, 'Ignoring system-provided facility', facility
109
            continue
110
        if not facilities.has_key(facility):
111
            facilities[facility] = {}
112
113
        facilities[facility][initfile] = (startpri, stoppri)
114
    initdutils.save_facilities(facilities)
115
116
defstart = headers.get('Default-Start', defstart)
117
defstop = headers.get('Default-Stop', defstop)
118
119
# A set type would be nice... [range(2,6) = 2..5]
120
for level in range(2,6):
121
    if level in defstart:
122
        for i in range(2,6):
123
            if i not in defstart:
124
                defstart.append(i)
125
    if level in defstop:
126
        for i in range(2,6):
127
            if i not in defstop:
128
                defstop.append(i)
129
130
defstart.sort()
131
defstop.sort()
132
133
start_runlevels = " ".join(map(str, defstart))
134
stop_runlevels = " ".join(map(str, defstop))
135
136
initfile = initfile.replace('/etc/init.d/', '')
137
138
os.system("/usr/sbin/update-rc.d %(initfile)s start %(startpri)d %(start_runlevels)s . stop %(stoppri)d %(stop_runlevels)s ." % locals())