# -*- coding: utf-8 -*-
"""EVEN_20250408_00.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1DyiyqWD_kL5DlHenl7fxJ_ZVNgKcYOms

# EasyVen

settings_ven.env から生成される環境変数を利用しやすい形で読み込む

```
2025/03/25 0.1.0 完成
2025/03/25 0.1.1 バグ取り
2025/03/25 0.1.2 バグ取り
2025/03/25 0.1.5 循環参照回避の試み
2025/03/27 0.1.7 バグ取り
2025/03/27 0.1.8 arc_path 修正
2025/03/28 0.2.0 tmp_path 追加
2025/03/30 0.2.1 lcl_path バグ取り
2025/03/30 0.2.4 表示ルーチンの改良
2025/03/31 0.2.5 バグ取り
2025/04/01 0.2.6 org_path 追加
2025/04/02 0.3.0 arz_path, orz_path追加
2025/04/04 0.4.0 enva_path 追加
2025/04/06 0.4.2 依存関係修復のため
2025/04/08 0.5.0 Project Unicornのため、envb_path, uni_path を追加
```

# EasyVen

## 1. モジュール定義
"""

# @title a. EasyVen 定義
import os
from pathlib import Path
# env_file_list に読み込む.envファイルを指定（デフォルトは settings_ven.env）
get_ipython().system( "pip install an_DebugHelper" )
from an_debughelper import DebugHelper
get_ipython().system( "pip install an_EnvManager" )
from an_envmanager import EnvManager


