Blame lib/dbtexmf/dblatex/grubber/util.py
|
Packit |
0f19cf |
# This file is part of Rubber and thus covered by the GPL
|
|
Packit |
0f19cf |
# (c) Emmanuel Beffara, 2002--2006
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
This module contains utility functions and classes used by the main system and
|
|
Packit |
0f19cf |
by the modules for various tasks.
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
try:
|
|
Packit |
0f19cf |
import hashlib
|
|
Packit |
0f19cf |
except ImportError:
|
|
Packit |
0f19cf |
# Fallback for python 2.4:
|
|
Packit |
0f19cf |
import md5 as hashlib
|
|
Packit |
0f19cf |
import os
|
|
Packit Service |
f3de8e |
from io import open
|
|
Packit Service |
f3de8e |
|
|
Packit Service |
f3de8e |
from dbtexmf.dblatex.grubber.msg import _, msg
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
def md5_file(fname):
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
Compute the MD5 sum of a given file.
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
m = hashlib.md5()
|
|
Packit Service |
f3de8e |
file = open(fname, "rb")
|
|
Packit |
0f19cf |
for line in file.readlines():
|
|
Packit |
0f19cf |
m.update(line)
|
|
Packit |
0f19cf |
file.close()
|
|
Packit |
0f19cf |
return m.digest()
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
class Watcher:
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
Watch for any changes of the files to survey, by checking the file MD5 sums.
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
def __init__(self):
|
|
Packit |
0f19cf |
self.files = {}
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
def watch(self, file):
|
|
Packit |
0f19cf |
if os.path.exists(file):
|
|
Packit |
0f19cf |
self.files[file] = md5_file(file)
|
|
Packit |
0f19cf |
else:
|
|
Packit |
0f19cf |
self.files[file] = None
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
def update(self):
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
Update the MD5 sums of all files watched, and return the name of one
|
|
Packit |
0f19cf |
of the files that changed, or None of they didn't change.
|
|
Packit |
0f19cf |
"""
|
|
Packit |
0f19cf |
changed = []
|
|
Packit |
0f19cf |
for file in self.files.keys():
|
|
Packit |
0f19cf |
if os.path.exists(file):
|
|
Packit |
0f19cf |
new = md5_file(file)
|
|
Packit |
0f19cf |
if self.files[file] != new:
|
|
Packit |
0f19cf |
msg.debug(_("%s MD5 checksum changed") % \
|
|
Packit |
0f19cf |
os.path.basename(file))
|
|
Packit |
0f19cf |
changed.append(file)
|
|
Packit |
0f19cf |
self.files[file] = new
|
|
Packit |
0f19cf |
return changed
|
|
Packit |
0f19cf |
|