Gentoo Archives: gentoo-catalyst

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

Replies