wesnoth/update_copyrights
2023-09-25 11:47:07 -05:00

98 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python3
# encoding: utf-8
##
# This script checks all media files in the repository for whether they've been modified or added without updating the file tracking image, sound, and music copyright
##
import argparse
import contextlib
import csv
from operator import itemgetter
import os
from pathlib import Path
from subprocess import check_output
import sys
def do_git(file):
result = str(check_output(["git", "log", "-1", "--format=%ad,,,,,", "--date=format:%Y/%m/%d", file]), 'UTF-8').split(sep=",")
if(len(result) < 6):
print("bad result for file"+file+"`: "+",".join(result)+"`")
result[1] = os.path.normpath(result[1])
return result
args = argparse.ArgumentParser()
args.add_argument("--repo", default=".", help="The directory of the Wesnoth repository to run this script against.")
args.add_argument("--output", default="output.csv", help="The file to write the results of this script to.")
args.add_argument("--input", default="copyrights.csv", help="The file to read the existing copyright data from.")
options = args.parse_args()
os.chdir(options.repo)
with contextlib.suppress(FileNotFoundError):
os.remove(options.output)
current_data = {}
for root, _, files in os.walk(options.repo):
for filename in files:
filetype = Path(filename).suffix
if filetype == ".png" or filetype == ".jpg" or filetype == ".webp" or filetype == ".wav" or filetype == ".ogg":
file = os.path.join(root, filename)
current_data[os.path.normpath(file)] = do_git(file)
added = []
changed = []
unchanged = []
removed = []
previous_data = {}
with open(options.input) as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if row[0] == "Date":
continue
date = row[0]
file = row[1]
if file in current_data:
if(date != current_data[file][0]):
while(len(row) != 6):
row.append("")
row[5] = current_data[file][0]
changed.append(row)
else:
unchanged.append(row)
else:
removed.append(row)
previous_data[file] = row
for key in current_data:
if not key in previous_data:
added.append(["", key, "", "", "", current_data[key][0]])
added.sort(key=itemgetter(1))
changed.sort(key=itemgetter(1))
unchanged.sort(key=itemgetter(1))
final_output = added + changed + unchanged
if options.output != "":
with open(options.output, 'w') as f:
f.write("Date,File,License,Author - Real Name(other name);Real Name(other name);etc,Notes,Needs Update\n")
for row in final_output:
f.write(",".join(row)+"\n")
else:
for row in final_output:
print(",".join(row)+"\n")
if len(removed) > 0:
print("There are "+str(len(removed))+" removed images")
if len(added) > 0 or len(changed) > 0:
print("There are "+str(len(added))+" new images")
print("There are "+str(len(changed))+" changed images")
sys.exit(1)