Skip to content

Commit

Permalink
#
Browse files Browse the repository at this point in the history
  • Loading branch information
czy21 committed May 18, 2024
1 parent d31f1f8 commit 5aec7f7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
21 changes: 12 additions & 9 deletions server/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import pathlib
import re
import shutil
import sys
import typing
Expand Down Expand Up @@ -140,7 +141,8 @@ def select_namespace(root_path: pathlib.Path, deep: int = 1, exclude_rules=None,


def execute(cmd, is_return: bool = False, dry_run=False):
return basic_util.execute(cmd, is_input=False, is_return=is_return, stack_index=2, dry_run=dry_run)
logger.debug("\n{0}".format(re.sub(r'&&\s+', '&&\n', cmd)))
return basic_util.execute(cmd, is_input=False, is_return=is_return, dry_run=dry_run)


class CustomHelpFormatter(argparse.MetavarTypeHelpFormatter):
Expand Down Expand Up @@ -351,6 +353,7 @@ def __loop_namespaces(self, namespaces: list[Namespace], global_env: dict, args:
role_name = r.name
role_path: pathlib.Path = r.path
role_title = "%s.%s" % (role_key, role_name)
role_log_prefix = "{0} {1}".format(role_title, args.command)
role_temp_path = role_path.joinpath("___temp")
role_temp_path.mkdir(parents=True, exist_ok=True)
role_bak_path = role_temp_path.joinpath("bak")
Expand All @@ -369,6 +372,7 @@ def __loop_namespaces(self, namespaces: list[Namespace], global_env: dict, args:
"param_role_temp_path": role_temp_path.as_posix(),
"param_role_bak_path": role_bak_path.as_posix()
}
logger.info(role_log_prefix)
if args.command == Command.backup.value:
role_bak_path.mkdir(exist_ok=True)
# process env
Expand All @@ -390,14 +394,11 @@ def __loop_namespaces(self, namespaces: list[Namespace], global_env: dict, args:
file_util.copy(t, role_output_file)

# collect command
_cmds = [
echo_action(role_title, args.command)
]
_cmds = []
if args.command == Command.build.value:
if args.target == "build.sh":
target_file = role_output_path.joinpath(args.target)
if target_file.exists():
_cmds.append(echo_action(role_title, Command.build.value, target_file.as_posix()))
_cmds.append("sh {0} {1}".format(target_file.as_posix(), " ".join(args.build_args)))
role_instance = self.role_class(home_path=self.home_path,
root_path=self.root_path,
Expand All @@ -410,15 +411,17 @@ def __loop_namespaces(self, namespaces: list[Namespace], global_env: dict, args:
namespace=namespace,
args=args)
_cmds.extend(getattr(role_instance, args.command)())
execute(collection_util.flat_to_str(_cmds, delimiter=" && "), dry_run=args.dry_run)
_cmds_string = collection_util.flat_to_str(_cmds, delimiter=" && ")
execute(_cmds_string, dry_run=args.dry_run)

def cp_role_to_root(src: pathlib.Path, dst: pathlib.Path):
return "mkdir -p {0} && cp -r {1} {0}".format(dst.joinpath(role_path.relative_to(self.root_path)).as_posix(), src.as_posix())
execute(collection_util.flat_to_str([

_post_cmds_string = collection_util.flat_to_str([
cp_role_to_root(role_build_path, self.build_path),
cp_role_to_root(role_temp_path, self.tmp_path) if any(role_temp_path.iterdir()) else []
], delimiter=" && "))
], delimiter=" && ")
execute(_post_cmds_string)

def run(self, **kwargs):
args: argparse.Namespace = self.arg_parser.parse_args()
Expand Down
8 changes: 0 additions & 8 deletions utility/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,9 @@ def execute(
encoding="utf-8",
is_input=True,
is_return=True,
stack_index=1,
shell=os.name == 'nt',
dry_run=False
):
stack = inspect.stack()
stack_logs = []
if len(stack) - 1 >= stack_index:
stack_item = stack[stack_index]
stack_logs.append("{0} line: {1} func: {2}".format(pathlib.Path(stack_item.filename).as_posix(), str(stack_item.lineno), stack_item.function))
stack_logs.append(re.sub('&&\s+', '&&\n', cmd))
logger.info("\n".join(stack_logs))
if is_input:
input_exec = str(input("Are you sure you want to execute (y/n)?").strip())
if input_exec != "y":
Expand Down
4 changes: 2 additions & 2 deletions utility/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ def init_logger(name=None, file: pathlib.Path = None):
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
ch = colorlog.StreamHandler()
color_format = '%(asctime)s %(log_color)s%(levelname)s %(reset)s[ %(threadName)s ] %(cyan)s%(name)s %(module)s.%(funcName)s %(reset)s- %(message)s'
color_format = '%(asctime)s %(log_color)s%(levelname)-6s %(reset)s[ %(threadName)s ] %(cyan)s%(name)s %(module)s.%(funcName)s %(reset)s- %(message)s'
ch.setFormatter(colorlog.ColoredFormatter(color_format))
logger.addHandler(ch)
if file:
file.parent.mkdir(exist_ok=True, parents=True)
file.write_text("")
fh = logging.FileHandler(filename=file.absolute().as_posix(), encoding='utf-8')
fh.setFormatter(logging.Formatter('%(asctime)s %(levelname)s [ %(threadName)s ] %(name)s %(module)s.%(funcName)s - %(message)s'))
fh.setFormatter(logging.Formatter('%(asctime)s %(levelname)-6s [ %(threadName)s ] %(name)s %(module)s.%(funcName)s - %(message)s'))
logger.addHandler(fh)

0 comments on commit 5aec7f7

Please sign in to comment.