156 lines
6.0 KiB
Python
156 lines
6.0 KiB
Python
import os
|
||
|
||
|
||
def creat_sh(input_folder, anion, sh_file_path='analyze.sh'):
|
||
"""
|
||
创建shell脚本,只处理两类CIF文件:
|
||
1. 纯数字命名的CIF文件 (例如: 123.cif)
|
||
2. 数字-坐标格式的CIF文件 (例如: 123-x1y2z3.cif)
|
||
|
||
参数:
|
||
input_folder: 输入文件夹路径
|
||
anion: 阴离子类型
|
||
sh_file_path: 生成的shell脚本路径
|
||
"""
|
||
# 文件夹路径
|
||
folder_path = input_folder
|
||
|
||
import re
|
||
|
||
# 定义两种文件名模式的正则表达式
|
||
pattern1 = re.compile(r'^\d+\.cif$') # 纯数字.cif
|
||
pattern2 = re.compile(r'^\d+-x\d+y\d+z\d+\.cif$') # 数字-x数字y数字z数字.cif
|
||
|
||
# 打开SH脚本文件用于写入
|
||
with open(sh_file_path, 'w') as sh_file:
|
||
# 写入脚本头部
|
||
sh_file.write('#!/bin/bash\n')
|
||
|
||
# 遍历文件夹中的所有文件
|
||
for filename in os.listdir(folder_path):
|
||
file_path = os.path.join(folder_path, filename)
|
||
|
||
# 只处理文件(不处理文件夹)
|
||
if os.path.isfile(file_path):
|
||
# 检查文件名是否匹配两种模式之一
|
||
if pattern1.match(filename) or pattern2.match(filename):
|
||
# 生成对应的命令
|
||
command = f"python ../../../tool/analyze_voronoi_nodes.py {filename} -i ../../../tool/{anion}.yaml > {filename}.txt\n"
|
||
# 将命令写入SH脚本文件
|
||
sh_file.write(command)
|
||
|
||
print(f"SH脚本已生成:{sh_file_path}")
|
||
|
||
|
||
import os
|
||
|
||
|
||
def create_sh_recursive(base_folder, tool_path="tool", relative_depth=2):
|
||
"""
|
||
递归遍历文件夹,为每个包含.cif文件的文件夹生成analyze.sh脚本,
|
||
并在基础文件夹下创建一个sh_all.sh来执行所有脚本。
|
||
|
||
参数:
|
||
base_folder: 起始文件夹路径
|
||
tool_path: 工具目录的基本路径
|
||
relative_depth: 基础相对深度,用于计算正确的相对路径
|
||
"""
|
||
# 用于收集所有生成的analyze.sh脚本的相对路径
|
||
analyze_sh_paths = []
|
||
base_folder_name = os.path.basename(base_folder)
|
||
|
||
def process_folder(folder_path, current_depth=0):
|
||
print(f"处理文件夹: {folder_path}")
|
||
|
||
# 获取当前文件夹名称
|
||
folder_name = os.path.basename(folder_path)
|
||
|
||
# 检查当前文件夹是否包含.cif文件
|
||
has_cif_files = any(
|
||
f.endswith('.cif') for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)))
|
||
|
||
# 如果当前文件夹包含.cif文件,生成脚本
|
||
if has_cif_files:
|
||
# 计算正确的工具路径(根据深度增加../)
|
||
dots = "../" * (relative_depth + current_depth)
|
||
tool_relative_path = f"{dots}{tool_path}"
|
||
|
||
# 确定anion参数(使用文件夹名)
|
||
anion = folder_name
|
||
|
||
# 生成脚本文件路径
|
||
sh_file_path = os.path.join(folder_path, "analyze.sh")
|
||
|
||
# 创建脚本
|
||
with open(sh_file_path, 'w') as sh_file:
|
||
sh_file.write('#!/bin/bash\n')
|
||
for filename in os.listdir(folder_path):
|
||
file_path = os.path.join(folder_path, filename)
|
||
if os.path.isfile(file_path) and filename.endswith('.cif'):
|
||
command = f"python {tool_relative_path}/analyze_voronoi_nodes.py {filename} -i {tool_relative_path}/{anion}.yaml > {filename}.txt\n"
|
||
sh_file.write(command)
|
||
|
||
# 将此脚本添加到收集器中
|
||
# 计算相对于基础文件夹的路径
|
||
rel_path = os.path.relpath(folder_path, base_folder)
|
||
analyze_sh_paths.append(rel_path)
|
||
|
||
print(f"生成脚本: {sh_file_path} (工具路径: {tool_relative_path})")
|
||
|
||
# 获取子文件夹列表
|
||
subdirs = [d for d in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, d))]
|
||
|
||
# 处理复合阴离子文件夹的特殊情况
|
||
if "+" in folder_name:
|
||
elements = folder_name.split("+")
|
||
for element in elements:
|
||
element_dir = os.path.join(folder_path, element)
|
||
# 如果对应元素的子文件夹不存在,创建它
|
||
if not os.path.exists(element_dir):
|
||
os.makedirs(element_dir)
|
||
print(f"创建子文件夹: {element_dir}")
|
||
# 确保这个子文件夹被包含在递归处理列表中
|
||
if element not in subdirs:
|
||
subdirs.append(element)
|
||
|
||
# 递归处理所有子文件夹
|
||
for subdir in subdirs:
|
||
subdir_path = os.path.join(folder_path, subdir)
|
||
process_folder(subdir_path, current_depth + 1)
|
||
|
||
# 开始递归处理
|
||
process_folder(base_folder)
|
||
|
||
# 创建sh_all.sh脚本
|
||
sh_all_path = os.path.join(base_folder, "sh_all.sh")
|
||
with open(sh_all_path, 'w') as sh_all:
|
||
sh_all.write('#!/bin/bash\n\n')
|
||
sh_all.write(f'# process all analyze.sh\n\n')
|
||
|
||
# 记录初始目录
|
||
sh_all.write('# remember current dir\n')
|
||
sh_all.write('INITIAL_DIR=$(pwd)\n\n')
|
||
|
||
# 为每个包含analyze.sh的目录添加命令
|
||
for path in analyze_sh_paths:
|
||
sh_all.write(f'echo "process {path}/analyze.sh"\n')
|
||
sh_all.write(f'cd "{path}"\n')
|
||
sh_all.write('bash analyze.sh\n')
|
||
sh_all.write(f'cd "$INITIAL_DIR"\n\n')
|
||
|
||
# 添加完成消息
|
||
sh_all.write('echo "done!"\n')
|
||
|
||
# 修改权限使脚本可执行
|
||
os.chmod(sh_all_path, 0o755)
|
||
print(f"生成总执行脚本: {sh_all_path}")
|
||
print("所有脚本生成完成!")
|
||
# 示例调用
|
||
# create_sh_recursive("../data/after_step1")
|
||
|
||
if __name__ == '__main__':
|
||
# creat_sh("../data/after_step1/O","O","../data/after_step1/O/analyze.sh")
|
||
# creat_sh("../data/after_step1/S","S","../data/after_step1/S/analyze.sh")
|
||
# creat_sh("../data/after_step1/Cl","Cl","../data/after_step1/Cl/analyze.sh")
|
||
# creat_sh("../data/after_step1/Br","Br","../data/after_step1/Br/analyze.sh")
|
||
create_sh_recursive("../data/after_step1") |