Functions | Variables
samweb_client.utility Namespace Reference

Functions

def fileChecksum (path, checksum_types=None, oldformat=False)
 
def calculateChecksum (fileobj, checksum_types=None, oldformat=False)
 
def fileEnstoreChecksum (path)
 
def enstoreChecksum (fileobj)
 
def _make_adler32 (startval=None, hex_output=True)
 
def _make_hash (algorithm)
 
def _get_checksum_algorithm (algorithm)
 
def list_checksum_algorithms ()
 

Variables

 _Adler32 = None
 
 _Hasher = None
 
 _value
 
 _hex
 
 hash
 

Function Documentation

def samweb_client.utility._get_checksum_algorithm (   algorithm)
private

Definition at line 116 of file utility.py.

References samweb_client.utility._make_adler32(), and samweb_client.utility._make_hash().

Referenced by samweb_client.utility.calculateChecksum().

116 def _get_checksum_algorithm(algorithm):
117 
118  if algorithm == 'enstore':
119  return _make_adler32(0, hex_output=False)
120  elif algorithm == 'adler32':
121  return _make_adler32()
122  else:
123  return _make_hash(algorithm)
124 
def _get_checksum_algorithm(algorithm)
Definition: utility.py:116
def _make_adler32(startval=None, hex_output=True)
Definition: utility.py:58
def _make_hash(algorithm)
Definition: utility.py:85
def samweb_client.utility._make_adler32 (   startval = None,
  hex_output = True 
)
private

Definition at line 58 of file utility.py.

References samweb_client.utility._Adler32.

Referenced by samweb_client.utility._get_checksum_algorithm().

58 def _make_adler32(startval=None, hex_output=True):
59  global _Adler32
60  import zlib
61 
62  if not _Adler32:
63  class _Adler32(object):
64  def __init__(self, startval=None, hex_output=hex_output):
65  if startval is not None:
66  self._value = zlib.adler32('', startval)
67  else:
68  self._value = zlib.adler32('')
69  self._hex = hex_output
70  def update(self, data):
71  self._value = zlib.adler32(data, self._value)
72  def value(self):
73  crc = long(self._value)
74  if crc < 0:
75  # Return 32 bit unsigned value
76  crc = (crc & 0x7FFFFFFFL) | 0x80000000L
77  if self._hex:
78  return '%08x' % crc
79  else:
80  return crc
81 
82  return _Adler32(startval, hex_output)
83 
Definition: novas.h:112
const XML_Char int const XML_Char * value
Definition: expat.h:331
def _make_adler32(startval=None, hex_output=True)
Definition: utility.py:58
def samweb_client.utility._make_hash (   algorithm)
private

Definition at line 85 of file utility.py.

References samweb_client.utility._Hasher.

Referenced by samweb_client.utility._get_checksum_algorithm().

85 def _make_hash(algorithm):
86 
87  global _Hasher
88  if not _Hasher:
89  class _Hasher(object):
90  def __init__(self, hasher):
91  self.hash = hasher
92  def update(self, data):
93  self.hash.update(data)
94  def value(self):
95  return self.hash.hexdigest()
96 
97  try:
98  from hashlib import md5,sha1,new
99  except ImportError:
100  from md5 import new as md5
101  from sha import new as sha1
102  new = None
103 
104  if algorithm == 'md5':
105  return _Hasher(md5())
106  elif algorithm == 'sha1':
107  return _Hasher(sha1())
108  elif new:
109  try:
110  return _Hasher(new(algorithm))
111  except ValueError:
112  pass
113 
114  raise Error("No checksum algorithm for %s" % algorithm)
115 
Definition: novas.h:112
const XML_Char int const XML_Char * value
Definition: expat.h:331
def _make_hash(algorithm)
Definition: utility.py:85
def samweb_client.utility.calculateChecksum (   fileobj,
  checksum_types = None,
  oldformat = False 
)

Definition at line 23 of file utility.py.

References samweb_client.utility._get_checksum_algorithm(), and runNovaSAM.str.

Referenced by samweb_client.utility.enstoreChecksum(), and samweb_client.utility.fileChecksum().

