commit:     6398b227f47e2af2f310ad8af53c6e26680c60b0
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Fri Jan  3 18:46:55 2014 +0000
Commit:     Brian Dolbec <brian.dolbec <AT> gmail <DOT> com>
CommitDate: Sat Mar 22 18:01:30 2014 +0000
URL:        
http://git.overlays.gentoo.org/gitweb/?p=proj/catalyst.git;a=commit;h=6398b227

Initial separation and creation of contents.py

---
 catalyst/contents.py                     | 87 ++++++++++++++++++++++++++++++++
 catalyst/main.py                         |  8 ++-
 catalyst/support.py                      | 52 -------------------
 catalyst/targets/generic_stage_target.py |  3 +-
 4 files changed, 96 insertions(+), 54 deletions(-)

diff --git a/catalyst/contents.py b/catalyst/contents.py
new file mode 100644
index 0000000..79ef9a6
--- /dev/null
+++ b/catalyst/contents.py
@@ -0,0 +1,87 @@
+
+from collections import namedtuple
+from subprocess import Popen, PIPE
+
+from support import CatalystError, warn
+
+
+# use ContentsMap.fields for the value legend
+# Key:[function, cmd]
+CONTENTS_DEFINITIONS = {
+       # 'find' is disabled because it requires the source path, which is not
+       # always available
+       #"find"         :["calc_contents","find %(path)s"],
+       "tar_tv":["calc_contents","tar tvf %(file)s"],
+       "tar_tvz":["calc_contents","tar tvzf %(file)s"],
+       "tar_tvj":["calc_contents","tar -I lbzip2 -tvf %(file)s"],
+       "isoinfo_l":["calc_contents","isoinfo -l -i %(file)s"],
+       # isoinfo_f should be a last resort only
+       "isoinfo_f":["calc_contents","isoinfo -f -i %(file)s"],
+}
+
+
+class ContentsMap(object):
+       '''Class to encompass all known commands to list
+       the contents of an archive'''
+
+
+       fields = ['func', 'cmd']
+
+
+       def __init__(self, defs=None):
+               '''Class init
+
+               @param defs: dictionary of Key:[function, cmd]
+               '''
+               if defs is None:
+                       defs = {}
+               #self.contents = {}
+               self.contents_map = {}
+
+               # create the archive type namedtuple classes
+               for name in list(defs):
+                       #obj = self.contents[name] = namedtuple(name, 
self.fields)
+                       obj = namedtuple(name, self.fields)
+                       obj.__slots__ = ()
+                       self.contents_map[name] = obj._make(defs[name])
+               del obj
+
+
+       def generate_contents(self, file_, getter="auto", verbose=False):
+               try:
+                       archive = getter
+                       if archive == 'auto' and file_.endswith('.iso'):
+                               archive = 'isoinfo_l'
+                       if (archive in ['tar_tv','auto']):
+                               if file_.endswith('.tgz') or 
file_.endswith('.tar.gz'):
+                                       archive = 'tar_tvz'
+                               elif file_.endswith('.tbz2') or 
file_.endswith('.tar.bz2'):
+                                       archive = 'tar_tvj'
+                               elif file_.endswith('.tar'):
+                                       archive = 'tar_tv'
+
+                       if archive == 'auto':
+                               warn('File %r has unknown type for automatic 
detection.'
+                                       % (file_, ))
+                               return None
+                       else:
+                               getter = archive
+                               func = getattr(self, '_%s_' % 
self.contents_map[getter].func)
+                               return func(file_, 
self.contents_map[getter].cmd, verbose)
+               except:
+                       raise CatalystError,\
+                               "Error generating contents, is appropriate 
utility " +\
+                               "(%s) installed on your system?" \
+                               % (self.contents_map[getter].cmd)
+
+
+       @staticmethod
+       def _calc_contents_(file_, cmd, verbose):
+               _cmd = (cmd % {'file': file_ }).split()
+               proc = Popen(_cmd, stdout=PIPE, stderr=PIPE)
+               results = proc.communicate()
+               result = "\n".join(results)
+               if verbose:
+                       print result
+               return result
+

diff --git a/catalyst/main.py b/catalyst/main.py
index 7bcf2cb..4146bca 100644
--- a/catalyst/main.py
+++ b/catalyst/main.py
@@ -25,6 +25,7 @@ from catalyst.support import (required_build_targets,
        valid_build_targets, CatalystError, find_binary, LockInUse)
 
 from hash_utils import HashMap, HASH_DEFINITIONS
