forked from scott-linder/irsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath
executable file
·26 lines (23 loc) · 1003 Bytes
/
path
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
#!/usr/bin/env python3
from os import path, sep, environ
from sys import argv, stdout
def chan_path(chan, path):
if sep in path:
# Strictly we would require a leading sep; as we do not have more than one
# level of directories we simply treat `/chan/file` and `chan/file`
# equivalently. The latter is easier to work with, so we normalize to it.
# Thus the only time it is required to lead with sep is in the case where
# only the root (i.e. `/`) or a chan (e.g. `/chan`) is specified, as
# otherwise this would be interpreted as a file alone.
if path.startswith(sep):
path = path.lstrip(sep)
splits = path.split(sep)
chan = splits[0]
name = ''.join(splits[1:])
else:
name = path
return 'var/root/{}/{}'.format(chan, name).replace('..', '')
if __name__ == '__main__':
chan = environ['CHAN']
path = ' '.join(argv[1:])
stdout.buffer.write(chan_path(chan, path).encode('utf-8'))