| import os |
| import shutil |
| from datetime import datetime |
| import pytz |
| import subprocess |
| import argparse |
|
|
| |
| beijing_tz = pytz.timezone('Asia/Shanghai') |
| |
| beijing_time = datetime.now(beijing_tz) |
| |
| date_str = beijing_time.strftime('%m%d') |
| |
| |
| target_base_dir = f'/mnt/workspace/{date_str}/' |
|
|
| def create_directories(): |
| |
| os.makedirs(target_base_dir, exist_ok=True) |
| |
| for i in range(1, 11): |
| subdir = f'{i:02d}' |
| target_dir = os.path.join(target_base_dir, subdir) |
| |
| if not os.path.exists(target_dir): |
| os.makedirs(target_dir) |
| print(f'创建文件夹: {target_dir}') |
| else: |
| print(f'文件夹已存在: {target_dir}') |
| |
| |
| description_src = '/mnt/workspace/description.txt' |
| description_dest = os.path.join(target_base_dir, 'description.txt') |
| if os.path.exists(description_src): |
| shutil.copy(description_src, description_dest) |
| print(f'复制文件: {description_src} 到 {description_dest}') |
| else: |
| print(f'文件不存在: {description_src}') |
|
|
| def move_files_by_date(): |
| |
| if not os.path.exists(target_base_dir): |
| print("WRONG") |
| return |
| |
| source_dir = '/mnt/workspace/stable-diffusion-webui/models/Lora/' |
| |
| |
| for i in range(1, 11): |
| seq_num = f'{i:02d}' |
| source_file = os.path.join(source_dir, f'{date_str}_{seq_num}.safetensors') |
| target_dir = os.path.join(target_base_dir, seq_num) |
| |
| |
| if os.path.exists(source_file): |
| os.makedirs(target_dir, exist_ok=True) |
| shutil.move(source_file, target_dir) |
| print(f'{source_file} 移动到 {target_dir}') |
| else: |
| print(f'{source_file} 不存在') |
|
|
| def packup(): |
| zip_command = ['7z', 'a', f'/mnt/workspace/LoraUpload/{date_str}.zip', f'/mnt/workspace/{date_str}'] |
| subprocess.run(zip_command) |
|
|
| def move_upscale(): |
| |
| if not os.path.exists(target_base_dir): |
| create_directories() |
| |
| target_to_move = None |
| |
| |
| for i in range(1, 11): |
| subdir = f'{i:02d}' |
| check_dir = os.path.join(target_base_dir, subdir) |
| upscale_dir = os.path.join(check_dir, 'Like_Upscale') |
| if not os.path.exists(upscale_dir): |
| target_to_move = check_dir |
| break |
| |
| |
| if target_to_move is None: |
| print("FULL") |
| else: |
| |
| source_upscale = '/mnt/workspace/Photo/Like_Upscale' |
| if os.path.exists(source_upscale): |
| shutil.move(source_upscale, target_to_move) |
| print(f'{source_upscale} 移动到 {target_to_move}') |
| else: |
| print(f'{source_upscale} 不存在') |
|
|
| def clear_LoraUpload_model(): |
| lora_upload_dir = '/mnt/workspace/LoraUpload' |
| |
| today_str = datetime.now(beijing_tz).strftime('%m%d') |
|
|
| if os.path.exists(lora_upload_dir): |
| |
| zip_files = sorted( |
| [f for f in os.listdir(lora_upload_dir) if f.endswith('.zip')], |
| reverse=True |
| ) |
| |
| |
| for zip_file in zip_files[1:]: |
| zip_path = os.path.join(lora_upload_dir, zip_file) |
| os.remove(zip_path) |
| print(f'已删除: {zip_path}') |
| else: |
| print(f'{lora_upload_dir} 不存在') |
|
|
| def upload_to_modelscope(): |
| from modelscope.hub.api import HubApi |
| YOUR_ACCESS_TOKEN = '4654205d-1369-44e3-b3ca-d12678e2950d' |
| api = HubApi() |
| api.login(YOUR_ACCESS_TOKEN) |
| api.push_model( |
| model_id="LIBLIBAI/LoraMerge", |
| model_dir="/mnt/workspace/LoraUpload" |
| ) |
| |
| def ClearLike(): |
| path = "/mnt/workspace/Photo/Like" |
| if os.path.exists(path): |
| shutil.rmtree(path) |
| print(f"{path} 已删除") |
| else: |
| print(f"{path} 不存在") |
| |
| def move_and_delete_like_upscale(): |
| base_dir = target_base_dir |
| |
| |
| for i in range(1, 11): |
| subdir = f'{i:02d}' |
| like_upscale_dir = os.path.join(base_dir, subdir, 'Like_Upscale') |
| |
| |
| if os.path.exists(like_upscale_dir) and os.path.isdir(like_upscale_dir): |
| |
| for file_name in os.listdir(like_upscale_dir): |
| file_path = os.path.join(like_upscale_dir, file_name) |
| |
| |
| if os.path.isfile(file_path): |
| |
| shutil.move(file_path, os.path.join(base_dir, subdir)) |
| print(f'已移动: {file_path} 到 {os.path.join(base_dir, subdir)}') |
| |
| |
| os.rmdir(like_upscale_dir) |
| print(f'已删除文件夹: {like_upscale_dir}') |
| else: |
| print(f'{like_upscale_dir} 不存在或不是文件夹') |
|
|
| |
| if __name__ == "__main__": |
| |
| parser = argparse.ArgumentParser(description='执行特定函数的脚本') |
| |
| |
| parser.add_argument('--create_directories', action='store_true', help='执行 create_directories 函数') |
| parser.add_argument('--move_and_delete_like_upscale', action='store_true', help='执行 move_and_delete_like_upscale 函数') |
| parser.add_argument('--ClearLike', action='store_true', help='执行 ClearLike 函数') |
| parser.add_argument('--move_files_by_date', action='store_true', help='执行 move_files_by_date 函数') |
| parser.add_argument('--packup', action='store_true', help='执行 packup 函数') |
| parser.add_argument('--move_upscale', action='store_true', help='执行 move_upscale 函数') |
| parser.add_argument('--clear_LoraUpload_model', action='store_true', help='执行 clear_LoraUpload_model 函数') |
| parser.add_argument('--upload_to_modelscope', action='store_true', help='执行 upload_to_modelscope 函数') |
|
|
| |
| args = parser.parse_args() |
|
|
| |
| if args.create_directories: |
| create_directories() |
| if args.ClearLike: |
| ClearLike() |
| if args.move_files_by_date: |
| move_files_by_date() |
| if args.packup: |
| packup() |
| if args.move_upscale: |
| move_upscale() |
| if args.move_and_delete_like_upscale: |
| move_and_delete_like_upscale() |
| if args.clear_LoraUpload_model: |
| clear_LoraUpload_model() |
| if args.upload_to_modelscope: |
| upload_to_modelscope() |