Back to index

enigmail  1.4.3
Functions | Variables
build.checksums Namespace Reference

Functions

def digest_file
def process_files
def setup_logging
def main

Variables

 hashlib = None

Function Documentation

def build.checksums.digest_file (   filename,
  digest,
  chunk_size = 1024 
)
Produce a checksum for the file specified by 'filename'.  'filename'
is a string path to a file that is opened and read in this function.  The
checksum algorithm is specified by 'digest' and is a valid OpenSSL
algorithm.  If the digest used is not valid or Python's hashlib doesn't
work, the None object will be returned instead.  The size of blocks
that this function will read from the file object it opens based on
'filename' can be specified by 'chunk_size', which defaults to 1K

Definition at line 49 of file checksums.py.

00049 
00050 def digest_file(filename, digest, chunk_size=1024):
00051     '''Produce a checksum for the file specified by 'filename'.  'filename'
00052     is a string path to a file that is opened and read in this function.  The
00053     checksum algorithm is specified by 'digest' and is a valid OpenSSL
00054     algorithm.  If the digest used is not valid or Python's hashlib doesn't
00055     work, the None object will be returned instead.  The size of blocks
00056     that this function will read from the file object it opens based on
00057     'filename' can be specified by 'chunk_size', which defaults to 1K'''
00058     assert not os.path.isdir(filename), 'this function only works with files'
00059     logger = logging.getLogger('checksums.py')
00060     if hashlib is not None:
00061         logger.debug('Creating new %s object' % digest)
00062         h = hashlib.new(digest)
00063         with open(filename, 'rb') as f:
00064             while True:
00065                 data = f.read(chunk_size)
00066                 if not data:
00067                     logger.debug('Finished reading in file')
00068                     break
00069                 h.update(data)
00070         hash = h.hexdigest()
00071         logger.debug('Hash for %s is %s' % (filename, hash))
00072         return hash
00073     else:
00074         # In this case we could subprocess.Popen and .communicate with
00075         # sha1sum or md5sum
00076         logger.warn('The python module for hashlib is missing!')
00077         return None
00078 

Here is the caller graph for this function:

This is a main function that parses arguments, sets up logging
and generates a checksum file

Definition at line 139 of file checksums.py.

00139 
00140 def main():
00141     '''This is a main function that parses arguments, sets up logging
00142     and generates a checksum file'''
00143     # Parse command line arguments
00144     parser = OptionParser()
00145     parser.add_option('-d', '--digest', help='checksum algorithm to use',
00146                       action='append', dest='digests')
00147     parser.add_option('-o', '--output', help='output file to use',
00148                       action='store', dest='outfile', default='checksums')
00149     parser.add_option('-v', '--verbose',
00150                       help='Be noisy (takes precedence over quiet)',
00151                       action='store_true', dest='verbose', default=False)
00152     parser.add_option('-q', '--quiet', help='Be quiet', action='store_true',
00153                       dest='quiet', default=False)
00154     parser.add_option('-s', '--strip',
00155                       help='strip this path from the filenames',
00156                       dest='strip', default=os.getcwd())
00157     options, args = parser.parse_args()
00158 
00159     #Figure out which logging level to use
00160     if options.verbose:
00161         loglevel = logging.DEBUG
00162     elif options.quiet:
00163         loglevel = logging.ERROR
00164     else:
00165         loglevel = logging.INFO
00166 
00167     #Set up logging
00168     setup_logging(loglevel)
00169     logger = logging.getLogger('checksums.py')
00170 
00171     # Validate the digest type to use
00172     if not options.digests:
00173         options.digests = ['sha1']
00174     try:
00175         for digest in options.digests:
00176             hashlib.new(digest)
00177     except ValueError, ve:
00178         logger.error('Could not create a "%s" hash object (%s)' %
00179                      (digest, ve.args[0]))
00180         exit(1)
00181 
00182     # Validate the files to checksum
00183     files = []
00184     for i in args:
00185         if os.path.exists(i):
00186             files.append(i)
00187         else:
00188             logger.info('File "%s" was not found on the filesystem' % i)
00189     process_files(files, options.outfile, options.digests, options.strip)

