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

1from . import OsError 

2from . import cwd 

3from . import getenv 

4from .path import Path 

5 

6c"""source-before-namespace 

7#include <unistd.h> 

8""" 

9 

10func _create_template(directory: Path?) -> bytes: 

11 if directory is None: 

12 tmpdir = getenv("TMPDIR", "/tmp").strip_right("/") 

13 else: 

14 tmpdir = str(directory) 

15 

16 name = f"{tmpdir}/tmp.XXXXXX".to_utf8() 

17 name += 0 

18 

19 return name 

20 

21class File: 

22 """A temporary file. 

23 

24 """ 

25 

26 _path: Path? 

27 _remove: bool 

28 

29 func __init__(self, remove: bool = True, directory: Path? = None): 

30 self._path = None 

31 self._remove = remove 

32 name = _create_template(directory) 

33 

34 c""" 

35 int fd = mkstemp((char *)name.m_bytes->data()); 

36 

37 if (fd != -1) { 

38 close(fd); 

39 name.m_bytes->pop_back(); 

40 this->_path = mys::make_shared<path::Path>(String(name)); 

41 } 

42 """ 

43 

44 if self._path is None: 

45 raise OsError("Failed to create temporary file.") 

46 

47 func __del__(self): 

48 self._del() 

49 

50 func _del(self): 

51 if self._remove: 

52 self._path.rm(force=True) 

53 

54 func path(self) -> Path: 

55 """The file system path to the file. 

56 

57 """ 

58 

59 return self._path 

60 

61class Directory: 

62 """A temporary directory. 

63 

64 """ 

65 

66 _path: Path? 

67 _remove: bool 

68 

69 func __init__(self, remove: bool = True, directory: Path? = None): 

70 self._path = None 

71 self._remove = remove 

72 name = _create_template(directory) 

73 

74 c""" 

75 if (mkdtemp((char *)name.m_bytes->data()) != NULL) { 

76 name.m_bytes->pop_back(); 

77 this->_path = mys::make_shared<path::Path>(String(name)); 

78 } 

79 """ 

80 

81 if self._path is None: 

82 raise OsError("Failed to create temporary directory.") 

83 

84 func __del__(self): 

85 self._del() 

86 

87 func _del(self): 

88 if self._remove: 

89 self._path.rm(force=True, recursive=True) 

90 

91 func path(self) -> Path: 

92 """The file system path to the directory. 

93 

94 """ 

95 

96 return self._path 

97 

98test file(): 

99 tmpfile = File() 

100 assert tmpfile.path().exists() 

101 

102func _create_files() -> (Path, Path): 

103 return (File().path(), File(remove=False).path()) 

104 

105test file_remove(): 

106 path_remove_true, path_remove_false = _create_files() 

107 assert not path_remove_true.exists() 

108 assert path_remove_false.exists() 

109 path_remove_false.rm() 

110 

111test file_in_custom_directory(): 

112 tmpdir = Directory() 

113 directory = tmpdir.path() 

114 tmpfile = File(directory=directory) 

115 assert tmpfile.path().exists() 

116 assert tmpfile.path().parent() == directory 

117 

118test directory(): 

119 tmpdir = Directory() 

120 assert tmpdir.path().exists() 

121 

122func _create_directories() -> (Path, Path): 

123 return (Directory().path(), Directory(remove=False).path()) 

124 

125test directory_remove(): 

126 path_remove_true, path_remove_false = _create_directories() 

127 assert not path_remove_true.exists() 

128 assert path_remove_false.exists() 

129 path_remove_false.rm() 

130 

131test remove_directory(): 

132 tmpdir = Directory() 

133 original_path = cwd() 

134 tmpdir.path().cd() 

135 Path("foo.txt").touch() 

136 original_path.cd() 

137 

138test directory_in_custom_directory(): 

139 tmpdir = Directory() 

140 directory = tmpdir.path() 

141 tmpfile = Directory(directory=directory) 

142 assert tmpfile.path().exists() 

143 assert tmpfile.path().parent() == directory