-
Notifications
You must be signed in to change notification settings - Fork 5
/
atom.py
153 lines (121 loc) · 4.85 KB
/
atom.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
import datetime
import os
import subprocess
import uuid
import jinja2
ATOM_TEMPLATE = '''<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ title|e }}</title>
<link href="{{ link|e }}" />
<updated>{{ updated|e }}</updated>
<id>{{ id|e }}</id>
{% for entry in entries %}
<entry>
<title>{{ entry.title|e }}</title>
<link href="{{ entry.link }}"/>
<id>{{ entry.id|e }}</id>
<updated>{{ entry.updated|e }}</updated>
{% if entry.content %}
<content type="html">{{ entry.content|e }}</content>
{% else %}
<summary type="html">{{ entry.summary|e }}</summary>
{% endif %}
<author>
<name>{{ entry.author.name|e }}</name>
<email>{{ entry.author.email|e }}</email>
</author>
</entry>
{% endfor %}
</feed>
'''
def make_atom(**kwargs):
template = jinja2.Template(ATOM_TEMPLATE)
return template.render(**kwargs)
# 便利関数
class CDContext(object):
def __init__(self, cwd):
self._cwd = cwd
def __enter__(self):
self._old_cwd = os.getcwd()
os.chdir(self._cwd)
def __exit__(self, exception, value, traceback):
os.chdir(self._old_cwd)
return False
def cd(cwd):
return CDContext(cwd)
def run(command, shell=True, check=True):
call = subprocess.check_call if check else subprocess.call
return call(command, shell=shell)
def run_with_output(command, shell=True, check=True):
p = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, errors='ignore')
stdout, stderr = p.communicate()
if check:
if p.returncode != 0:
raise Exception('return code is non-zero: {}, stdout={}, stderr={}'.format(p.returncode, stdout, stderr))
return p.returncode, stdout, stderr
class GitAtom(object):
def __init__(self, is_target, get_title, get_link, get_html_content):
self._is_target = is_target
self._get_title = get_title
self._get_link = get_link
self._get_html_content = get_html_content
def _get_content(self, commit, file):
exitcode, output, _ = run_with_output('git show {commit}:{file}'.format(**locals()), check=False)
return output if exitcode == 0 else ''
def _file_to_entry(self, commit, file, author_name, author_email, updated, commit_title):
content = self._get_content(commit, file)
if not self._is_target(commit, file, content):
return None
title = self._get_title(commit, file, content) + ' -- ' + commit_title
link = self._get_link(commit, file, content)
id = commit + ':' + file
_, summary, _ = run_with_output('git diff {commit}^ {commit} -- {file}'.format(**locals()))
summary = jinja2.Template('<pre><code>{{ summary|e }}</code></pre>').render(summary=summary)
result = {
'title': title,
'link': link,
'id': id,
'updated': updated,
'summary': summary,
'author': {
'name': author_name,
'email': author_email,
}
}
try:
html_data = self._get_html_content(commit, file, content)
if html_data:
result.update({
'content': html_data,
})
except Exception:
pass
return result
def _hash_to_entries(self, commit):
_, author_name, _ = run_with_output('git show {commit} -s --format=%aN'.format(**locals()))
_, author_email, _ = run_with_output('git show {commit} -s --format=%aE'.format(**locals()))
_, updated, _ = run_with_output('git show {commit} -s --format=%ai'.format(**locals()))
updated = updated.replace(' ', 'T', 1).replace(' ', '').replace('\n', '')[:-2] + ':00'
_, commit_title, _ = run_with_output('git show {commit} -s --format=%s'.format(**locals()))
_, files_str, _ = run_with_output('git diff {commit}^ {commit} --name-only'.format(**locals()))
files = files_str[:-1].split('\n')
entries = filter(None, [self._file_to_entry(commit, file, author_name[:-1], author_email[:-1], updated, commit_title[:-1]) for file in files])
return entries
def git_to_atom(self, cwd, title, link):
with cd(cwd):
_, commits_str, _ = run_with_output('git log -5 --format=%H')
commits = commits_str[:-1].split('\n')
entries = []
for commit in commits:
entries += self._hash_to_entries(commit)
updated = datetime.datetime.now().isoformat()
id = uuid.uuid4()
dic = {
'title': title,
'link': link,
'updated': updated,
'id': id,
'entries': entries,
}
return make_atom(**dic)