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
176
177
|
import os
from pprint import pprint
from string import Template
from collections import deque
class PackagePrioritizer(object):
def __init__(self):
self.weight = {}
self.packages = []
def insert(self, package):
self.packages.append(package)
def generate_weightmap(self):
self.weight = { str(x):[] for x in self.packages }
for package in self.packages:
for priority in package.precedence:
self.weight[str(package)].append(priority.name)
class PackageException(Exception):
pass
class Package(object):
def __init__(self, filename, load=True):
self.valid_keywords = [
'Type',
'Requires',
'Precedes',
'Synopsis',
'Description',
'Environment',
'LdLibrary',
'IncPath',
'Root',
'Path',
'ManPath',
'Default',
'Source'
]
self.filename = os.path.join(ENVCONFIG_PATH, filename)
self.exists = False
self.name = ''
self.name_internal = ''
self.description = ''
self.dependencies = []
self.precedence = []
self.priority = 0 # 0 - 99
self.shell = ''
self.env = {}
self.script = ''
self.invisible = False
self.mtime = 0
self.data = {}
if os.path.exists(self.filename):
self.exists = True
self.name = os.path.basename(self.filename.replace(' ', '_'))
if not load:
return
if self.filename is not None \
and self.exists:
self.data = self.load()
self.get_requirements()
self.get_precedence()
def __repr__(self):
return self.name
def get_requirements(self):
if not self.data:
raise PackageException('Package data not loaded.')
if 'Requires' in self.data:
for next_req in self.data['Requires']:
req = Package(next_req)
self.dependencies.append(req)
if not req.exists:
print("Requirement warning, {}: {} does not exist".format(self.name, os.path.basename(req.filename)))
def get_precedence(self):
if not self.data:
raise PackageException('Package data not loaded.')
if 'Precedes' in self.data:
for next_req in self.data['Precedes']:
req = Package(next_req, load=False)
self.precedence.append(req)
if not req.exists:
print("Precedence warning, {}: {} does not exist".format(self.name, os.path.basename(req.filename)))
def load(self):
pairs = []
comment = '#'
delimiter = ':'
keyword = ''
value = ''
with open(self.filename, 'r') as f:
for line in f.readlines():
line = line.strip()
if not line:
continue
if not line.find(delimiter):
continue
if line.startswith(comment):
continue
keyword = line[0:line.find(delimiter)]
if keyword not in self.valid_keywords:
continue
if keyword == 'Source':
continue
value = line[line.find(delimiter) + 1:].strip()
pairs.append([keyword, value])
# Do source block
with open(self.filename, 'r') as f:
value = ''
shell_type = ''
data_block = False
for line in f.readlines():
if line.startswith('Source:'):
data_block = True
shell_type = line[line.find(delimiter) + 1:line.rfind(delimiter)].strip()
line = line[line.rfind(delimiter) + 1:-1]
if data_block:
value += line
if line.startswith('"') and line.endswith('\\'):
data_block = False
pairs.append(['Source', value])
pairs.append(['Shell', shell_type])
pairs = dict(pairs)
# Alias the filename to be the same as "Root" value
if 'Root' in pairs:
pairs[self.name] = pairs['Root']
# Substitute configuration values
for key, value in pairs.items():
s = Template(value)
pairs[key] = s.safe_substitute(pairs)
if 'Requires' in pairs:
pairs['Requires'] = str(pairs['Requires']).split()
if 'Precedes' in pairs:
pairs['Precedes'] = str(pairs['Precedes']).split()
return dict(pairs)
if __name__ == '__main__':
ENVCONFIG_PATH = os.path.abspath(os.path.normpath('../envconfig'))
pkgs = []
for r, d, fs in os.walk(ENVCONFIG_PATH):
for f in fs:
pkgs.append(Package(f))
#pkg = Package('JDK1.6.07')
pp = PackagePrioritizer()
for pkg in pkgs:
pp.insert(pkg)
pp.generate_weightmap()
pprint(pp.weight)
#pprint(pp.packages)
#pkg = Package(None)
#pprint(pkg.data)
|