cockpit-navigator/navigator/scripts/ls.py3

107 lines
2.9 KiB
Plaintext
Raw Normal View History

2021-05-21 20:57:47 +02:00
#!/usr/bin/env python3
2021-05-27 20:27:16 +02:00
"""
Cockpit Navigator - A File System Browser for Cockpit.
Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
This file is part of Cockpit Navigator.
Cockpit Navigator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Cockpit Navigator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
"""
2021-05-21 20:57:47 +02:00
import os
2021-05-28 19:50:06 +02:00
from stat import S_ISDIR, S_ISLNK, filemode
2021-05-21 20:57:47 +02:00
import json
import sys
from pwd import getpwuid
from grp import getgrgid
def get_stat(full_path, filename = '/'):
try:
stats = os.lstat(full_path)
except OSError:
return {
"filename": filename,
"isdir": False,
"link-target": "",
"stat": {
"inaccessible": True,
"mode": 0,
"mode-str": "?",
"uid": 0,
"owner": "?",
"gid": 0,
"group": "?",
"size": 0,
"atime": 0,
"mtime": 0,
"ctime": 0
}
}
isdir = False
try:
isdir = S_ISDIR(os.stat(full_path).st_mode)
except OSError:
pass
2021-05-28 19:50:06 +02:00
link_target = '?'
if S_ISLNK(stats.st_mode):
link_target = os.readlink(full_path)
owner = '?'
try:
owner = getpwuid(stats.st_uid).pw_name
except:
pass
group = '?'
try:
group = getgrgid(stats.st_gid).gr_name
except:
pass
response = {
"inaccessible": False,
"filename": filename,
"isdir": isdir,
2021-05-28 19:50:06 +02:00
"link-target": link_target,
"stat": {
"mode": stats.st_mode,
"mode-str": filemode(stats.st_mode),
"uid": stats.st_uid,
"owner": owner,
"gid": stats.st_gid,
"group": group,
"size": stats.st_size,
"atime": stats.st_atime,
"mtime": stats.st_mtime,
"ctime": stats.st_ctime
}
}
return response
2021-05-21 20:57:47 +02:00
def main():
if(len(sys.argv) < 2):
sys.exit(1)
try:
nodes = os.listdir(sys.argv[1])
except Exception as e:
print(e)
2021-05-21 20:57:47 +02:00
sys.exit(1)
response = {
".": get_stat(sys.argv[1]),
"children": []
}
2021-05-21 20:57:47 +02:00
for node in nodes:
full_path = sys.argv[1] + "/" + node
response["children"].append(get_stat(full_path, node))
print(json.dumps(response))
2021-05-21 20:57:47 +02:00
if __name__ == "__main__":
main()