class EasyVen:
    def __init__( self, env_dir = "/content/drive/MyDrive/env/" ):
        self.env_dir = env_dir
        self.envmanager = EnvManager()

    def setup( self,venv_name, back = 0):
        """
        定義ファイル[settings_ven.env]から環境変数を読み込みます
        args:
            venv_name : 仮想環境名
        return:
            [ bch_path,
              dst_path,
              cur_path,
              src_path,
              arc_path,
              arz_path,
              lcl_path,
              bin_path,
              lib_path,
              tmp_path,
              org_path,
              orz_path,
              enva_path,
              envb_path,
              uni_path ]
        """
        self.venv_name = venv_name

        self.debug = DebugHelper()
        self.debug = DebugHelper( instance_name = "EasyVen" )
        self.debug.enable_log_to_file_stdout()
        self.debug.enable_log_to_file_stderr()
        self.debug.enable_timestamp()

        # 環境変数から各種パスを読み出し
        self.base_folder            = self.envmanager.get_env_var("Ven_base_folder")
        self.venv_folder            = self.envmanager.get_env_var("Ven_venv_folder")
        self.app_folder             = self.envmanager.get_env_var("Ven_app_folder")
        self.venv_config_folder     = self.envmanager.get_env_var("Ven_venv_config_folder")
        self.venv_contents_folder   = self.envmanager.get_env_var("Ven_venv_contents_folder")
        self.local_venv_folder      = self.envmanager.get_env_var("Ven_local_venv_folder")
        self.local_app_folder       = self.envmanager.get_env_var("Ven_local_app_folder")
        self.archive_folder         = self.envmanager.get_env_var("Ven_archive_folder")

        self.debug.log_step( f"base_folder           : { self.base_folder }", success = None, back = back )
        self.debug.log_step( f"venv_folder           : { self.venv_folder }", success = None, back = back )
        self.debug.log_step( f"app_folder            : { self.app_folder }", success = None, back = back )
        self.debug.log_step( f"venv_config_folder    : { self.venv_config_folder }", success = None, back = back )
        self.debug.log_step( f"venv_contents_folder  : { self.venv_contents_folder }", success = None, back = back )
        self.debug.log_step( f"local_venv_folder     : { self.local_venv_folder }", success = None, back = back )
        self.debug.log_step( f"local_app_folder      : { self.local_app_folder }", success = None, back = back )
        self.debug.log_step( f"archive_folder        : { self.archive_folder }", success = None, back = back )


        self.bch_path = os.path.join( self.venv_contents_folder )
        self.dst_path = os.path.join( self.local_venv_folder )

        self.cur_path = Path( self.base_folder )
        self.src_path = Path( self.venv_folder ).joinpath( self.venv_name, self.venv_contents_folder )
        self.arc_path = Path( self.archive_folder ).joinpath( self.venv_name ).with_suffix( ".tar.gz" )
        self.arz_path = Path( self.archive_folder ).joinpath( self.venv_name ).with_suffix( ".tar.zst" )
        self.lcl_path = Path( self.local_venv_folder ).joinpath( self.venv_name, self.venv_contents_folder )
        self.bin_path = self.lcl_path.joinpath( "bin" )
        self.lib_path = self.lcl_path.joinpath( "lib" )
        self.tmp_path = Path( self.local_app_folder ).joinpath( self.venv_name )
        self.org_path = Path( self.archive_folder ).joinpath( "origin" ).with_suffix( ".tar.gz" )
        self.orz_path = Path( self.archive_folder ).joinpath( "origin" ).with_suffix( ".tar.zst" )
        self.enva_path = Path( self.archive_folder ).joinpath( self.venv_name.replace( "_env", "_enva" ) ).with_suffix( ".tar.gz" )
        self.envb_path = Path( self.archive_folder ).joinpath( self.venv_name.replace( "_env", "_envb" ) ).with_suffix( ".tar.gz" )
        self.uni_path = Path( self.archive_folder ).joinpath( "starlinka_env_setup.sh")

        self.debug.log_step( f"venv_name : { self.venv_name }", success = None, back = back )
        self.debug.log_step( f"bch_path  : { self.bch_path }", success = None, back = back )
        self.debug.log_step( f"dst_path  : { self.dst_path }", success = None, back = back )
        self.debug.log_step( f"cur_path  : { self.cur_path }", success = None, back = back )
        self.debug.log_step( f"src_path  : { self.src_path }", success = None, back = back )
        self.debug.log_step( f"arc_path  : { self.arc_path }", success = None, back = back )
        self.debug.log_step( f"arz_path  : { self.arz_path }", success = None, back = back )
        self.debug.log_step( f"lcl_path  : { self.lcl_path }", success = None, back = back )
        self.debug.log_step( f"bin_path  : { self.bin_path }", success = None, back = back )
        self.debug.log_step( f"lib_path  : { self.lib_path }", success = None, back = back )
        self.debug.log_step( f"tmp_path  : { self.tmp_path }", success = None, back = back )
        self.debug.log_step( f"org_path  : { self.org_path }", success = None, back = back )
        self.debug.log_step( f"orz_path  : { self.orz_path }", success = None, back = back )
        self.debug.log_step( f"enva_path : { self.enva_path }", success = None, back = back )
        self.debug.log_step( f"envb_path : { self.envb_path }", success = None, back = back )
        self.debug.log_step( f"uni_path : { self.uni_path }", success = None, back = back )

        return  [ self.bch_path,
                  self.dst_path,
                  self.cur_path,
                  self.src_path,
                  self.arc_path,
                  self.arz_path,
                  self.lcl_path,
                  self.bin_path,
                  self.lib_path,
                  self.tmp_path,
                  self.org_path,
                  self.orz_path,
                  self.enva_path,
                  self.envb_path,
                  self.uni_path ]

    def check( self, venv_name = "kohya_env", back = 0 ):
        """
        読み込んだ環境変数をチェックします
        args:
            venv_name : 仮想環境名
        """
        [ bch_path,
        dst_path,
        cur_path,
        src_path,
        arc_path,
        arz_path,
        lcl_path,
        bin_path,
        lib_path,
        tmp_path,
        org_path,
        orz_path,
        enva_path,
        envb_path,
        uni_path ] = self.setup( venv_name = venv_name )
        # self.debug.log_step( f"venv_name : { venv_name }", success = None )
        self.debug.log_step( f"bch_path  : { bch_path }", success = None, back = back )
        self.debug.log_step( f"dst_path  : { dst_path }", success = None, back = back )
        self.debug.log_step( f"cur_path  : { cur_path }", success = None, back = back )
        self.debug.log_step( f"src_path  : { src_path }", success = None, back = back )
        self.debug.log_step( f"arc_path  : { arc_path }", success = None, back = back )
        self.debug.log_step( f"arz_path  : { arz_path }", success = None, back = back )
        self.debug.log_step( f"lcl_path  : { lcl_path }", success = None, back = back )
        self.debug.log_step( f"bin_path  : { bin_path }", success = None, back = back )
        self.debug.log_step( f"lib_path  : { lib_path }", success = None, back = back )
        self.debug.log_step( f"tmp_path  : { tmp_path }", success = None, back = back )
        self.debug.log_step( f"org_path  : { org_path }", success = None, back = back )
        self.debug.log_step( f"orz_path  : { orz_path }", success = None, back = back )
        self.debug.log_step( f"enva_path : { enva_path }", success = None, back = back )
        self.debug.log_step( f"envb_path : { envb_path }", success = None, back = back )
        self.debug.log_step( f"uni_path : { uni_path }", success = None, back = back )


if __name__ == "__main__":
    from google.colab import drive
    drive.mount( '/content/drive' )
    even = EasyVen()
    even.check( venv_name = "kohya_env" )