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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| python3 - <<'PY' > inventory.jsonl import os import json import stat from datetime import datetime
ROOT = "."
ROOT_ABS = os.path.abspath(ROOT)
entries = []
for dirpath, dirnames, filenames in os.walk( ROOT, topdown=True, followlinks=False, ): for dirname in list(dirnames): full = os.path.join(dirpath, dirname) rel = os.path.relpath(full, ROOT)
try: st = os.lstat(full) except OSError as e: entries.append({ "path": rel, "type": "error", "error": str(e), }) continue
if stat.S_ISLNK(st.st_mode): file_type = "symlink" elif stat.S_ISDIR(st.st_mode): file_type = "directory" else: file_type = "other"
item = { "path": rel, "name": dirname, "type": file_type, "size": st.st_size, "mtime": datetime.fromtimestamp( st.st_mtime ).astimezone().isoformat(), "symlink": stat.S_ISLNK(st.st_mode), }
if item["symlink"]: try: item["target"] = os.readlink(full) except OSError: item["target"] = None
entries.append(item)
if dirname == "@eaDir" or item["symlink"]: dirnames.remove(dirname)
for filename in filenames: full = os.path.join(dirpath, filename) rel = os.path.relpath(full, ROOT)
try: st = os.lstat(full) except OSError as e: entries.append({ "path": rel, "type": "error", "error": str(e), }) continue
if stat.S_ISLNK(st.st_mode): file_type = "symlink" elif stat.S_ISREG(st.st_mode): file_type = "file" else: file_type = "other"
_, ext = os.path.splitext(filename)
item = { "path": rel, "name": filename, "type": file_type, "extension": ext.lower(), "size": st.st_size, "mtime": datetime.fromtimestamp( st.st_mtime ).astimezone().isoformat(), "symlink": stat.S_ISLNK(st.st_mode), }
if item["symlink"]: try: item["target"] = os.readlink(full) except OSError: item["target"] = None
entries.append(item)
entries.sort(key=lambda x: x["path"])
print(json.dumps({ "type": "inventory_meta", "root": ROOT_ABS, }, ensure_ascii=False, separators=(",", ":")))
for item in entries: print(json.dumps( item, ensure_ascii=False, separators=(",", ":"), )) PY
|