diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-01-01 03:14:11 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-01-01 03:14:11 +0200 |
commit | 2220cc038941de88a26b981e797c091f7869e8d5 (patch) | |
tree | 20a1d542df720572ea20771fee85da3657d2bd0f /summainlib.py | |
parent | 37810eaf498b82c8fb54bcf22c0861ad2c370d69 (diff) | |
download | summain-2220cc038941de88a26b981e797c091f7869e8d5.tar.gz |
Add support for computing SHA-1 of files. Currently assumes it is
a regular file.
Diffstat (limited to 'summainlib.py')
-rw-r--r-- | summainlib.py | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/summainlib.py b/summainlib.py index 4d0a89c..509bc50 100644 --- a/summainlib.py +++ b/summainlib.py @@ -15,6 +15,7 @@ import grp +import hashlib import os import pwd import time @@ -28,12 +29,15 @@ class FilesystemObject(object): Responsible for gathering information and formatting it for reporting. - The optional stat_result argument is intended for unit tests. + The optional arguments are intended for unit tests. ''' - def __init__(self, filename, stat_result=None): + def __init__(self, filename, stat_result=None, sha1=None, + open_file=None): stat_result = stat_result or os.lstat(filename) + sha1 = sha1 or hashlib.sha1() + self.open_file = open_file or self.open_file self.values = dict() self['Name'] = filename self['Mtime'] = self.format_time(stat_result.st_mtime) @@ -46,6 +50,7 @@ class FilesystemObject(object): self['Username'] = self.lookup_username(stat_result.st_uid) self['Gid'] = '%d' % stat_result.st_gid self['Group'] = self.lookup_group(stat_result.st_gid) + self['Sha-1'] = self.compute_sha1(filename, sha1) def format_time(self, timestamp): return time.strftime('%Y-%m-%d %H:%M:%S +0000', @@ -57,6 +62,16 @@ class FilesystemObject(object): def lookup_group(self, gid): return grp.getgrgid(gid).gr_name + def compute_sha1(self, filename, sha1): + f = self.open_file(filename) + while True: + data = f.read(64*1024) # 64 KiB seems reasonable. + if not data: + break + sha1.update(data) + f.close() + return sha1.hexdigest() + def hook_name(self, value): return urllib.quote(value) |