Steven Smith revised this gist . Go to revision
1 file changed, 12 insertions, 5 deletions
checksummer.py
@@ -9,22 +9,29 @@ def crc32_file(filename): | |||
9 | 9 | return hex(zlib.crc32(f.read())).upper()[2:].zfill(8) | |
10 | 10 | ||
11 | 11 | def get_files_recursively(directory="."): | |
12 | - | for root, dirs, files in os.walk(directory): | |
12 | + | for root, dirs, files in os.walk(os.path.abspath(directory)): | |
13 | 13 | for name in files: | |
14 | 14 | yield os.path.join(root, name) | |
15 | 15 | ||
16 | 16 | def add_crc_to_fn(filename): | |
17 | 17 | new_name = "{0} [{2}]{1}".format(*os.path.splitext(filename), crc32_file(filename)) | |
18 | - | print("{} >> {}".format(filename, new_name)) | |
19 | - | os.rename(filename, new_name) | |
18 | + | try: | |
19 | + | os.rename(filename, new_name) | |
20 | + | print("{} >> {}".format(filename, new_name), file=sys.stderr) | |
21 | + | except (OSError, IOError) as e: | |
22 | + | print("{} Unable to rename {}".format(e.__class__.__name__, filename)) | |
20 | 23 | ||
21 | 24 | def add_crc_to_sfv(filename): | |
22 | 25 | crc = crc32_file(filename) | |
23 | 26 | dirname = os.path.dirname(filename) | |
24 | 27 | line = "{} {}\n".format(os.path.basename(filename), crc) | |
25 | 28 | sfv_path = os.path.join(dirname, os.path.basename(dirname) + ".sfv") | |
26 | - | with open(sfv_path, "a") as f: | |
27 | - | f.write(line) | |
29 | + | try: | |
30 | + | with open(sfv_path, "a") as f: | |
31 | + | f.write(line) | |
32 | + | print("{} >> {}".format(filename, crc)) | |
33 | + | except (OSError, IOError) as e: | |
34 | + | print("{} Unable to create {}".format(e.__class__.__name__, sfv_path)) | |
28 | 35 | ||
29 | 36 | if __name__ == "__main__": | |
30 | 37 | parser = argparse.ArgumentParser() |
Steven Smith revised this gist . Go to revision
1 file changed, 38 insertions
checksummer.py(file created)
@@ -0,0 +1,38 @@ | |||
1 | + | #!/usr/bin/env python3 | |
2 | + | import zlib | |
3 | + | import os | |
4 | + | import sys | |
5 | + | import argparse | |
6 | + | ||
7 | + | def crc32_file(filename): | |
8 | + | with open(filename, "rb") as f: | |
9 | + | return hex(zlib.crc32(f.read())).upper()[2:].zfill(8) | |
10 | + | ||
11 | + | def get_files_recursively(directory="."): | |
12 | + | for root, dirs, files in os.walk(directory): | |
13 | + | for name in files: | |
14 | + | yield os.path.join(root, name) | |
15 | + | ||
16 | + | def add_crc_to_fn(filename): | |
17 | + | new_name = "{0} [{2}]{1}".format(*os.path.splitext(filename), crc32_file(filename)) | |
18 | + | print("{} >> {}".format(filename, new_name)) | |
19 | + | os.rename(filename, new_name) | |
20 | + | ||
21 | + | def add_crc_to_sfv(filename): | |
22 | + | crc = crc32_file(filename) | |
23 | + | dirname = os.path.dirname(filename) | |
24 | + | line = "{} {}\n".format(os.path.basename(filename), crc) | |
25 | + | sfv_path = os.path.join(dirname, os.path.basename(dirname) + ".sfv") | |
26 | + | with open(sfv_path, "a") as f: | |
27 | + | f.write(line) | |
28 | + | ||
29 | + | if __name__ == "__main__": | |
30 | + | parser = argparse.ArgumentParser() | |
31 | + | parser.add_argument("--directory", default=".") | |
32 | + | parser.add_argument("--filename", help="Writes checksum into filename", action="store_true") | |
33 | + | args = parser.parse_args() | |
34 | + | for filename in get_files_recursively(directory=args.directory): | |
35 | + | if args.filename: | |
36 | + | add_crc_to_fn(filename) | |
37 | + | else: | |
38 | + | add_crc_to_sfv(filename) |