1 |
commit: 6398b227f47e2af2f310ad8af53c6e26680c60b0 |
2 |
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Jan 3 18:46:55 2014 +0000 |
4 |
Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> |
5 |
CommitDate: Sat Mar 22 18:01:30 2014 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/catalyst.git;a=commit;h=6398b227 |
7 |
|
8 |
Initial separation and creation of contents.py |
9 |
|
10 |
--- |
11 |
catalyst/contents.py | 87 ++++++++++++++++++++++++++++++++ |
12 |
catalyst/main.py | 8 ++- |
13 |
catalyst/support.py | 52 ------------------- |
14 |
catalyst/targets/generic_stage_target.py | 3 +- |
15 |
4 files changed, 96 insertions(+), 54 deletions(-) |
16 |
|
17 |
diff --git a/catalyst/contents.py b/catalyst/contents.py |
18 |
new file mode 100644 |
19 |
index 0000000..79ef9a6 |
20 |
--- /dev/null |
21 |
+++ b/catalyst/contents.py |
22 |
@@ -0,0 +1,87 @@ |
23 |
+ |
24 |
+from collections import namedtuple |
25 |
+from subprocess import Popen, PIPE |
26 |
+ |
27 |
+from support import CatalystError, warn |
28 |
+ |
29 |
+ |
30 |
+# use ContentsMap.fields for the value legend |
31 |
+# Key:[function, cmd] |
32 |
+CONTENTS_DEFINITIONS = { |
33 |
+ # 'find' is disabled because it requires the source path, which is not |
34 |
+ # always available |
35 |
+ #"find" :["calc_contents","find %(path)s"], |
36 |
+ "tar_tv":["calc_contents","tar tvf %(file)s"], |
37 |
+ "tar_tvz":["calc_contents","tar tvzf %(file)s"], |
38 |
+ "tar_tvj":["calc_contents","tar -I lbzip2 -tvf %(file)s"], |
39 |
+ "isoinfo_l":["calc_contents","isoinfo -l -i %(file)s"], |
40 |
+ # isoinfo_f should be a last resort only |
41 |
+ "isoinfo_f":["calc_contents","isoinfo -f -i %(file)s"], |
42 |
+} |
43 |
+ |
44 |
+ |
45 |
+class ContentsMap(object): |
46 |
+ '''Class to encompass all known commands to list |
47 |
+ the contents of an archive''' |
48 |
+ |
49 |
+ |
50 |
+ fields = ['func', 'cmd'] |
51 |
+ |
52 |
+ |
53 |
+ def __init__(self, defs=None): |
54 |
+ '''Class init |
55 |
+ |
56 |
+ @param defs: dictionary of Key:[function, cmd] |
57 |
+ ''' |
58 |
+ if defs is None: |
59 |
+ defs = {} |
60 |
+ #self.contents = {} |
61 |
+ self.contents_map = {} |
62 |
+ |
63 |
+ # create the archive type namedtuple classes |
64 |
+ for name in list(defs): |
65 |
+ #obj = self.contents[name] = namedtuple(name, self.fields) |
66 |
+ obj = namedtuple(name, self.fields) |
67 |
+ obj.__slots__ = () |
68 |
+ self.contents_map[name] = obj._make(defs[name]) |
69 |
+ del obj |
70 |
+ |
71 |
+ |
72 |
+ def generate_contents(self, file_, getter="auto", verbose=False): |
73 |
+ try: |
74 |
+ archive = getter |
75 |
+ if archive == 'auto' and file_.endswith('.iso'): |
76 |
+ archive = 'isoinfo_l' |
77 |
+ if (archive in ['tar_tv','auto']): |
78 |
+ if file_.endswith('.tgz') or file_.endswith('.tar.gz'): |
79 |
+ archive = 'tar_tvz' |
80 |
+ elif file_.endswith('.tbz2') or file_.endswith('.tar.bz2'): |
81 |
+ archive = 'tar_tvj' |
82 |
+ elif file_.endswith('.tar'): |
83 |
+ archive = 'tar_tv' |
84 |
+ |
85 |
+ if archive == 'auto': |
86 |
+ warn('File %r has unknown type for automatic detection.' |
87 |
+ % (file_, )) |
88 |
+ return None |
89 |
+ else: |
90 |
+ getter = archive |
91 |
+ func = getattr(self, '_%s_' % self.contents_map[getter].func) |
92 |
+ return func(file_, self.contents_map[getter].cmd, verbose) |
93 |
+ except: |
94 |
+ raise CatalystError,\ |
95 |
+ "Error generating contents, is appropriate utility " +\ |
96 |
+ "(%s) installed on your system?" \ |
97 |
+ % (self.contents_map[getter].cmd) |
98 |
+ |
99 |
+ |
100 |
+ @staticmethod |
101 |
+ def _calc_contents_(file_, cmd, verbose): |
102 |
+ _cmd = (cmd % {'file': file_ }).split() |
103 |
+ proc = Popen(_cmd, stdout=PIPE, stderr=PIPE) |
104 |
+ results = proc.communicate() |
105 |
+ result = "\n".join(results) |
106 |
+ if verbose: |
107 |
+ print result |
108 |
+ return result |
109 |
+ |
110 |
|
111 |
diff --git a/catalyst/main.py b/catalyst/main.py |
112 |
index 7bcf2cb..4146bca 100644 |
113 |
--- a/catalyst/main.py |
114 |
+++ b/catalyst/main.py |
115 |
@@ -25,6 +25,7 @@ from catalyst.support import (required_build_targets, |
116 |
valid_build_targets, CatalystError, find_binary, LockInUse) |
117 |
|
118 |
from hash_utils import HashMap, HASH_DEFINITIONS |
119 |
+from contents import ContentsMap, CONTENTS_DEFINITIONS |
120 |
|
121 |
|
122 |
|
123 |
@@ -184,7 +185,8 @@ def parse_config(myconfig): |
124 |
if "digests" in myconf: |
125 |
conf_values["digests"]=myconf["digests"] |
126 |
if "contents" in myconf: |
127 |
- conf_values["contents"]=myconf["contents"] |
128 |
+ # replace '-' with '_' (for compatibility with existing configs) |
129 |
+ conf_values["contents"] = myconf["contents"].replace("-", '_') |
130 |
|
131 |
if "envscript" in myconf: |
132 |
print "Envscript support enabled." |
133 |
@@ -348,6 +350,10 @@ def main(): |
134 |
# import configuration file and import our main module using those settings |
135 |
parse_config(myconfig) |
136 |
|
137 |
+ # initialize our contents generator |
138 |
+ contents_map = ContentsMap(CONTENTS_DEFINITIONS) |
139 |
+ conf_values["contents_map"] = contents_map |
140 |
+ |
141 |
# initialze our hash and contents generators |
142 |
hash_map = HashMap(HASH_DEFINITIONS) |
143 |
conf_values["hash_map"] = hash_map |
144 |
|
145 |
diff --git a/catalyst/support.py b/catalyst/support.py |
146 |
index 308d9c0..e25394e 100644 |
147 |
--- a/catalyst/support.py |
148 |
+++ b/catalyst/support.py |
149 |
@@ -62,58 +62,6 @@ def hexify(str): |
150 |
return r |
151 |
# hexify() |
152 |
|
153 |
-def generate_contents(file,contents_function="auto",verbose=False): |
154 |
- try: |
155 |
- _ = contents_function |
156 |
- if _ == 'auto' and file.endswith('.iso'): |
157 |
- _ = 'isoinfo-l' |
158 |
- if (_ in ['tar-tv','auto']): |
159 |
- if file.endswith('.tgz') or file.endswith('.tar.gz'): |
160 |
- _ = 'tar-tvz' |
161 |
- elif file.endswith('.tbz2') or file.endswith('.tar.bz2'): |
162 |
- _ = 'tar-tvj' |
163 |
- elif file.endswith('.tar'): |
164 |
- _ = 'tar-tv' |
165 |
- |
166 |
- if _ == 'auto': |
167 |
- warn('File %r has unknown type for automatic detection.' % (file, )) |
168 |
- return None |
169 |
- else: |
170 |
- contents_function = _ |
171 |
- _ = contents_map[contents_function] |
172 |
- return _[0](file,_[1],verbose) |
173 |
- except: |
174 |
- raise CatalystError,\ |
175 |
- "Error generating contents, is appropriate utility (%s) installed on your system?" \ |
176 |
- % (contents_function, ) |
177 |
- |
178 |
-def calc_contents(file,cmd,verbose): |
179 |
- args={ 'file': file } |
180 |
- cmd=cmd % dict(args) |
181 |
- a=os.popen(cmd) |
182 |
- mylines=a.readlines() |
183 |
- a.close() |
184 |
- result="".join(mylines) |
185 |
- if verbose: |
186 |
- print result |
187 |
- return result |
188 |
- |
189 |
-# This has map must be defined after the function calc_content |
190 |
-# It is possible to call different functions from this but they must be defined |
191 |
-# before hash_map |
192 |
-# Key,function,cmd |
193 |
-contents_map={ |
194 |
- # 'find' is disabled because it requires the source path, which is not |
195 |
- # always available |
196 |
- #"find" :[calc_contents,"find %(path)s"], |
197 |
- "tar-tv":[calc_contents,"tar tvf %(file)s"], |
198 |
- "tar-tvz":[calc_contents,"tar tvzf %(file)s"], |
199 |
- "tar-tvj":[calc_contents,"tar -I lbzip2 -tvf %(file)s"], |
200 |
- "isoinfo-l":[calc_contents,"isoinfo -l -i %(file)s"], |
201 |
- # isoinfo-f should be a last resort only |
202 |
- "isoinfo-f":[calc_contents,"isoinfo -f -i %(file)s"], |
203 |
-} |
204 |
- |
205 |
|
206 |
def read_from_clst(file): |
207 |
line = '' |
208 |
|
209 |
diff --git a/catalyst/targets/generic_stage_target.py b/catalyst/targets/generic_stage_target.py |
210 |
index b6a6200..de4842c 100644 |
211 |
--- a/catalyst/targets/generic_stage_target.py |
212 |
+++ b/catalyst/targets/generic_stage_target.py |
213 |
@@ -1703,6 +1703,7 @@ class generic_stage_target(generic_target): |
214 |
if os.path.exists(file+".CONTENTS"): |
215 |
os.remove(file+".CONTENTS") |
216 |
if "contents" in self.settings: |
217 |
+ contents_map = self.settings["contents_map"] |
218 |
if os.path.exists(file): |
219 |
myf=open(file+".CONTENTS","w") |
220 |
keys={} |
221 |
@@ -1711,7 +1712,7 @@ class generic_stage_target(generic_target): |
222 |
array=keys.keys() |
223 |
array.sort() |
224 |
for j in array: |
225 |
- contents=generate_contents(file,contents_function=j,\ |
226 |
+ contents = contents_map.generate_contents(file, j, |
227 |
verbose="VERBOSE" in self.settings) |
228 |
if contents: |
229 |
myf.write(contents) |