This commit is contained in:
flaskfarm
2022-10-07 01:48:42 +09:00
parent 4b72b7dc65
commit cde69d4d8a
55 changed files with 523 additions and 7703 deletions

View File

@@ -1,20 +1 @@
from framework import logger
from .notify import ToolBaseNotify
from .file import ToolBaseFile
from .aes_cipher import ToolAESCipher
from .celery_shutil import ToolShutil
from .subprocess import ToolSubprocess
from .rclone import ToolRclone
from .ffmpeg import ToolFfmpeg
from .util import ToolUtil
from .hangul import ToolHangul
from .os_command import ToolOSCommand
from .util_yaml import ToolUtilYaml
def d(data):
if type(data) in [type({}), type([])]:
import json
return '\n' + json.dumps(data, indent=4, ensure_ascii=False)
else:
return str(data)

View File

@@ -2,7 +2,7 @@
#########################################################
import os, sys, traceback, subprocess, json, platform
from framework import app, logger, path_data
from .subprocess import ToolSubprocess
from ..support.base.subprocess import ToolSubprocess
class ToolFfmpeg(object):

View File

@@ -1,45 +1,18 @@
# -*- coding: utf-8 -*-
#########################################################
import os, traceback, io, re, json, codecs
import codecs
import io
import json
import os
import re
import traceback
from . import logger
class ToolBaseFile(object):
@classmethod
def read(cls, filepath, mode='r'):
try:
import codecs
ifp = codecs.open(filepath, mode, encoding='utf8')
data = ifp.read()
ifp.close()
if isinstance(data, bytes):
data = data.decode('utf-8')
return data
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
@classmethod
def download(cls, url, filepath):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language' : 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
'Connection': 'Keep-Alive',
}
import requests
with open(filepath, "wb") as file_is: # open in binary mode
response = requests.get(url, headers=headers) # get request
file_is.write(response.content) # write to file
return True
except Exception as exception:
logger.debug('Exception:%s', exception)
logger.debug(traceback.format_exc())
return False
@classmethod
def write(cls, data, filepath, mode='w'):
@@ -80,7 +53,8 @@ class ToolBaseFile(object):
@classmethod
def file_move(cls, source_path, target_dir, target_filename):
try:
import time, shutil
import shutil
import time
if os.path.exists(target_dir) == False:
os.makedirs(target_dir)
target_path = os.path.join(target_dir, target_filename)
@@ -196,7 +170,8 @@ class ToolBaseFile(object):
@classmethod
def makezip_simple(cls, zip_path, zip_extension='cbz', remove_zip_path=True):
import zipfile, shutil
import shutil
import zipfile
try:
if os.path.exists(zip_path) == False:
return False

View File

@@ -1,8 +1,16 @@
# -*- coding: utf-8 -*-
#########################################################
import os, sys, traceback, subprocess, json, platform
import json
import os
import platform
import subprocess
import sys
import traceback
from framework import app, logger, path_data
from .subprocess import ToolSubprocess
from ..support.base.subprocess import ToolSubprocess
class ToolRclone(object):
@@ -132,4 +140,4 @@ class ToolRclone(object):
return ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error(traceback.format_exc())

View File

@@ -1,120 +0,0 @@
# -*- coding: utf-8 -*-
#########################################################
import os, traceback, subprocess, json
from framework import frame
class ToolSubprocess(object):
@classmethod
def execute_command_return(cls, command, format=None, force_log=False, shell=False, env=None):
try:
#logger.debug('execute_command_return : %s', ' '.join(command))
if frame.config['running_type'] == 'windows':
tmp = []
if type(command) == type([]):
for x in command:
if x.find(' ') == -1:
tmp.append(x)
else:
tmp.append(f'"{x}"')
command = ' '.join(tmp)
iter_arg = ''
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, encoding='utf8')
#process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, encoding='utf8')
ret = []
with process.stdout:
for line in iter(process.stdout.readline, iter_arg):
ret.append(line.strip())
if force_log:
logger.debug(ret[-1])
process.wait() # wait for the subprocess to exit
if format is None:
ret2 = '\n'.join(ret)
elif format == 'json':
try:
index = 0
for idx, tmp in enumerate(ret):
#logger.debug(tmp)
if tmp.startswith('{') or tmp.startswith('['):
index = idx
break
ret2 = json.loads(''.join(ret[index:]))
except:
ret2 = None
return ret2
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error('command : %s', command)
# 2021-10-25
# timeout 적용
@classmethod
def execute_command_return2(cls, command, format=None, force_log=False, shell=False, env=None, timeout=None, uid=0, gid=0, pid_dict=None):
def demote(user_uid, user_gid):
def result():
os.setgid(user_gid)
os.setuid(user_uid)
return result
try:
if app.config['config']['running_type'] == 'windows':
tmp = []
if type(command) == type([]):
for x in command:
if x.find(' ') == -1:
tmp.append(x)
else:
tmp.append(f'"{x}"')
command = ' '.join(tmp)
iter_arg = b'' if app.config['config']['is_py2'] else ''
if app.config['config']['running_type'] == 'windows':
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, encoding='utf8')
else:
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, preexec_fn=demote(uid, gid), encoding='utf8')
new_ret = {'status':'finish', 'log':None}
try:
process_ret = process.wait(timeout=timeout) # wait for the subprocess to exit
except:
import psutil
process = psutil.Process(process.pid)
for proc in process.children(recursive=True):
proc.kill()
process.kill()
new_ret['status'] = "timeout"
ret = []
with process.stdout:
for line in iter(process.stdout.readline, iter_arg):
ret.append(line.strip())
if force_log:
logger.debug(ret[-1])
if format is None:
ret2 = '\n'.join(ret)
elif format == 'json':
try:
index = 0
for idx, tmp in enumerate(ret):
#logger.debug(tmp)
if tmp.startswith('{') or tmp.startswith('['):
index = idx
break
ret2 = json.loads(''.join(ret[index:]))
except:
ret2 = None
new_ret['log'] = ret2
return new_ret
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
logger.error('command : %s', command)