Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit ef9a047

Browse files
committed
Switch to Python 3 and fix a few things
Python 2 and Unicode is such a nightmare…
1 parent 8d34e2d commit ef9a047

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

templates/build_deploy.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python{{ ((ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 8) or (ansible_os_family == "Debian" and ansible_distribution_major_version|int >= 10)) | ternary('3', '') }}
1+
#!/usr/bin/env python3
22
#
33
# Copyright (c) 2015 Michael Scherer <[email protected]>
44
#
@@ -34,6 +34,7 @@
3434
import syslog
3535
import argparse
3636
import shutil
37+
from io import open
3738

3839

3940
parser = argparse.ArgumentParser(description="Build middleman sites based "
@@ -96,7 +97,7 @@ def log_print(message):
9697
except NameError:
9798
pass
9899
else:
99-
log_fd.write(message + "\n")
100+
log_fd.write(message + u"\n")
100101
log_fd.flush()
101102

102103

@@ -112,7 +113,7 @@ def refresh_checkout(checkout_dir):
112113
result = subprocess.check_output(['git', 'fetch', '-q'], stderr=subprocess.STDOUT)
113114
except subprocess.CalledProcessError as C:
114115
notify_error('setup', C.output)
115-
debug_print(result.decode())
116+
debug_print(result.decode('utf-8'))
116117

117118

118119
def get_last_commit(checkout_dir):
@@ -122,7 +123,7 @@ def get_last_commit(checkout_dir):
122123
'refs/remotes/origin/%s' % config['git_version']])
123124
except subprocess.CalledProcessError as C:
124125
notify_error('setup', C.output)
125-
return r.decode().split()[0]
126+
return r.decode('utf-8').split()[0]
126127

127128

128129
def get_last_commit_submodule(checkout_dir, submodule):
@@ -132,7 +133,7 @@ def get_last_commit_submodule(checkout_dir, submodule):
132133
'refs/remotes/origin/HEAD'])
133134
except subprocess.CalledProcessError as C:
134135
notify_error('setup', C.output)
135-
return r.decode().split()[0]
136+
return r.decode('utf-8').split()[0]
136137

137138

138139
def get_submodules_checkout(checkout_dir):
@@ -142,7 +143,7 @@ def get_submodules_checkout(checkout_dir):
142143
submodule_status = subprocess.check_output(['git', 'submodule', 'status'])
143144
except subprocess.CalledProcessError as C:
144145
notify_error('setup', C.output)
145-
for s in submodule_status.decode().split('\n'):
146+
for s in submodule_status.decode('utf-8').split('\n'):
146147
# there is a empty line at the end...
147148
if s:
148149
result.append(s.split()[1])
@@ -157,7 +158,7 @@ def load_config(config_file):
157158
print("Error %s is not a file" % config_file)
158159
sys.exit(1)
159160

160-
with open(config_file) as f:
161+
with open(config_file, encoding='utf-8') as f:
161162
config = yaml.safe_load(f)
162163

163164
return config
@@ -169,7 +170,7 @@ def has_submodules(checkout_dir):
169170
r = subprocess.check_output(['git', 'submodule', 'status'])
170171
except subprocess.CalledProcessError as C:
171172
notify_error('setup', C.output)
172-
return len(r.decode()) > 0
173+
return len(r.decode('utf-8')) > 0
173174

174175

175176
# TODO complete that
@@ -197,7 +198,7 @@ def do_rsync(source):
197198
config['remote']], stderr=subprocess.STDOUT)
198199
except subprocess.CalledProcessError as C:
199200
notify_error('setup', C.output)
200-
return r.decode()
201+
return r.decode('utf-8')
201202

202203

203204

@@ -229,7 +230,7 @@ def do_rsync(source):
229230
status_file = os.path.expanduser('~/status_%s.yml' % name)
230231
status = {}
231232
if os.path.exists(status_file):
232-
with open(status_file) as f:
233+
with open(status_file, encoding='utf-8') as f:
233234
# in case it's empty
234235
status = yaml.safe_load(f) or {}
235236

