Skip to content

Commit

Permalink
add ct-env
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-black-tea committed Apr 3, 2024
1 parent 2a83176 commit 13b2eda
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 167 deletions.
38 changes: 0 additions & 38 deletions dependencies.json

This file was deleted.

28 changes: 28 additions & 0 deletions requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
dependencies:
- rich
- filelock >= 3.4.0

dev-dependencies:
- pyyaml

optional-dependencies:
requests:
- 'requests[socks]'
frida:
- frida>=15.0.0
objection:
- objection
lief:
- lief>0.10.1
- python-magic; platform_system=="Linux"
- python-magic; platform_system=="Darwin"
- python-magic-bin; platform_system=="Windows"
ssh:
- paramiko
- scp
ssl:
- pyOpenSSL
container:
- GitPython
- jinja2
- pyyaml
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def append_module(self, path, script_prefix, module_prefix):
)
)

with open(get_path("dependencies.json"), "rt", encoding="utf-8") as fd:
data = json.load(fd)
with open(get_path("requirements.yml"), "rt", encoding="utf-8") as fd:
data = yaml.safe_load(fd)
# install_requires = dependencies + dev-dependencies
install_requires = data.get("dependencies")
if not release:
Expand Down
17 changes: 5 additions & 12 deletions src/linktools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,15 @@
/ ==ooooooooooooooo==.o. ooo= // ,`\--{)B ,"
/_==__==========__==_ooo__ooo=_/' /___________,"
"""
from argparse import ArgumentParser, Namespace
from typing import Optional
from typing import Any

from .cli import BaseCommand, commands
from .cli import commands, BaseCommandGroup


class Command(BaseCommand):
class Command(BaseCommandGroup):

def init_arguments(self, parser: ArgumentParser) -> None:
self.add_subcommands(parser=parser, target=commands)

def run(self, args: Namespace) -> Optional[int]:
subcommand = self.parse_subcommand(args)
if not subcommand or subcommand.is_group:
return self.print_subcommands(args, subcommand, max_level=2)
return subcommand.run(args)
def init_subcommands(self) -> Any:
return commands


command = Command()
Expand Down
35 changes: 19 additions & 16 deletions src/linktools/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ def optionxform(self, optionstr: str):

class ConfigCacheParser:

def __init__(self, path: str, namespace: str):
def __init__(self, config: "Config"):
self._parser = ConfigParser(default_section="ENV") # 兼容老版本,默认ENV作为默认节
self._path = path
self._section = f"{namespace}.CACHE".upper()
self._path = config.cache_path
self._section = f"{config._namespace}.CACHE".upper()
self.load()

def load(self):
if self._path and os.path.exists(self._path):
Expand Down Expand Up @@ -165,7 +166,7 @@ def load(self, config: "Config", key: str, type: "Union[Type[T], ConfigType]" =
type = type or self._type
if self._cached:
# load cache from config file
config_parser = config._get_cache_parser()
config_parser = ConfigCacheParser(config)
config_cache = cache
if config_cache == __missing__:
config_cache = config_parser.get(key, __missing__)
Expand Down Expand Up @@ -436,11 +437,18 @@ def update_from_dir(self, path: str, recursion: bool = False) -> bool:
self.update_from_file(os.path.join(root, name))
return True

@property
def cache_path(self) -> str:
"""
缓存文件路径
"""
return self._cache_path

def load_cache(self) -> None:
"""
从缓存中加载配置
"""
parser = self._get_cache_parser()
parser = ConfigCacheParser(self)
with self.__lock__:
self._cache.clear()
self._cache.update(parser.items())
Expand All @@ -450,7 +458,7 @@ def save_cache(self, **kwargs: Any) -> None:
保存配置到缓存
:param kwargs: 需要保存的配置
"""
parser = self._get_cache_parser()
parser = ConfigCacheParser(self)
with self.__lock__:
for key, value in kwargs.items():
self._cache[key] = value
Expand All @@ -462,7 +470,7 @@ def remove_cache(self, *keys: str) -> None:
删除缓存
:param keys: 需要删除的缓存键
"""
parser = self._get_cache_parser()
parser = ConfigCacheParser(self)
with self.__lock__:
for key in keys:
self._cache.pop(key, None)
Expand All @@ -480,11 +488,6 @@ def __getitem__(self, key: str) -> Any:
def __setitem__(self, key: str, value: Any):
self.set(key, value)

def _get_cache_parser(self) -> ConfigCacheParser:
parser = ConfigCacheParser(self._cache_path, self._namespace)
parser.load()
return parser

class Prompt(ConfigProperty):

def __init__(
Expand Down Expand Up @@ -634,10 +637,10 @@ def __init__(self, message: str = None):

def _load(self, config: "Config", key: str, cache: Any):
message = self.message or \
f"Cannot find config \"{key}\". {os.linesep}" \
f"You can use any of the following methods to fix it: {os.linesep}" \
f"1. set \"{config._prefix}{key}\" as an environment variable, {os.linesep}" \
f"2. call config.save_cache method to save the value to file. {os.linesep}"
f"Cannot find config \"{key}\". {os.linesep}" \
f"You can use any of the following methods to fix it: {os.linesep}" \
f"1. set \"{config._prefix}{key}\" as an environment variable, {os.linesep}" \
f"2. call config.save_cache method to save the value to file. {os.linesep}"
raise ConfigError(message)


Expand Down
35 changes: 33 additions & 2 deletions src/linktools/_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import logging
import os
import pathlib
import shutil
import time
from typing import TYPE_CHECKING, TypeVar, Type, Any

from . import utils, metadata
Expand Down Expand Up @@ -111,7 +113,7 @@ def data_path(self) -> str:
"""
存放文件目录
"""
prefix = f"{metadata.__name__}_".upper()
prefix = f"{metadata.__name__}".upper()
path = os.environ.get(f"{prefix}_DATA_PATH", None)
if path: # 优先使用环境变量中的${DATA_PATH}
return path
Expand All @@ -126,7 +128,7 @@ def temp_path(self) -> str:
"""
存放临时文件目录
"""
prefix = f"{metadata.__name__}_".upper()
prefix = f"{metadata.__name__}".upper()
path = os.environ.get(f"{prefix}_TEMP_PATH", None)
if path: # 优先使用环境变量中的${TEMP_PATH}
return path
Expand Down Expand Up @@ -168,6 +170,35 @@ def get_temp_dir(self, *paths: str, create: bool = False) -> str:
"""
return utils.get_path(self.temp_path, *paths, create=create, create_parent=False)

