linter.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import os
  2. import os.path as osp
  3. import pathlib
  4. import subprocess
  5. def recursively_lint_files():
  6. """Recursively lint all python files in chosen subdirectories of megatron-lm"""
  7. try:
  8. import autopep8
  9. except ModuleNotFoundError:
  10. print("Please first install autopep8 via `pip install autopep8`")
  11. return
  12. # get all python file paths from top level directory
  13. file_dir = str(pathlib.Path(__file__).parent.absolute())
  14. working_dir = osp.join(file_dir, os.pardir)
  15. all_py_paths = set(os.path.join(working_dir, fname)
  16. for fname in os.listdir(working_dir) if ".py" in fname)
  17. # get all python file paths from chosen subdirectories
  18. check_dirs = ['docker', 'megatron', 'openwebtext', 'scripts', 'tasks']
  19. for sub_dir in check_dirs:
  20. for path, _, fnames in os.walk(osp.join(working_dir, sub_dir)):
  21. all_py_paths.update(set(osp.join(path, fname) for fname in fnames if ".py" in fname))
  22. print("Linting the following: ")
  23. for py_path in all_py_paths:
  24. print(py_path)
  25. command = 'autopep8 --max-line-length 100 --aggressive --in-place {}'.format(py_path)
  26. subprocess.check_call(command)
  27. if __name__ == "__main__":
  28. recursively_lint_files()