@@ -42,19 +42,27 @@ class TransportError(Exception):
42
42
pass
43
43
44
44
45
+ class TransportExecError (TransportError ):
46
+ def __init__ (self , status_code , error_output ):
47
+ self .status_code = status_code
48
+ self .error_output = error_output
49
+ super ().__init__ (error_output )
50
+
51
+
45
52
listdir_result = namedtuple ("dir_result" , ["name" , "st_mode" , "st_ino" , "st_size" ])
46
53
47
54
48
55
# Takes a Transport error (containing the text of an OSError traceback) and
49
56
# raises it as the corresponding OSError-derived exception.
50
57
def _convert_filesystem_error (e , info ):
51
- if len (e .args ) >= 3 :
52
- if b"OSError" in e .args [2 ] and b"ENOENT" in e .args [2 ]:
53
- return FileNotFoundError (info )
54
- if b"OSError" in e .args [2 ] and b"EISDIR" in e .args [2 ]:
55
- return IsADirectoryError (info )
56
- if b"OSError" in e .args [2 ] and b"EEXIST" in e .args [2 ]:
57
- return FileExistsError (info )
58
+ if "OSError" in e .error_output and "ENOENT" in e .error_output :
59
+ return FileNotFoundError (info )
60
+ if "OSError" in e .error_output and "EISDIR" in e .error_output :
61
+ return IsADirectoryError (info )
62
+ if "OSError" in e .error_output and "EEXIST" in e .error_output :
63
+ return FileExistsError (info )
64
+ if "OSError" in e .error_output and "ENODEV" in e .error_output :
65
+ return FileNotFoundError (info )
58
66
return e
59
67
60
68
@@ -72,7 +80,7 @@ def repr_consumer(b):
72
80
buf .extend (b"[" )
73
81
self .exec (cmd , data_consumer = repr_consumer )
74
82
buf .extend (b"]" )
75
- except TransportError as e :
83
+ except TransportExecError as e :
76
84
raise _convert_filesystem_error (e , src ) from None
77
85
78
86
return [
@@ -84,7 +92,7 @@ def fs_stat(self, src):
84
92
try :
85
93
self .exec ("import os" )
86
94
return os .stat_result (self .eval ("os.stat(%s)" % ("'%s'" % src )))
87
- except TransportError as e :
95
+ except TransportExecError as e :
88
96
raise _convert_filesystem_error (e , src ) from None
89
97
90
98
def fs_exists (self , src ):
@@ -109,7 +117,7 @@ def fs_printfile(self, src, chunk_size=256):
109
117
)
110
118
try :
111
119
self .exec (cmd , data_consumer = stdout_write_bytes )
112
- except TransportError as e :
120
+ except TransportExecError as e :
113
121
raise _convert_filesystem_error (e , src ) from None
114
122
115
123
def fs_readfile (self , src , chunk_size = 256 , progress_callback = None ):
@@ -128,7 +136,7 @@ def fs_readfile(self, src, chunk_size=256, progress_callback=None):
128
136
if progress_callback :
129
137
progress_callback (len (contents ), src_size )
130
138
self .exec ("f.close()" )
131
- except TransportError as e :
139
+ except TransportExecError as e :
132
140
raise _convert_filesystem_error (e , src ) from None
133
141
134
142
return contents
@@ -148,37 +156,37 @@ def fs_writefile(self, dest, data, chunk_size=256, progress_callback=None):
148
156
if progress_callback :
149
157
progress_callback (written , src_size )
150
158
self .exec ("f.close()" )
151
- except TransportError as e :
159
+ except TransportExecError as e :
152
160
raise _convert_filesystem_error (e , dest ) from None
153
161
154
162
def fs_mkdir (self , path ):
155
163
try :
156
164
self .exec ("import os\n os.mkdir('%s')" % path )
157
- except TransportError as e :
165
+ except TransportExecError as e :
158
166
raise _convert_filesystem_error (e , path ) from None
159
167
160
168
def fs_rmdir (self , path ):
161
169
try :
162
170
self .exec ("import os\n os.rmdir('%s')" % path )
163
- except TransportError as e :
171
+ except TransportExecError as e :
164
172
raise _convert_filesystem_error (e , path ) from None
165
173
166
174
def fs_rmfile (self , path ):
167
175
try :
168
176
self .exec ("import os\n os.remove('%s')" % path )
169
- except TransportError as e :
177
+ except TransportExecError as e :
170
178
raise _convert_filesystem_error (e , path ) from None
171
179
172
180
def fs_touchfile (self , path ):
173
181
try :
174
182
self .exec ("f=open('%s','a')\n f.close()" % path )
175
- except TransportError as e :
183
+ except TransportExecError as e :
176
184
raise _convert_filesystem_error (e , path ) from None
177
185
178
186
def fs_hashfile (self , path , algo , chunk_size = 256 ):
179
187
try :
180
188
self .exec ("import hashlib\n h = hashlib.{algo}()" .format (algo = algo ))
181
- except TransportError :
189
+ except TransportExecError :
182
190
# hashlib (or hashlib.{algo}) not available on device. Do the hash locally.
183
191
data = self .fs_readfile (path , chunk_size = chunk_size )
184
192
return getattr (hashlib , algo )(data ).digest ()
0 commit comments