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
|
#!/usr/bin/env python3
# Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Linking this program statically or dynamically with other modules is making a
# combined work based on this program. Thus, the terms and conditions of the GNU
# General Public License cover the whole combination.
#
# In addition, as a special exception, the copyright holders of this program
# give you permission to combine it with free software programs or libraries
# that are released under the GNU LGPL or the Apache 2.0 license.
# You may copy and distribute such a system following the terms of the GNU GPL
# for this program and the licenses of the other code concerned.
import os
import re
import sys
import sh
from types import MethodType
from redminelib import Redmine
# We want to order all the wiki changes by date to get a linear git history
def usage(progname):
print('Usage: {0} <URL> <PROJECT> <textile|markdown> <DIRECTORY>'.format(
progname))
sys.exit(1)
def write_page(extension, output_directory, page):
file_path = None
if 'parent' in dir(page):
parent_dir_path = output_directory + os.sep + page.parent.title
if not os.path.exists(parent_dir_path):
os.mkdir(parent_dir_path)
file_path = parent_dir_path + os.sep + page.title + extension
else:
file_path = output_directory + os.sep + page.title + extension
f = open(file_path, 'w')
f.write(page.text)
f.close()
return [file_path]
def write_attachments(output_directory, page):
results = []
if not page.attachments:
return results
parent_dir_path = None
if 'parent' in dir(page):
parent_dir_path = output_directory + os.sep \
+ page.parent.title + page.title
if not os.path.exists(parent_dir_path):
os.mkdir(parent_dir_path)
else:
parent_dir_path = output_directory + os.sep + page.title
for attachement in page.attachments:
# TODO: print attachement.filename
attachement.download(savepath=parent_dir_path)
results.append(parent_dir_path + os.path.sep + attachement.filename)
return results
def git_commit(extension, output_directory, page, file_paths):
# +=============+==================================+========+
# | Page fields | Conversion in git | Status |
# +=============+==================================+========+
# | attachments | File | DONE |
# +-------------+----------------------------------+--------+
# | author | - Commit author | DONE |
# | | - Commiter | |
# +-------------+----------------------------------+--------+
# | comments | Commit short message | DONE |
# +-------------+----------------------------------+--------+
# | created_on | Ignore | DONE |
# +-------------+----------------------------------+--------+
# | parent | Add directories? | DONE |
# +-------------+----------------------------------+--------+
# | text | Commit content | DONE |
# +-------------+----------------------------------+--------+
# | title | File name | DONE |
# +-------------+----------------------------------+--------+
# | updated_on | - Commit creation date | DONE |
# | | - Commited date | |
# +-------------+----------------------------------+--------+
# | version | Add in commit message | DONE |
# | | Redmine-page-version: <version> | DONE |
# +-------------+----------------------------------+--------+
regex = re.compile('^' + re.escape(output_directory) + os.sep)
for file_path in file_paths:
path = re.sub(regex, '', file_path)
sh.git('-C', output_directory, 'add', path)
commit_author='{author} <{author}@redmine.replicant.us>'.format(
author=page.author)
commit_date = page.updated_on
if len(page.comments) > 1:
commit_message_sumarry='{}: {}\n'.format(page.title, page.comments)
else:
commit_message_sumarry='{}: page version {}\n'.format(page.title,
page.version)
commit_message = commit_message_sumarry
commit_message += '\n'
commit_message += 'Redmine-page-version: {}'.format(page.version)
# Print commit preview
print('-' * 80)
print('Author: {}'.format(commit_author))
print('Date: {}'.format(commit_date))
# Commit message
for line in commit_message.split('\n'):
print(' ' + line)
if len(file_paths) > 0:
print('')
for file_path in file_paths:
print('# Files to be committed:')
print('# File: {}'.format(file_path))
print('')
print('-' * 80)
# Git commands
command_args = []
command_args.append(
'--author="{author} <{author}@redmine.replicant.us>"'.format(
author=commit_author))
command_args.append('--message={}'.format(commit_message))
command_args.append('--cleanup=verbatim')
command_args.append('--date="{date}"'.format(date=commit_date))
print (' '.join(['git', '-C', output_directory, 'commit' ] + command_args))
sh.git('-C', output_directory, 'commit', *command_args)
def sort_pages_by_date(redmine_instance, redmine_project):
all_pages = []
total_pages = len(redmine_project.wiki_pages)
total_revisions = 0
for page in redmine_project.wiki_pages:
total_revisions += page.version
print('Found {} revisions (including {} pages)'.format(total_revisions,
total_pages))
print('')
print('Sorting pages by date:')
page_revision_nr = 0
for page in redmine_project.wiki_pages:
for current_revision in range(1, page.version +1):
page_revision_nr += 1
print(' [{}/{}]: {} @ version {}'.format(page_revision_nr,
total_revisions,
page.title,
current_revision))
page_version = redmine_instance.wiki_page.get(
page.title,
project_id=redmine_project.identifier,
version=current_revision)
all_pages.append(page_version)
# This should sort by modification date
all_pages.sort(key=lambda item: item.updated_on)
return all_pages
def main():
if len(sys.argv) != 5:
usage(sys.argv[0])
redmine_instance_url = sys.argv[1]
redmine_project_name = sys.argv[2]
redmine_wiki_format = sys.argv[3]
output_directory = sys.argv[4]
extension = ''
if redmine_wiki_format not in ['textile', 'markdown']:
usage(sys.argv[0])
elif redmine_wiki_format == 'markdown':
extension = '.md'
elif redmine_wiki_format == 'textile':
extension = '.textile'
redmine_instance = Redmine(redmine_instance_url)
redmine_project = redmine_instance.project.get(redmine_project_name)
pages = sort_pages_by_date(redmine_instance, redmine_project)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
if not os.path.exists(output_directory + os.sep + '.git'):
sh.git('-C', output_directory, 'init')
for page in pages:
# DEBUG
print(page.title, page.updated_on)
# END DEBUG
files = []
files += write_page(extension, output_directory, page)
files += write_attachments(output_directory, page)
git_commit(extension, output_directory, page, files)
if __name__ == '__main__':
main()
|