-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
executable file
·250 lines (183 loc) · 6.73 KB
/
deploy.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
import os
import subprocess
import re
import datetime
import argparse
from PIL import Image
########################################################################################################################
# Global contants
d = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
gitmessage = 'Rebuilding site {0}'.format(d)
cwd = os.getcwd()
public = os.path.join(cwd, 'public')
content = os.path.join(cwd, 'content')
########################################################################################################################
# Generic functions
def filesin(top, ext=None):
ret = []
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
f = os.path.join(root, name)
add = True
if ext is not None:
e = os.path.splitext(f)[1]
if e != ext:
add = False
if add:
ret.append(f)
return ret
def remove_file(file_to_remove: str, verbose: bool, dry_run: bool):
if verbose:
print('Removing file', file_to_remove)
if dry_run:
pass
else:
os.remove(file_to_remove)
def is_dir_empty(folder: str) -> bool:
return not os.listdir(folder)
def remove_files(folder: str, verbose: bool, dry: bool):
for root, dirs, files in os.walk(folder, topdown=False):
for file_name in files:
if file_name.startswith('.'):
print('Not removing file {} in {}'.format(file_name, root))
elif file_name == 'CNAME':
print('Not removing CNAME')
else:
file_path = os.path.join(root, file_name)
remove_file(file_path, verbose, dry)
for dir_name in dirs:
if dir_name.startswith('.'):
print('Not removing dir {} in {}'.format(dir_name, root))
else:
dir_path = os.path.join(root, dir_name)
if not is_dir_empty(dir_path):
print('{} is not empty, ignoring...'.format(dir_path))
else:
if verbose:
print('Removing directory ', dir_path)
if not dry:
os.rmdir(dir_path)
def call(args, cwd):
p = subprocess.Popen(args, cwd=cwd, universal_newlines=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
p.wait()
return p.stdout.read()
########################################################################################################################
# Actual deploy logic
def clean_html_files(public):
rebegin = [
re.compile(r'<pre')
]
reend = [
re.compile(r'</pre')
]
publichtml = filesin(public, '.html')
for path in publichtml:
html = []
with open(path, 'r') as f:
inside = 0
for line in f:
for r in rebegin:
inside += len(r.findall(line))
for r in reend:
inside -= len(r.findall(line))
if inside <= 0:
if not line.isspace():
html.append(line)
else:
html.append(line)
with open(path, 'w') as f:
f.write("".join(html))
# call(['tidy', '-m', '-utf8', '-i', path], cwd)
def clean_public():
global public
print('Cleaning old public files')
remove_files(public, False, False)
def generate_public():
global cwd
global public
print('Generating public files')
call('hugo', cwd)
print('Cleaning up public html files')
clean_html_files(public)
def update_github_repo():
global public
global gitmessage
print('Adding generated files to github.io repo')
call(['git', 'add', '-A'], public)
print('Comming files to github.io repo')
call(['git', 'commit', '-m', gitmessage], public)
print('Pushing files to github.io')
call(['git', 'push', 'origin', 'master'], public)
def update_homepage_repo():
global cwd
global gitmessage
print('Adding github.io push to homepage repo')
call(['git', 'add', '-A'], cwd)
print('Commiting github.io push')
call(['git', 'commit', '-m', gitmessage], cwd)
def is_repo_clean(cwd):
if len(call(['git', 'status', '-s'], cwd)) > 0:
return False
else:
return True
def deploy():
global cwd
global public
if not is_repo_clean(cwd):
print('The working directory is dirty. Please commit any pending changes.')
exit(-1)
if not is_repo_clean(public):
print('The public directory is dirty. Please commit any pending changes.')
exit(-1)
print('Deploying updates to GitHub...')
# first clean hugo folder
clean_public()
# then generate a new fresh copy
generate_public()
# commit and push local github.io repo
update_github_repo()
# commit in the homepage repo
update_homepage_repo()
print('Done')
def dirty_repo_as_string(repo):
return 'Clean' if is_repo_clean(repo) else 'Dirty'
########################################################################################################################
# Sub commands
def handle_deployment(args):
deploy()
def handle_info(args):
global gitmessage
global cwd
global public
global content
print('Git commit message would be', gitmessage)
print('Working direction is', cwd, dirty_repo_as_string(cwd))
print('Public directory is', public, dirty_repo_as_string(public))
print('Content directory is', content)
def handle_generate_public(args):
generate_public()
def handle_clean(args):
clean_public()
########################################################################################################################
# Main
def main():
parser = argparse.ArgumentParser(description='Manage my hugo homepage repo')
sub_parsers = parser.add_subparsers(dest='command_name', title='Commands', help='', metavar='<command>')
sub = sub_parsers.add_parser('deploy', help='Run whole deploy process')
sub.set_defaults(func=handle_deployment)
sub = sub_parsers.add_parser('info', help='Display some paths')
sub.set_defaults(func=handle_info)
sub = sub_parsers.add_parser('generate', help='Generate github.io data without publishing')
sub.set_defaults(func=handle_generate_public)
sub = sub_parsers.add_parser('clean', help='Clean all generated github.io data')
sub.set_defaults(func=handle_clean)
args = parser.parse_args()
if args.command_name is not None:
args.func(args)
else:
parser.print_help()
print()
########################################################################################################################
if __name__ == "__main__":
main()