Skip to content

Commit 8649dc7

Browse files
Python3
1 parent faf40f7 commit 8649dc7

File tree

8 files changed

+35
-46
lines changed

8 files changed

+35
-46
lines changed

atom.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import
3-
from __future__ import division
4-
from __future__ import print_function
5-
from __future__ import unicode_literals
62

73
import datetime
84
import os
@@ -70,12 +66,12 @@ def run(command, shell=True, check=True):
7066

7167

7268
def run_with_output(command, shell=True, check=True):
73-
p = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
69+
p = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, errors='ignore')
7470
stdout, stderr = p.communicate()
7571
if check:
7672
if p.returncode != 0:
7773
raise Exception('return code is non-zero: {}'.format(p.returncode))
78-
return p.returncode, unicode(stdout, encoding='utf-8', errors='ignore'), unicode(stderr, encoding='utf-8', errors='ignore')
74+
return p.returncode, stdout, stderr
7975

8076

8177
class GitAtom(object):

boostjp/templates/content.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@
9292
{% if latest_commit_info %}
9393
<p class="text-right"><small>
9494
最終更新日時:
95-
<span itemprop="datePublished" content="{{ latest_commit_info['last_updated'].strftime('%Y-%m-%dT%H:%M:%S'.encode('utf-8')).decode('utf-8') }}">
96-
{{ latest_commit_info['last_updated'].strftime('%Y年%m月%d日 %H時%M分%S秒'.encode('utf-8')).decode('utf-8') }}
95+
<span itemprop="datePublished" content="{{ latest_commit_info['last_updated'].strftime('%Y-%m-%dT%H:%M:%S') }}">
96+
{{ latest_commit_info['last_updated'].strftime('%Y年%m月%d日 %H時%M分%S秒') }}
9797
</span>
9898
<br/>
9999
<span itemprop="author" itemscope itemtype="http://schema.org/Person">

cpprefjp/templates/content.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@
102102
{% if latest_commit_info %}
103103
<p class="text-right"><small>
104104
最終更新日時(UTC):
105-
<span itemprop="datePublished" content="{{ latest_commit_info['last_updated'].strftime('%Y-%m-%dT%H:%M:%S'.encode('utf-8')).decode('utf-8') }}">
106-
{{ latest_commit_info['last_updated'].strftime('%Y年%m月%d日 %H時%M分%S秒'.encode('utf-8')).decode('utf-8') }}
105+
<span itemprop="datePublished" content="{{ latest_commit_info['last_updated'].strftime('%Y-%m-%dT%H:%M:%S') }}">
106+
{{ latest_commit_info['last_updated'].strftime('%Y年%m月%d日 %H時%M分%S秒') }}
107107
</span>
108108
<br/>
109109
<span itemprop="author" itemscope itemtype="http://schema.org/Person">

crsearch.json/docker/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.6.2-alpine3.6
1+
FROM python:3.7.2-alpine3.8
22

33
MAINTAINER melpon <[email protected]>
44

docker/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:2.7.14-alpine3.6
1+
FROM python:3.7.2-alpine3.8
22

33
MAINTAINER melpon <[email protected]>
44

run.py

+24-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
from __future__ import absolute_import
4-
from __future__ import division
5-
from __future__ import print_function
6-
from __future__ import unicode_literals
73

84
from datetime import datetime
95
import glob
@@ -27,7 +23,7 @@
2723

2824

2925
if len(sys.argv) < 2:
30-
print('{} <setting> [--all] [--prefix=<target>] [--concurrency=<num>]'.format(sys.argv[0]).encode('utf-8'))
26+
print('{} <setting> [--all] [--prefix=<target>] [--concurrency=<num>]'.format(sys.argv[0]))
3127
sys.exit(0)
3228

