1
1
#!/usr/bin/env python
2
2
# -*- 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
7
3
8
4
from datetime import datetime
9
5
import glob
27
23
28
24
29
25
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 ]))
31
27
sys .exit (0 )
32
28
33
29
settings = importlib .import_module (sys .argv [1 ])
43
39
elif settings .CACHEBUST_TYPE == 'time' :
44
40
_CACHEBUST = '?cachebust=' + str (int (round (time .time () * 1000 )))
45
41
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' )
48
43
_CACHEBUST = '?cachebust=' + stdout .strip ()
49
44
else :
50
45
raise RuntimeError ('Invalid _CACHEBUST {}' .format (_CACHEBUST ))
@@ -105,7 +100,8 @@ def remove_tags(html):
105
100
106
101
107
102
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 ()
109
105
body , info = md_to_html (md_data , path , hrefs , global_qualify_list )
110
106
meta = info ['meta_result' ]
111
107
codes = info ['example_codes' ]
@@ -132,7 +128,8 @@ def convert(path, template, context, hrefs, global_qualify_list):
132
128
if settings .USE_MINIFY :
133
129
import htmlmin
134
130
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 )
136
133
137
134
138
135
def is_target (path ):
@@ -207,7 +204,7 @@ def get_meta(md):
207
204
return result
208
205
209
206
210
- _DESCRIPTION_RE = re .compile (ur '#.*?概要.*?\n(?P<description>.*?)(\n#|$)' , re .MULTILINE )
207
+ _DESCRIPTION_RE = re .compile (r '#.*?概要.*?\n(?P<description>.*?)(\n#|$)' , re .MULTILINE )
211
208
212
209
213
210
def get_description (md ):
@@ -220,7 +217,8 @@ def make_pageinfo(path):
220
217
paths = path .split ('/' )
221
218
md_data = None
222
219
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 ()
224
222
except Exception :
225
223
print ('open file error : {}' .format (path ))
226
224
raise
@@ -380,7 +378,7 @@ def get_priority(pageinfo):
380
378
if pageinfo ['path' ] == 'index' :
381
379
return 1.0
382
380
depth = len (pageinfo ['paths' ])
383
- return 1.0 - depth * 0.1
381
+ return ( 10 - depth ) / 10
384
382
385
383
info = get_self_latest_commit_info ()
386
384
@@ -398,7 +396,8 @@ class Cache(object):
398
396
399
397
def __init__ (self , cache_file ):
400
398
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 ())
402
401
except Exception :
403
402
self ._cache = {}
404
403
self ._cache_file = cache_file
@@ -436,23 +435,23 @@ def converted(self, path):
436
435
}
437
436
438
437
def flush (self ):
439
- with open (self ._cache_file , 'w' ) as f :
438
+ with open (self ._cache_file , 'w' , encoding = 'utf-8' ) as f :
440
439
f .write (json .dumps (self ._cache ))
441
440
442
441
443
442
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' )
445
444
if not commit_log :
446
445
return None
447
- timestamp , author = unicode ( commit_log , encoding = 'utf-8' ) .split (' ' , 1 )
446
+ timestamp , author = commit_log .split (' ' , 1 )
448
447
return {
449
448
'last_updated' : datetime .fromtimestamp (int (timestamp )),
450
449
'last_author' : author ,
451
450
}
452
451
453
452
454
453
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 ()
456
455
return {
457
456
'last_updated' : last_updated ,
458
457
}
@@ -469,7 +468,7 @@ def remove_not_target_paths(paths):
469
468
for f in files :
470
469
target_file_path = os .path .join (root , f )
471
470
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 ))
473
472
os .remove (target_file_path )
474
473
# 空ディレクトリの削除
475
474
for root , dirs , files in os .walk (settings .OUTPUT_DIR , topdown = False ):
@@ -479,7 +478,7 @@ def remove_not_target_paths(paths):
479
478
if len (files ) == 0 :
480
479
try :
481
480
os .rmdir (root )
482
- print ('remove directory: {}' .format (root ). encode ( 'utf-8' ) )
481
+ print ('remove directory: {}' .format (root ))
483
482
except Exception :
484
483
pass
485
484
@@ -524,10 +523,8 @@ def main():
524
523
else :
525
524
sidebar_index = None
526
525
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 ])
531
528
532
529
cache = Cache (CACHE_FILE )
533
530
env = jinja2 .Environment (loader = jinja2 .FileSystemLoader (settings .PAGE_TEMPLATE_DIR ))
@@ -575,11 +572,11 @@ def run(pageinfos):
575
572
cache .flush ()
576
573
remove_not_target_paths (pageinfo ['path' ] for pageinfo in pageinfos )
577
574
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 ())
580
577
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 ))
583
580
584
581
# 静的ファイルをコピーする
585
582
subprocess .call (['cp' , '-v' , '-RL' ] + glob .glob (os .path .join (settings .STATIC_DIR , '*' )) + [settings .OUTPUT_DIR ])
0 commit comments