forked from Git-Lit/git-lit
/
local.py
167 lines (135 loc) · 5.08 KB
/
local.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Makes an organized git repo of a book folder.
Based on code from GITenburg project.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import codecs
import jinja2
import sh
import logging
import lxml
import shutil
import tempfile
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class CdContext():
""" A context manager using `sh` to cd to a directory and back
`with CdContext(new path to go to)`
"""
def __init__(self, path):
self._og_directory = str(sh.pwd()).strip('\n')
self._dest_directory = path
def __enter__(self):
sh.cd(self._dest_directory)
def __exit__(self, exception_type, exception_value, traceback):
sh.cd(self._og_directory)
class LocalRepo():
def __init__(self, book, directory):
self.book = book
self.directory = directory
logging.info("Now attempting to initialize a local git repository for text: "
+ self.book.book_id + " a.k.a. " + self.book.title )
def add_file(self, filename):
sh.git('add', filename)
def add_all_files(self):
SUFFIXES = {'_dat.zip',
'_metadata.xml',
'README.md',
'CONTRIBUTING.md',
'LICENSE.md',
'.adoc'
}
with CdContext(self.directory):
sh.git.init('.')
logging.debug("Files to add: " + str(sh.ls()))
# NOTE: repo.untracked_files is unreliable with CdContext
# using sh.ls() instead, this doesn't recognize .gitignore
for _file in sh.ls('-1'):
# TODO: This attempts to add existing files a second time
_file = _file.rstrip('\n')
for suffix in SUFFIXES:
if _file.endswith(suffix):
logging.info("Adding file: " + _file)
self.add_file(_file)
break
else:
logging.debug('Skipping ' + _file)
def commit(self, message):
with CdContext(self.directory):
try:
# note the double quotes around the message
sh.git(
'commit',
'-m',
'{message}'.format(message=message)
)
except sh.ErrorReturnCode_1 as e:
print("Commit aborted for {0} with msg {1}".format(self.book.book_id, message))
print("Error: " + e.message)
# TODO: It's very weird partitioning to have this as a separate class. Refactor!
class NewFilesHandler():
""" NewFilesHandler - templates and copies additional files to book repos
"""
def __init__(self, book):
self.book = book
self.basename = self.book.book_id
if book.volume:
self.basename += '_%02d' % self.book.volume
# TODO: Temp dirs being created locally to ease debugging. Remove for production
self.directory = tempfile.mkdtemp(prefix='tmprepo%s' % self.basename, dir='.')
self.add_new_files()
def add_new_files(self):
shutil.copy(self.book.zipfile, self.directory)
self.write_metadata()
self.write_text()
self.template_readme()
self.copy_files()
def write_text(self):
with codecs.open(self.directory+'/'+ self.basename + '.adoc','w','utf-8') as f:
f.write(self.book.text + '\n')
def write_metadata(self):
with codecs.open(self.directory+'/'+self.basename + '_metadata.xml','w','utf-8') as f:
f.write(lxml.etree.tostring(self.book.metadata) + '\n')
def template_readme(self):
with codecs.open('templates/README.md.j2','r','utf-8') as f:
templateSrc = f.read()
template = jinja2.Template(templateSrc)
readme_text = template.render(
title=self.book.title,
author=self.book.author,
book_id=self.book.book_id
)
readme_path = "{0}/{1}".format(
self.directory,
'README.md'
)
with codecs.open(readme_path, 'w', 'utf-8') as readme_file:
readme_file.write(readme_text)
def copy_files(self):
""" Copy the LICENSE and CONTRIBUTING files to each folder repo """
# TODO: Add .gitattributes for line endings (and .gitignore?)
FILES = ['LICENSE.md', 'CONTRIBUTING.md']
this_dir = sh.pwd().strip()
for _file in FILES:
sh.cp(
'{0}/templates/{1}'.format(this_dir, _file),
'{0}/'.format(self.directory)
)
def make(book):
# Create files from zip
handler = NewFilesHandler(book)
# Initial commit of book files
local_repo = LocalRepo(book, handler.directory)
local_repo.add_all_files()
local_repo.commit("Initial import from British Library originals.")
# TODO: Cleanup temp dir
return handler.directory
def test():
pass
if __name__ == '__main__':
test()