action_utils.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import re
  3. from file_processer import replace_file_by_path
  4. from utils.module_utils import get_modules_from_settings_file
  5. from utils.other_utils import unify_path_separator, get_excluded_dirs
  6. parse_included_files = set()
  7. def replace_action(project_dir: str, included_files: set, included_pattern: set, included_keywords: set):
  8. print("任务开始执行-------------------------------------------\n")
  9. modules = get_modules_from_settings_file(project_dir)
  10. result_paths = set()
  11. for module in modules:
  12. print("-------------------------------------------\n")
  13. module_dir = project_dir + os.path.sep + module.replace(":", os.path.sep)
  14. print(f"检测{module}模块:{module_dir}")
  15. for root, dirs, files in os.walk(module_dir):
  16. excluded_dirs = get_excluded_dirs()
  17. dirs[:] = [d for d in dirs if d not in excluded_dirs]
  18. for file in files:
  19. path = os.path.join(root, file)
  20. file_path = unify_path_separator(path)
  21. # 是否指定文件名称
  22. file_name = os.path.basename(file_path)
  23. if file_name in included_files:
  24. result_paths.add(file_path)
  25. # 是否指定正则表达式
  26. for keyword in included_pattern:
  27. pattern = re.compile(keyword)
  28. if pattern.search(file_path):
  29. result_paths.add(file_path)
  30. # 指定关键字
  31. for keyword in included_keywords:
  32. tmp = unify_path_separator(keyword)
  33. if tmp in file_path:
  34. result_paths.add(file_path)
  35. for path in result_paths:
  36. # 处理文件
  37. print(f"处理文件{path}")
  38. replace_file_by_path(path, lambda layout_name: {
  39. parse_included_files.add(layout_name)
  40. })
  41. if parse_included_files.__len__() != 0:
  42. print(f"处理遍历进来的布局文件{parse_included_files}")
  43. temp = parse_included_files.copy()
  44. parse_included_files.clear()
  45. replace_action(project_dir, temp, set(), set())