138 directory = params["directory"] |
138 directory = params["directory"] |
139 if not directory: |
139 if not directory: |
140 directory = os.getcwd() |
140 directory = os.getcwd() |
141 |
141 |
142 try: |
142 try: |
143 listing = [] |
143 listing = self.__scanDirectory(directory, params["recursive"]) |
144 for dirEntry in os.scandir(directory): |
144 |
145 filestat = dirEntry.stat() |
145 return { |
|
146 "ok": True, |
|
147 "directory": directory, |
|
148 "listing": listing, |
|
149 "separator": os.sep, |
|
150 } |
|
151 except OSError as err: |
|
152 return { |
|
153 "ok": False, |
|
154 "error": str(err), |
|
155 } |
|
156 |
|
157 def __scanDirectory(self, directory, recursive, withHidden=False): |
|
158 """ |
|
159 Private method to scan a given directory. |
|
160 |
|
161 @param directory path of the directory to be scanned |
|
162 @type str |
|
163 @param recursive flag indicating a recursive scan |
|
164 @type bool |
|
165 @param withHidden flag indicating to list hidden files and directories |
|
166 as well (defaults to False) |
|
167 @type bool (optional) |
|
168 """ |
|
169 listing = [] |
|
170 for dirEntry in os.scandir(directory): |
|
171 filestat = dirEntry.stat() |
|
172 if withHidden or not dirEntry.name.startswith("."): |
146 entry = { |
173 entry = { |
147 "name": dirEntry.name, |
174 "name": dirEntry.name, |
148 "path": dirEntry.path, |
175 "path": dirEntry.path, |
149 "is_dir": dirEntry.is_dir(), |
176 "is_dir": dirEntry.is_dir(), |
150 "is_file": dirEntry.is_file(), |
177 "is_file": dirEntry.is_file(), |
151 "is_link": dirEntry.is_symlink(), |
178 "is_link": dirEntry.is_symlink(), |
152 "mode": filestat.st_mode, |
179 "mode": filestat.st_mode, |
153 "mode_str": stat.filemode(filestat.st_mode), |
180 "mode_str": stat.filemode(filestat.st_mode), |
154 "size": filestat.st_size, |
181 "size": filestat.st_size, |
155 "mtime": time.strftime( |
182 "mtime": filestat.st_mtime, |
|
183 "mtime_str": time.strftime( |
156 "%Y-%m-%d %H:%M:%S", time.localtime(filestat.st_mtime) |
184 "%Y-%m-%d %H:%M:%S", time.localtime(filestat.st_mtime) |
157 ), |
185 ), |
158 } |
186 } |
159 listing.append(entry) |
187 listing.append(entry) |
160 |
188 |
161 return { |
189 if entry["is_dir"] and recursive: |
162 "ok": True, |
190 listing += self.__scanDirectory(dirEntry.path, recursive) |
163 "directory": directory, |
191 |
164 "listing": listing, |
192 return listing |
165 "separator": os.sep, |
|
166 } |
|
167 except OSError as err: |
|
168 return { |
|
169 "ok": False, |
|
170 "error": str(err), |
|
171 } |
|
172 |
193 |
173 def __stat(self, params): |
194 def __stat(self, params): |
174 """ |
195 """ |
175 Private method to get the status of a file. |
196 Private method to get the status of a file. |
176 |
197 |
325 |
346 |
326 if params["create"] and not os.path.exists(filename): |
347 if params["create"] and not os.path.exists(filename): |
327 with open(filename, "wb"): |
348 with open(filename, "wb"): |
328 pass |
349 pass |
329 |
350 |
330 try: |
351 newline = None if params["newline"] == "<<none>>" else params["newline"] |
331 with open(filename, "rb") as f: |
352 try: |
|
353 with open(filename, "rb", newline=newline) as f: |
332 data = f.read() |
354 data = f.read() |
333 return { |
355 return { |
334 "ok": True, |
356 "ok": True, |
335 "filedata": str(base64.b85encode(data), encoding="ascii"), |
357 "filedata": str(base64.b85encode(data), encoding="ascii"), |
336 } |
358 } |
367 os.remove(backupFilename) |
389 os.remove(backupFilename) |
368 with contextlib.suppress(OSError): |
390 with contextlib.suppress(OSError): |
369 os.rename(filename, backupFilename) |
391 os.rename(filename, backupFilename) |
370 |
392 |
371 # 2. write the data to the file and reset the permissions |
393 # 2. write the data to the file and reset the permissions |
372 try: |
394 newline = None if params["newline"] == "<<none>>" else params["newline"] |
373 with open(filename, "wb") as f: |
395 try: |
|
396 with open(filename, "wb", newline=newline) as f: |
374 f.write(data) |
397 f.write(data) |
375 if params["with_backup"] and perms_valid: |
398 if params["with_backup"] and perms_valid: |
376 os.chmod(filename, permissions) |
399 os.chmod(filename, permissions) |
377 return {"ok": True} |
400 return {"ok": True} |
378 except OSError as err: |
401 except OSError as err: |