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 importlib.util 

10import os 

11from os.path import commonpath 

12from platform import system 

13from shutil import rmtree 

14import subprocess 

15 

16 

17def create_file(file, contents=None): 

18 create_directory(file.parent, empty=False) 

19 

20 contents = "" if contents is None else contents 

21 with file.open("w") as file_handle: 

22 file_handle.write(contents) 

23 

24 return file 

25 

26 

27def read_file(file): 

28 with file.open() as file_handle: 

29 return file_handle.read() 

30 

31 

32def read_last_lines_of_file(file, num_lines): 

33 """ 

34 Read a number of lines from the end of a file, without buffering the whole file. 

35 Similar to unix ``tail`` command. 

36 

37 Arguments: 

38 file (`pathlib.Path`): The file that shall be read. 

39 num_lines (int): The number of lines to read. 

40 

41 Return: 

42 str: The last lines of the file. 

43 """ 

44 result_lines = [] 

45 blocks_to_read = 0 

46 

47 with open(file) as file_handle: 

48 while len(result_lines) < num_lines: 

49 # Since we do not know the line lengths, there is some guessing involved. Keep reading 

50 # larger and larger blocks until we have all the lines that are requested. 

51 blocks_to_read += 1 

52 

53 try: 

54 # Read a block from the end 

55 file_handle.seek(-blocks_to_read * 4096, os.SEEK_END) 

56 except IOError: 

57 # Tried to read more data than what is available. Read whatever we have and return 

58 # to user. 

59 file_handle.seek(0) 

60 result_lines = file_handle.readlines() 

61 break 

62 

63 result_lines = file_handle.readlines() 

64 

65 result = "".join(result_lines[-num_lines:]) 

66 return result 

67 

68 

69def delete(path): 

70 if path.exists(): 

71 if path.is_dir(): 

72 rmtree(path) 

73 else: 

74 path.unlink() 

75 return path 

76 

77 

78def create_directory(directory, empty=True): 

79 if empty: 

80 delete(directory) 

81 elif directory.exists(): 

82 return directory 

83 

84 directory.mkdir(parents=True) 

85 return directory 

86 

87 

88def file_is_in_directory(file_path, directories): 

89 """ 

90 Check if the file is in any of the directories. 

91 

92 Arguments: 

93 file_path (`pathlib.Path`): The file to be checked. 

94 directories (list(`pathlib.Path`)): The directories to be controlled. 

95 

96 Returns: 

97 bool: True if there is a comon path. 

98 """ 

99 for directory in directories: 

100 if commonpath([str(file_path), str(directory)]) == str(directory): 

101 return True 

102 return False 

103 

104 

105def run_command(cmd, cwd=None): 

106 if not isinstance(cmd, list): 

107 raise ValueError("Must be called with a list, not a string") 

108 

109 subprocess.check_call(cmd, cwd=cwd) 

110 

111 

112def load_python_module(file): 

113 python_module_name = file.stem 

114 

115 spec = importlib.util.spec_from_file_location(python_module_name, file) 

116 module = importlib.util.module_from_spec(spec) 

117 spec.loader.exec_module(module) 

118 

119 return module 

120 

121 

122def system_is_windows(): 

123 return system() == "Windows"