IntelliJ でEAをコンパイル後に自動デプロイする (2)

この投稿文は次の言語で読めます: 日本語

概要

前回はをご紹介しました。

今回は、処理本体のスクリプト部分のサンプルです。

スポンサーリンク

設定

Pythonで以下のスクリプトを作成します。

IntelliJ を使用していれば、 Python 用のIDEである Pycharm は、ほぼ同じ操作感で使用することができます。
言語が変わっても環境が変わら無いのはストレスが無くて助かります!

"""自動デプロイ
    IntelliJ からコンパイル実行したタイミングで
    成果物ファイル(.ex4)出力、及びバックテスト用ディレクトリに成果物複製するためのスクリプト
"""

import sys
import os
import os.path
import subprocess
import shutil
from distutils.dir_util import copy_tree
from configparser import ConfigParser

# グローバル変数
app_home = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
sys.path.append(os.path.join(app_home, 'lib'))


class Deploy:
    def __init__(self):
        """初期設定"""
        self.__source_path = ''
        self.__dest_path = ''
        self.__app_name = ''

        # pythonスクリプト用config
        self.config = ConfigParser()
        conf_path = os.path.join(app_home, 'conf', 'deploy.conf')
        self.config.read(conf_path)

    @property
    def app_name(self):
        """プログラム名称"""
        return self.__app_name

    @app_name.setter
    def app_name(self, value):
        """プログラム名称"""
        self.__app_name = value

    @property
    def dest_path(self):
        """コピー先パス"""
        return self.__dest_path

    @dest_path.setter
    def dest_path(self, value):
        """コピー先パス"""
        self.__dest_path = value

    @property
    def source_path(self):
        """コピー元パス"""
        return self.__source_path

    @source_path.setter
    def source_path(self, value):
        """コピー元パス"""
        self.__source_path = value

    def run(self):
        """コンパイル"""
        try:
            # workディレクトリにソースをコピーする
            # origin_path → source_path
            self.origin_directory_experts = os.path.join(self.config.get('deploy', 'origin_path'), 'Experts')
            self.origin_directory_include = os.path.join(self.config.get('deploy', 'origin_path'), 'include', 'Original')

            self.source_directory_experts = os.path.join(self.config.get('deploy', 'source_path'), 'Experts')
            self.source_directory_include = os.path.join(self.config.get('deploy', 'source_path'), 'include', 'Original')

            copy_tree(self.origin_directory_experts, self.source_directory_experts)
            copy_tree(self.origin_directory_include, self.source_directory_include)

            # コンパイル処理
            self.meta_editor_path = self.config.get('deploy', 'meta_editor_path')
            self.batch_file_path = os.path.join(self.config.get('deploy', 'source_path'), 'Experts', 'compile.bat')
            self.source_path = os.path.join(self.config.get('deploy', 'source_path'), 'Experts', self.app_name)
            self.artifact_source_path =  self.source_path.replace('.mq4','.ex4')

            # バッチファイルを開く
            output_text = self.meta_editor_path + ' /compile:"' + self.source_path + '"' + ' /log'
            f = open(self.batch_file_path, 'w')
            f.write(output_text)
            f.close()

            # コンパイル実行
            p = subprocess.Popen([self.batch_file_path])
            p.wait()

            if not os.path.exists(self.artifact_source_path):
                raise FileNotFoundError(self.artifact_source_path)

            # 実行ファイル(.ex4) をバックテスト用ディレクトリへ複製する
            self.dest_path = os.path.join(self.config.get('deploy', 'dest_path'), 'Experts', self.app_name)
            self.artifact_dest_path =  self.dest_path.replace('.mq4','.ex4')
            shutil.copyfile(self.artifact_source_path, self.artifact_dest_path)

            self.dest_path = os.path.join(self.config.get('deploy', 'dest_path2'), 'Experts', self.app_name)
            self.artifact_dest_path =  self.dest_path.replace('.mq4','.ex4')
            shutil.copyfile(self.artifact_source_path, self.artifact_dest_path)


        except Exception as e:
            print(e)
            exit(-1)

if __name__ == '__main__':
    args = sys.argv
    deploy = Deploy()
    try:
        # 引数設定
        deploy.app_name = args[1]

    except IndexError:
        print('Error: Invalid Arguments. ')
        print('arg1 : branch_name, arg2 : execute_mode (clone/pull)')

    # 配信実行
    try:
        deploy.run()
        print('success.')
    except Exception as e:
        print(e)

 

スポンサーリンク

Twitterでフォローしよう

おすすめの記事