1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#!/usr/bin/env python
from grp import getgrgid
from os import lstat, mkdir, stat
from pwd import getpwuid
from stat import *
from string import join, split
def print_failure():
if '-v' in argv: print "\033[60G[\033[1;31mFAILED\033[0;39m]\r"
def print_success():
if '-v' in argv: print "\033[60G[ \033[1;32mOK\033[0;39m ]\r"
def CalcPerms(initial,perms):
tempperms = initial
(s,u,g,o) = map(int, map(lambda x:perms[x], range(4)))
if s & 1:
tempperms |= S_ISVTX
if s & 2:
tempperms |= S_ISGID
if s & 4:
tempperms |= S_ISUID
if u & 1:
tempperms |= S_IXUSR
if u & 2:
tempperms |= S_IWUSR
if u & 4:
tempperms |= S_IRUSR
if g & 1:
tempperms |= S_IXGRP
if g & 2:
tempperms |= S_IWGRP
if g & 4:
tempperms |= S_IRGRP
if o & 1:
tempperms |= S_IXOTH
if o & 2:
tempperms |= S_IWOTH
if o & 4:
tempperms |= S_IROTH
return tempperms
class Toolset(object):
__important__ = []
def __init__(self, cfg, setup):
self.setup = setup
if self.__important__:
for c in cfg.findall(".//ConfigFile"):
for name in self.__important__:
if c.get("name") == name:
self.InstallConfigFile(c)
def VerifySymLink(self, src, dst):
try:
s = readlink(dst)
if s == src:
return True
return False
except OSError:
return False
def InstallSymLink(self, src, dst):
try:
fmode = lstat(dst)[ST_MODE]
if S_ISREG(fmode) or S_ISLNK(fmode):
unlink(dst)
elif S_ISDIR(fmode):
system("mv %s/ %s.bak"%(dst, dst))
else:
unlink(dst)
except OSError, e:
pass
try:
symlink(src, dst)
except OSError, e:
return False
def VerifyDirectory(self, entry):
try:
ondisk=stat(entry.attrib['name'])
except OSError:
return False
try:
owner=getpwuid(ondisk[ST_UID])[0]
group=getgrgid(ondisk[ST_GID])[0]
except OSError:
owner='root'
group='root'
perms=stat(entry.attrib['name'])[ST_MODE]
if ((owner == entry.attrib['owner']) and
(group == entry.attrib['group']) and
(perms == entry.attrib['perms'])):
return True
else:
return False
def InstallDirectory(self, entry):
try:
fmode = lstat(entry.attrib['name'])
if not S_ISDIR(fmode[0]):
try:
unlink(entry.attrib['name'])
except:
return False
except OSError:
pass
try:
mkdir(entry.attrib['name'])
except OSError:
return False
try:
chown(entry.attrib['name'],getpwnam(self.attrib['owner'])[2],getgrnam(entry.attrib['group'])[2])
chmod(entry.attrib['name'],entry.attrib['perms'])
except:
return False
def VerifyConfigFile(self, entry):
try:
ondisk=stat(entry.attrib['name'])
except OSError:
return False
try:
data=open(entry.attrib['name']).read()
except IOError:
return False
try:
owner=getpwuid(ondisk[ST_UID])[0]
group=getgrgid(ondisk[ST_GID])[0]
except KeyError:
return False
perms=stat(entry.attrib['name'])[ST_MODE]
tempdata = entry.text
if ((data == tempdata) and (owner == entry.attrib['owner']) and
(group == entry.attrib['group']) and (perms == CalcPerms(S_IFREG, entry.attrib['perms']))):
return True
return False
def InstallConfigFile(self, entry):
if self.setup['dryrun'] or self.setup['verbose']:
print "Installing ConfigFile %s"%(entry.attrib['name'])
if self.setup['dryrun']:
return False
parent = join(split(entry.attrib['name'],"/")[:-1],"/")
if parent:
try:
s = lstat(parent)
try:
if not S_ISDIR(s[ST_MODE]):
unlink(parent)
mkdir(parent)
except OSError:
return False
except OSError:
# need to handle mkdir -p case
mkdir(parent)
# If we get here, then the parent directory should exist
try:
newfile = open("%s.new"%(entry.attrib['name']), 'w')
newfile.write(entry.text)
newfile.close()
chown(newfile.name, entry.attrib['owner'], entry.attrib['group'])
chmod(newfile.name, CalcPerms(S_IFREG, entry.attrib['perms']))
if entry.attrib.get("paranoid", False) and setup.get("paranoid", False):
system("diff -u %s %s.new"%(entry.attrib['name'], entry.attrib['name']))
rename(newfile.name, entry.attrib['name'])
return True
except (OSError, IOError), e:
print e
return False
|