+from contents import ContentsMap, CONTENTS_DEFINITIONS
 
 
 
@@ -184,7 +185,8 @@ def parse_config(myconfig):
        if "digests" in myconf:
                conf_values["digests"]=myconf["digests"]
        if "contents" in myconf:
-               conf_values["contents"]=myconf["contents"]
+               # replace '-' with '_' (for compatibility with existing configs)
+               conf_values["contents"] = myconf["contents"].replace("-", '_')
 
        if "envscript" in myconf:
                print "Envscript support enabled."
@@ -348,6 +350,10 @@ def main():
        # import configuration file and import our main module using those 
settings
        parse_config(myconfig)
 
+       # initialize our contents generator
+       contents_map = ContentsMap(CONTENTS_DEFINITIONS)
+       conf_values["contents_map"] = contents_map
+
        # initialze our hash and contents generators
        hash_map = HashMap(HASH_DEFINITIONS)
        conf_values["hash_map"] = hash_map

diff --git a/catalyst/support.py b/catalyst/support.py
index 308d9c0..e25394e 100644
--- a/catalyst/support.py
+++ b/catalyst/support.py
@@ -62,58 +62,6 @@ def hexify(str):
        return r
 # hexify()
 
-def generate_contents(file,contents_function="auto",verbose=False):
-       try:
-               _ = contents_function
-               if _ == 'auto' and file.endswith('.iso'):
-                       _ = 'isoinfo-l'
-               if (_ in ['tar-tv','auto']):
-                       if file.endswith('.tgz') or file.endswith('.tar.gz'):
-                               _ = 'tar-tvz'
-                       elif file.endswith('.tbz2') or 
file.endswith('.tar.bz2'):
-                               _ = 'tar-tvj'
-                       elif file.endswith('.tar'):
-                               _ = 'tar-tv'
-
-               if _ == 'auto':
-                       warn('File %r has unknown type for automatic 
detection.' % (file, ))
-                       return None
-               else:
-                       contents_function = _
-                       _ = contents_map[contents_function]
-                       return _[0](file,_[1],verbose)
-       except:
-               raise CatalystError,\
-                       "Error generating contents, is appropriate utility (%s) 
installed on your system?" \
-                       % (contents_function, )
-
-def calc_contents(file,cmd,verbose):
-       args={ 'file': file }
-       cmd=cmd % dict(args)
-       a=os.popen(cmd)
-       mylines=a.readlines()
-       a.close()
-       result="".join(mylines)
-       if verbose:
-               print result
-       return result
-
-# This has map must be defined after the function calc_content
-# It is possible to call different functions from this but they must be defined
-# before hash_map
-# Key,function,cmd
-contents_map={
-       # 'find' is disabled because it requires the source path, which is not
-       # always available
-       #"find"         :[calc_contents,"find %(path)s"],
-       "tar-tv":[calc_contents,"tar tvf %(file)s"],
-       "tar-tvz":[calc_contents,"tar tvzf %(file)s"],
-       "tar-tvj":[calc_contents,"tar -I lbzip2 -tvf %(file)s"],
-       "isoinfo-l":[calc_contents,"isoinfo -l -i %(file)s"],
-       # isoinfo-f should be a last resort only
-       "isoinfo-f":[calc_contents,"isoinfo -f -i %(file)s"],
-}
-
 
 def read_from_clst(file):
        line = ''

diff --git a/catalyst/targets/generic_stage_target.py 
b/catalyst/targets/generic_stage_target.py
index b6a6200..de4842c 100644
--- a/catalyst/targets/generic_stage_target.py
+++ b/catalyst/targets/generic_stage_target.py
@@ -1703,6 +1703,7 @@ class generic_stage_target(generic_target):
                if os.path.exists(file+".CONTENTS"):
                        os.remove(file+".CONTENTS")
                if "contents" in self.settings:
+                       contents_map = self.settings["contents_map"]
                        if os.path.exists(file):
                                myf=open(file+".CONTENTS","w")
                                keys={}
@@ -1711,7 +1712,7 @@ class generic_stage_target(generic_target):
                                        array=keys.keys()
                                        array.sort()
                                for j in array:
-                                       
contents=generate_contents(file,contents_function=j,\
+                                       contents = 
contents_map.generate_contents(file, j,
                                                verbose="VERBOSE" in 
self.settings)
                                        if contents:
                                                myf.write(contents)

Reply via email to