| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import os |
| 9 |
import backend |
| 10 |
import CtagsManager |
| 11 |
from collections import OrderedDict |
| 12 |
|
| 13 |
is_debug = os.getenv('SEASCOPE_WEB_DEBUG') |
| 14 |
|
| 15 |
g_proj_d = OrderedDict() |
| 16 |
|
| 17 |
def p_open(proj_path): |
| 18 |
bl = backend.plugin_guess(proj_path) |
| 19 |
if len(bl) == 0: |
| 20 |
msg = "Project '%s': No backend is interested" % proj_path |
| 21 |
print(msg) |
| 22 |
return None |
| 23 |
proj_type = bl[0] |
| 24 |
if len(bl) > 1: |
| 25 |
msg = "Project '%s': Many backends interested" % proj_path |
| 26 |
for b in be: |
| 27 |
msg += '\n\t' + b.name() |
| 28 |
msg += '\n\nGoing ahead with: ' + proj_type |
| 29 |
print(msg) |
| 30 |
print("Project '%s': using '%s' backend" % (proj_path, proj_type)) |
| 31 |
|
| 32 |
bp = backend.BProject() |
| 33 |
rc = bp.proj_open(proj_path, proj_type) |
| 34 |
if rc: |
| 35 |
return bp |
| 36 |
else: |
| 37 |
return None |
| 38 |
|
| 39 |
def find_prj(rd): |
| 40 |
pdir = rd.get('project', None) |
| 41 |
if not pdir: |
| 42 |
return None |
| 43 |
bp = g_proj_d.get(pdir, False) |
| 44 |
return bp |
| 45 |
|
| 46 |
def p_query(bp, rquery): |
| 47 |
return bp.proj_query(rquery) |
| 48 |
|
| 49 |
def p_query_fl(bp): |
| 50 |
return bp.proj_query_fl() |
| 51 |
|
| 52 |
def validate_op(rd): |
| 53 |
for r in ['cmd_type', 'cmd_str', 'req']: |
| 54 |
if r not in rd: |
| 55 |
return False |
| 56 |
return True |
| 57 |
|
| 58 |
def _run_op(rd): |
| 59 |
if is_debug: |
| 60 |
print('rd', rd) |
| 61 |
|
| 62 |
if not validate_op: |
| 63 |
return { 'err_data' : 'invalid request: validate_op failed' } |
| 64 |
|
| 65 |
cmd_type = rd['cmd_type'] |
| 66 |
|
| 67 |
if cmd_type == 'project_list': |
| 68 |
res = list(g_proj_d.keys()) |
| 69 |
outd = { 'res' : res } |
| 70 |
return outd |
| 71 |
if cmd_type == 'query': |
| 72 |
bp = find_prj(rd) |
| 73 |
if not bp: |
| 74 |
return { 'err_data' : 'failed to find project for query %s' % rd } |
| 75 |
|
| 76 |
if rd['cmd_str'] == 'FLIST': |
| 77 |
outd = p_query_fl(bp) |
| 78 |
res = [] |
| 79 |
res.append(['file', 'path']) |
| 80 |
for r in outd['res']: |
| 81 |
res.append([os.path.basename(r), r]) |
| 82 |
outd['res'] = res |
| 83 |
return outd |
| 84 |
if rd['cmd_str'] == 'FDATA': |
| 85 |
fname = rd['req'] |
| 86 |
pdir = bp.proj_dir() |
| 87 |
if not fname.startswith(pdir): |
| 88 |
return { 'err_data' : 'file %s not in project %s' % (fname, pdir)} |
| 89 |
try: |
| 90 |
data = open(fname).read() |
| 91 |
outd = { 'res' : [ data ] } |
| 92 |
return outd |
| 93 |
except: |
| 94 |
return { 'err_data' : 'failed to read file %s' % fname } |
| 95 |
if rd['cmd_str'] == 'FCTAGS': |
| 96 |
fname = rd['req'] |
| 97 |
if not fname.startswith(bp.proj_dir()): |
| 98 |
return [] |
| 99 |
try: |
| 100 |
data = CtagsManager.ct_query(rd['req']) |
| 101 |
outd = { 'res' : [data] } |
| 102 |
return outd |
| 103 |
except: |
| 104 |
pass |
| 105 |
rquery = { |
| 106 |
'cmd' : rd['cmd_str'], |
| 107 |
'req' : rd['req'], |
| 108 |
'opt' : rd.get('opt', '').split(','), |
| 109 |
'hint_file' : rd.get('hint_file', ''), |
| 110 |
} |
| 111 |
outd = p_query(bp, rquery) |
| 112 |
if is_debug: |
| 113 |
print('rquery', rquery) |
| 114 |
return outd |
| 115 |
|
| 116 |
return { 'err_data' : 'invalid request: unknown cmd_type=%s' % cmd_type } |
| 117 |
|
| 118 |
def run_op(rd): |
| 119 |
if is_debug: |
| 120 |
print('-' * 80) |
| 121 |
outd = _run_op(rd) |
| 122 |
if is_debug: |
| 123 |
print('-' * 80) |
| 124 |
|
| 125 |
if ('err_data' in outd) and outd['err_data']: |
| 126 |
print('==> ERR_DATA:', outd['err_data']) |
| 127 |
res = outd.get('res', None) |
| 128 |
if not res and 'out_data' in outd: |
| 129 |
res = outd['out_data'].splitlines() |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if is_debug: |
| 134 |
print('-' * 80) |
| 135 |
return outd |
| 136 |
|
| 137 |
def load_projects(plist): |
| 138 |
backend.load_plugins() |
| 139 |
|
| 140 |
for p in plist: |
| 141 |
bp = p_open(p) |
| 142 |
if not bp: |
| 143 |
return False |
| 144 |
global g_proj_d |
| 145 |
pdir = bp.proj_dir() |
| 146 |
g_proj_d[pdir] = bp |
| 147 |
|
| 148 |
if not len(g_proj_d): |
| 149 |
print('No projects') |
| 150 |
return False |
| 151 |
|
| 152 |
return True |