23 def calculateChecksum(fileobj, checksum_types=None, oldformat=False):
24  algorithms = {}
25  if oldformat:
26  if not (checksum_types is None or checksum_types==['enstore']):
27  raise Error("Old format checksums only support enstore type")
28  algorithms["enstore"] = _get_checksum_algorithm("enstore")
29  else:
30  if checksum_types is None: checksum_types = ["enstore"]
31  for ct in checksum_types:
32  algorithms[ct] = _get_checksum_algorithm(ct)
33 
34  readblocksize = 1024*1024
35  while 1:
36  try:
37  s = fileobj.read(readblocksize)
38  except (OSError, IOError), ex:
39  raise Error(str(ex))
40  if not s: break
41  for a in algorithms.itervalues():
42  a.update(s)
43 
44  if oldformat:
45  return { "crc_value" : algorithms["enstore"].value(), "crc_type" : "adler 32 crc type" }
46  else:
47  return [ "%s:%s" % (a,v.value()) for a,v in algorithms.iteritems() ]
48 
49 # for compatibility
def calculateChecksum(fileobj, checksum_types=None, oldformat=False)
Definition: utility.py:23
def _get_checksum_algorithm(algorithm)
Definition: utility.py:116
def samweb_client.utility.enstoreChecksum (   fileobj)

Definition at line 52 of file utility.py.

References samweb_client.utility.calculateChecksum().

52 def enstoreChecksum(fileobj):
53  return calculateChecksum(fileobj, oldformat=True)
54 
55 # Don't create the algorithm classes unless they are actually needed
56 
def calculateChecksum(fileobj, checksum_types=None, oldformat=False)
Definition: utility.py:23
def enstoreChecksum(fileobj)
Definition: utility.py:52
def samweb_client.utility.fileChecksum (   path,
  checksum_types = None,
  oldformat = False 
)
Calculate enstore compatible CRC value

Definition at line 12 of file utility.py.

References samweb_client.utility.calculateChecksum(), open(), and runNovaSAM.str.

Referenced by samweb_client.utility.fileEnstoreChecksum().

12 def fileChecksum(path, checksum_types=None, oldformat=False):
13  """Calculate enstore compatible CRC value"""
14  try:
15  f =open(path,'rb')
16  except (IOError, OSError), ex:
17  raise Error(str(ex))
18  try:
19  return calculateChecksum(f, checksum_types=checksum_types, oldformat=oldformat)
20  finally:
21  f.close()
22 
def calculateChecksum(fileobj, checksum_types=None, oldformat=False)
Definition: utility.py:23
def fileChecksum(path, checksum_types=None, oldformat=False)
Definition: utility.py:12
procfile open("FD_BRL_v0.txt")
def samweb_client.utility.fileEnstoreChecksum (   path)

Definition at line 50 of file utility.py.

References samweb_client.utility.fileChecksum().

Referenced by runNovaSAM.createMetadata(), runNovaSAM_cmake.createMetadata(), and MetadataUtils.createMetadata().

51  return fileChecksum(path, oldformat=True)
def fileChecksum(path, checksum_types=None, oldformat=False)
Definition: utility.py:12
def fileEnstoreChecksum(path)
Definition: utility.py:50
def samweb_client.utility.list_checksum_algorithms ( )

Definition at line 125 of file utility.py.

126  algos = set(['enstore', 'adler32'])
127  try:
128  import hashlib
129  if hasattr(hashlib, 'algorithms_available'):
130  algos.update(hashlib.algorithms_available)
131  elif hasattr(hashlib, 'algorithms'):
132  algos.update(hashlib.algorithms)
133  else:
134  algos.update(['md5','sha1','sha224', 'sha256', 'sha384', 'sha512'])
135  except ImportError:
136  algos.update(['md5','sha1',])
137  return algos
138 
def list_checksum_algorithms()
Definition: utility.py:125

Variable Documentation

samweb_client.utility._Adler32 = None
private

Definition at line 57 of file utility.py.

Referenced by samweb_client.utility._make_adler32().

samweb_client.utility._Hasher = None
private

Definition at line 84 of file utility.py.

Referenced by samweb_client.utility._make_hash().

samweb_client.utility._hex
private

Definition at line 69 of file utility.py.

samweb_client.utility._value
private

Definition at line 66 of file utility.py.

Referenced by photrans::BrightnessLevel.BrightnessLevel().