This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs2json.py
154 lines (117 loc) · 3.8 KB
/
fs2json.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# From https://github.com/copy/fs2json
import argparse
import json
import os
import stat
import sys
import itertools
import logging
VERSION = 2
IDX_NAME = 0
IDX_SIZE = 1
IDX_MTIME = 2
IDX_MODE = 3
IDX_UID = 4
IDX_GID = 5
# target for symbolic links
# child nodes for directories
# nothing for files
IDX_TARGET = 6
def main():
logging.basicConfig(format="%(message)s")
logger = logging.getLogger("fs2json")
logger.setLevel(logging.DEBUG)
args = argparse.ArgumentParser(description="Create filesystem JSON. Example:\n"
" ./fs2xml.py --exclude /boot/ --out fs.json /mnt/",
formatter_class=argparse.RawTextHelpFormatter
)
args.add_argument("--exclude",
action="append",
metavar="path",
help="Path to exclude (relative to base path). Can be specified multiple times.")
args.add_argument("--out",
metavar="out",
nargs="?",
type=argparse.FileType("w"),
help="File to write to (defaults to stdout)",
default=sys.stdout)
args.add_argument("path",
metavar="path",
help="Base path to include in JSON")
args = args.parse_args()
path = os.path.normpath(args.path)
path = path + "/"
exclude = args.exclude or []
exclude = [os.path.join("/", os.path.normpath(p)) for p in exclude]
exclude = set(exclude)
def onerror(oserror):
logger.warning(oserror)
rootdepth = path.count("/")
files = os.walk(path, onerror=onerror)
prevpath = []
mainroot = []
result = {
"fsroot": mainroot,
"version": VERSION,
"size": 0,
}
rootstack = [mainroot]
def make_node(st, name):
obj = [None] * 7
obj[IDX_NAME] = name
obj[IDX_SIZE] = st.st_size
obj[IDX_MTIME] = int(st.st_mtime)
obj[IDX_MODE] = int(st.st_mode)
obj[IDX_UID] = st.st_uid
obj[IDX_GID] = st.st_gid
result["size"] += st.st_size
# Missing:
# int(st.st_atime),
# int(st.st_ctime),
return obj
for f in files:
dirpath, dirnames, filenames = f
pathparts = dirpath.split("/")
pathparts = pathparts[rootdepth:]
fullpath = os.path.join("/", *pathparts)
if fullpath in exclude:
dirnames[:] = []
continue
depth = 0
for this, prev in zip(pathparts, prevpath):
if this != prev:
break
depth += 1
for name in prevpath[depth:]:
rootstack.pop()
oldroot = rootstack[-1]
assert len(pathparts[depth:]) == 1
openname = pathparts[-1]
if openname == "":
root = mainroot
else:
root = []
st = os.stat(dirpath)
rootobj = make_node(st, openname)
rootobj[IDX_TARGET] = root
oldroot.append(rootobj)
rootstack.append(root)
for filename in itertools.chain(filenames, dirnames):
absname = os.path.join(dirpath, filename)
st = os.lstat(absname)
isdir = stat.S_ISDIR(st.st_mode)
islink = stat.S_ISLNK(st.st_mode)
if isdir and not islink:
continue
obj = make_node(st, filename)
if islink:
target = os.readlink(absname)
obj[IDX_TARGET] = target
while obj[-1] is None:
obj.pop()
root.append(obj)
prevpath = pathparts
json.dump(result, args.out, check_circular=False, separators=(',', ':'))
if __name__ == "__main__":
main()