136 lines
4.4 KiB
Python
136 lines
4.4 KiB
Python
import os
|
||
import shutil
|
||
|
||
# 1. 定义全局参数
|
||
a = "gateway1" # 功能名
|
||
b = "" # 次功能名
|
||
|
||
def main():
|
||
# 确定新目录名称
|
||
if b:
|
||
new_dir_name = f"api_{a}_{b}"
|
||
else:
|
||
new_dir_name = f"api_{a}"
|
||
|
||
# 目标目录调整为当前目录的deploy/api
|
||
new_dir = os.path.join("deploy", "api", new_dir_name)
|
||
|
||
# 确保deploy/api目录存在
|
||
os.makedirs(os.path.join("deploy", "api"), exist_ok=True)
|
||
|
||
# 2. 复制并重命名api_template目录
|
||
source_dir = "api_template"
|
||
if not os.path.exists(source_dir):
|
||
print(f"错误:源目录 {source_dir} 不存在")
|
||
return
|
||
|
||
if os.path.exists(new_dir):
|
||
print(f"警告:目标目录 {new_dir} 已存在,将被覆盖")
|
||
shutil.rmtree(new_dir)
|
||
|
||
shutil.copytree(source_dir, new_dir)
|
||
print(f"已创建目录:{new_dir}")
|
||
|
||
# 确定新yaml文件名
|
||
if b:
|
||
yaml_filename = f"docker-compose.{a}.{b}.yaml"
|
||
else:
|
||
yaml_filename = f"docker-compose.{a}.yaml"
|
||
|
||
# 3. 重命名docker-compose.temp.yaml
|
||
temp_yaml = os.path.join(new_dir, "docker-compose.temp.yaml")
|
||
new_yaml = os.path.join(new_dir, yaml_filename)
|
||
if os.path.exists(temp_yaml):
|
||
os.rename(temp_yaml, new_yaml)
|
||
print(f"已重命名yaml文件:{yaml_filename}")
|
||
|
||
# 4-5. 处理yaml文件内容(移除端口号修改)
|
||
with open(new_yaml, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# 替换"template-api"
|
||
if b:
|
||
content = content.replace("template-api", f"{a}-{b}-api")
|
||
else:
|
||
content = content.replace("template-api", f"{a}-api")
|
||
|
||
# 替换"_template"
|
||
if b:
|
||
content = content.replace("_template", f"_{a}_{b}")
|
||
else:
|
||
content = content.replace("_template", f"_{a}")
|
||
|
||
with open(new_yaml, 'w', encoding='utf-8') as f:
|
||
f.write(content)
|
||
print(f"已更新yaml文件内容")
|
||
else:
|
||
print(f"警告:未找到 {temp_yaml} 文件,跳过yaml处理")
|
||
|
||
# 6-8. 处理README.md(移除端口号修改)
|
||
readme_path = os.path.join(new_dir, "README.md")
|
||
if os.path.exists(readme_path):
|
||
with open(readme_path, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# 替换"template_"
|
||
if b:
|
||
content = content.replace("template_", f"{a}_{b}_")
|
||
else:
|
||
content = content.replace("template_", f"{a}_")
|
||
|
||
# 替换"template-"
|
||
if b:
|
||
content = content.replace("template-", f"{a}-{b}-")
|
||
else:
|
||
content = content.replace("template-", f"{a}-")
|
||
|
||
with open(readme_path, 'w', encoding='utf-8') as f:
|
||
f.write(content)
|
||
print(f"已更新README.md内容")
|
||
else:
|
||
print(f"警告:未找到 {readme_path} 文件,跳过README处理")
|
||
|
||
# 9. 处理release.sh
|
||
release_path = os.path.join(new_dir, "release.sh")
|
||
if os.path.exists(release_path):
|
||
with open(release_path, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# 替换"template"
|
||
if b:
|
||
content = content.replace("template", f"{a}-{b}")
|
||
else:
|
||
content = content.replace("template", f"{a}")
|
||
|
||
with open(release_path, 'w', encoding='utf-8') as f:
|
||
f.write(content)
|
||
print(f"已更新release.sh内容")
|
||
else:
|
||
print(f"警告:未找到 {release_path} 文件,跳过release.sh处理")
|
||
|
||
# 10-11. 处理init.py(移除端口号修改)
|
||
init_path = os.path.join(new_dir, "init.py")
|
||
if os.path.exists(init_path):
|
||
with open(init_path, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# 替换"_template_"
|
||
if b:
|
||
content = content.replace("_template_", f"_{a}_{b}_")
|
||
else:
|
||
content = content.replace("_template_", f"_{a}_")
|
||
|
||
# 替换"template &&"
|
||
if b:
|
||
content = content.replace("template &&", f"{a}_{b} &&")
|
||
else:
|
||
content = content.replace("template &&", f"{a} &&")
|
||
|
||
with open(init_path, 'w', encoding='utf-8') as f:
|
||
f.write(content)
|
||
print(f"已更新init.py内容")
|
||
else:
|
||
print(f"警告:未找到 {init_path} 文件,跳过init.py处理")
|
||
|
||
if __name__ == "__main__":
|
||
main() |