-
Notifications
You must be signed in to change notification settings - Fork 205
/
smc_close.py
executable file
·75 lines (61 loc) · 2.32 KB
/
smc_close.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
#!/usr/bin/python
# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
# License: AGPLv3 s.t. "Commons Clause" – read LICENSE.md for details
from __future__ import absolute_import, print_function
MAX_FILES = 100
import json, os, sys
home = os.environ['HOME']
if 'TMUX' in os.environ:
prefix = '\x1bPtmux;\x1b'
postfix = '\x1b\\'
else:
prefix = ''
postfix = ''
def process(paths):
v = []
if len(paths) > MAX_FILES:
sys.stderr.write(
"You may close at most %s at once using the open command; truncating list\n"
% MAX_FILES)
paths = paths[:MAX_FILES]
for path in paths:
if not path:
continue
if not os.path.exists(path) and any(c in path for c in '{?*'):
# If the path doesn't exist and does contain a shell glob character which didn't get expanded,
# then don't try to just create that file. See https://github.com/sagemathinc/cocalc/issues/1019
sys.stderr.write("no match for '%s', so not closing\n" % path)
continue
if not os.path.exists(path):
# Doesn't exist, so doesn't matter
continue
if not path.startswith('/'):
# we use pwd instead of getcwd or os.path.abspath since we want this to
# work when used inside a directory that is a symlink! I could find
# no analogue of pwd directly in Python (getcwd is not it!).
path = os.path.join(os.popen('pwd').read().strip(), path)
# determine name relative to home directory
if path.startswith(home):
name = path[len(home) + 1:]
else:
name = path
# Is it a file or directory?
if os.path.isdir(path):
v.append({'directory': name})
else:
v.append({'file': name})
if v:
mesg = {'event': 'close', 'paths': v}
ser = json.dumps(mesg, separators=(',', ':'))
print(prefix + '\x1b]49;%s\x07' % ser + postfix)
def main():
if len(sys.argv) == 1:
print("Usage: close [path names] ...")
print(
"Closes each file (or directory) in the CoCalc web-based editor from the shell."
)
print("If the named file doesn't exist, it is created.")
else:
process(sys.argv[1:])
if __name__ == "__main__":
main()