cengal.build_tools.modules.create.versions.v_0.create
Module Docstring Docstrings: http://www.python.org/dev/peps/pep-0257/
1#!/usr/bin/env python 2# coding=utf-8 3 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space> 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18 19""" 20Module Docstring 21Docstrings: http://www.python.org/dev/peps/pep-0257/ 22""" 23 24 25__author__ = "ButenkoMS <gtalk@butenkoms.space>" 26__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>" 27__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ] 28__license__ = "Apache License, Version 2.0" 29__version__ = "4.4.1" 30__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>" 31__email__ = "gtalk@butenkoms.space" 32# __status__ = "Prototype" 33__status__ = "Development" 34# __status__ = "Production" 35 36 37from os import rename, remove 38from os.path import sep as os_sep, dirname, exists, isfile 39from distutils.dir_util import copy_tree 40from distutils.file_util import copy_file 41from cengal.file_system.win_fs.global_install_uninstall import win_fs 42from cengal.file_system.path_manager import path_relative_to_src, RelativePath 43from cengal.file_system.file_manager import file_exists 44from cengal.text_processing.text_processing import removeprefix 45from cengal.file_system.file_patch.simple import patch_text_file 46import os 47from typing import Set 48 49 50create_env_variants: Set[str] = { 51 'create_env.sh', 52 'create_env.cmd', 53 'command/_common.sh', 54 'command/_common.cmd', 55} 56 57 58def create(src_dir: str, module_submodule_path: str, sep: str = '/'): 59 with win_fs(): 60 src_dir_path = RelativePath(src_dir) 61 template_module_dir = src_dir_path('_template_module') 62 template_submodule_dir = src_dir_path('_template_module/_template_submodule') 63 required_path_input = module_submodule_path 64 required_path_input = required_path_input.replace(os_sep, sep) 65 required_path_input = required_path_input.strip() 66 required_path_input = required_path_input.strip(sep) 67 # if sep not in required_path_input: 68 # print('Wrong path: both module and submodule are mandatory') 69 # return 70 71 required_path_input_parts = required_path_input.split(sep) 72 submodule_name = required_path_input_parts[-1] 73 submodule_path = src_dir_path(required_path_input) 74 module_path = RelativePath(submodule_path)('..') 75 copy_tree(template_submodule_dir, submodule_path, update=True) 76 77 template_submodule_py_path = RelativePath(submodule_path)('versions/v_0/_template_submodule.py') 78 template_submodule_py_new_path = RelativePath(submodule_path)(f'versions/v_0/{submodule_name}.py') 79 if exists(template_submodule_py_new_path) and isfile(template_submodule_py_new_path): 80 remove(template_submodule_py_path) 81 else: 82 rename(template_submodule_py_path, template_submodule_py_new_path) 83 84 init_py_path = RelativePath(submodule_path)('versions/v_0/__init__.py') 85 patch_text_file(init_py_path, [('_template_submodule', submodule_name)]) 86 87 for index in range(1, 1 + len(required_path_input_parts)): 88 subpath: str = sep.join(required_path_input_parts[:index]) 89 subpath_init: str = src_dir_path(subpath + sep + '__init__.py') 90 if not file_exists(subpath_init): 91 copy_file(src_dir_path('_template_module/__init__.py'), subpath_init, verbose=0) 92 93 # create_env_sh_path = RelativePath(submodule_path)('versions/v_0/development/create_env.sh') 94 # development_dir_path = dirname(create_env_sh_path) 95 development_dir_path = RelativePath(submodule_path)('versions/v_0/development') 96 remaining_path = removeprefix(development_dir_path, src_dir) 97 sep_num = remaining_path.count(sep) 98 cd_2_parent_num = 1 + sep_num # since we need dir parent to the `src_dir` 99 new_relativeness = '/..' * cd_2_parent_num 100 new_relativeness_win32 = '\\..' * cd_2_parent_num 101 102 development_dir_path_rel: RelativePath = RelativePath(development_dir_path) 103 for create_env_variant in create_env_variants: 104 create_env_sh_path = development_dir_path_rel(create_env_variant) 105 if not file_exists(create_env_sh_path): 106 continue 107 108 patch_text_file(create_env_sh_path, [ 109 ('$WORKDIR/../../../../../..`', f'$WORKDIR{new_relativeness}`'), 110 ('$CURRENT_PROJECT_DIR_PATH/../../../../../.."', f'$CURRENT_PROJECT_DIR_PATH{new_relativeness}"'), 111 ('%CURRENT_PROJECT_DIR_PATH%\..\..\..\..\..\.."', f'%CURRENT_PROJECT_DIR_PATH%{new_relativeness_win32}"'), 112 ])
create_env_variants: Set[str] =
{'create_env.cmd', 'create_env.sh', 'command/_common.cmd', 'command/_common.sh'}
def
create(src_dir: str, module_submodule_path: str, sep: str = '/'):
59def create(src_dir: str, module_submodule_path: str, sep: str = '/'): 60 with win_fs(): 61 src_dir_path = RelativePath(src_dir) 62 template_module_dir = src_dir_path('_template_module') 63 template_submodule_dir = src_dir_path('_template_module/_template_submodule') 64 required_path_input = module_submodule_path 65 required_path_input = required_path_input.replace(os_sep, sep) 66 required_path_input = required_path_input.strip() 67 required_path_input = required_path_input.strip(sep) 68 # if sep not in required_path_input: 69 # print('Wrong path: both module and submodule are mandatory') 70 # return 71 72 required_path_input_parts = required_path_input.split(sep) 73 submodule_name = required_path_input_parts[-1] 74 submodule_path = src_dir_path(required_path_input) 75 module_path = RelativePath(submodule_path)('..') 76 copy_tree(template_submodule_dir, submodule_path, update=True) 77 78 template_submodule_py_path = RelativePath(submodule_path)('versions/v_0/_template_submodule.py') 79 template_submodule_py_new_path = RelativePath(submodule_path)(f'versions/v_0/{submodule_name}.py') 80 if exists(template_submodule_py_new_path) and isfile(template_submodule_py_new_path): 81 remove(template_submodule_py_path) 82 else: 83 rename(template_submodule_py_path, template_submodule_py_new_path) 84 85 init_py_path = RelativePath(submodule_path)('versions/v_0/__init__.py') 86 patch_text_file(init_py_path, [('_template_submodule', submodule_name)]) 87 88 for index in range(1, 1 + len(required_path_input_parts)): 89 subpath: str = sep.join(required_path_input_parts[:index]) 90 subpath_init: str = src_dir_path(subpath + sep + '__init__.py') 91 if not file_exists(subpath_init): 92 copy_file(src_dir_path('_template_module/__init__.py'), subpath_init, verbose=0) 93 94 # create_env_sh_path = RelativePath(submodule_path)('versions/v_0/development/create_env.sh') 95 # development_dir_path = dirname(create_env_sh_path) 96 development_dir_path = RelativePath(submodule_path)('versions/v_0/development') 97 remaining_path = removeprefix(development_dir_path, src_dir) 98 sep_num = remaining_path.count(sep) 99 cd_2_parent_num = 1 + sep_num # since we need dir parent to the `src_dir` 100 new_relativeness = '/..' * cd_2_parent_num 101 new_relativeness_win32 = '\\..' * cd_2_parent_num 102 103 development_dir_path_rel: RelativePath = RelativePath(development_dir_path) 104 for create_env_variant in create_env_variants: 105 create_env_sh_path = development_dir_path_rel(create_env_variant) 106 if not file_exists(create_env_sh_path): 107 continue 108 109 patch_text_file(create_env_sh_path, [ 110 ('$WORKDIR/../../../../../..`', f'$WORKDIR{new_relativeness}`'), 111 ('$CURRENT_PROJECT_DIR_PATH/../../../../../.."', f'$CURRENT_PROJECT_DIR_PATH{new_relativeness}"'), 112 ('%CURRENT_PROJECT_DIR_PATH%\..\..\..\..\..\.."', f'%CURRENT_PROJECT_DIR_PATH%{new_relativeness_win32}"'), 113 ])