182 lines
4.9 KiB
Python
182 lines
4.9 KiB
Python
import os
|
|
import rarfile
|
|
import zipfile
|
|
import shutil
|
|
import difflib
|
|
|
|
img_exts = [".jpg",".png",".jpeg",".webp"]
|
|
TrgBasePath = "/Volumes/NewDataStor/Backup/files/"
|
|
m_Debug = False
|
|
|
|
def main(debug=False):
|
|
m_Debug = debug
|
|
|
|
TrgBasePath = os.path.abspath("/Volumes/NewDataStor/Backup/files/")
|
|
|
|
# 설정한 경로가 유효한가?
|
|
if False == os.path.exists(TrgBasePath):
|
|
print("Not Valid Path")
|
|
return
|
|
|
|
# 압축을 죄다 푼다.
|
|
# -> 일단 이 과정은 생략
|
|
|
|
# 폴더만 죄다 골라내서..
|
|
listTrgPaths = ListSubDirectories(TrgBasePath)
|
|
|
|
dbgmsg(listTrgPaths)
|
|
for path in listTrgPaths:
|
|
#이미지 파일만 골라 리스트업.
|
|
contents = os.listdir(path)
|
|
listImgFiles = []
|
|
|
|
for item in contents:
|
|
if True == os.path.isdir(item):
|
|
continue
|
|
|
|
ext = get_extension_str(item)
|
|
if ext in img_exts:
|
|
if os.path.exists(item):
|
|
listImgFiles.append(item)
|
|
else:
|
|
strTemp = os.path.join(path, item)
|
|
listImgFiles.append(strTemp)
|
|
|
|
dbgmsg(listImgFiles)
|
|
|
|
if 0 >= len(listImgFiles):
|
|
continue
|
|
|
|
# 폴더 이름을 조합해서 파일 이름을 만든다.
|
|
strArcName = MakeCBZName(path) + ".cbz"
|
|
strArcPath = os.path.join(TrgBasePath, strArcName)
|
|
|
|
# 이미 있으면 패스
|
|
if os.path.exists(strArcName):
|
|
continue
|
|
else:
|
|
CreateZIP(strArcPath, *listImgFiles)
|
|
|
|
|
|
# 폴더를 압축한다.
|
|
# 폴더 안의 파일을 정리한다. .url, .txt, .db 뺸다
|
|
# 일단 test.zip 로 압축하고, 원래 폴더 이름으로 변경
|
|
# 만약 작가폴더 안에 작품이 들어있다면...
|
|
# 이걸 알 수는 없으니 그냥 앞에 대괄호 넣고 폴더 이름을 붙인다.
|
|
# 가능하면 하위 폴더 이름에 작가 이름이 들어있다면 그냥 생략하자. 가능하다면...
|
|
|
|
|
|
def MakeCBZName(path):
|
|
strSrcPath = os.path.abspath(path)
|
|
|
|
if len(strSrcPath) < len(TrgBasePath) or TrgBasePath == strSrcPath:
|
|
return strSrcPath.replace("/", "-")
|
|
|
|
strTemp = RmvSubString(strSrcPath, TrgBasePath)
|
|
listPar = strTemp.split(os.sep)
|
|
|
|
strRet = listPar.pop();
|
|
for item in reversed(listPar):
|
|
IdxTmp = strRet.find(item)
|
|
if IdxTmp == -1:
|
|
strRet = "[" + item + "]" + strRet
|
|
else:
|
|
continue
|
|
|
|
return strRet
|
|
|
|
#
|
|
def RmvSubString(main_string, substring):
|
|
# 문자열에서 부분 문자열의 인덱스를 찾습니다.
|
|
start_index = main_string.find(substring)
|
|
if start_index == -1: # 부분 문자열이 존재하지 않으면 그대로 반환합니다.
|
|
return main_string
|
|
end_index = start_index + len(substring)
|
|
|
|
# 부분 문자열을 제거하고 새로운 문자열을 반환합니다.
|
|
return main_string[:start_index] + main_string[end_index:]
|
|
|
|
#
|
|
def dbgmsg(string):
|
|
if True == m_Debug:
|
|
print(string)
|
|
|
|
#
|
|
def ExtractRAR(file_path, extract_path):
|
|
with rarfile.RarFile(file_path, 'r') as rf:
|
|
rf.extractall(extract_path)
|
|
|
|
#
|
|
def ExtractZIP(zip_file, extract_to):
|
|
with zipfile.ZipFile(zip_file, 'r') as zf:
|
|
zf.extractall(extract_to)
|
|
|
|
#
|
|
def CreateZIP(output_zip, *files):
|
|
with zipfile.ZipFile(output_zip, 'w') as zf:
|
|
for file in files:
|
|
zf.write(file, os.path.basename(file))
|
|
|
|
bRet = False
|
|
if os.path.exists(output_zip):
|
|
bRet = True
|
|
|
|
return bRet
|
|
|
|
#
|
|
def ListSubDirectories(root_dir):
|
|
subdirectories = []
|
|
|
|
# root_dir에서 하위 디렉토리 및 파일 목록을 얻음
|
|
for dirpath, dirnames, filenames in os.walk(root_dir):
|
|
# 하위 디렉토리 목록을 반복하며 하위 디렉토리만 추출
|
|
for dirname in dirnames:
|
|
path = os.path.join(dirpath, dirname)
|
|
|
|
if True == IsFinalFolder(path):
|
|
subdirectories.append(path)
|
|
|
|
return subdirectories
|
|
|
|
#
|
|
def IsFinalFolder(path):
|
|
bRet = True
|
|
|
|
contents = os.listdir(path)
|
|
for item in contents:
|
|
if True == os.path.isdir(item):
|
|
bRt = False
|
|
break
|
|
|
|
return bRet;
|
|
|
|
#
|
|
def get_extension_str(file_path):
|
|
# 파일 경로에서 마지막 점을 찾아 확장자를 추출
|
|
last_dot_index = file_path.rfind('.')
|
|
if last_dot_index == -1:
|
|
return "" # 점이 없는 경우 확장자가 없음
|
|
else:
|
|
return file_path[last_dot_index:]
|
|
|
|
#
|
|
def isRAR(file):
|
|
if True == os.path.isdir(file):
|
|
return False
|
|
|
|
if ".rar" != get_extension_str(file):
|
|
return False
|
|
|
|
return True
|
|
|
|
#
|
|
def ReExt(path, srcExt, trgExt):
|
|
nCnt = -1
|
|
|
|
return nCnt
|
|
|
|
|
|
# For Main Loop
|
|
if __name__ == '__main__':
|
|
main(False)
|