# Copyright 2002-2009 Gentoo Foundation; 2008-2009 Various authors (see AUTHORS)
# Distributed under the GPL v2
+ 11 Jan 2009; Andrew Gaffney <agaffney@gentoo.org>
+ +modules/catalyst/hash.py, modules/catalyst/support.py,
+ modules/catalyst/target/generic_stage.py,
+ modules/catalyst/target/livecd_stage2.py,
+ modules/catalyst/target/stage2.py, modules/catalyst/util.py:
+ Move all hash and contents functions to catalyst.hash
+
11 Jan 2009; Andrew Gaffney <agaffney@gentoo.org>
modules/catalyst/support.py, modules/catalyst/target/generic_stage.py,
modules/catalyst/util.py:
--- /dev/null
+"""
+This module contains functions for generating the CONTENTS and hash files
+"""
+
+import os
+
+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):
+ cmd = cmd % { 'file': file }
+ a = os.popen(cmd)
+ result = "".join(a.readlines())
+ a.close()
+ 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 tvjf %(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 generate_hash(file, hash_function="crc32", verbose=False):
+ try:
+ return hash_map[hash_function][0](file, hash_map[hash_function][1], hash_map[hash_function][2], \
+ hash_map[hash_function][3],verbose)
+ except:
+ raise CatalystError, "Error generating hash, is appropriate utility installed on your system?"
+
+def calc_hash(file, cmd, cmd_args, id_string="MD5", verbose=False):
+ a = os.popen(cmd + " " + cmd_args + " " + file)
+ result = a.readline().split()[0]
+ a.close()
+ if verbose:
+ print "%s (%s) = %s" % (id_string, file, result)
+ return result
+
+def calc_hash2(file, cmd, cmd_args, id_string="MD5", verbose=False):
+ a = os.popen(cmd + " " + cmd_args + " " + file)
+ header = a.readline()
+ myline = a.readline().split()
+ a.close()
+ hash = myline[0]
+ short_file = os.path.split(myline[1])[1]
+ result = header + hash + " " + short_file + "\n"
+ if verbose:
+ print "%s (%s) = %s" % (header, short_file, result)
+ return result
+
+# This has map must be defined after the function calc_hash
+# It is possible to call different functions from this but they must be defined
+# before hash_map
+# Key,function,cmd,cmd_args,Print string
+hash_map={
+ "adler32":[calc_hash2,"shash","-a ADLER32","ADLER32"],
+ "crc32":[calc_hash2,"shash","-a CRC32","CRC32"],
+ "crc32b":[calc_hash2,"shash","-a CRC32B","CRC32B"],
+ "gost":[calc_hash2,"shash","-a GOST","GOST"],
+ "haval128":[calc_hash2,"shash","-a HAVAL128","HAVAL128"],
+ "haval160":[calc_hash2,"shash","-a HAVAL160","HAVAL160"],
+ "haval192":[calc_hash2,"shash","-a HAVAL192","HAVAL192"],
+ "haval224":[calc_hash2,"shash","-a HAVAL224","HAVAL224"],
+ "haval256":[calc_hash2,"shash","-a HAVAL256","HAVAL256"],
+ "md2":[calc_hash2,"shash","-a MD2","MD2"],
+ "md4":[calc_hash2,"shash","-a MD4","MD4"],
+ "md5":[calc_hash2,"shash","-a MD5","MD5"],
+ "ripemd128":[calc_hash2,"shash","-a RIPEMD128","RIPEMD128"],
+ "ripemd160":[calc_hash2,"shash","-a RIPEMD160","RIPEMD160"],
+ "ripemd256":[calc_hash2,"shash","-a RIPEMD256","RIPEMD256"],
+ "ripemd320":[calc_hash2,"shash","-a RIPEMD320","RIPEMD320"],
+ "sha1":[calc_hash2,"shash","-a SHA1","SHA1"],
+ "sha224":[calc_hash2,"shash","-a SHA224","SHA224"],
+ "sha256":[calc_hash2,"shash","-a SHA256","SHA256"],
+ "sha384":[calc_hash2,"shash","-a SHA384","SHA384"],
+ "sha512":[calc_hash2,"shash","-a SHA512","SHA512"],
+ "snefru128":[calc_hash2,"shash","-a SNEFRU128","SNEFRU128"],
+ "snefru256":[calc_hash2,"shash","-a SNEFRU256","SNEFRU256"],
+ "tiger":[calc_hash2,"shash","-a TIGER","TIGER"],
+ "tiger128":[calc_hash2,"shash","-a TIGER128","TIGER128"],
+ "tiger160":[calc_hash2,"shash","-a TIGER160","TIGER160"],
+ "whirlpool":[calc_hash2,"shash","-a WHIRLPOOL","WHIRLPOOL"],
+}
-import sys,string,os,types,re,signal,traceback,time
+import sys, os, types, re, signal, traceback
from catalyst.output import warn
import catalyst.util
-#import md5,sha
selinux_capable = False
#userpriv_capable = (os.getuid() == 0)
BASH_BINARY = "/bin/bash"
try:
- import resource
- max_fd_limit=resource.getrlimit(RLIMIT_NOFILE)
+ import resource
+ max_fd_limit=resource.getrlimit(RLIMIT_NOFILE)
except SystemExit, e:
- raise
+ raise
except:
- # hokay, no resource module.
- max_fd_limit=256
+ # hokay, no resource module.
+ max_fd_limit=256
# pids this process knows of.
spawned_pids = []
-try:
- import urllib
-except SystemExit, e:
- raise
-
def cleanup(pids,block_exceptions=True):
"""function to go through and reap the list of pids passed to it"""
global spawned_pids
except IndexError: pass
-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 tvjf %(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 generate_hash(file,hash_function="crc32",verbose=False):
- try:
- return hash_map[hash_function][0](file,hash_map[hash_function][1],hash_map[hash_function][2],\
- hash_map[hash_function][3],verbose)
- except:
- raise CatalystError,"Error generating hash, is appropriate utility installed on your system?"
-
-def calc_hash(file,cmd,cmd_args,id_string="MD5",verbose=False):
- a=os.popen(cmd+" "+cmd_args+" "+file)
- mylines=a.readlines()
- a.close()
- mylines=mylines[0].split()
- result=mylines[0]
- if verbose:
- print id_string+" (%s) = %s" % (file, result)
- return result
-
-def calc_hash2(file,cmd,cmd_args,id_string="MD5",verbose=False):
- a=os.popen(cmd+" "+cmd_args+" "+file)
- header=a.readline()
- mylines=a.readline().split()
- hash=mylines[0]
- short_file=os.path.split(mylines[1])[1]
- a.close()
- result=header+hash+" "+short_file+"\n"
- if verbose:
- print header+" (%s) = %s" % (short_file, result)
- return result
-
-# This has map must be defined after the function calc_hash
-# It is possible to call different functions from this but they must be defined
-# before hash_map
-# Key,function,cmd,cmd_args,Print string
-hash_map={
- "adler32":[calc_hash2,"shash","-a ADLER32","ADLER32"],\
- "crc32":[calc_hash2,"shash","-a CRC32","CRC32"],\
- "crc32b":[calc_hash2,"shash","-a CRC32B","CRC32B"],\
- "gost":[calc_hash2,"shash","-a GOST","GOST"],\
- "haval128":[calc_hash2,"shash","-a HAVAL128","HAVAL128"],\
- "haval160":[calc_hash2,"shash","-a HAVAL160","HAVAL160"],\
- "haval192":[calc_hash2,"shash","-a HAVAL192","HAVAL192"],\
- "haval224":[calc_hash2,"shash","-a HAVAL224","HAVAL224"],\
- "haval256":[calc_hash2,"shash","-a HAVAL256","HAVAL256"],\
- "md2":[calc_hash2,"shash","-a MD2","MD2"],\
- "md4":[calc_hash2,"shash","-a MD4","MD4"],\
- "md5":[calc_hash2,"shash","-a MD5","MD5"],\
- "ripemd128":[calc_hash2,"shash","-a RIPEMD128","RIPEMD128"],\
- "ripemd160":[calc_hash2,"shash","-a RIPEMD160","RIPEMD160"],\
- "ripemd256":[calc_hash2,"shash","-a RIPEMD256","RIPEMD256"],\
- "ripemd320":[calc_hash2,"shash","-a RIPEMD320","RIPEMD320"],\
- "sha1":[calc_hash2,"shash","-a SHA1","SHA1"],\
- "sha224":[calc_hash2,"shash","-a SHA224","SHA224"],\
- "sha256":[calc_hash2,"shash","-a SHA256","SHA256"],\
- "sha384":[calc_hash2,"shash","-a SHA384","SHA384"],\
- "sha512":[calc_hash2,"shash","-a SHA512","SHA512"],\
- "snefru128":[calc_hash2,"shash","-a SNEFRU128","SNEFRU128"],\
- "snefru256":[calc_hash2,"shash","-a SNEFRU256","SNEFRU256"],\
- "tiger":[calc_hash2,"shash","-a TIGER","TIGER"],\
- "tiger128":[calc_hash2,"shash","-a TIGER128","TIGER128"],\
- "tiger160":[calc_hash2,"shash","-a TIGER160","TIGER160"],\
- "whirlpool":[calc_hash2,"shash","-a WHIRLPOOL","WHIRLPOOL"],\
- }
-
required_config_file_values=["storedir","sharedir","distdir","portdir"]
valid_config_file_values=required_config_file_values[:]
valid_config_file_values.append("PKGCACHE")
mydata=fd.readlines()
fd.close()
if emulate_gso:
- mydata=string.join(mydata)
+ mydata="".join(mydata)
if len(mydata) and mydata[-1] == "\n":
mydata=mydata[:-1]
retval=os.waitpid(mypid[0],0)[1]
raise
except Exception, e:
if not func_call:
- raise str(e)+":\n "+myc+" "+string.join(myargs)
+ raise str(e)+":\n "+myc+" "+"".join(myargs)
print "func call failed"
# If the execve fails, we need to report it, and exit
import catalyst.arch
from catalyst.output import warn
import catalyst.util
+import catalyst.hash
class generic_stage_target(generic_target):
# XXX: Is this even necessary if the previous check passes?
if os.path.exists(self.settings["source_path"]):
self.settings["source_path_hash"]=\
- generate_hash(self.settings["source_path"],\
+ catalyst.hash.generate_hash(self.settings["source_path"],\
hash_function=self.settings["hash_function"],\
verbose=False)
print "Source path set to "+self.settings["source_path"]
if os.path.exists(self.settings["snapshot_path"]):
self.settings["snapshot_path_hash"]=\
- generate_hash(self.settings["snapshot_path"],\
+ catalyst.hash.generate_hash(self.settings["snapshot_path"],\
hash_function=self.settings["hash_function"],verbose=False)
def set_snapcache_path(self):
array=keys.keys()
array.sort()
for j in array:
- contents=catalyst.util.generate_contents(file,contents_function=j,\
+ contents=catalyst.hash.generate_contents(file,contents_function=j,\
verbose=("VERBOSE" in self.settings))
if contents:
myf.write(contents)
if os.path.exists(f):
if "all" in array:
for k in hash_map.keys():
- hash=generate_hash(f,hash_function=k,verbose=\
+ hash=catalyst.hash.generate_hash(f,hash_function=k,verbose=\
("VERBOSE" in self.settings))
myf.write(hash)
else:
for j in array:
- hash=generate_hash(f,hash_function=j,verbose=\
+ hash=catalyst.hash.generate_hash(f,hash_function=j,verbose=\
("VERBOSE" in self.settings))
myf.write(hash)
myf.close()
from catalyst.support import *
from generic_stage import *
import catalyst.util
+import catalyst.hash
class livecd_stage2_target(generic_stage_target):
def __init__(self,spec,addlargs):
def set_source_path(self):
self.settings["source_path"]=catalyst.util.normpath(self.settings["storedir"]+"/builds/"+self.settings["source_subpath"]+".tar.bz2")
if os.path.isfile(self.settings["source_path"]):
- self.settings["source_path_hash"]=generate_hash(self.settings["source_path"])
+ self.settings["source_path_hash"]=catalyst.hash.generate_hash(self.settings["source_path"])
else:
self.settings["source_path"]=catalyst.util.normpath(self.settings["storedir"]+"/tmp/"+self.settings["source_subpath"]+"/")
if not os.path.exists(self.settings["source_path"]):
from catalyst.support import *
from generic_stage import *
import catalyst.util
+import catalyst.hash
class stage2_target(generic_stage_target):
def __init__(self,spec,addlargs):
if os.path.isfile(self.settings["source_path"]):
if os.path.exists(self.settings["source_path"]):
# XXX: Is this even necessary if the previous check passes?
- self.settings["source_path_hash"]=generate_hash(self.settings["source_path"],\
+ self.settings["source_path_hash"]=catalyst.hash.generate_hash(self.settings["source_path"],\
hash_function=self.settings["hash_function"],verbose=False)
print "Source path set to "+self.settings["source_path"]
if os.path.isdir(self.settings["source_path"]):
except IOError:
raise CatalystError, "Could not touch " + myfile + "."
-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 countdown(secs=5, doing="Starting"):
if secs:
print ">>> Waiting " + secs + " seconds before starting..."