Last active
January 23, 2021 16:42
-
-
Save reo7sp/9aa2b89c90ac9bc729de327cf7045884 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import hashlib | |
import os | |
import traceback | |
import json | |
import time | |
def checksum_file(path): | |
with open(path, mode='rb') as f: | |
return hashlib.md5(f.read()).hexdigest() | |
def make_index(dir_path): | |
result = {} | |
for root, dirs, files in os.walk(dir_path): | |
for name in files: | |
path = os.path.join(root, name) | |
result[path] = checksum_file(path) | |
return result | |
def load_index(path): | |
try: | |
with open(path, mode='r') as f: | |
return json.load(f) | |
except: | |
return {} | |
def save_index(index, path): | |
with open(path, mode='w') as f: | |
json.dump(index, f, sort_keys=True, ensure_ascii=False, indent=4) | |
def diff_index(index1, index2): | |
result = {path: None for path in set(index1.keys()) | set(index2.keys())} | |
for path, checksum in index2.items(): | |
if path not in index1 or checksum != index1[path]: | |
result[path] = 1 | |
for path, checksum in index1.items(): | |
if path not in index2: | |
result[path] = 0 | |
result = {k: v for k, v in result.items() if v is not None} | |
return result | |
def add_diff_to_changelog(changelog, diff): | |
changelog[str(int(time.time()))] = diff | |
def print_diff(diff): | |
print('NEW DIFF:') | |
print(json.dumps(diff, sort_keys=True, ensure_ascii=False, indent=4)) | |
def load_changelog(path): | |
try: | |
with open(path, mode='r') as f: | |
return json.load(f) | |
except: | |
return {} | |
def save_changelog(changelog, path): | |
with open(path, mode='w') as f: | |
json.dump(changelog, f, sort_keys=True, ensure_ascii=False, indent=4) | |
def main(): | |
try: | |
source_dir_path = os.sys.argv[1] | |
output_index_path = os.sys.argv[2] | |
output_changelog_path = os.sys.argv[3] | |
except ValueError: | |
traceback.print_exc() | |
print('USAGE: python3 update-maker.py SOURCE_DIR OUTPUT_INDEX OUTPUT_CHANGELOG') | |
exit(1) | |
old_index = load_index(output_index_path) | |
new_index = make_index(source_dir_path) | |
diff = diff_index(old_index, new_index) | |
changelog = load_changelog(output_changelog_path) | |
add_diff_to_changelog(changelog, diff) | |
print_diff(diff) | |
if len(diff) != 0: | |
save_index(new_index, output_index_path) | |
save_changelog(changelog, output_changelog_path) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment