MoviePilot/app/utils/system.py
2023-06-09 14:36:54 +08:00

100 lines
2.3 KiB
Python

import os
import platform
import re
import shutil
from pathlib import Path
from typing import List
class SystemUtils:
@staticmethod
def execute(cmd: str) -> str:
"""
执行命令,获得返回结果
"""
try:
with os.popen(cmd) as p:
return p.readline().strip()
except Exception as err:
print(str(err))
return ""
@staticmethod
def is_docker() -> bool:
return Path("/.dockerenv").exists()
@staticmethod
def is_synology() -> bool:
if SystemUtils.is_windows():
return False
return True if "synology" in SystemUtils.execute('uname -a') else False
@staticmethod
def is_windows() -> bool:
return True if os.name == "nt" else False
@staticmethod
def is_macos() -> bool:
return True if platform.system() == 'Darwin' else False
@staticmethod
def copy(src: Path, dest: Path):
"""
复制
"""
try:
shutil.copy2(src, dest)
return 0, ""
except Exception as err:
print(str(err))
return -1, str(err)
@staticmethod
def move(src: Path, dest: Path):
"""
移动
"""
try:
shutil.move(src.replace(dest.name), dest)
return 0, ""
except Exception as err:
print(str(err))
return -1, str(err)
@staticmethod
def link(src: Path, dest: Path):
"""
硬链接
"""
try:
dest.hardlink_to(src)
return 0, ""
except Exception as err:
print(str(err))
return -1, str(err)
@staticmethod
def softlink(src: Path, dest: Path):
"""
软链接
"""
try:
dest.symlink_to(src)
return 0, ""
except Exception as err:
print(str(err))
return -1, str(err)
@staticmethod
def list_files_with_extensions(directory: Path, extensions: list) -> List[Path]:
files = []
pattern = r".*(" + "|".join(extensions) + ")$"
# 遍历目录及子目录
for path in directory.rglob('**/*'):
if path.is_file() and re.match(pattern, path.name, re.IGNORECASE):
files.append(path)
return files