forked from LibraryOfCongress/citizen-dj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_collections.py
60 lines (51 loc) · 1.67 KB
/
sync_collections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
import argparse
import glob
import os
from pprint import pprint
import sys
# input
parser = argparse.ArgumentParser()
parser.add_argument("-dir", dest="REFERENCE_DIR", default="_collections", help="Directory of collections to use as reference")
parser.add_argument("-sync", dest="SYNC_TO", default="use,remix,explore", help="Directories to sync to")
a = parser.parse_args()
files = glob.glob(a.REFERENCE_DIR+"/*.md")
syncDirs = [{"name": name, "dir": "_"+name+"/"} for name in a.SYNC_TO.strip().split(",")]
# remove existing files
for d in syncDirs:
dir = d["dir"]
removeFiles = glob.glob(dir+"*.md")
for rfn in removeFiles:
os.remove(rfn)
# make sure directories exist
for d in syncDirs:
dir = d["dir"]
dirname = os.path.dirname(dir)
if not os.path.exists(dirname):
os.makedirs(dirname)
for fn in files:
contents = ""
with open(fn, "r", encoding="utf8", errors="replace") as f:
contents = f.read()
if len(contents) < 1:
print("No contents for %s" % fn)
continue
basename = os.path.basename(fn)
uid = basename[:-3]
findStrings = [
"/"+uid+"/layout/",
"layout: layout"
]
for d in syncDirs:
syncFn = d["dir"] + basename
replaceStrings = [
"/"+uid+"/"+d["name"]+"/",
"layout: "+d["name"]
]
syncContents = contents
for findString, replaceString in zip(findStrings, replaceStrings):
syncContents = syncContents.replace(findString, replaceString)
with open(syncFn, "w", encoding="utf8") as f:
f.write(syncContents)
print("Wrote to %s" % syncFn)
print("Done.")