lint.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. import os
  3. import re
  4. import shutil
  5. import subprocess
  6. import multiprocessing
  7. from flipper.app import App
  8. SOURCE_CODE_FILE_EXTENSIONS = [".h", ".c", ".cpp", ".cxx", ".hpp"]
  9. SOURCE_CODE_FILE_PATTERN = r"^[0-9A-Za-z_]+\.[a-z]+$"
  10. SOURCE_CODE_DIR_PATTERN = r"^[0-9A-Za-z_]+$"
  11. class Main(App):
  12. def init(self):
  13. self.subparsers = self.parser.add_subparsers(help="sub-command help")
  14. # generate
  15. self.parser_check = self.subparsers.add_parser(
  16. "check", help="Check source code format and file names"
  17. )
  18. self.parser_check.add_argument("input", nargs="+")
  19. self.parser_check.set_defaults(func=self.check)
  20. # merge
  21. self.parser_format = self.subparsers.add_parser(
  22. "format", help="Format source code and fix file names"
  23. )
  24. self.parser_format.add_argument(
  25. "input",
  26. nargs="+",
  27. )
  28. self.parser_format.set_defaults(func=self.format)
  29. def _check_folders(self, folders: list):
  30. show_message = False
  31. pattern = re.compile(SOURCE_CODE_DIR_PATTERN)
  32. for folder in folders:
  33. for dirpath, dirnames, filenames in os.walk(folder):
  34. for dirname in dirnames:
  35. if not pattern.match(dirname):
  36. to_fix = os.path.join(dirpath, dirname)
  37. self.logger.warning(f"Found incorrectly named folder {to_fix}")
  38. show_message = True
  39. if show_message:
  40. self.logger.warning(
  41. f"Folders are not renamed automatically, please fix it by yourself"
  42. )
  43. def _find_sources(self, folders: list):
  44. output = []
  45. for folder in folders:
  46. for dirpath, dirnames, filenames in os.walk(folder):
  47. for filename in filenames:
  48. ext = os.path.splitext(filename.lower())[1]
  49. if not ext in SOURCE_CODE_FILE_EXTENSIONS:
  50. continue
  51. output.append(os.path.join(dirpath, filename))
  52. return output
  53. @staticmethod
  54. def _format_source(task):
  55. try:
  56. subprocess.check_call(task)
  57. return True
  58. except subprocess.CalledProcessError as e:
  59. return False
  60. def _format_sources(self, sources: list, dry_run: bool = False):
  61. args = ["clang-format", "--Werror", "--style=file", "-i"]
  62. if dry_run:
  63. args.append("--dry-run")
  64. files_per_task = 69
  65. tasks = []
  66. while len(sources) > 0:
  67. tasks.append(args + sources[:files_per_task])
  68. sources = sources[files_per_task:]
  69. pool = multiprocessing.Pool()
  70. results = pool.map(self._format_source, tasks)
  71. return all(results)
  72. def _fix_filename(self, filename: str):
  73. return filename.replace("-", "_")
  74. def _replace_occurrence(self, sources: list, old: str, new: str):
  75. old = old.encode()
  76. new = new.encode()
  77. for source in sources:
  78. content = open(source, "rb").read()
  79. if content.count(old) > 0:
  80. self.logger.info(f"Replacing {old} with {new} in {source}")
  81. content = content.replace(old, new)
  82. open(source, "wb").write(content)
  83. def _apply_file_naming_convention(self, sources: list, dry_run: bool = False):
  84. pattern = re.compile(SOURCE_CODE_FILE_PATTERN)
  85. good = []
  86. bad = []
  87. # Check sources for invalid filenames
  88. for source in sources:
  89. basename = os.path.basename(source)
  90. if not pattern.match(basename):
  91. new_basename = self._fix_filename(basename)
  92. if not pattern.match(new_basename):
  93. self.logger.error(f"Unable to fix name for {basename}")
  94. return False
  95. bad.append((source, basename, new_basename))
  96. else:
  97. good.append(source)
  98. # Notify about errors or replace all occurrences
  99. if dry_run:
  100. if len(bad) > 0:
  101. self.logger.error(f"Found {len(bad)} incorrectly named files")
  102. self.logger.info(bad)
  103. return False
  104. else:
  105. # Replace occurrences in text files
  106. for source, old, new in bad:
  107. self._replace_occurrence(sources, old, new)
  108. # Rename files
  109. for source, old, new in bad:
  110. shutil.move(source, source.replace(old, new))
  111. return True
  112. def _apply_file_permissions(self, sources: list, dry_run: bool = False):
  113. execute_permissions = 0o111
  114. pattern = re.compile(SOURCE_CODE_FILE_PATTERN)
  115. good = []
  116. bad = []
  117. # Check sources for unexpected execute permissions
  118. for source in sources:
  119. st = os.stat(source)
  120. perms_too_many = st.st_mode & execute_permissions
  121. if perms_too_many:
  122. good_perms = st.st_mode & ~perms_too_many
  123. bad.append((source, oct(perms_too_many), good_perms))
  124. else:
  125. good.append(source)
  126. # Notify or fix
  127. if dry_run:
  128. if len(bad) > 0:
  129. self.logger.error(f"Found {len(bad)} incorrect permissions")
  130. self.logger.info([record[0:2] for record in bad])
  131. return False
  132. else:
  133. for source, perms_too_many, new_perms in bad:
  134. os.chmod(source, new_perms)
  135. return True
  136. def _perform(self, dry_run: bool):
  137. result = 0
  138. sources = self._find_sources(self.args.input)
  139. if not self._format_sources(sources, dry_run=dry_run):
  140. result |= 0b001
  141. if not self._apply_file_naming_convention(sources, dry_run=dry_run):
  142. result |= 0b010
  143. if not self._apply_file_permissions(sources, dry_run=dry_run):
  144. result |= 0b100
  145. self._check_folders(self.args.input)
  146. return result
  147. def check(self):
  148. return self._perform(dry_run=True)
  149. def format(self):
  150. return self._perform(dry_run=False)
  151. if __name__ == "__main__":
  152. Main()()