Compare commits

..

3 Commits

Author SHA1 Message Date
18d92c1437 init commit 2023-02-27 23:48:25 +00:00
7cd176378f ignore csv dir 2023-02-27 23:47:46 +00:00
0cb52f74f1 added BB_dirs, excluding README 2023-02-27 23:42:34 +00:00
2 changed files with 48 additions and 0 deletions

9
.gitignore vendored
View File

@@ -127,3 +127,12 @@ dmypy.json
# Pyre type checker
.pyre/
# BBGradebookOrganiser
BB_gradebooks/
BB_submissions/
csv/
!BB_gradebooks/README.md
!BB_submissions/README.md

39
hash_submissions.py Normal file
View File

@@ -0,0 +1,39 @@
import os, sys
from datetime import datetime
import csv
import hashlib
def hash_files_in_dir(dir_path: str, csv_suffix: str):
os.makedirs('csv', exist_ok=True)
csv_file_name = f'file_hashes_{csv_suffix}_{datetime.now().strftime("%Y%m%d-%H%M%S")}.csv'
csv_file = os.path.join('csv', csv_file_name)
with open(csv_file, 'w', newline='') as csvfile: # Open the output CSV file for writing
fieldnames = ['Student ID', 'file', 'sha256 hash']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for subdir, dirs, files in os.walk(dir_path): # Loop through all files in the directory and generate hashes
for file in files:
if 'README.md' not in file:
directories = [d for d in os.path.abspath(subdir).split(os.path.sep)] # list of directories in the file path
student_id = directories[directories.index(csv_suffix)+1] # use the index of 'csv_suffix' which is the gradebook name, and get the next directory which is the student id
filepath = os.path.join(subdir, file)
with open(filepath, 'rb') as f:
filehash = hashlib.sha256(f.read()).hexdigest()
writer.writerow({'Student ID': student_id, 'file': filepath, 'sha256 hash': filehash})
def main():
submissions_dir_name = ' '.join(sys.argv[1:]) if len(sys.argv) > 1 else exit(f'\nNo submissions dir name given. Provide the name as an argument.\n\nUsage: python {sys.argv[0]} [submissions dir name]\n')
submissions_dir = os.path.join('BB_submissions', submissions_dir_name) # dir with extracted submissions
if os.path.isdir(submissions_dir):
hash_files_in_dir(submissions_dir, submissions_dir_name)
else:
exit(f'Directory {submissions_dir} does not exist.\nMake sure "{submissions_dir_name}" exists in "BB_submissions".')
if __name__ == '__main__':
main()