pyplusplus.file_writers.md5sum_repository

Package pyplusplus :: Package file_writers :: Module md5sum_repository |

# Source Code for Module pyplusplus.file_writers.md5sum_repository

     1  # Copyright 2004-2008 Roman Yakovenko.
     2  # Distributed under the Boost Software License, Version 1.0. (See
     3  # accompanying file LICENSE_1_0.txt or copy at
     4  # http://www.boost.org/LICENSE_1_0.txt)
     5
     6  """defines interface for repository of generated files hash"""
     7
     8  import os
     9  try:
    10      from hashlib import md5
    11  except:
    12      from md5 import new as md5
    13
    14
    15 -def get_md5_text_value( text ):
    16      m = md5()
    17      m.update( text )
    18      return m.hexdigest()
    19
    20 -def get_md5_file_value( fpath ):
    21      if not os.path.exists( fpath ):
    22          return None #file does not exist
    23      f = file( fpath, 'rb' )
    24      fcontent = f.read()
    25      f.close()
    26      return get_md5_text_value( fcontent )
    27
    28 -class repository_t( object ):
    29 -    def __init__( self ):
    30          object.__init__( self )
    31
    32 -    def get_file_value( self, fpath ):
    33          return NotImplementedError( self.__class__.__name__ )
    34
    35 -    def get_text_value( self, fpath ):
    36          return NotImplementedError( self.__class__.__name__ )
    37
    38 -    def update_value( self, fpath, hash_value ):
    39          return NotImplementedError( self.__class__.__name__ )
    40
    41 -    def save_values( self ):
    42          return NotImplementedError( self.__class__.__name__ )
    43
    44 -class dummy_repository_t( repository_t ):
    45 -    def __init__( self ):
    46          repository_t.__init__( self )
    47
    48 -    def get_file_value( self, fpath ):
    49          return get_md5_file_value( fpath )
    50
    51 -    def get_text_value( self, text ):
    52          return get_md5_text_value( text )
    53
    54 -    def update_value( self, fpath, hash_value ):
    55          pass
    56
    57 -    def save_values( self ):
    58          pass
    59
    60 -class cached_repository_t( repository_t ):
    61      separator = ' '
    62      hexdigest_len = 32
    63      hexdigest_separator_len = 33
    64
    65 -    def __init__( self, file_name ):
    66          repository_t.__init__( self )
    67          self.__repository = {}
    68          self.__repository_file = file_name
    69          if os.path.exists( self.__repository_file ):
    70              f = file( self.__repository_file, 'r' )
    71              for line in f:
    72                  if len(line) < self.hexdigest_separator_len:
    73                      continue
    74                  hexdigest = line[:self.hexdigest_len]
    75                  fname = line[self.hexdigest_separator_len:].rstrip()
    76                  self.__repository[ fname ] = hexdigest
    77              f.close()
    78
    79 -    def get_file_value( self, fpath ):
    80          try:
    81              return self.__repository[ fpath ]
    82          except KeyError:
    83              return None
    84
    85 -    def get_text_value( self, text ):
    86          return get_md5_text_value( text )
    87
    88 -    def update_value( self, fpath, hash_value ):
    89          self.__repository[ fpath ] = hash_value
    90
    91 -    def save_values( self ):
    92          lines = []
    93          for fpath, hexdigest in self.__repository.iteritems():
    94              lines.append( '%s%s%s%s' % ( hexdigest, self.separator, fpath, os.linesep ) )
    95          f = file( self.__repository_file, 'w+' )
    96          f.writelines( lines )
    97          f.close()
    98