-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarifs
executable file
·162 lines (132 loc) · 4.02 KB
/
varifs
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
155
156
157
158
159
160
161
162
#!/usr/bin/python
import os, sys, fuse, configparser, re
from pathlib import Path
from stat import *
fuse.fuse_python_api = (0, 2)
fuse.feature_assert('stateful_files', 'has_init')
vars = {}
varifile = os.environ.get('VARIFS_VARIFILE') or ".varifs"
secretfile = os.environ.get('VARIFS_SECRETFILE') or ".varifs.secret"
prefix = os.environ.get('VARIFS_PREFIX') or "«"
suffix = os.environ.get('VARIFS_SUFFIX') or "»"
modsep = os.environ.get('VARIFS_MODSEP') or "|"
modargsep = os.environ.get('VARIFS_MODARGSEP') or ":"
def stat2fstat(stat):
fstat = fuse.Stat()
for key in fstat.__dict__.keys():
setattr(fstat, key, getattr(stat, key))
return fstat
def flag2mode(flags):
md = {os.O_RDONLY: 'rb', os.O_WRONLY: 'wb', os.O_RDWR: 'wb+'}
m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
if flags | os.O_APPEND:
m = m.replace('w', 'a', 1)
return m
def resolveVariables(data):
global vars, prefix, suffix, modsep, modargsep
matches = re.findall(prefix+'([^'+suffix+']+)'+suffix, data.decode())
for match in matches:
mods = match.split(modsep)
key = mods.pop(0)
if not key in vars:
# TODO: how to print to stdout from here?!
continue
val = vars[key]
for mod in mods:
meh = mod.split(modargsep)
cmd = meh[0]
if len(meh) > 1:
arg = meh[1]
if cmd == "start":
arg = int(arg)
val = val[arg:]
elif cmd == "mul":
val = str(float(val) * float(arg))
elif cmd == "add":
if "." in arg:
val = str(float(val) + float(arg))
else:
val = str(int(val) + int(arg))
elif cmd == "hex2rgb":
val = val[1:]
rgb = tuple(int(val[i:i+2], 16) for i in (0, 2, 4))
val = ','.join(map(str, rgb))
data = data.replace(bytes(prefix+match+suffix, "utf-8"), bytes(val, "utf-8"))
return data
class VariFS(fuse.Fuse):
def __init__(self, *args, **kw):
fuse.Fuse.__init__(self, *args, **kw)
self.root = os.getcwd()
def getattr(self, path):
fstat = stat2fstat(os.lstat("." + path))
if S_ISREG(fstat.st_mode):
file = os.fdopen(os.open("." + path, os.O_RDONLY), flag2mode(os.O_RDONLY))
data = resolveVariables(file.read())
file.close()
fstat.st_size = len(data)
return fstat
def readlink(self, path):
data = os.readlink("." + path)
return resolveVariables(str.encode(data)).decode()
def readdir(self, path, offset):
for e in os.listdir("." + path):
yield fuse.Direntry(e)
def utime(self, path, times):
os.utime("." + path, times)
def access(self, path, mode):
if not os.access("." + path, mode):
return -EACCES
def fsinit(self):
os.chdir(self.root)
class VariFile(object):
def __init__(self, path, flags, *mode):
self.file = os.fdopen(os.open("." + path, flags, *mode), flag2mode(flags))
self.fd = self.file.fileno()
def read(self, length, offset):
data = resolveVariables(self.file.read())
return data[offset:offset+length]
def release(self, flags):
self.file.close()
def fgetattr(self):
fstat = stat2fstat(os.fstat(self.fd))
if S_ISREG(fstat.st_mode):
data = resolveVariables(file.read())
file.seek(0)
fstat.st_size = len(data)
return fstat
def main(self, *a, **kw):
self.file_class = self.VariFile
return fuse.Fuse.main(self, *a, **kw)
def main():
global vars, varifile, secretfile, logger
config = configparser.ConfigParser()
txt = "[varifs]\n";
if Path(varifile).is_file():
txt += Path(varifile).read_text()
if Path(secretfile).is_file():
txt += Path(secretfile).read_text()
config.read_string(txt)
vars = dict(config.items("varifs"))
server = VariFS(
version = "%prog " + fuse.__version__,
usage = fuse.Fuse.fusage,
dash_s_do = "setsingle"
)
server.parser.add_option(
mountopt = "root",
metavar = "PATH",
default = os.getcwd(),
help = "path to mount [default: %default]"
)
server.parse(values=server, errex=1)
try:
if server.fuse_args.mount_expected():
os.chdir(server.root)
except OSError:
print("can't enter root of underlying filesystem", file=sys.stderr)
sys.exit(1)
server.main()
if __name__ == "__main__":
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
main()