#!/usr/bin/env python # coding=utf-8 """根据表名从 DataWorks 获取生产代码,保存到 production_code/ 目录。 用法: python fetch_table_code.py loghubods.dwd_recsys_alg_exposure_base_20250108 python fetch_table_code.py loghubods.dwd_recsys_alg_exposure_base_20250108 --force python fetch_table_code.py loghubods.dwd_recsys_alg_exposure_base_20250108 --recursive python fetch_table_code.py loghubods.dwd_recsys_alg_exposure_base_20250108 --recursive --depth 5 """ import sys import argparse def main(): parser = argparse.ArgumentParser(description="获取表的 DataWorks 生产代码") parser.add_argument("table_name", help="表名,格式: project.table 或 table") parser.add_argument("--force", action="store_true", help="跳过缓存,强制从 API 拉取") parser.add_argument("--recursive", "-r", action="store_true", help="递归获取所有上游表的代码") parser.add_argument("--depth", type=int, default=3, help="递归最大深度(默认 3)") args = parser.parse_args() from lib.odps_module import DataWorksClient dw = DataWorksClient() if args.recursive: dw.get_node_code_recursive(args.table_name, max_depth=args.depth, force=args.force) else: results = dw.get_node_code(args.table_name, force=args.force) if not results: print(f"未找到 '{args.table_name}' 的生产代码") sys.exit(1) for r in results: print(f"任务: {r['task_name']} 代码长度: {len(r['content'])} chars") if __name__ == "__main__": main()