Here is the call graph for this function:

def build.checksums.process_files (   files,
  output_filename,
  digests,
  strip 
)
This function takes a list of file names, 'files'.  It will then
compute the checksum for each of the files by opening the files.
Once each file is read and its checksum is computed, this function
will write the information to the file specified by 'output_filename'.
The path written in the output file will have anything specified by 'strip'
removed from the path.  The output file is closed before returning nothing
The algorithm to compute checksums with can be specified by 'digests' 
and needs to be a list of valid OpenSSL algorithms.

The output file is written in the format:
    <hash> <algorithm> <filesize> <filepath>
Example:
    d1fa09a<snip>e4220 sha1 14250744 firefox-4.0b6pre.en-US.mac64.dmg

Definition at line 79 of file checksums.py.

00079 
00080 def process_files(files, output_filename, digests, strip):
00081     '''This function takes a list of file names, 'files'.  It will then
00082     compute the checksum for each of the files by opening the files.
00083     Once each file is read and its checksum is computed, this function
00084     will write the information to the file specified by 'output_filename'.
00085     The path written in the output file will have anything specified by 'strip'
00086     removed from the path.  The output file is closed before returning nothing
00087     The algorithm to compute checksums with can be specified by 'digests' 
00088     and needs to be a list of valid OpenSSL algorithms.
00089 
00090     The output file is written in the format:
00091         <hash> <algorithm> <filesize> <filepath>
00092     Example:
00093         d1fa09a<snip>e4220 sha1 14250744 firefox-4.0b6pre.en-US.mac64.dmg
00094     '''
00095 
00096     logger = logging.getLogger('checksums.py')
00097     if os.path.exists(output_filename):
00098         logger.debug('Overwriting existing checksums file "%s"' %
00099                      output_filename)
00100     else:
00101         logger.debug('Creating a new checksums file "%s"' % output_filename)
00102     with open(output_filename, 'w+') as output:
00103         for file in files:
00104             if os.path.isdir(file):
00105                 logger.warn('%s is a directory, skipping' % file)
00106             else:
00107                 for digest in digests:
00108                     hash = digest_file(file, digest)
00109                     if hash is None:
00110                         logger.warn('Unable to generate a hash for %s. ' +
00111                                     'Skipping.' % file)
00112                         continue
00113                     if file.startswith(strip):
00114                         short_file = file[len(strip):]
00115                         short_file = short_file.lstrip('/')
00116                     else:
00117                         short_file = file
00118                     print >>output, '%s %s %s %s' % (hash, digest,
00119                                                      os.path.getsize(file),
00120                                                      short_file)

Here is the call graph for this function:

Here is the caller graph for this function:

def build.checksums.setup_logging (   level = logging.DEBUG)
This function sets up the logging module using a speficiable logging
module logging level.  The default log level is DEBUG.

The output is in the format:
    <level> - <message>
Example:
    DEBUG - Finished reading in file

Definition at line 121 of file checksums.py.

00121 
00122 def setup_logging(level=logging.DEBUG):
00123     '''This function sets up the logging module using a speficiable logging
00124     module logging level.  The default log level is DEBUG.
00125 
00126     The output is in the format:
00127         <level> - <message>
00128     Example:
00129         DEBUG - Finished reading in file
00130 '''
00131 
00132     logger = logging.getLogger('checksums.py')
00133     logger.setLevel(logging.DEBUG)
00134     handler = logging.StreamHandler()
00135     handler.setLevel(level)
00136     formatter = logging.Formatter("%(levelname)s - %(message)s")
00137     handler.setFormatter(formatter)
00138     logger.addHandler(handler)

Here is the caller graph for this function:


Variable Documentation

Definition at line 47 of file checksums.py.