3329
settings = importlib.import_module(sys.argv[1])
@@ -43,8 +39,7 @@
4339
elif settings.CACHEBUST_TYPE == 'time':
4440
_CACHEBUST = '?cachebust=' + str(int(round(time.time() * 1000)))
4541
elif settings.CACHEBUST_TYPE == 'git':
46-
stdout = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=settings.CACHEBUST_DIR)
47-
stdout = unicode(stdout, encoding='utf-8', errors='ignore')
42+
stdout = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=settings.CACHEBUST_DIR, text=True, errors='ignore')
4843
_CACHEBUST = '?cachebust=' + stdout.strip()
4944
else:
5045
raise RuntimeError('Invalid _CACHEBUST {}'.format(_CACHEBUST))
@@ -105,7 +100,8 @@ def remove_tags(html):
105100

106101

107102
def convert(path, template, context, hrefs, global_qualify_list):
108-
md_data = unicode(open(make_md_path(path)).read(), encoding='utf-8')
103+
with open(make_md_path(path), encoding='utf-8') as f:
104+
md_data = f.read()
109105
body, info = md_to_html(md_data, path, hrefs, global_qualify_list)
110106
meta = info['meta_result']
111107
codes = info['example_codes']
@@ -132,7 +128,8 @@ def convert(path, template, context, hrefs, global_qualify_list):
132128
if settings.USE_MINIFY:
133129
import htmlmin
134130
html_data = htmlmin.minify(html_data)
135-
open(make_html_path(path), 'w').write(html_data.encode('utf-8'))
131+
with open(make_html_path(path), 'w', encoding='utf-8') as f:
132+
f.write(html_data)
136133

137134

138135
def is_target(path):
@@ -207,7 +204,7 @@ def get_meta(md):
207204
return result
208205

209206

210-
_DESCRIPTION_RE = re.compile(ur'#.*?概要.*?\n(?P<description>.*?)(\n#|$)', re.MULTILINE)
207+
_DESCRIPTION_RE = re.compile(r'#.*?概要.*?\n(?P<description>.*?)(\n#|$)', re.MULTILINE)
211208

212209

213210
def get_description(md):
@@ -220,7 +217,8 @@ def make_pageinfo(path):
220217
paths = path.split('/')
221218
md_data = None
222219
try:
223-
md_data = unicode(open(make_md_path(path)).read(), encoding='utf-8')
220+
with open(make_md_path(path), encoding='utf-8') as f:
221+
md_data = f.read()
224222
except Exception:
225223
print('open file error : {}'.format(path))
226224
raise
@@ -380,7 +378,7 @@ def get_priority(pageinfo):
380378
if pageinfo['path'] == 'index':
381379
return 1.0
382380
depth = len(pageinfo['paths'])
383-
return 1.0 - depth * 0.1
381+
return (10 - depth) / 10
384382

385383
info = get_self_latest_commit_info()
386384

@@ -398,7 +396,8 @@ class Cache(object):
398396

399397
def __init__(self, cache_file):
400398
try:
401-
self._cache = json.loads(open(cache_file).read())
399+
with open(cache_file, encoding='utf-8') as f:
400+
self._cache = json.loads(f.read())
402401
except Exception:
403402
self._cache = {}
404403
self._cache_file = cache_file
@@ -436,23 +435,23 @@ def converted(self, path):
436435
}
437436

438437
def flush(self):
439-
with open(self._cache_file, 'w') as f:
438+
with open(self._cache_file, 'w', encoding='utf-8') as f:
440439
f.write(json.dumps(self._cache))
441440

442441

443442
def get_latest_commit_info(path):
444-
commit_log = subprocess.check_output(['git', 'log', '-1', '--date=iso', '--pretty=format:%at %an', path + '.md'], cwd=settings.INPUT_DIR)
443+
commit_log = subprocess.check_output(['git', 'log', '-1', '--date=iso', '--pretty=format:%at %an', path + '.md'], cwd=settings.INPUT_DIR, text=True, errors='ignore')
445444
if not commit_log:
446445
return None
447-
timestamp, author = unicode(commit_log, encoding='utf-8').split(' ', 1)
446+
timestamp, author = commit_log.split(' ', 1)
448447
return {
449448
'last_updated': datetime.fromtimestamp(int(timestamp)),
450449
'last_author': author,
451450
}
452451

