sum5.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Tools » scripts » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Tools » scripts » sum5.py
#! /usr/bin/env python

# print md5 checksum for files

bufsize = 8096
fnfilter = None
rmode = 'r'

usage = """
usage: sum5 [-b] [-t] [-l] [-s bufsize] [file ...]
-b        : read files in binary mode
-t        : read files in text mode (default)
-l        : print last pathname component only
-s bufsize: read buffer size (default %d)
file ...  : files to sum; '-' or no files means stdin
""" % bufsize

import sys
import string
import os
import md5
import regsub

StringType = type('')
FileType = type(sys.stdin)

def sum(*files):
  sts = 0
  if files and type(files[-1]) == FileType:
    out, files = files[-1], files[:-1]
  else:
    out = sys.stdout
  if len(files) == 1 and type(files[0]) != StringType:
    files = files[0]
  for f in files:
    if type(f) == StringType:
      if f == '-':
        sts = printsumfp(sys.stdin, '<stdin>', out) or sts
      else:
        sts = printsum(f, out) or sts
    else:
      sts = sum(f, out) or sts
  return sts

def printsum(file, out = sys.stdout):
  try:
    fp = open(file, rmode)
  except IOError, msg:
    sys.stderr.write('%s: Can\'t open: %s\n' % (file, msg))
    return 1
  if fnfilter:
    file = fnfilter(file)
  sts = printsumfp(fp, file, out)
  fp.close()
  return sts

def printsumfp(fp, file, out = sys.stdout):
  m = md5.md5()
  try:
    while 1:
      data = fp.read(bufsize)
      if not data: break
      m.update(data)
  except IOError, msg:
    sys.stderr.write('%s: I/O error: %s\n' % (file, msg))
    return 1
  out.write('%s %s\n' % (hexify(m.digest()), file))
  return 0

def hexify(s):
  res = ''
  for c in s:
    res = res + '%02x' % ord(c)
  return res

def main(args = sys.argv[1:], out = sys.stdout):
  global fnfilter, rmode, bufsize
  import getopt
  try:
    opts, args = getopt.getopt(args, 'blts:')
  except getopt.error, msg:
    sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage))
    return 2
  for o, a in opts:
    if o == '-l':
      fnfilter = os.path.basename
    if o == '-b':
      rmode = 'rb'
    if o == '-t':
      rmode = 'r'
    if o == '-s':
      bufsize = string.atoi(a)
  if not args: args = ['-']
  return sum(args, out)

if __name__ == '__main__' or __name__ == sys.argv[0]:
  sys.exit(main(sys.argv[1:], sys.stdout))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.