@@ -281,32 +282,32 @@ def do_rsync(source):
281282
# Do not open earlier or we would end-up logging a lot of
282283
# "Nothing to build" messages and lose the last build log.
283284
log_file = os.path.expanduser('~/%s.log' % name)
284-
log_fd = open(log_file, "w")
285-
log_fd.write("last_build_date: {}\n".format(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S (UTC)")))
286-
log_fd.write("last_build_commit: {}\n".format(current_commit))
287-
log_fd.write("submodule_commits: {}\n".format(current_submodule_commits))
288-
log_fd.write("\n")
285+
log_fd = open(log_file, "w", encoding='utf-8')
286+
log_fd.write(u"last_build_date: {}\n".format(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S (UTC)")))
287+
log_fd.write(u"last_build_commit: {}\n".format(current_commit))
288+
log_fd.write(u"submodule_commits: {}\n".format(current_submodule_commits))
289+
log_fd.write(u"\n")
289290

290291
syslog.syslog("Start the build of {}".format(name))
291292

292293
os.chdir(checkout_dir)
293294
if not args.no_refresh:
294295
try:
295296
result = subprocess.check_output(['git', 'stash'], stderr=subprocess.STDOUT)
296-
debug_print(result.decode())
297+
debug_print(result.decode('utf-8'))
297298
result = subprocess.check_output(['git', 'stash', 'clear'], stderr=subprocess.STDOUT)
298-
debug_print(result.decode())
299+
debug_print(result.decode('utf-8'))
299300
result = subprocess.check_output(['git', 'pull', '--rebase'], stderr=subprocess.STDOUT)
300-
debug_print(result.decode())
301+
debug_print(result.decode('utf-8'))
301302
except subprocess.CalledProcessError as C:
302303
notify_error('setup', C.output)
303304

304305
if has_submodules(checkout_dir):
305306
try:
306307
result = subprocess.check_output(['git', 'submodule', 'init'], stderr=subprocess.STDOUT)
307-
debug_print(result.decode())
308+
debug_print(result.decode('utf-8'))
308309
result = subprocess.check_output(['git', 'submodule', 'sync'], stderr=subprocess.STDOUT)
309-
debug_print(result.decode())
310+
debug_print(result.decode('utf-8'))
310311
except subprocess.CalledProcessError as C:
311312
notify_error('setup', C.output)
312313

@@ -336,7 +337,7 @@ def do_rsync(source):
336337
# don't use embedded libraries to build Nokogiri
337338
os.environ['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = '1'
338339
result = subprocess.check_output(['bundle', 'install'], stderr=subprocess.STDOUT)
339-
debug_print(result.decode())
340+
debug_print(result.decode('utf-8'))
340341
except subprocess.CalledProcessError as C:
341342
log_print(C.output)
342343
if config['remote']:
@@ -356,7 +357,7 @@ def do_rsync(source):
356357
command = builder_info[config['builder']]['build_command']
357358
syslog.syslog("Build of {}: {}".format(name, ' '.join(command)))
358359
result = subprocess.check_output(command, stderr=subprocess.STDOUT)
359-
debug_print(result.decode())
360+
debug_print(result.decode('utf-8'))
360361
except subprocess.CalledProcessError as C:
361362
log_print(C.output)
362363
if config['remote']:
@@ -379,7 +380,7 @@ def do_rsync(source):
379380
else:
380381
command = builder_info[config['builder']]['deploy_command']
381382
if command:
382-
result = subprocess.check_output(command).decode()
383+
result = subprocess.check_output(command).decode('utf-8')
383384
else:
384385
result = "No deployment done: no Rsync settings provided and this builder has no reployment method defined"
385386
debug_print(result)
@@ -391,9 +392,9 @@ def do_rsync(source):
391392

392393
status = {}
393394
status['last_build_commit'] = current_commit
394-
status['last_build'] = datetime.datetime.now().strftime("%s")
395-
status['last_build_human'] = datetime.datetime.now().strftime("%c")
395+
status['last_build'] = datetime.datetime.now().strftime(u"%s")
396+
status['last_build_human'] = datetime.datetime.now().strftime(u"%c")
396397
status['submodule_commits'] = current_submodule_commits
397398

398-
with open(status_file, 'w+') as f:
399+
with open(status_file, 'w+', encoding='utf-8') as f:
399400
f.write(yaml.safe_dump(status, default_flow_style=False))

vars/Debian.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
rsync_package: rsync
33
package_list:
44
common:
5+
- python3
6+
- python3-yaml
57
- git
68
# used to send email about build broken
79
- mutt
@@ -17,7 +19,6 @@ package_list:
1719
# nokogiri also requires zlib-devel and tar to build libxml
1820
- zlib1g-dev
1921
- tar
20-
- python-yaml
2122
- libxml2-dev
2223
- libxslt1-dev
2324
- libidn11-dev

vars/RedHat.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
rsync_package: rsync
33
package_list:
44
common:
5+
- python3
6+
- "{{ (ansible_distribution_major_version|int >= 8) | ternary('python3-pyyaml', 'python36-PyYAML') }}"
57
- git
68
# used to send email about build broken
79
- mutt
@@ -19,7 +21,6 @@ package_list:
1921
# nokogiri also requires zlib-devel and tar to build libxml
2022
- zlib-devel
2123
- tar
22-
- "{{ (ansible_python['version']['major'] == 3) | ternary('python3-pyyaml', 'PyYAML') }}"
2324
- libxml2-devel
2425
- libxslt-devel
2526
- libidn-devel

0 commit comments

Comments
 (0)