Skip to content

Commit

Permalink
add 增加同步排除项规则与判断方法-后端
Browse files Browse the repository at this point in the history
  • Loading branch information
dr34m-cn committed Sep 23, 2024
1 parent 7d8c790 commit 8d6110f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
3 changes: 1 addition & 2 deletions common/sqlInit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def init_sql(conn):
"second text DEFAULT NULL," # 0-59
"start_date text DEFAULT NULL," # 开始时间
"end_date text DEFAULT NULL," # 结束时间
"exclude text DEFAULT NULL," # 排除无需同步项,以类型-值,英文冒号分隔,示例:0-path/:1-.
# 类型:0-全匹配,1-开头,2-任意包含,3-结尾
"exclude text DEFAULT NULL," # 排除无需同步项,类似gitignore语法,英文冒号分隔多个规则
"createTime integer DEFAULT (strftime('%s', 'now')),"
" unique (srcPath, dstPath, alistId))")
cursor.execute("create table job_task("
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tornado==6.4.1
requests==2.32.3
APScheduler==3.10.4
configparser==7.1.0
configparser==7.1.0
igittigitt==2.1.4
45 changes: 34 additions & 11 deletions service/alist/alistClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@
from common.LNG import G


def checkExs(path, rts, parser):
"""
检查并排除排除项
:param path: 所在路径
:param rts: 内容列表,例如
{
"test1-1/": {}, # key以/结尾表示目录
"test1.txt": 4 # 不以/结尾,表示文件,存文件大小
}
:param parser: 排除规则
:return: 排除后的内容列表
"""

rtsNew = rts.copy()
for rtsItem in rts.keys():
if parser.match(path + rtsItem):
del rtsNew[rtsItem]
return rtsNew


class AlistClient:
def __init__(self, url, token, alistId=None):
"""
Expand Down Expand Up @@ -95,20 +115,17 @@ def updateAlistId(self, alistId):
"""
self.alistId = alistId

def fileListApi(self, path, speed=0, excludes=None):
def fileListApi(self, path, speed=0, parser=None, rootPath=None):
"""
目录列表
:param path: 目录
:param speed: 速度,0-标准,1-快速,2-低速
:param excludes: 排除项
[{
'type': 0, # 0-全匹配,1-开头,2-任意包含,3-结尾
'val': 'xx'
}]
:param parser: 排除项规则
:return: {
"test1-1/": {}, # key以/结尾表示目录
"test1.txt": 4 # 不以/结尾,表示文件,存文件大小
}
:param rootPath: 同步根目录
"""
if speed == 2:
time.sleep(3)
Expand All @@ -123,7 +140,10 @@ def fileListApi(self, path, speed=0, excludes=None):
}
else:
rts = {}
# TODO 排除项判断
if parser and rts:
if rootPath is None:
rootPath = path
rts = checkExs('/' + path[len(rootPath):], rts, parser)
return rts

def filePathList(self, path):
Expand All @@ -141,12 +161,13 @@ def filePathList(self, path):
else:
return []

def allFileList(self, path, speed=0, excludes=None):
def allFileList(self, path, speed=0, parser=None, rootPath=None):
"""
递归获取文件列表
:param path: 根路径
:param speed: 速度,0-标准,1-快速,2-低速
:param excludes: 排除项
:param parser: 排除项规则
:param rootPath: 同步根目录
:return: {
"test1-1/": {
"test1-3/": {
Expand All @@ -157,10 +178,12 @@ def allFileList(self, path, speed=0, excludes=None):
"test1.txt": 4
}
"""
fList = self.fileListApi(path, speed, excludes)
if rootPath is None:
rootPath = path
fList = self.fileListApi(path, speed, parser, rootPath)
for key in fList.keys():
if key.endswith('/'):
fList[key] = self.allFileList(f"{path}/{key[:-1]}", speed)
fList[key] = self.allFileList(f"{path}/{key[:-1]}", speed, parser, rootPath)
return fList

def deleteFile(self, path, names=None):
Expand Down
19 changes: 8 additions & 11 deletions service/alist/alistSync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"""
import logging

import igittigitt

from service.alist.alistService import getClientById


Expand Down Expand Up @@ -99,25 +101,20 @@ def sync(srcPath, dstPath, alistId, speed=0, method=0, copyHook=None, delHook=No
:param job: 作业
"""
jobExclude = job['exclude']
excludes = []
parser = None
if jobExclude is not None and jobExclude.strip() != '':
jobExcludeList = jobExclude.split(':')
for item in jobExcludeList:
itemSplit = item.split('-', maxsplit=1)
if len(itemSplit) == 2:
excludes.append({
'type': itemSplit[0],
'val': itemSplit[1]
})
parser = igittigitt.IgnoreParser()
for exItem in jobExclude.split(':'):
parser.add_rule(exItem, '/')
client = getClientById(alistId)
if not srcPath.endswith('/'):
srcPath = srcPath + '/'
srcFiles = client.allFileList(srcPath, excludes)
srcFiles = client.allFileList(srcPath, parser=parser)
dstPathList = dstPath.split(':')
for dstItem in dstPathList:
if not dstItem.endswith('/'):
dstItem = dstItem + '/'
dstFiles = client.allFileList(dstItem, speed, excludes)
dstFiles = client.allFileList(dstItem, speed, parser=parser)
needCopy = getSrcMore(srcFiles, dstFiles)
if job is not None and job['enable'] == 0:
return
Expand Down

0 comments on commit 8d6110f

Please sign in to comment.