453452

454453
def get_self_latest_commit_info():
455-
last_updated = subprocess.check_output(['git', 'log', '-1', '--format=%ai']).strip()
454+
last_updated = subprocess.check_output(['git', 'log', '-1', '--format=%ai'], text=True, errors='ignore').strip()
456455
return {
457456
'last_updated': last_updated,
458457
}
@@ -469,7 +468,7 @@ def remove_not_target_paths(paths):
469468
for f in files:
470469
target_file_path = os.path.join(root, f)
471470
if target_file_path not in html_path_set:
472-
print('remove: {}'.format(target_file_path).encode('utf-8'))
471+
print('remove: {}'.format(target_file_path))
473472
os.remove(target_file_path)
474473
# 空ディレクトリの削除
475474
for root, dirs, files in os.walk(settings.OUTPUT_DIR, topdown=False):
@@ -479,7 +478,7 @@ def remove_not_target_paths(paths):
479478
if len(files) == 0:
480479
try:
481480
os.rmdir(root)
482-
print('remove directory: {}'.format(root).encode('utf-8'))
481+
print('remove directory: {}'.format(root))
483482
except Exception:
484483
pass
485484

@@ -524,10 +523,8 @@ def main():
524523
else:
525524
sidebar_index = None
526525

527-
global_qualify_list = []
528-
for line in open('{}/GLOBAL_QUALIFY_LIST.txt'.format(settings.INPUT_DIR)):
529-
global_qualify_list.append(line.lstrip())
530-
global_qualify_list = "\n".join(global_qualify_list)
526+
with open('{}/GLOBAL_QUALIFY_LIST.txt'.format(settings.INPUT_DIR), encoding='utf-8') as f:
527+
global_qualify_list = '\n'.join([line.strip() for line in f])
531528

532529
cache = Cache(CACHE_FILE)
533530
env = jinja2.Environment(loader=jinja2.FileSystemLoader(settings.PAGE_TEMPLATE_DIR))
@@ -575,11 +572,11 @@ def run(pageinfos):
575572
cache.flush()
576573
remove_not_target_paths(pageinfo['path'] for pageinfo in pageinfos)
577574

578-
with open(os.path.join(settings.OUTPUT_DIR, settings.RSS_PATH), 'w') as f:
579-
f.write(make_atom().encode('utf-8'))
575+
with open(os.path.join(settings.OUTPUT_DIR, settings.RSS_PATH), 'w', encoding='utf-8') as f:
576+
f.write(make_atom())
580577

581-
with open(os.path.join(settings.OUTPUT_DIR, settings.SITEMAP_PATH), 'w') as f:
582-
f.write(make_sitemap(pageinfos).encode('utf-8'))
578+
with open(os.path.join(settings.OUTPUT_DIR, settings.SITEMAP_PATH), 'w', encoding='utf-8') as f:
579+
f.write(make_sitemap(pageinfos))
583580

584581
# 静的ファイルをコピーする
585582
subprocess.call(['cp', '-v', '-RL'] + glob.glob(os.path.join(settings.STATIC_DIR, '*')) + [settings.OUTPUT_DIR])

sitemap.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import
3-
from __future__ import division
4-
from __future__ import print_function
5-
from __future__ import unicode_literals
62

73
import os
84
import subprocess
@@ -54,12 +50,12 @@ def run(command, shell=True, check=True):
5450

5551

5652
def run_with_output(command, shell=True, check=True):
57-
p = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
53+
p = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, errors='ignore')
5854
stdout, stderr = p.communicate()
5955
if check:
6056
if p.returncode != 0:
6157
raise Exception('return code is non-zero: {}'.format(p.returncode))
62-
return p.returncode, unicode(stdout, encoding='utf-8', errors='ignore'), unicode(stderr, encoding='utf-8', errors='ignore')
58+
return p.returncode, stdout, stderr
6359

6460

6561
class GitSitemap(object):

0 commit comments

Comments
 (0)