newdwh2021/stepfunctions/TOOLS/convert_definition.py
2022-09-20 14:54:49 +09:00

174 lines
4.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import csv
import os
import re
CONF_FOLDER_PATH = '.'
DEFINITION_FOLDER_BASE = '..'
CONVERTED_FOLDER_BASE = 'build'
CONVERT_CONF = 'convert.conf'
CHAR_CODE = 'utf-8'
PRD_NAME = 'product'
STG_NAME = 'staging'
COMMENT_PATTERN = r'(^\s*(#.*|)$)|(^(\s*|)(\r\n|\r|\n)$)'
def main(args=None):
try:
"""
args1: StepFunctionsステート名
args2: 変換先環境名product or staging
"""
# カレントディレクトリ移動
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# 引数チェック
state_name, to_env = check_args(args)
print('引数確認OK')
# ファイル存在チェック
check_file_exist(state_name, to_env)
print('ファイル存在確認OK')
# 変換
converted_file_name = convert_definition(state_name, to_env)
print(f'変換が完了しました ファイル名:{converted_file_name}')
return
except Exception as e:
raise Exception(e)
def check_args(args):
try:
check_length(args)
check_aws_environment(args)
return args[1], args[2]
except Exception as e:
raise Exception(f'引数確認に失敗しました {e}')
def check_length(args):
if len(args) != 3:
raise Exception('引数の数が異常です')
return
def check_aws_environment(args):
if args[2] == STG_NAME or args[2] == PRD_NAME:
return
raise Exception('第2引数が不正です')
def check_file_exist(state_name, to_env):
if not os.path.isfile(f'{CONF_FOLDER_PATH}/{CONVERT_CONF}'):
raise Exception('変換定義ファイルが存在しません')
if not os.path.isfile(f'{DEFINITION_FOLDER_BASE}/{state_name}/{state_name}.json'):
raise Exception('変換元のステートメント定義が存在しません')
if not os.path.isdir(f'{DEFINITION_FOLDER_BASE}/{state_name}/{CONVERTED_FOLDER_BASE}/{to_env}'):
os.makedirs(f'{DEFINITION_FOLDER_BASE}/{state_name}/{CONVERTED_FOLDER_BASE}/{to_env}', exist_ok=True)
return
def convert_definition(state_name, to_env):
try:
# 定義フォルダのパス生成
from_folder = f'{DEFINITION_FOLDER_BASE}/{state_name}'
to_folder = f'{from_folder}/{CONVERTED_FOLDER_BASE}/{to_env}'
# 変換定義のリスト化
convert_list = set_convert_list()
# 変換定義のTOの列番号を確認
to_attr_num = set_attribute(to_env, convert_list)
# 読込み、書込みファイルオープン
from_file = open(f'{from_folder}/{state_name}.json',
mode='r', encoding=CHAR_CODE)
to_file = open(f'{to_folder}/{state_name}.json', mode='w',
encoding=CHAR_CODE, newline='\n')
# 変換元ファイルを1行ずつ読込み
for line in from_file:
# 変換定義を1要素ずつ読込み
for convert_value in convert_list:
# 変換
line = line.replace(
convert_value[0], convert_value[to_attr_num])
to_file.write(line)
# 各ファイルクローズ
from_file.close()
to_file.close()
return f'{to_folder}/{state_name}.json'
except Exception as e:
raise Exception(f'変換に失敗しました {e}')
def set_convert_list():
try:
# 変換定義ファイルオープン
convert_conf_file = open(
f'{CONF_FOLDER_PATH}/{CONVERT_CONF}', mode='r', encoding=CHAR_CODE)
# ファイル読込み
convert_conf = convert_conf_file.readlines()
# 変換定義ファイルリスト化
convert_list = []
for row in convert_conf:
if re.match(COMMENT_PATTERN, row[0]):
continue
row = row.rstrip('\n')
data = row.split(',')
convert_list.append(data)
convert_conf_file.close()
return convert_list
except Exception as e:
Exception('変換定義ファイルのリスト化に失敗しました')
def set_attribute(from_env, convert_list):
"""
変換定義ファイルのどちらの列がどちらの環境かを特定
1列目にどちらかの環境名product or stagingがある場合、定義内のその1列が対象の環境となる
"""
to_attr_num = None
for attr in convert_list:
if not attr[1] or not attr[2]:
raise Exception('変換定義が正しくありません')
if attr[1] == from_env:
to_attr_num = 1
elif attr[2] == from_env:
to_attr_num = 2
return to_attr_num
if __name__ == '__main__':
main(args=sys.argv)