-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
235 lines (201 loc) · 7.49 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from atlassian import Confluence
import os
from pathlib import Path
import argparse
import json
import subprocess
import hashlib
from multiprocessing import Pool
import jinja2
import markdown
# A version of argparse.ArgumentParser, that report errors
# with a non-zero exit code
class WrappedArgumentParser(argparse.ArgumentParser):
def error(self, message):
self.exit(125, '%s: error: %s\n' % (self.prog, message))
def _parse_args_or_exit():
parser = WrappedArgumentParser(description='A utility for pushing a markdown wiki to confluence.')
parser.add_argument('--config', metavar=('<config-file>'), required=True, help='List of one or more config files that are merged (latter having precedence).')
args_config = parser.parse_args()
return _load_json_with_comments(args_config.config)
def _load_json_with_comments(json_file):
with open(json_file, 'r') as in_file:
# Load config file, stripping off comments
json_lines = []
for line in in_file:
li=line.strip()
if li.startswith('//'):
json_lines.append('')
else:
json_lines.append(line)
return json.loads(''.join(json_lines))
def _get_markdown_files(source_dir):
markdown_files=[]
for filename in os.listdir(source_dir):
if not filename.endswith('.md'):
continue
markdown_files.append(filename)
markdown_files.sort()
return markdown_files
def _dump_confluence_pages_to_json(confluence, confluence_space, parent_page_id, out_file):
child_pages = []
start_record = 0
limit_records = 100
while True:
had_result = False
for child in confluence.get_page_child_by_type(
parent_page_id, type='page', start=start_record, limit=limit_records, expand="body"):
had_result = True
child_pages.append({"Parent id": parent_page_id, "Id": child["id"], "Title": child["title"]})
if not had_result:
break
start_record += limit_records
with open(out_file, 'w') as f:
json.dump(child_pages, f, indent=2)
def _get_confluence_child_pages(page_list_dump_file, parent_page_id):
with open(page_list_dump_file, 'r') as f:
child_pages = json.load(f)
result = []
for child_page in child_pages:
# Skip any pages not immediate children (including the parent page itself)
if str(child_page['Parent id']) != parent_page_id:
continue
result.append(child_page)
return result
def _process_markdown_file(confluence, confluence_space, parent_page_id, page_template, source_dir, state_dir, markdown_filename, update_confluence_page):
markdown_file = source_dir/markdown_filename
temp_content_file = state_dir/(markdown_filename+'.confluence')
markdown_hash_file = state_dir/(markdown_filename+'.lastSynced')
# Generate the content for Confluence
_generate_confluence_content(
markdown_file, temp_content_file, page_template)
# Read file hashes
new_markdown_hash = _read_hash(temp_content_file)
previous_markdown_hash = ''
if markdown_hash_file.exists():
with open(markdown_hash_file, 'r') as f:
previous_markdown_hash = f.read()
# Stop if content hasn't changed
if new_markdown_hash == previous_markdown_hash:
print('Skipping (already synced): '+markdown_filename)
return
else:
print('Updating: '+markdown_filename)
# Update the page
update_confluence_page(
confluence,
parent_page_id,
confluence_space,
markdown_filename,
temp_content_file)
# Store the last-synced hash
with open(markdown_hash_file, 'w') as f:
f.write(new_markdown_hash)
def _generate_confluence_content(markdown_file, confluence_file, page_template):
markdown_content = ""
with open(markdown_file, 'r') as in_file:
markdown_content = in_file.read()
markdown_content = markdown.markdown(markdown_content, extensions=['tables', 'fenced_code'])
with open(page_template, 'r') as f:
template = jinja2.Template(f.read())
with open(confluence_file, 'w') as f:
f.write(template.render(markdown_filename=markdown_file.name, markdown_content=markdown_content))
def _update_confluence_page_by_id(confluence, page_id, title, content_file):
content = ""
with open(content_file, 'r') as f:
content = f.read()
confluence.update_page(page_id, title, content, representation='storage')
def _update_confluence_page_as_child(confluence, confluence_space, parent_id, title, content_file):
content = ""
with open(content_file, 'r') as f:
content = f.read()
confluence.update_or_create(parent_id, title, content, representation='storage')
def _delete_confluence_page(confluence, page_id, page_title):
print('Deleting: '+page_title)
confluence.remove_page(page_id, status=None, recursive=False)
def _exec(cmd):
print("Executing: %s" % cmd)
subprocess.run(cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
check=True)
def _read_hash(file):
with open(file,"rb") as f:
sha256_hash = hashlib.sha256()
for byte_block in iter(lambda: f.read(4096),b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def join_throwing_any_exception(worker_pool, jobs):
worker_pool.close()
# Check for exceptions on any jobs (as soon as possible--before join)
for job in jobs:
job.get()
worker_pool.join()
# This would be an in-line lambda, if multiprocessing could use one
def _lambda_update_confluence_page_as_child(confluence, parent_page_id, confluence_space, markdown_filename, temp_content_file):
_update_confluence_page_as_child(confluence, confluence_space, parent_page_id, markdown_filename, temp_content_file)
def _main():
config = _parse_args_or_exit()
source_dir = Path(config['markdown_dir'])
state_dir = Path(config['state_dir'])
print('')
print('[[ Identifying Markdown Files ]]')
# Read markdown file list
markdown_files = sorted(_get_markdown_files(source_dir))
markdown_files_set = set(markdown_files)
print('')
print('[[ Pushing Pages to Confluence ]]')
print('')
confluence = Confluence(url=config['confluence_url'], token=config['confluence_token'])
# Update README.md
_process_markdown_file(
confluence,
config['confluence_space'],
config['parent_page_id'],
config['page_template'],
source_dir,
state_dir,
'README.md',
lambda confluence, parent_page_id, confluence_space, markdown_filename, temp_content_file:
_update_confluence_page_by_id(confluence, parent_page_id, config['root_page_title'], temp_content_file))
# Create/update all other .md files
worker_pool = Pool(processes=config['confluence_threads'])
jobs = []
for markdown_filename in markdown_files:
job = worker_pool.apply_async(_process_markdown_file, [
confluence,
config['confluence_space'],
config['parent_page_id'],
config['page_template'],
source_dir,
state_dir,
markdown_filename,
_lambda_update_confluence_page_as_child])
jobs.append(job)
join_throwing_any_exception(worker_pool, jobs)
print('')
print('[[ Reading Page List from Confluence ]]')
print('')
# Read confluence page list
page_list_dump_file = state_dir/'page_list_dump.json'
_dump_confluence_pages_to_json(
confluence,
config['confluence_space'],
config['parent_page_id'],
page_list_dump_file)
confluence_child_pages = _get_confluence_child_pages(page_list_dump_file, config['parent_page_id'])
print('')
print('[[ Pruning Deleted Pages from Confluence ]]')
print('')
# Delete any confluence pages that do not exist as markdown files
for confluence_child_page in confluence_child_pages:
# NOTE: We prune 'README.md' as well, since this is the parent page's content
if (confluence_child_page['Title'] == 'README.md') or (confluence_child_page['Title'] not in markdown_files_set):
_delete_confluence_page(
confluence,
str(confluence_child_page['Id']),
confluence_child_page['Title'])
print('')
print('[[ Complete ]]')
print('')
_main()