Opt: Use logger in deploy

This commit is contained in:
18870 2022-06-25 20:24:34 +08:00
parent 5bae649009
commit 079a3a8c61
14 changed files with 114 additions and 115 deletions

View File

@ -1,5 +1,6 @@
import os
import re
from deploy.logger import logger
BASE_FOLDER = './deploy/AidLux'
@ -41,10 +42,10 @@ def write_file(file, data):
def aidlux_requirements_generate(requirements_in='requirements-in.txt'):
print('aidlux_requirements_generate')
logger.info('aidlux_requirements_generate')
requirements = read_file(requirements_in)
for aidlux in iter_version():
print(f'Generate requirements for AidLux {aidlux}')
logger.info(f'Generate requirements for AidLux {aidlux}')
pre_installed = read_file(os.path.join(BASE_FOLDER, f'./{aidlux}/pre-installed.txt'))
new = {}
for name, version in requirements.items():

View File

@ -2,11 +2,12 @@ import logging
from deploy.config import DeployConfig
from deploy.emulator import EmulatorConnect
from deploy.logger import logger
from deploy.utils import *
def show_fix_tip(module):
print(f"""
logger.info(f"""
To fix this:
1. Open console.bat
2. Execute the following commands:
@ -22,18 +23,18 @@ class AdbManager(DeployConfig):
return self.filepath('AdbExecutable')
def adb_install(self):
hr0('Start ADB service')
logger.hr('Start ADB service', 0)
emulator = EmulatorConnect(adb=self.adb)
if self.ReplaceAdb:
hr1('Replace ADB')
logger.hr('Replace ADB', 1)
emulator.adb_replace()
elif self.AutoConnect:
hr1('ADB Connect')
logger.hr('ADB Connect', 1)
emulator.brute_force_connect()
if self.InstallUiautomator2:
hr1('Uiautomator2 Init')
logger.hr('Uiautomator2 Init', 1)
try:
import adbutils
except ModuleNotFoundError as e:
@ -57,8 +58,8 @@ class AdbManager(DeployConfig):
try:
init.install()
except AssertionError:
print(f'AssertionError when installing uiautomator2 on device {device.serial}')
print('If you are using BlueStacks or LD player or WSA, '
logger.info(f'AssertionError when installing uiautomator2 on device {device.serial}')
logger.info('If you are using BlueStacks or LD player or WSA, '
'please enable ADB in the settings of your emulator')
exit(1)
init._device.shell(["rm", "/data/local/tmp/minicap"])

View File

@ -1,4 +1,5 @@
from deploy.config import DeployConfig
from deploy.logger import logger
from deploy.utils import *
@ -25,7 +26,7 @@ class AlasManager(DeployConfig):
try:
from win32com.client import GetObject
except ModuleNotFoundError:
print('pywin32 not installed, skip')
logger.info('pywin32 not installed, skip')
return False
try:
@ -44,7 +45,7 @@ class AlasManager(DeployConfig):
except Exception as e:
# Possible exception
# pywintypes.com_error: (-2147217392, 'OLE error 0x80041010', None, None)
print(str(e))
logger.info(str(e))
return False
def kill_by_name(self, name):
@ -52,12 +53,12 @@ class AlasManager(DeployConfig):
Args:
name (str): Process name
"""
hr1(f'Kill {name}')
logger.hr(f'Kill {name}', 1)
for row in self.iter_process_by_name(name):
print(' '.join(map(str, row)))
logger.info(' '.join(map(str, row)))
self.execute(f'taskkill /f /pid {row[2]}', allow_failure=True)
def alas_kill(self):
hr0(f'Kill existing Alas')
logger.hr(f'Kill existing Alas', 0)
self.kill_by_name('alas.exe')
self.kill_by_name('python.exe')

View File

@ -2,6 +2,7 @@ import filecmp
import shutil
from deploy.config import DeployConfig
from deploy.logger import logger
from deploy.utils import *
@ -17,36 +18,36 @@ class AppManager(DeployConfig):
bool: If updated.
"""
source = os.path.abspath(os.path.join(folder, path))
print(f'Old file: {source}')
logger.info(f'Old file: {source}')
try:
import alas_webapp
except ImportError:
print(f'Dependency alas_webapp not exists, skip updating')
logger.info(f'Dependency alas_webapp not exists, skip updating')
return False
update = alas_webapp.app_file()
print(f'New version: {alas_webapp.__version__}')
print(f'New file: {update}')
logger.info(f'New version: {alas_webapp.__version__}')
logger.info(f'New file: {update}')
if os.path.exists(source):
if filecmp.cmp(source, update, shallow=True):
print('app.asar is already up to date')
logger.info('app.asar is already up to date')
return False
else:
print(f'Copy {update} -----> {source}')
logger.info(f'Copy {update} -----> {source}')
os.remove(source)
shutil.copy(update, source)
return True
else:
print(f'{source} not exists, skip updating')
logger.info(f'{source} not exists, skip updating')
return False
def app_update(self):
hr0(f'Update app.asar')
logger.hr(f'Update app.asar', 0)
if not self.AutoUpdate:
print('AutoUpdate is disabled, skip')
logger.info('AutoUpdate is disabled, skip')
return False
return self.app_asar_replace(os.getcwd())

View File

@ -1,6 +1,7 @@
import copy
from typing import Optional, Union
from deploy.logger import logger
from deploy.utils import *
@ -66,15 +67,15 @@ class DeployConfig(ConfigModel):
self.show_config()
def show_config(self):
hr0("Show deploy config")
logger.hr("Show deploy config", 0)
for k, v in self.config.items():
if k in ("Password"):
continue
if self.config_template[k] == v:
continue
print(f"{k}: {v}")
logger.info(f"{k}: {v}")
print(f"Rest of the configs are the same as default")
logger.info(f"Rest of the configs are the same as default")
def read(self):
self.config = poor_yaml_read(DEPLOY_TEMPLATE)
@ -123,25 +124,25 @@ class DeployConfig(ConfigModel):
Terminate installation if failed to execute and not allow_failure.
"""
command = command.replace(r"\\", "/").replace("\\", "/").replace('"', '"')
print(command)
logger.info(command)
error_code = os.system(command)
if error_code:
if allow_failure:
print(f"[ allowed failure ], error_code: {error_code}")
logger.info(f"[ allowed failure ], error_code: {error_code}")
return False
else:
print(f"[ failure ], error_code: {error_code}")
logger.info(f"[ failure ], error_code: {error_code}")
self.show_error()
raise ExecutionError
else:
print(f"[ success ]")
logger.info(f"[ success ]")
return True
def show_error(self):
self.show_config()
print("")
hr1("Update failed")
print(
logger.info("")
logger.hr("Update failed", 1)
logger.info(
"Please check your deploy settings in config/deploy.yaml "
"and re-open Alas.exe"
)

View File

@ -1,7 +1,8 @@
import os
from deploy.logger import logger
BASE_FOLDER = os.path.dirname(os.path.abspath(__file__))
print(BASE_FOLDER)
logger.info(BASE_FOLDER)
def read_file(file):
out = {}
@ -37,10 +38,10 @@ def docker_requirements_generate(requirements_in='requirements-in.txt'):
requirements = read_file(requirements_in)
print(f'Generate requirements for Docker image')
logger.info(f'Generate requirements for Docker image')
new = {}
print(requirements)
logger.info(requirements)
for name, version in requirements.items():
# alas-webapp is for windows only
if name == 'alas-webapp':

View File

@ -6,6 +6,7 @@ import shutil
import subprocess
import winreg
from deploy.logger import logger
from deploy.utils import cached_property
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
@ -96,31 +97,31 @@ class VirtualBoxEmulator:
adb (str): Absolute path to adb.exe
"""
for ori, bak in zip(self.adb_binary, self.adb_backup):
print(f'Replacing {ori}')
logger.info(f'Replacing {ori}')
if os.path.exists(ori):
if filecmp.cmp(adb, ori, shallow=True):
print(f'{adb} is same as {ori}, skip')
logger.info(f'{adb} is same as {ori}, skip')
else:
print(f'{ori} -----> {bak}')
logger.info(f'{ori} -----> {bak}')
shutil.move(ori, bak)
print(f'{adb} -----> {ori}')
logger.info(f'{adb} -----> {ori}')
shutil.copy(adb, ori)
else:
print(f'{ori} not exists, skip')
logger.info(f'{ori} not exists, skip')
def adb_recover(self):
""" Revert adb replacement """
for ori in self.adb_binary:
print(f'Recovering {ori}')
logger.info(f'Recovering {ori}')
bak = f'{ori}.bak'
if os.path.exists(bak):
print(f'Delete {ori}')
logger.info(f'Delete {ori}')
if os.path.exists(ori):
os.remove(ori)
print(f'{bak} -----> {ori}')
logger.info(f'{bak} -----> {ori}')
shutil.move(bak, ori)
else:
print(f'Not exists {bak}, skip')
logger.info(f'Not exists {bak}, skip')
# NoxPlayer 夜神模拟器
@ -185,14 +186,14 @@ class EmulatorConnect:
self.adb_binary = adb
def _execute(self, cmd, timeout=10):
print(' '.join(cmd))
logger.info(' '.join(cmd))
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
try:
stdout, stderr = process.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
process.kill()
stdout, stderr = process.communicate()
print(f'TimeoutExpired, stdout={stdout}, stderr={stderr}')
logger.info(f'TimeoutExpired, stdout={stdout}, stderr={stderr}')
return stdout
@cached_property
@ -209,7 +210,7 @@ class EmulatorConnect:
except FileNotFoundError:
continue
if len(serial):
print(f'Emulator {emulator.name} found, instances: {serial}')
logger.info(f'Emulator {emulator.name} found, instances: {serial}')
return emulators
@ -227,7 +228,7 @@ class EmulatorConnect:
if status == 'device':
devices.append(serial)
print(f'Devices: {devices}')
logger.info(f'Devices: {devices}')
return devices
def adb_kill(self):
@ -235,7 +236,7 @@ class EmulatorConnect:
# self._execute([self.adb_binary, 'kill-server'])
# Just kill it, because some adb don't obey.
print('Kill all known ADB')
logger.info('Kill all known ADB')
for exe in [
# Most emulator use this
'adb.exe',
@ -303,4 +304,4 @@ class EmulatorConnect:
if __name__ == '__main__':
emu = EmulatorConnect()
print(emu.brute_force_connect())
logger.info(emu.brute_force_connect())

View File

@ -1,4 +1,5 @@
from deploy.config import DeployConfig
from deploy.logger import logger
from deploy.utils import *
@ -8,10 +9,10 @@ class GitManager(DeployConfig):
return self.filepath('GitExecutable')
def git_repository_init(self, repo, source='origin', branch='master', proxy='', keep_changes=False):
hr1('Git Init')
logger.hr('Git Init', 1)
self.execute(f'"{self.git}" init')
hr1('Set Git Proxy')
logger.hr('Set Git Proxy', 1)
if proxy:
self.execute(f'"{self.git}" config --local http.proxy {proxy}')
self.execute(f'"{self.git}" config --local https.proxy {proxy}')
@ -19,18 +20,18 @@ class GitManager(DeployConfig):
self.execute(f'"{self.git}" config --local --unset http.proxy', allow_failure=True)
self.execute(f'"{self.git}" config --local --unset https.proxy', allow_failure=True)
hr1('Set Git Repository')
logger.hr('Set Git Repository', 1)
if not self.execute(f'"{self.git}" remote set-url {source} {repo}', allow_failure=True):
self.execute(f'"{self.git}" remote add {source} {repo}')
hr1('Fetch Repository Branch')
logger.hr('Fetch Repository Branch', 1)
self.execute(f'"{self.git}" fetch {source} {branch}')
hr1('Pull Repository Branch')
logger.hr('Pull Repository Branch', 1)
# Remove git lock
lock_file = './.git/index.lock'
if os.path.exists(lock_file):
print(f'Lock file {lock_file} exists, removing')
logger.info(f'Lock file {lock_file} exists, removing')
os.remove(lock_file)
if keep_changes:
if self.execute(f'"{self.git}" stash', allow_failure=True):
@ -39,23 +40,23 @@ class GitManager(DeployConfig):
pass
else:
# No local changes to existing files, untracked files not included
print('Stash pop failed, there seems to be no local changes, skip instead')
logger.info('Stash pop failed, there seems to be no local changes, skip instead')
else:
print('Stash failed, this may be the first installation, drop changes instead')
logger.info('Stash failed, this may be the first installation, drop changes instead')
self.execute(f'"{self.git}" reset --hard {source}/{branch}')
self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
else:
self.execute(f'"{self.git}" reset --hard {source}/{branch}')
self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
hr1('Show Version')
logger.hr('Show Version', 1)
self.execute(f'"{self.git}" log --no-merges -1')
def git_install(self):
hr0('Update Alas')
logger.hr('Update Alas', 0)
if not self.AutoUpdate:
print('AutoUpdate is disabled, skip')
logger.info('AutoUpdate is disabled, skip')
return
self.git_repository_init(

33
deploy/logger.py Normal file
View File

@ -0,0 +1,33 @@
import logging
import sys
logger = logging.getLogger("deploy")
_logger = logger
formatter = logging.Formatter(fmt="%(message)s")
hdlr = logging.StreamHandler(stream=sys.stdout)
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
def hr(title, level=3):
if logger is not _logger:
return logger.hr(title, level)
title = str(title).upper()
if level == 0:
middle = "|" + " " * 20 + title + " " * 20 + "|"
border = "+" + "-" * (len(middle) - 2) + "+"
logger.info(border)
logger.info(middle)
logger.info(border)
if level == 1:
logger.info("=" * 20 + " " + title + " " + "=" * 20)
if level == 2:
logger.info("-" * 20 + " " + title + " " + "-" * 20)
if level == 3:
logger.info(f"<<< {title} >>>")
logger.hr = hr

View File

@ -1,6 +1,7 @@
from urllib.parse import urlparse
from deploy.config import DeployConfig
from deploy.logger import logger
from deploy.utils import *
@ -21,13 +22,13 @@ class PipManager(DeployConfig):
return f'"{self.python}" -m pip'
def pip_install(self):
hr0('Update Dependencies')
logger.hr('Update Dependencies', 0)
if not self.InstallDependencies:
print('InstallDependencies is disabled, skip')
logger.info('InstallDependencies is disabled, skip')
return
hr1('Check Python')
logger.hr('Check Python', 1)
self.execute(f'"{self.python}" --version')
arg = []
@ -39,10 +40,10 @@ class PipManager(DeployConfig):
arg += ['--trusted-host', urlparse(mirror).hostname]
# Don't update pip, just leave it.
# hr1('Update pip')
# logger.hr('Update pip', 1)
# self.execute(f'"{self.pip}" install --upgrade pip{arg}')
arg += ['--disable-pip-version-check']
hr1('Update Dependencies')
logger.hr('Update Dependencies', 1)
arg = ' ' + ' '.join(arg) if arg else ''
self.execute(f'{self.pip} install -r {self.requirements_file}{arg}')

View File

@ -82,16 +82,4 @@ def poor_yaml_write(data, file, template_file=DEPLOY_TEMPLATE):
text = re.sub(f'{key}:.*?\n', f'{key}: {value}\n', text)
with open(file, 'w', encoding='utf-8', newline='') as f:
f.write(text)
def hr1(title):
print('=' * 20 + ' ' + title + ' ' + '=' * 20)
def hr0(title):
middle = '|' + ' ' * 20 + title + ' ' * 20 + '|'
border = '+' + '-' * (len(middle) - 2) + '+'
print(border)
print(middle)
print(border)
f.write(text)

View File

@ -1,5 +1,3 @@
import builtins
from deploy.git import GitManager
from deploy.utils import *
from module.handler.login import LoginHandler
@ -28,7 +26,6 @@ class AzurLaneUncensored(LoginHandler):
# Running in ./toolkit/AzurLaneUncensored
os.chdir(folder)
# Monkey patch `print()` build-in to show logs.
backup, builtins.print = builtins.print, logger.info
manager.git_repository_init(
repo=repo,
source='origin',
@ -36,7 +33,6 @@ class AzurLaneUncensored(LoginHandler):
proxy=manager.config['GitProxy'],
keep_changes=False
)
builtins.print = backup
logger.hr('Push Uncensored Files', level=1)
logger.info('This will take a few seconds')

View File

@ -1,2 +1,5 @@
# This must be the first to import
from module.logger import logger # Change folder
from module.logger import logger # Change folder
import deploy.logger
deploy.logger.logger = logger

View File

@ -1,6 +1,4 @@
import builtins
import datetime
import os
import subprocess
import threading
import time
@ -25,31 +23,6 @@ class Updater(DeployConfig, GitManager, PipManager):
self.state = 0
self.event: threading.Event = None
def execute(self, command, allow_failure=False):
"""
Args:
command (str):
allow_failure (bool):
Returns:
bool: If success.
Terminate installation if failed to execute and not allow_failure.
"""
command = command.replace(r"\\", "/").replace("\\", "/").replace('"', '"')
print(command)
error_code = os.system(command)
if error_code:
if allow_failure:
print(f"[ allowed failure ], error_code: {error_code}")
return False
else:
print(f"[ failure ], error_code: {error_code}")
# self.show_error()
raise ExecutionError
else:
print(f"[ success ]")
return True
@property
def delay(self):
self.read()
@ -208,14 +181,11 @@ class Updater(DeployConfig, GitManager, PipManager):
def update(self):
logger.hr("Run update")
backup, builtins.print = builtins.print, logger.info
try:
self.git_install()
self.pip_install()
except ExecutionError:
builtins.print = backup
return False
builtins.print = backup
return True
def run_update(self):