-
Notifications
You must be signed in to change notification settings - Fork 22
/
common.py
170 lines (139 loc) · 4.75 KB
/
common.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# http://inamidst.com/saxo/
# Created by Sean B. Palmer
# You know your code is good when you don't have a generic module
import base64
import collections
import os
import pickle
import signal
import socket
import sys
import threading
# Usage as of 534f8c68:
# b64pickle: client
# b64unpickle: client, scheduler
# error: client, create, script
# exit_cleanly: client, saxo
# populate: client, create
# thread: client, script
def console():
# TODO: This can probably be removed
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client_sock = os.path.expanduser("~/.saxo/client.sock")
client.connect(client_sock)
while True:
try: text = input("$ ")
except (EOFError, KeyboardInterrupt):
print("")
print("Quitting...")
break
if " " in text:
instruction, args = text.split(" ", 1)
if args:
args = eval("(%s,)" % args)
args = b64pickle(args)
else:
instruction, args = text, b""
octets = instruction.encode("ascii") + b" " + args
client.send(octets + b"\n")
def error(short, long=None, err=None, code=1):
print("saxo: error: " + short, file=sys.stderr)
if long is not None:
print(long.rstrip(), file=sys.stderr)
if err is not None:
if long is not None:
print("", file=sys.stderr)
print("This is the error message that python gave:", file=sys.stderr)
print("", file=sys.stderr)
print(" %s" % err.__class__.__name__)
print(" %s" % err)
sys.exit(code)
def exit_cleanly():
def quit(signum, frame):
print("Exiting cleanly (SIG %s)" % signum)
try: sys.exit()
finally: os._exit(0)
signal.signal(signal.SIGINT, quit)
signal.signal(signal.SIGTERM, quit)
def populate(saxo_path, base):
# TODO: This is being called twice
plugins = os.path.join(base, "plugins")
saxo_plugins = os.path.join(saxo_path, "plugins")
if not os.path.isdir(plugins):
os.mkdir(plugins)
commands = os.path.join(base, "commands")
saxo_commands = os.path.join(saxo_path, "commands")
if not os.path.isdir(commands):
os.mkdir(commands)
def symlink(source, dest):
try: os.symlink(source, dest)
except FileExistsError:
...
for name in os.listdir(saxo_plugins):
dest = os.path.join(plugins, name)
if not (os.path.exists(dest) or os.path.islink(dest)):
symlink(os.path.join(saxo_plugins, name), dest)
# Why is this being done?
with open(os.path.join(commands, "saxo.pth"), "w") as f:
f.write(saxo_path + "\n")
old_path_file = os.path.join(commands, ".saxo-path")
if os.path.islink(old_path_file):
os.remove(old_path_file)
for name in os.listdir(saxo_commands):
dest = os.path.join(commands, name)
if not (os.path.exists(dest) or os.path.islink(dest)):
symlink(os.path.join(saxo_commands, name), dest)
# Clean up any broken symlinks
for directory in (plugins, commands):
for name in os.listdir(directory):
link = os.path.join(directory, name)
if not os.path.islink(link):
continue
target = os.readlink(link)
target = os.path.join(directory, target)
if not os.path.exists(target):
os.remove(link)
def b64pickle(obj):
pickled = pickle.dumps(obj)
return base64.b64encode(pickled)
def b64unpickle(data):
if data:
pickled = base64.b64decode(data)
return pickle.loads(pickled)
return tuple()
def thread(target, *args):
t = threading.Thread(target=target, args=tuple(args), daemon=True)
t.start()
return t
def tarjan(graph):
# Robert E. Tarjan's 1975 strongly connected nodes algorithm
# This is a kind of robust topological sort
index = {}
lowlinks = {}
stack = collections.OrderedDict()
result = []
def search(node):
index[node] = len(index)
lowlinks[node] = index[node]
stack[node] = None
for succ in graph.get(node, ()):
if succ not in index:
search(succ)
lowlinks[node] = min(lowlinks[succ], lowlinks[node])
elif succ in stack:
lowlinks[node] = min(lowlinks[node], index[succ])
if lowlinks[node] == index[node]:
connected = []
succ = None
while succ != node:
succ = stack.popitem()[0]
connected.append(succ)
result.append(connected)
for node in graph:
if not node in index:
search(node)
return result
def tsort(graph):
for connected in tarjan(graph):
for node in connected:
yield node