Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9import argparse 

10from pathlib import Path 

11import re 

12import sys 

13 

14PATH_TO_TSFPGA = Path(__file__).parent.parent 

15sys.path.append(str(PATH_TO_TSFPGA.resolve())) 

16 

17import tsfpga 

18from tsfpga.git_utils import find_git_files 

19from tsfpga.system_utils import create_file, read_file 

20from tsfpga.test.lint.test_copyright import CopyrightHeader, files_to_check_for_copyright_header 

21 

22 

23RE_TRAILING_WHITESPACE = re.compile(" +\n", re.DOTALL) 

24RE_TAB = re.compile("\t", re.DOTALL) 

25 

26 

27def fix_trailing_whitespace(file): 

28 contents = read_file(file) 

29 

30 if RE_TRAILING_WHITESPACE.search(contents): 

31 print(f"Fixing trailing whitespace in {file}") 

32 create_file(file, RE_TRAILING_WHITESPACE.sub("\n", contents)) 

33 

34 

35def fix_tabs(file, tab_width): 

36 contents = read_file(file) 

37 

38 if RE_TAB.search(contents): 

39 print(f"Fixing tabs in {file}") 

40 replacement = " " * tab_width 

41 create_file(file, RE_TAB.sub(replacement, contents)) 

42 

43 

44def fix_lint(files, tab_width): 

45 for file in files: 

46 fix_trailing_whitespace(file) 

47 fix_tabs(file, tab_width) 

48 

49 

50def arguments(): 

51 parser = argparse.ArgumentParser("Automatically fix some lint problems") 

52 parser.add_argument( 

53 "--tab-width", 

54 type=int, 

55 default=2, 

56 help="number of spaces to use when replacing tab characters", 

57 ) 

58 parser.add_argument("files", nargs="*", help="fixup these specific files") 

59 return parser.parse_args() 

60 

61 

62def main(): 

63 args = arguments() 

64 

65 files = ( 

66 args.files 

67 if args.files 

68 else list(find_git_files(directory=tsfpga.REPO_ROOT, file_endings_avoid=(".png"))) 

69 ) 

70 fix_lint(files, args.tab_width) 

71 

72 files = args.files if args.files else list(files_to_check_for_copyright_header()) 

73 for filename in files: 

74 copyright_header = CopyrightHeader("Lukas Vik", filename) 

75 if not copyright_header.check_file(): 

76 copyright_header.fix_file() 

77 

78 

79if __name__ == "__main__": 

80 main()