-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcsl-timestamp-update-2.py
73 lines (58 loc) · 2.53 KB
/
csl-timestamp-update-2.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
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
# Python script to update cs:updated timestamp to time of last commit
# (when timestamps are on different days)
# Author: Rintze M. Zelle
# Version: 2012-10-29
# * Requires lxml library (http://lxml.de/)
## https://groups.google.com/forum/?fromgroups=#!topic/git-python/IWE7EkVX9SQ
import os, glob, re, time, datetime, git
from lxml import etree
path = 'C:\Documents and Settings\zelle\My Documents\CSL\styles\\'
repo = git.Repo(path)
assert repo.bare == False
##limit = 0
commitTime = {}
for t in repo.tree().trees:
if t.path == "dependent":
for blob in t.blobs:
last_commit = [c for c in repo.iter_commits(rev=None, paths=blob.path)][0]
blob.path = blob.path.replace('/','\\')
commitTime[os.path.join(path, blob.path)] = last_commit.authored_date
## limit += 1
## if limit == 10:
## break
#print(commitTime)
styles = []
for stylepath in glob.glob( os.path.join(path, "dependent", '*.csl') ):
if stylepath in commitTime:
styles.append(os.path.join(stylepath))
#print(styles)
for style in styles:
modTime = datetime.datetime.fromtimestamp(commitTime[style]//1)
modDate = datetime.datetime.date(modTime)
parser = etree.XMLParser(remove_blank_text=True)
parsedStyle = etree.parse(style, parser)
styleElement = parsedStyle.getroot()
updatedTimeStamp = styleElement.find(".//{http://purl.org/net/xbiblio/csl}updated")
updatedDate = updatedTimeStamp.text[0:10]
updatedDate = datetime.datetime.strptime(updatedDate, "%Y-%m-%d")
updatedDate = updatedDate.date()
if (updatedDate != modDate):
#print(updatedTimeStamp)
#print(str(datetime.datetime.isoformat(modTime))+"+00:00")
updatedTimeStamp.text = str(datetime.datetime.isoformat(modTime))+"+00:00"
#print(updatedTimeStamp)
#print(styleElement.find(".//{http://purl.org/net/xbiblio/csl}updated").text)
try:
parsedStyle = etree.tostring(parsedStyle, pretty_print=True, xml_declaration=True, encoding="utf-8")
parsedStyle = parsedStyle.replace("'", '"', 4)
parsedStyle = parsedStyle.replace(" ", " ")#no-break space
parsedStyle = parsedStyle.replace("ᵉ", "ᵉ")
parsedStyle = parsedStyle.replace("‑", "‑")#non-breaking hyphen
parsedStyle = parsedStyle.replace("–", "–")#en dash
parsedStyle = parsedStyle.replace("—", "—")#em dash
f = open(style, 'w')
f.write ( parsedStyle )
f.close()
except:
pass