Files
user_service/build.py
2025-10-03 16:39:24 +08:00

140 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import shutil
# 1. 定义全局参数
a = "gateway" # 功能名
b = "" # 次功能名
c = "20000" # 端口号
def main():
# 确定新目录名称
if b:
new_dir = f"api_{a}_{b}"
else:
new_dir = f"api_{a}"
# 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-6. 处理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}")
# 替换端口号"20001"
content = content.replace("20001", c)
with open(new_yaml, 'w', encoding='utf-8') as f:
f.write(content)
print(f"已更新yaml文件内容")
else:
print(f"警告:未找到 {temp_yaml} 文件跳过yaml处理")
# 7-9. 处理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}-")
# 替换端口号"20001"
content = content.replace("20001", c)
with open(readme_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"已更新README.md内容")
else:
print(f"警告:未找到 {readme_path} 文件跳过README处理")
# 10. 处理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处理")
# 11-13. 处理depend.py
depend_path = os.path.join(new_dir, "depend.py")
if os.path.exists(depend_path):
with open(depend_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} &&")
# 替换端口号"20001"
content = content.replace("20001", c)
with open(depend_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"已更新depend.py内容")
else:
print(f"警告:未找到 {depend_path} 文件跳过depend.py处理")
if __name__ == "__main__":
main()