Add script to prune non-free xml files
Browse files- prune-nonfree.py +52 -0
prune-nonfree.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Prune non-free ThML files """
|
2 |
+
|
3 |
+
import re
|
4 |
+
import os
|
5 |
+
import xml.etree.ElementTree as ET
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
|
9 |
+
def get_field(root, field):
|
10 |
+
try:
|
11 |
+
text = root.find(f".//{field}").text
|
12 |
+
text = re.sub(r"\s+", " ", text)
|
13 |
+
except:
|
14 |
+
text = None
|
15 |
+
|
16 |
+
return text
|
17 |
+
|
18 |
+
|
19 |
+
def prune():
|
20 |
+
global count
|
21 |
+
|
22 |
+
for filename in list(Path(".").rglob("*.xml")):
|
23 |
+
filename = str(filename)
|
24 |
+
if "authInfo." in filename:
|
25 |
+
continue
|
26 |
+
try:
|
27 |
+
tree = ET.parse(filename)
|
28 |
+
except:
|
29 |
+
print("ERROR: Unable to parse:", filename)
|
30 |
+
continue
|
31 |
+
|
32 |
+
root = tree.getroot()
|
33 |
+
|
34 |
+
# Skip content with copyright restrictions
|
35 |
+
# The copyright statements aren’t consistent.
|
36 |
+
# The ones that contain the text "public domain" (case insensitive)
|
37 |
+
# or nothing at all should be safe.
|
38 |
+
rights = get_field(root, "DC.Rights")
|
39 |
+
|
40 |
+
if rights and "public domain" not in rights.lower():
|
41 |
+
print(f"Removed {filename} due to copyright: {rights}")
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
print(
|
46 |
+
"This script will permanently delete any xml files with non-free DC.Rights under the current directory."
|
47 |
+
)
|
48 |
+
confirm = input("Type `delete` to continue: ")
|
49 |
+
if confirm != "delete":
|
50 |
+
print("Exiting without modification")
|
51 |
+
exit(1)
|
52 |
+
prune()
|