|
|
@@ -1,7 +1,147 @@
|
|
|
-# 在其他Python文件中导入模块
|
|
|
-import my_module
|
|
|
-my_module.function()
|
|
|
+import os
|
|
|
+import shutil
|
|
|
+from datetime import datetime, timedelta
|
|
|
|
|
|
-# 或从模块中导入特定函数
|
|
|
-from my_module import function
|
|
|
-function()
|
|
|
+def print_sql_files():
|
|
|
+ """返回 Origin 目录下所有 sql 文件的内容字典
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ dict: 字典,key 是文件名,value 是文件内容
|
|
|
+ """
|
|
|
+ # 获取当前文件所在目录
|
|
|
+ current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
+ # 构建 Origin 目录路径
|
|
|
+ origin_dir = os.path.join(current_dir, "Origin")
|
|
|
+
|
|
|
+ # 创建结果字典
|
|
|
+ sql_files_dict = {}
|
|
|
+
|
|
|
+ # 遍历 Origin 目录
|
|
|
+ for file_name in os.listdir(origin_dir):
|
|
|
+ # 检查是否为 .sql 文件
|
|
|
+ if file_name.endswith(".sql"):
|
|
|
+ # 构建完整路径
|
|
|
+ full_path = os.path.join(origin_dir, file_name)
|
|
|
+ # 读取文件内容
|
|
|
+ try:
|
|
|
+ with open(full_path, 'r', encoding='utf-8') as f:
|
|
|
+ content = f.read()
|
|
|
+ sql_files_dict[file_name] = content
|
|
|
+ except Exception as e:
|
|
|
+ print(f"读取文件 {file_name} 时出错: {e}")
|
|
|
+
|
|
|
+ return sql_files_dict
|
|
|
+
|
|
|
+def get_month_data():
|
|
|
+ """获取并返回当前月份和次月等数据
|
|
|
+
|
|
|
+ 返回格式:
|
|
|
+ ["202602", "202603", "202601", "2026-02"]
|
|
|
+ 分别对应:当前月份、次月、上月、当前月份(带分隔符)
|
|
|
+ """
|
|
|
+ # 获取当前日期
|
|
|
+ now = datetime.now()
|
|
|
+
|
|
|
+ # 当前月份 (格式: YYYYMM)
|
|
|
+ current_month = now.strftime("%Y%m")
|
|
|
+
|
|
|
+ # 次月
|
|
|
+ next_month = (now.replace(day=28) + timedelta(days=4)).strftime("%Y%m")
|
|
|
+
|
|
|
+ # 上月
|
|
|
+ last_month = (now.replace(day=1) - timedelta(days=1)).strftime("%Y%m")
|
|
|
+
|
|
|
+ # 当前月份 (带分隔符,格式: YYYY-MM)
|
|
|
+ current_month_with_separator = now.strftime("%Y-%m")
|
|
|
+
|
|
|
+ return [current_month, next_month, last_month, current_month_with_separator]
|
|
|
+
|
|
|
+def ensure_directory(directory_path):
|
|
|
+ """判断指定目录是否存在,不存在则创建,存在则删除里面的内容
|
|
|
+
|
|
|
+ Args:
|
|
|
+ directory_path: 要检查的目录路径
|
|
|
+ """
|
|
|
+ print(f"处理目录: {directory_path}")
|
|
|
+
|
|
|
+ # 检查目录是否存在
|
|
|
+ if not os.path.exists(directory_path):
|
|
|
+ # 目录不存在,创建目录
|
|
|
+ os.makedirs(directory_path)
|
|
|
+ print(f"目录不存在,已创建: {directory_path}")
|
|
|
+ else:
|
|
|
+ # 目录存在,删除里面的内容
|
|
|
+ print(f"目录存在,删除里面的内容")
|
|
|
+ for item in os.listdir(directory_path):
|
|
|
+ item_path = os.path.join(directory_path, item)
|
|
|
+ if os.path.isfile(item_path):
|
|
|
+ os.remove(item_path)
|
|
|
+ print(f"删除文件: {item_path}")
|
|
|
+ elif os.path.isdir(item_path):
|
|
|
+ shutil.rmtree(item_path)
|
|
|
+ print(f"删除目录: {item_path}")
|
|
|
+
|
|
|
+def generate_sql_files():
|
|
|
+ """生成SQL文件
|
|
|
+
|
|
|
+ 步骤:
|
|
|
+ 1. 调用 ensure_directory 确保 SqlOut 目录存在且为空
|
|
|
+ 2. 调用 get_month_data 读取参数列表
|
|
|
+ 3. 调用 print_sql_files 获得文件键值对
|
|
|
+ 4. 遍历步骤3的返回值,每条把结果按照步骤2的返回值进行string替换其中{0}{1}{2}{3}的值
|
|
|
+ 5. 按照key值输出到步骤1所说的目录里
|
|
|
+ """
|
|
|
+ # 步骤1: 确保 SqlOut 目录存在且为空
|
|
|
+ current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
+ sql_out_dir = os.path.join(current_dir, "SqlOut")
|
|
|
+ ensure_directory(sql_out_dir)
|
|
|
+
|
|
|
+ # 步骤2: 读取参数列表
|
|
|
+ month_data = get_month_data()
|
|
|
+ print(f"\n获取到的月份参数: {month_data}")
|
|
|
+
|
|
|
+ # 步骤3: 获得文件键值对
|
|
|
+ sql_files = print_sql_files()
|
|
|
+ print(f"\n获取到 {len(sql_files)} 个 SQL 文件")
|
|
|
+
|
|
|
+ # 步骤4-5: 处理并输出文件
|
|
|
+ print("\n开始处理并输出文件:")
|
|
|
+ for file_name, content in sql_files.items():
|
|
|
+ # 替换内容中的 {0}{1}{2}{3}
|
|
|
+ try:
|
|
|
+ # 先处理文件中可能存在的单独大括号,将它们转义
|
|
|
+ # 然后使用format方法进行格式化
|
|
|
+ # 注意:需要先将文件中的单独大括号转义为双大括号
|
|
|
+ # 这样format方法就不会将它们视为占位符
|
|
|
+ # 但要注意不要影响现有的{0}{1}{2}{3}占位符
|
|
|
+
|
|
|
+ # 先保存现有的占位符
|
|
|
+ temp_content = content
|
|
|
+ placeholders = {}
|
|
|
+ for i in range(4):
|
|
|
+ placeholder = f"{{{i}}}"
|
|
|
+ temp_content = temp_content.replace(placeholder, f"__PLACEHOLDER_{i}__")
|
|
|
+
|
|
|
+ # 转义单独的大括号
|
|
|
+ temp_content = temp_content.replace("{", "{{").replace("}", "}}")
|
|
|
+
|
|
|
+ # 恢复占位符
|
|
|
+ for i in range(4):
|
|
|
+ temp_content = temp_content.replace(f"__PLACEHOLDER_{i}__", f"{{{i}}}")
|
|
|
+
|
|
|
+ # 使用format方法进行格式化
|
|
|
+ replaced_content = temp_content.format(*month_data)
|
|
|
+
|
|
|
+ # 构建输出文件路径
|
|
|
+ output_path = os.path.join(sql_out_dir, file_name)
|
|
|
+
|
|
|
+ # 写入文件
|
|
|
+ with open(output_path, 'w', encoding='utf-8') as f:
|
|
|
+ f.write(replaced_content)
|
|
|
+
|
|
|
+ print(f"成功输出文件: {file_name}")
|
|
|
+ except Exception as e:
|
|
|
+ print(f"处理文件 {file_name} 时出错: {e}")
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ generate_sql_files()
|