1 | from gluon.admin import * |
---|
2 | from gluon.fileutils import abspath, read_file, write_file |
---|
3 | from gluon.tools import Service |
---|
4 | from glob import glob |
---|
5 | import shutil |
---|
6 | import platform |
---|
7 | import time |
---|
8 | import base64 |
---|
9 | import os |
---|
10 | from gluon._compat import StringIO |
---|
11 | |
---|
12 | |
---|
13 | service = Service(globals()) |
---|
14 | |
---|
15 | @service.jsonrpc |
---|
16 | def login(): |
---|
17 | "dummy function to test credentials" |
---|
18 | return True |
---|
19 | |
---|
20 | |
---|
21 | @service.jsonrpc |
---|
22 | def list_apps(): |
---|
23 | "list installed applications" |
---|
24 | regex = re.compile('^\w+$') |
---|
25 | apps = [f for f in os.listdir(apath(r=request)) if regex.match(f)] |
---|
26 | return apps |
---|
27 | |
---|
28 | |
---|
29 | @service.jsonrpc |
---|
30 | def list_files(app, pattern='.*\.py$'): |
---|
31 | files = listdir(apath('%s/' % app, r=request), pattern) |
---|
32 | return [x.replace('\\', '/') for x in files] |
---|
33 | |
---|
34 | |
---|
35 | @service.jsonrpc |
---|
36 | def read_file(filename, b64=False): |
---|
37 | """ Visualize object code """ |
---|
38 | f = open(apath(filename, r=request), "rb") |
---|
39 | try: |
---|
40 | data = f.read() |
---|
41 | if not b64: |
---|
42 | data = data.replace('\r', '') |
---|
43 | else: |
---|
44 | data = base64.b64encode(data) |
---|
45 | finally: |
---|
46 | f.close() |
---|
47 | return data |
---|
48 | |
---|
49 | |
---|
50 | @service.jsonrpc |
---|
51 | def write_file(filename, data, b64=False): |
---|
52 | f = open(apath(filename, r=request), "wb") |
---|
53 | try: |
---|
54 | if not b64: |
---|
55 | data = data.replace('\r\n', '\n').strip() + '\n' |
---|
56 | else: |
---|
57 | data = base64.b64decode(data) |
---|
58 | f.write(data) |
---|
59 | finally: |
---|
60 | f.close() |
---|
61 | |
---|
62 | |
---|
63 | @service.jsonrpc |
---|
64 | def hash_file(filename): |
---|
65 | data = read_file(filename) |
---|
66 | file_hash = md5_hash(data) |
---|
67 | path = apath(filename, r=request) |
---|
68 | saved_on = os.stat(path)[stat.ST_MTIME] |
---|
69 | size = os.path.getsize(path) |
---|
70 | return dict(saved_on=saved_on, file_hash=file_hash, size=size) |
---|
71 | |
---|
72 | |
---|
73 | @service.jsonrpc |
---|
74 | def install(app_name, filename, data, overwrite=True): |
---|
75 | f = StringIO(base64.b64decode(data)) |
---|
76 | installed = app_install(app_name, f, request, filename, |
---|
77 | overwrite=overwrite) |
---|
78 | |
---|
79 | return installed |
---|
80 | |
---|
81 | |
---|
82 | @service.jsonrpc |
---|
83 | def attach_debugger(host='localhost', port=6000, authkey='secret password'): |
---|
84 | import gluon.contrib.dbg as dbg |
---|
85 | import gluon.debug |
---|
86 | from multiprocessing.connection import Listener |
---|
87 | |
---|
88 | if isinstance(authkey, unicode): |
---|
89 | authkey = authkey.encode('utf8') |
---|
90 | |
---|
91 | if not hasattr(gluon.debug, 'dbg_listener'): |
---|
92 | # create a remote debugger server and wait for connection |
---|
93 | address = (host, port) # family is deduced to be 'AF_INET' |
---|
94 | gluon.debug.dbg_listener = Listener(address, authkey=authkey) |
---|
95 | gluon.debug.dbg_connection = gluon.debug.dbg_listener.accept() |
---|
96 | # create the backend |
---|
97 | gluon.debug.dbg_debugger = dbg.Qdb(gluon.debug.dbg_connection) |
---|
98 | gluon.debug.dbg = gluon.debug.dbg_debugger |
---|
99 | # welcome message (this should be displayed on the frontend) |
---|
100 | print('debugger connected to', gluon.debug.dbg_listener.last_accepted) |
---|
101 | return True # connection successful! |
---|
102 | |
---|
103 | |
---|
104 | @service.jsonrpc |
---|
105 | def detach_debugger(): |
---|
106 | import gluon.contrib.dbg as dbg |
---|
107 | import gluon.debug |
---|
108 | # stop current debugger |
---|
109 | if gluon.debug.dbg_debugger: |
---|
110 | try: |
---|
111 | gluon.debug.dbg_debugger.do_quit() |
---|
112 | except: |
---|
113 | pass |
---|
114 | if hasattr(gluon.debug, 'dbg_listener'): |
---|
115 | if gluon.debug.dbg_connection: |
---|
116 | gluon.debug.dbg_connection.close() |
---|
117 | del gluon.debug.dbg_connection |
---|
118 | if gluon.debug.dbg_listener: |
---|
119 | gluon.debug.dbg_listener.close() |
---|
120 | del gluon.debug.dbg_listener |
---|
121 | gluon.debug.dbg_debugger = None |
---|
122 | return True |
---|
123 | |
---|
124 | |
---|
125 | def call(): |
---|
126 | session.forget() |
---|
127 | return service() |
---|