def clean_temp_files(self, expire_days: int = 7) -> None:
"""
清理临时文件
"""
current_time = time.time()
target_time = current_time - expire_days * 24 * 60 * 60
for root, dirs, files in os.walk(self.temp_path, topdown=False):
for name in files:
path = os.path.join(root, name)
last_time = max(
os.path.getatime(path),
os.path.getctime(path),
os.path.getmtime(path),
)
if last_time < target_time:
self.logger.info(f"Remove expired temp file: {path}")
os.remove(path)
for name in dirs:
path = os.path.join(root, name)
if os.path.exists(path) and not os.listdir(path):
last_time = max(
os.path.getatime(path),
os.path.getctime(path),
os.path.getmtime(path),
)
if last_time < target_time:
self.logger.info(f"Remove empty temp directory: {path}")
shutil.rmtree(path, ignore_errors=True)

@cached_classproperty
def _log_manager(self) -> logging.Manager:

Expand Down
17 changes: 0 additions & 17 deletions src/linktools/android/__main__.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/linktools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from . import argparse

from .command import \
BaseCommand, CommandError, \
BaseCommand, BaseCommandGroup, CommandError, \
SubCommand, SubCommandGroup, SubCommandWrapper, \
subcommand, subcommand_argument, SubCommandError

Expand Down
20 changes: 20 additions & 0 deletions src/linktools/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def subcommand(
"""
子命令装饰器
"""

def decorator(func):
if not hasattr(func, "__subcommand_info__"):
setattr(func, "__subcommand_info__", _SubCommandMethodInfo())
Expand Down Expand Up @@ -189,6 +190,7 @@ def subcommand_argument(
"""
子命令参数装饰器,与@subcommand配合使用
"""

def decorator(func):
subcommand_argument_info = _SubCommandMethodArgumentInfo()
subcommand_argument_info.set_args(
Expand Down Expand Up @@ -825,3 +827,21 @@ def __call__(self, args: Union[List[str], Namespace] = None) -> int:
)

return exit_code


class BaseCommandGroup(BaseCommand, metaclass=abc.ABCMeta):

def init_subcommands(self) -> Any:
return self

def init_arguments(self, parser: ArgumentParser) -> None:
self.add_subcommands(
parser=parser,
target=self.walk_subcommands(self.init_subcommands())
)

def run(self, args: Namespace) -> Optional[int]:
subcommand = self.parse_subcommand(args)
if not subcommand or subcommand.is_group:
return self.print_subcommands(args, subcommand, max_level=2)
return subcommand.run(args)
Loading

0 comments on commit 13b2eda

Please sign in to comment.