-
Notifications
You must be signed in to change notification settings - Fork 1
/
ash.py
executable file
·118 lines (99 loc) · 3.18 KB
/
ash.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
#!/usr/bin/env monkeyrunner
#!/usr/bin/env python
"""Main entry point of Ash. Run with CLI."""
__author__ = "SeongJae Park"
__email__ = "[email protected]"
__copyright__ = "Copyright (c) 2011-2013, SeongJae Park"
__license__ = "GPLv3"
#from java.lang import System
import socket
import sys
#if System.getProperty("os.name").startswith("Windows"):
# import os
# srcFileDir = os.path.dirname(os.path.abspath(__file__))
# os.chdir(srcFileDir)
# sys.path = [srcFileDir] + sys.path
import ashmon
import ashval
import modglob
import log
TAG = "Ash"
# Make variables global between modules.
modglob._conn_to_ashmon = False
modglob.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
modglob.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def print_result(result, depth=0):
if result.__class__ == list:
for sub_result in result:
if sub_result.__class__ == list:
print_result(sub_result, depth + 1)
elif sub_result: print depth * '\t' + '%s' % sub_result
else:
print result
def _get_expression():
user_expr = raw_input("ash$ ")
while user_expr[-1] == '\\':
user_expr = user_expr[0:-1]
user_expr += raw_input(" ")
return user_expr
def connect_ashmon(address):
modglob.sock.connect((address, ashmon.ASH_CONN_PORT))
print "connected!"
modglob._conn_to_ashmon = True
def disconnect_ashmon():
modglob._conn_to_ashmon = False
# Let's do ashval using only this.
def input(expr):
return ashval.ashval(expr)
def exec_script(file_path):
f = open(file_path, "r")
lines = f.readlines()
f.close()
expr = ''
for line in lines:
if line[-1] == '\n':
line = line[0:-1]
if line == '' or line[0] == '#':
continue
expr += line
if expr[-1] == '\\':
expr = expr[0:-1]
elif expr != '':
result = input(expr)
if result:
print_result(result)
expr = ''
def _get_and_process_user_input():
user_input = _get_expression()
if (user_input == ""):
# TODO: print help message.
return
result = None
if modglob._conn_to_ashmon:
modglob.sock.sendall(user_input + ashmon.END_OF_MSG)
tokens = ''
while True:
received = modglob.sock.recv(1024)
if not received or not modglob._conn_to_ashmon:
print "connection crashed!"
modglob.sock.close()
modglob.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
modglob.sock.setsockopt(
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
modglob._conn_to_ashmon = False
break
msgs, tokens = ashmon.get_complete_message(
received, tokens)
for msg in msgs:
result = eval(msg)
break
else:
result = ashval.ashval(user_input)
if result: print_result(result)
if __name__ == "__main__":
if len(sys.argv) >= 2:
init_cmd = "exec_script " + sys.argv[1]
ashval.ashval(init_cmd)
while (1):
_get_and_process_user_input()