lint.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. # Skipping 3rd-party code - usually resides in subfolder "lib"
  48. if "lib" in dirnames:
  49. dirnames.remove("lib")
  50. for filename in filenames:
  51. ext = os.path.splitext(filename.lower())[1]
  52. if not ext in SOURCE_CODE_FILE_EXTENSIONS:
  53. continue
  54. output.append(os.path.join(dirpath, filename))
  55. return output
  56. @staticmethod
  57. def _format_source(task):
  58. try:
  59. subprocess.check_call(task)
  60. return True
  61. except subprocess.CalledProcessError as e:
  62. return False
  63. def _format_sources(self, sources: list, dry_run: bool = False):
  64. args = ["clang-format", "--Werror", "--style=file", "-i"]
  65. if dry_run:
  66. args.append("--dry-run")
  67. files_per_task = 69
  68. tasks = []
  69. while len(sources) > 0:
  70. tasks.append(args + sources[:files_per_task])
  71. sources = sources[files_per_task:]
  72. pool = multiprocessing.Pool()
  73. results = pool.map(self._format_source, tasks)
  74. return all(results)
  75. def _fix_filename(self, filename: str):
  76. return filename.replace("-", "_")
  77. def _replace_occurrence(self, sources: list, old: str, new: str):
  78. old = old.encode()
  79. new = new.encode()
  80. for source in sources:
  81. content = open(source, "rb").read()
  82. if content.count(old) > 0:
  83. self.logger.info(f"Replacing {old} with {new} in {source}")
  84. content = content.replace(old, new)
  85. open(source, "wb").write(content)
  86. def _apply_file_naming_convention(self, sources: list, dry_run: bool = False):
  87. pattern = re.compile(SOURCE_CODE_FILE_PATTERN)
  88. good = []
  89. bad = []
  90. # Check sources for invalid filenames
  91. for source in sources:
  92. basename = os.path.basename(source)
  93. if not pattern.match(basename):
  94. new_basename = self._fix_filename(basename)
  95. if not pattern.match(new_basename):
  96. self.logger.error(f"Unable to fix name for {basename}")
  97. return False
  98. bad.append((source, basename, new_basename))
  99. else:
  100. good.append(source)
  101. # Notify about errors or replace all occurrences
  102. if dry_run:
  103. if len(bad) > 0:
  104. self.logger.error(f"Found {len(bad)} incorrectly named files")
  105. self.logger.info(bad)
  106. return False
  107. else:
  108. # Replace occurrences in text files
  109. for source, old, new in bad:
  110. self._replace_occurrence(sources, old, new)
  111. # Rename files
  112. for source, old, new in bad:
  113. shutil.move(source, source.replace(old, new))
  114. return True
  115. def _apply_file_permissions(self, sources: list, dry_run: bool = False):
  116. execute_permissions = 0o111
  117. pattern = re.compile(SOURCE_CODE_FILE_PATTERN)
  118. good = []
  119. bad = []
  120. # Check sources for unexpected execute permissions
  121. for source in sources:
  122. st = os.stat(source)
  123. perms_too_many = st.st_mode & execute_permissions
  124. if perms_too_many:
  125. good_perms = st.st_mode & ~perms_too_many
  126. bad.append((source, oct(perms_too_many), good_perms))
  127. else:
  128. good.append(source)
  129. # Notify or fix
  130. if dry_run:
  131. if len(bad) > 0:
  132. self.logger.error(f"Found {len(bad)} incorrect permissions")
  133. self.logger.info([record[0:2] for record in bad])
  134. return False
  135. else:
  136. for source, perms_too_many, new_perms in bad:
  137. os.chmod(source, new_perms)
  138. return True
  139. def _perform(self, dry_run: bool):
  140. result = 0
  141. sources = self._find_sources(self.args.input)
  142. if not self._format_sources(sources, dry_run=dry_run):
  143. result |= 0b001
  144. if not self._apply_file_naming_convention(sources, dry_run=dry_run):
  145. result |= 0b010
  146. if not self._apply_file_permissions(sources, dry_run=dry_run):
  147. result |= 0b100
  148. self._check_folders(self.args.input)
  149. return result
  150. def check(self):
  151. return self._perform(dry_run=True)
  152. def format(self):
  153. return self._perform(dry_run=False)
  154. if __name__ == "__main__":
  155. Main()()