-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mgoodfellow/hash-previous-backup-output
Write a hash file and handle updates etc.
- Loading branch information
Showing
7 changed files
with
169 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import hashlib | ||
import os | ||
from shutil import move | ||
from tempfile import mkstemp | ||
|
||
BLOCKSIZE=65535 | ||
|
||
def find_hash(hash_file, plan_name): | ||
# Try to find the hash in the hash file | ||
filename = os.path.normpath(hash_file) | ||
if os.path.isfile(filename): | ||
plan_hashes = open(filename, 'r').readlines() | ||
for line in plan_hashes: | ||
parts = line.strip().split('=') | ||
if len(parts) == 2 and parts[0] == plan_name: | ||
return parts[1] | ||
|
||
return None | ||
|
||
def update_hash(hash_file, plan_name, hash_value): | ||
# Do the update (create the file if it doesn't exist) | ||
filename = os.path.normpath(hash_file) | ||
|
||
# If it doesn't exist, we shortcut this | ||
if not os.path.isfile(hash_file): | ||
with open(hash_file, 'w') as new_file: | ||
new_file.write('%s=%s\n' % (plan_name, hash_value)) | ||
return | ||
|
||
# Otherwise, we need to rebuild the file | ||
fh, abs_path = mkstemp() | ||
is_written = False | ||
|
||
with open(abs_path, 'w') as new_file: | ||
with open(filename, 'r') as old_file: | ||
# Handle existing entries in the file | ||
for line in old_file: | ||
parts = line.strip().split('=') | ||
if parts[0] == plan_name: | ||
is_written = True | ||
new_file.write('%s=%s\n' % (plan_name, hash_value)) | ||
else: | ||
new_file.write(line) | ||
|
||
# If the hash wasn't already in the file | ||
if not is_written: | ||
new_file.write('%s=%s\n' % (plan_name, hash_value)) | ||
|
||
os.close(fh) | ||
|
||
# Remove original file | ||
os.remove(hash_file) | ||
|
||
# Move new file | ||
move(abs_path, hash_file) | ||
|
||
def calc_hash(filename): | ||
hasher = hashlib.md5() | ||
with open(filename, 'rb') as afile: | ||
buf = afile.read(BLOCKSIZE) | ||
while len(buf) > 0: | ||
hasher.update(buf) | ||
buf = afile.read(BLOCKSIZE) | ||
return hasher.hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Features to be added | ||
|
||
1. Ability to choose custom format strings on output file (rather than automatically appending date/time) | ||
2. Ability to run multiple commands | ||
2. Ability to run multiple commands | ||
3. Modify the glob2 library to support hidden files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters