-
Notifications
You must be signed in to change notification settings - Fork 38
/
ls.py2
39 lines (39 loc) · 2.09 KB
/
ls.py2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def ls(self, task_id, path, file_browser=False):
if path == ".": file_path = self.current_directory
else: file_path = path if path[0] == os.sep \
else os.path.join(self.current_directory,path)
file_details = os.stat(file_path)
target_is_file = os.path.isfile(file_path)
target_name = os.path.basename(file_path.rstrip(os.sep))
file_browser = {
"host": socket.gethostname(),
"is_file": target_is_file,
"permissions": {"octal": oct(file_details.st_mode)[-3:]},
"name": target_name if target_name != "." \
else os.path.basename(self.current_directory.rstrip(os.sep)), "parent_path": os.path.abspath(os.path.join(file_path, os.pardir)),
"success": True,
"access_time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(file_details.st_atime)),
"modify_time": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(file_details.st_mtime)),
"size": file_details.st_size,
"update_deleted": True,
}
files = []
if not target_is_file:
for entry in os.listdir(file_path):
full_path = os.path.join(file_path, entry)
file = {}
file['name'] = entry
file['is_file'] = True if os.path.isfile(full_path) else False
try:
file_details = os.stat(full_path)
file["permissions"] = { "octal": oct(file_details.st_mode)[-3:]}
file["access_time"] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(file_details.st_atime))
file["modify_time"] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(file_details.st_mtime))
file["size"] = file_details.st_size
except OSError as e:
pass
files.append(file)
file_browser["files"] = files
task = [task for task in self.taskings if task["task_id"] == task_id]
task[0]["file_browser"] = file_browser
return { "files": files }