Skip to content

Instantly share code, notes, and snippets.

@riceissa
Created March 22, 2018 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riceissa/88eab688ad4ede2dd936b38051aafdda to your computer and use it in GitHub Desktop.
Save riceissa/88eab688ad4ede2dd936b38051aafdda to your computer and use it in GitHub Desktop.
Some scripts I used when converting Cause Prioritization Wiki URLs from spaces to underscores
#!/usr/bin/env python3
# Run this like: ls -1 | ./check.py
import sys
ignored = [
"Free cities.page",
"UI Design.page",
"Artificial intelligence.page",
"Oxford Prioritization Project.page",
"Front Page.page",
"Keyhole solutions.page",
"Ethics offset.page",
"Importance, tractability, neglectedness.page",
"Cause prioritization websites.page",
"Cause prioritization people.page",
"Drug policy reform.page",
"@Front Page.page",
]
space_files = set()
underscore_files = set()
total = 0
total_need_change = 0
changed = 0
for line in sys.stdin:
filename = line.strip()
if filename.endswith(".page"):
total += 1
if " " in filename and filename not in ignored:
total_need_change += 1
space_files.add(filename)
elif "_" in filename and filename not in ignored:
changed += 1
underscore_files.add(filename)
not_changed = total_need_change - changed
for filename in sorted(space_files):
if filename.replace(" ", "_") not in underscore_files:
print(filename)
else:
# print("GOOD", filename)
pass
print(("\nSUMMARY: {} total files, {} needed to be " +
"changed (out of which\n {} changed and {} still need to " +
"be changed.)").format(total, total_need_change, changed, not_changed))
#!/usr/bin/env python3
import sys
import subprocess
import shlex
filename = sys.argv[1]
renamed = filename.replace(" ", "_")
# subprocess.run(["git", "mv", shlex.quote(filename), renamed])
subprocess.run(["git", "mv", filename, renamed])
subprocess.run(["git", "commit", "-m", "rename"])
with open(filename, "w") as f:
f.write("---\n")
f.write("redirect: " + renamed[:-len(".page")] + "\n")
f.write("...\n")
newcontents = []
line_num = 1
with open(renamed, "r") as f:
for line in f:
if line_num == 2 and not line.startswith("title:"):
newcontents.append("title: " + filename[:-len(".page")] + "\n")
newcontents.append(line)
line_num += 1
with open(renamed, "w") as f:
for line in newcontents:
f.write(line)
subprocess.run(["git", "add", filename])
subprocess.run(["git", "add", renamed])
subprocess.run(["git", "commit", "-m", "add redirect"])
subprocess.run(["git", "status"])
subprocess.run(["cat", filename])
subprocess.run(["head", "-n5", renamed])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment