file.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import re
  2. import subprocess
  3. import checksymbolslib.br as br
  4. import checksymbolslib.kconfig as kconfig
  5. import checksymbolslib.makefile as makefile
  6. file_types = [
  7. kconfig,
  8. makefile,
  9. ]
  10. def get_list_of_files_in_the_repo():
  11. cmd = ['git', 'ls-files']
  12. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  13. stdout = p.communicate()[0]
  14. processed_output = [str(line.decode().rstrip()) for line in stdout.splitlines() if line]
  15. return processed_output
  16. def get_list_of_files_to_process(all_files):
  17. files_to_process = []
  18. for f in all_files:
  19. if br.file_belongs_to_an_ignored_diretory(f):
  20. continue
  21. for t in file_types:
  22. if t.check_filename(f):
  23. files_to_process.append(f)
  24. break
  25. return files_to_process
  26. def get_list_of_filenames_with_pattern(all_files, exclude_list, pattern):
  27. re_pattern = re.compile(r'{}'.format(pattern))
  28. matching_filenames = []
  29. for filename in all_files:
  30. if re_pattern.search(filename):
  31. if filename not in exclude_list:
  32. matching_filenames.append(filename)
  33. return matching_filenames
  34. def read_file(filename):
  35. file_content_raw = []
  36. with open(filename, 'r', errors='surrogateescape') as f:
  37. for lineno, text in enumerate(f.readlines()):
  38. file_content_raw.append([lineno + 1, text])
  39. return file_content_raw
  40. def cleanup_file_content(file_content_raw):
  41. cleaned_up_content = []
  42. continuation = False
  43. last_line = None
  44. first_lineno = None
  45. for cur_lineno, cur_line in file_content_raw:
  46. if continuation:
  47. line = last_line + cur_line
  48. lineno = first_lineno
  49. else:
  50. line = cur_line
  51. lineno = cur_lineno
  52. continuation = False
  53. last_line = None
  54. first_lineno = None
  55. clean_line = line.rstrip('\n')
  56. if clean_line.endswith('\\'):
  57. continuation = True
  58. last_line = clean_line.rstrip('\\')
  59. first_lineno = lineno
  60. continue
  61. cleaned_up_content.append([lineno, clean_line])
  62. return cleaned_up_content
  63. def populate_db_from_file(db, filename):
  64. file_content_raw = read_file(filename)
  65. file_content_to_process = cleanup_file_content(file_content_raw)
  66. for t in file_types:
  67. if t.check_filename(filename):
  68. t.populate_db(db, filename, file_content_to_process)