Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appveyor で文字コードをチェックして UTF-8 になっていなかったら処理を失敗させる #478

Merged
merged 4 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ platform:

build_script:
- cmd: >-
call checkEncoding.bat

call build-all.bat %PLATFORM% %CONFIGURATION%

call tests\build-and-test.bat %PLATFORM% %CONFIGURATION%
Expand Down
13 changes: 13 additions & 0 deletions checkEncoding.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@echo off
if not "%APPVEYOR_BUILD_NUMBER%" == "" (
@echo installing chardet
pip install chardet --user
) else (
@echo skip installing chardet
)

@echo ---- start python checkEncoding.py ----
python checkEncoding.py || (echo error checkEncoding.py && exit /b 1)
@echo ---- end python checkEncoding.py ----
@echo.
exit /b 0
82 changes: 82 additions & 0 deletions checkEncoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from chardet.universaldetector import UniversalDetector
import chardet
import os
import sys
import subprocess

# 指定したファイルの文字コードを返す
def check_encoding(file_path):
detector = UniversalDetector()
with open(file_path, mode='rb') as f:
for binary in f:
detector.feed(binary)
if detector.done:
break
detector.close()
return detector.result['encoding']

# チェック対象の拡張子か判断する
def checkExtension(file):
extensions = [
".cpp",
".h",
berryzplus marked this conversation as resolved.
Show resolved Hide resolved
]
base, ext = os.path.splitext(file)
if ext in extensions:
return True
else:
return False

def getMergeBase():
output = subprocess.check_output('git show-branch --merge-base origin/master HEAD')
outputDec = output.decode()
mergeBase = outputDec.splitlines()
return mergeBase[0]

# ベースとの差分をチェック
def getDiffFiles():
mergeBase = getMergeBase()

output = subprocess.check_output('git diff ' + mergeBase + ' --name-only --diff-filter=dr')
diffFiles = output.decode()
return diffFiles.splitlines()

def checkEncodingResult(encoding):
expectEncoding = [
"utf-8",
"ascii",
]
encoding = encoding.lower()
if encoding in expectEncoding:
return True

return False

# デバッグ用
# すべてのファイルの文字コードを調べてチェックする
def checkAll():
for rootdir, dirs, files in os.walk('.'):
for file in files:
if checkExtension(file):
full = os.path.join(rootdir, file)
encoding = check_encoding(full)
if not checkEncodingResult(encoding):
print (encoding, full)

if __name__ == '__main__':
count = 0

files = getDiffFiles()
for file in files:
if checkExtension(file):
encoding = check_encoding(file)
if not checkEncodingResult(encoding):
print (encoding, file)
count = count + 1

if count > 0:
print ("return 1")
sys.exit(1)
else:
print ("return 0")
sys.exit(0)