-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
1095 lines (878 loc) · 24 KB
/
util.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import textwrap
import json
import pprint
import queue
import re
import hashlib
from os.path import isfile, join, relpath, islink, \
isdir, exists, basename, abspath, \
dirname, expanduser, splitext
import os
import sys
import shutil
import yaml
import threading
from semver import VersionInfo
import subprocess
from subprocess import CalledProcessError
import globber
from distutils import dir_util
LANGUAGES = [
'java', 'javascript', 'python',
'ruby', 'csharp', 'cpp', 'go'
]
def compiled_langs():
return [
'java',
'cpp',
'csharp'
]
def searchpath_append(searchpath, appendme):
return (searchpath + ':' + appendme) if searchpath else appendme
def isemptydir(directory, ignoredotfiles=False):
return isdir(directory) and not bool(
list(
filter(
lambda f: not ignoredotfiles or f[0] != '.',
os.listdir(directory)
)
)
)
def clear_dir(dirpath):
for n in os.listdir(dirpath):
f = join(dirpath, n)
if isfile(f):
os.unlink(f)
elif isdir(f):
shutil.rmtree(f)
def scriptdir():
return dirname(__file__)
def templatedir():
return join(scriptdir(), 'templates')
def commondir():
return join(templatedir(), 'common')
def tailor_template(
outdir,
lang, basename,
outname,
):
shutil.copytree(
join(commondir(), 'scripts'),
outdir,
dirs_exist_ok=True,
)
testpack_template = join(templatedir(), lang, 'unit-tests')
testpack = join(outdir, 'unit-tests')
shutil.copytree(
testpack_template,
testpack
)
pack_add_dep(
testpack,
outname,
'*'
)
set_pack_name(
testpack,
f'{outname}-tests',
)
str2file(
join(outdir, 'Customizations.qll'),
file2str(join(outdir, 'Customizations.qll')).format(
language=lang,
)
)
str2file(
join(outdir, 'Makefile'),
file2str(join(outdir, 'Makefile')).format(
basename=basename,
)
)
testqlref = lambda num: file2str(join(testpack_template, f'test_{num}', 'query.qlref')).strip()
str2file(
join(outdir, 'customize'),
file2str(join(outdir, 'customize')).format(
outname=outname,
defaultsuite=f'codeql-suites/{lang}-code-scanning.qls',
querypath1=testqlref(1),
querypath2=testqlref(2),
securityfolder=lang_security_query_dir(lang),
)
)
return
def lang_security_query_dir(lang):
if lang == 'csharp':
return 'Security Features'
elif lang == 'ruby':
return 'queries/security'
elif lang in ['java', 'cpp', 'python', 'javascript', 'go']:
return 'Security'
else:
error(f'Unsupported language "{lang}"!')
def hashstr(s):
sha1 = hashlib.sha1()
sha1.update(s.encode("utf-8"))
return sha1.hexdigest()
def add_versions(v1str, v2str):
semver1 = VersionInfo.parse(v1str)
semver2 = VersionInfo.parse(v2str)
return str(
VersionInfo(
semver1.major + semver2.major,
semver1.minor + semver2.minor,
semver1.patch + semver2.patch,
)
)
def match_version(versionstr, matchstr):
def adjust_matchstr(matchstr):
if matchstr == '*':
return '>=0.0.0'
elif matchstr[0].isdigit():
return '==' + matchstr
elif matchstr[0] == '=' and matchstr[1].isdigit():
return '=' + matchstr
else:
return matchstr
return VersionInfo.parse(versionstr).match(adjust_matchstr(matchstr))
def compare_version(v1, v2):
return VersionInfo.parse(v1).compare(VersionInfo.parse(v2))
def error(msg):
sys.exit('ERROR: ' + msg)
def info(msg):
print('INFO: ' + msg, flush=True)
def warning(msg):
print('WARNING: ' + msg, flush=True)
def file2str(filepath):
with open(filepath, 'r') as f:
return f.read()
def str2file(filepath, string):
with open(filepath, 'w') as f:
f.write(string)
def codeql_pack_lock_yml(ppath):
return join(ppath, 'codeql-pack.lock.yml')
def qlpackyml(ppath):
return join(ppath, 'qlpack.yml')
def is_pack(ppath):
return isfile(qlpackyml(ppath))
def get_pack_lang(ppath):
info = get_pack_info(ppath)
lang = info.get('extractor', None)
if lang is None:
for l in LANGUAGES:
if isdir(
join(
ppath,
'.codeql',
'libraries',
'codeql',
f'{l}-all'
)
):
lang = l
break
return lang
def get_pack_info(ppath):
with open(qlpackyml(ppath), 'r') as f:
return yaml.safe_load(f)
def default_pack_lock_info():
return {
'dependencies': {},
'compiled': False,
'lockVersion': '1.0.0'
}
def get_pack_lock_info(ppath, default=None):
fpath = codeql_pack_lock_yml(ppath)
if isfile(fpath):
with open(fpath, 'r') as f:
return yaml.safe_load(f)
return default
def set_pack_lock_info(ppath, info):
with open(codeql_pack_lock_yml(ppath), 'w') as f:
yaml.dump(info, f)
def parse_version(vstr):
if vstr[0:2] in ('<=', '>='):
idx = 2
elif vstr[0:1] in '<>=':
idx = 1
else:
idx = 0
if vstr == '*' or VersionInfo.isvalid(vstr[idx:]):
return vstr
error(
(f'Invalid pack version: "{vstr}". ' +
'Only "*", match expressions or concrete versions ' +
'(e.g. "1.0.0") are permitted!')
)
def rglob(dirpath, pattern, hidden=False):
for f in listdir(dirpath, hidden=hidden):
file2match = relpath(f, dirpath)
if globber.match(pattern, file2match):
yield f
def listdir(dirpath, hidden=False):
dirs = queue.Queue()
dirs.put(dirpath)
while not dirs.empty():
d = dirs.get()
for f in sorted(os.listdir(d)):
if (hidden == False and f[0] == '.'):
continue
absf = join(d, f)
if isdir(absf):
dirs.put(absf)
yield absf
def hash_dir(dirpath):
def hash_file(path, h):
if islink(path):
h.update(b'link')
h.update(
os.readlink(path).encode('utf-8')
)
elif isfile(path):
h.update(b'file')
if basename(path) == 'qlpack.yml':
with open(path, 'r') as f:
y = yaml.safe_load(f) or {}
y.get('buildMetadata', {}) \
.pop('creationTime', None)
y.pop('version', None)
h.update(
pprint.pformat(
y,
sort_dicts=True,
indent=1
).encode('utf-8')
)
else:
with open(path, 'rb') as f:
while True:
bs = f.read(4096)
if bs == b'':
break
h.update(bs)
elif isdir(path):
h.update(b'directory')
else:
error(f'Unexpected file type for "{path}"!')
h = hashlib.sha1()
for f in listdir(dirpath):
hash_file(f, h)
h.update(relpath(f, dirpath).encode('utf-8'))
return h.hexdigest()
def hash_pack(ppath):
return hash_dir(ppath)
def cmp_packs(ppath1, ppath2):
return hash_pack(ppath1) == hash_pack(ppath2)
def set_pack_info(ppath, info):
with open(qlpackyml(ppath), 'w') as f:
yaml.dump(info, f)
def get_pack_value(ppath, key, default=None):
return get_pack_info(ppath).get(key, default)
def set_pack_value(ppath, key, value):
info = get_pack_info(ppath)
info[key] = value
set_pack_info(ppath, info)
return value
def get_pack_name(ppath):
return get_pack_value(ppath, 'name')
def set_pack_name(ppath, name):
set_pack_value(ppath, 'name', name)
def get_pack_version(ppath):
return get_pack_value(ppath, 'version', '0.0.0')
def get_pack_cli_version(ppath, default='0.0.0'):
return get_pack_info(ppath) \
.get('buildMetadata', {}) \
.get('cliVersion', default)
def set_pack_version(ppath, version):
return set_pack_value(ppath, 'version', version)
def set_pack_defaultsuite(ppath, value):
set_pack_value(ppath, 'defaultSuiteFile', value)
def pack_add_dep(ppath, name, version):
deps = get_pack_value(ppath, 'dependencies', {})
deps[name] = version
set_pack_value(ppath, 'dependencies', deps)
def search_manifest_dir(path):
current = abspath(path)
while True:
manifest = join(current, '.codeqlmanifest.json')
if isfile(manifest):
return current
parent = abspath(join(current, os.pardir))
if parent == abspath(current):
return None
current = parent
def print_to_stdout(cmd, stream):
while True:
line = stream.readline()
if line == '':
break
print(line, end='', flush=True)
stream.close()
def close_stdin(cmd, stream):
stream.close()
class Recorder:
def __init__(self):
self.lines = []
def __call__(self, cmd, stream):
while True:
line = stream.readline()
if line == '':
break
self.lines.append(line)
stream.close()
class Executable:
def __init__(self, executable):
self.executable = executable
def __call__(
self,
*args,
outconsumer=print_to_stdout,
errconsumer=print_to_stdout,
combine_std_out_err=True,
inprovider=close_stdin,
cwd='.',
env=None,
):
outpipe = subprocess.PIPE
errpipe = subprocess.PIPE
if combine_std_out_err:
errpipe = subprocess.STDOUT
inpipe = subprocess.PIPE
command = [self.executable] + list(args)
with subprocess.Popen(
command,
bufsize = 1,
universal_newlines=True,
stdout=outpipe,
stderr=errpipe,
stdin=inpipe,
cwd=cwd,
env=env,
) as proc:
commandstr = ' '.join(command)
tout = threading.Thread(target=outconsumer, args=(commandstr, proc.stdout))
tout.start()
terr = None
if not combine_std_out_err:
terr = threading.Thread(target=errconsumer, args=(commandstr, proc.stderr))
terr.start()
tin = threading.Thread(target=inprovider, args=(commandstr, proc.stdin))
tin.start()
ret = proc.wait()
tout.join()
tin.join()
if terr:
terr.join()
if ret != 0:
raise CalledProcessError(cmd=commandstr, returncode=ret)
def exec_from_path_env(execname):
e = shutil.which(execname)
return Executable(e) if e else None
def codeql_dist_from_path_env():
codeql = exec_from_path_env(codeql_exec_name())
if codeql:
rec = Recorder()
codeql(
'version',
'--format', 'json',
combine_std_out_err=False,
outconsumer=rec
)
return json.loads(''.join(rec.lines))['unpackedLocation']
return None
def gh_token():
gh = exec_from_path_env('gh')
if gh:
try:
rec = Recorder()
gh(
'auth',
'status',
'--show-token',
combine_std_out_err=True,
outconsumer=rec
)
m = re.match(
'^.*Token: ([^\s]+).*$',
''.join(rec.lines),
flags=re.DOTALL
)
if m:
return m.group(1)
except CalledProcessError:
pass
return None
def env_with_token():
env = os.environ.copy()
token = env.get('GITHUB_TOKEN')
if not token:
info('Environment variable "GITHUB_TOKEN" is not set. Attempting to use token of the "gh" cli...')
token = gh_token()
if token:
env['GITHUB_TOKEN'] = token
else:
warning('Unable to get a valid "GITHUB_TOKEN", please set it manually if needed.')
return env
def codeql_dist_from_gh_codeql():
gh = exec_from_path_env('gh')
if gh:
try:
rec = Recorder()
gh(
'codeql',
'version',
'--format', 'json',
combine_std_out_err=False,
outconsumer=rec
)
return json.loads(''.join(rec.lines))['unpackedLocation']
except CalledProcessError:
pass
return None
def codeql_exec_name():
return 'codeql' + ("" if os.name == 'posix' else '.exe')
class CodeQL(Executable):
def __init__(self, distdir, additional_packs=None, search_path=None):
Executable.__init__(self, join(distdir, codeql_exec_name()))
self.distdir = distdir
self.additional_packs = additional_packs
self.search_path = search_path
def make_search_path_args(self):
args = []
if self.additional_packs:
args.append('--additional-packs')
args.append(self.additional_packs)
if self.search_path:
args.append('--search-path')
args.append(self.search_path)
return args
def make_lockfile(
self, ppath,
qlpackyml_backup_file,
match_cli=True, mode='merge-update'
):
if mode == 'update':
if isfile(codeql_pack_lock_yml(ppath)):
os.remove(codeql_pack_lock_yml(ppath))
li = get_pack_lock_info(ppath, default_pack_lock_info())
pi = get_pack_info(ppath)
lideps = li.get('dependencies') or {}
pideps = pi.get('dependencies') or {}
pi['dependencies'] = pideps
for d, v in pideps.items():
if d in lideps:
pideps[d] = lideps[d]['version']
else:
resolvedv = self.resolve_pack_version(
d,
v,
match_cli=match_cli
)
if resolvedv is None:
error(f'Could not resolve {d}@{v}!')
pideps[d] = resolvedv
shutil.copyfile(qlpackyml(ppath), qlpackyml_backup_file)
try:
set_pack_info(ppath, pi)
self.install(ppath, mode='update')
finally:
shutil.copyfile(qlpackyml_backup_file, qlpackyml(ppath))
def autoversion(self, ppath, mode, fail):
# set version and check uploadability
packname = get_pack_name(ppath)
packversion = get_pack_version(ppath)
def success():
info('Package does not exist yet, upload will succeed.')
if mode == 'manual':
if self.download_pack(
packname,
packversion,
use_search_path=False,
match_cli=False
):
warning(f'Package {packname}@{packversion} already exists!')
if fail:
return 2
else:
success()
elif mode == 'new':
latestpeer = self.download_pack(
packname,
'*',
use_search_path=False,
match_cli=False
)
if latestpeer:
identical = cmp_packs(ppath, latestpeer)
if identical:
warning('This pack and its latest version in the registry are identical!')
if fail:
return 2
else:
info('This pack and its latest version in the registry differ!')
newv = set_pack_version(
ppath,
add_versions(
get_pack_version(latestpeer),
'0.0.1',
)
)
info(f'Bumped version to "{newv}".')
else:
success()
elif mode == 'new-on-collision':
if self.download_pack(
packname,
packversion,
use_search_path=False,
match_cli=False
):
info(f'Package {packname}@{packversion} already exists.')
return self.autoversion(ppath, 'new', fail)
else:
success()
else:
error(f'Unknown mode "{mode}"!')
return 0
def install(self, ppath, mode='use-lock'):
self(
'pack', 'install',
'--mode', mode,
ppath
)
def create_inplace(self, ppath, tmppath):
self(
'pack', 'create',
'--threads', '0',
'--output', tmppath,
'-vv',
ppath
)
tmppack = join(
tmppath,
get_pack_name(ppath),
get_pack_version(ppath),
)
clear_dir(ppath)
dir_util.copy_tree(tmppack, ppath)
shutil.rmtree(tmppath)
def create(self, ppath, outdir, tmppath):
self(
'pack', 'create',
'--threads', '0',
'--output', tmppath,
'-vv',
ppath
)
tmppack = join(
tmppath,
get_pack_name(ppath),
get_pack_version(ppath),
)
shutil.copytree(tmppack, outdir)
shutil.rmtree(tmppath)
def list_packs(self, use_search_path=True, use_pack_cache=True):
if use_search_path:
rec = Recorder()
self(
'resolve', 'qlpacks',
'--format', 'json',
*self.make_search_path_args(),
combine_std_out_err=False,
outconsumer=rec,
)
j = json.loads(''.join(rec.lines))
for k in j:
for v in j[k]:
yield v
if use_pack_cache:
packcache = expanduser('~/.codeql/packages')
for p in rglob(packcache, '**'):
if is_pack(p):
yield p
def get_pack(self, packname, matchstr, use_search_path=True, use_pack_cache=True):
latestp = None
latestv = '0.0.0'
for p in self.list_packs(
use_search_path=use_search_path,
use_pack_cache=use_pack_cache,
):
if get_pack_name(p) == packname:
v = get_pack_version(p)
if match_version(v, matchstr) and compare_version(v, latestv) >= 0:
latestv = v
latestp = p
return latestp
def get_version(self):
rec = Recorder()
self(
'version',
'--format', 'json',
combine_std_out_err=False,
outconsumer=rec,
)
return json.loads(''.join(rec.lines))['version']
def download_pack(
self,
pname,
matchstr,
match_cli=True,
use_search_path=True
):
cli_version = self.get_version()
info(f'Downloading {pname}@{matchstr}...')
p = self.download_pack_impl(
pname,
matchstr,
use_search_path=use_search_path
)
while True:
if not(p and match_cli):
return p
pv = get_pack_version(p)
if not match_version(pv, matchstr):
return None
if compare_version(
get_pack_cli_version(p, cli_version),
cli_version
) <= 0:
return p
pred = '<' + pv
info(f'Previous pack was incompatible with this CLI, downloading {pname}@{pred} instead...')
p = self.download_pack_impl(
pname,
pred,
use_search_path=use_search_path
)
def resolve_pack_version(
self, pname, matchstr,
default=None, match_cli=True,
use_search_path=True
):
pack = self.download_pack(
pname,
matchstr,
match_cli=match_cli,
use_search_path=use_search_path
)
return get_pack_version(pack) if pack else default
def download_pack_impl(self, packname, matchstr, use_search_path=True):
not_found = set()
def errgobbler(cmd, stream):
while True:
line = stream.readline()
if line == '':
break
if re.match(
".*A fatal error occurred: '.*' not found in the registry .*",
line,
):
not_found.add(1)
else:
print(line, end='', flush=True)
stream.close()
try:
rec = Recorder()
search_path = self.make_search_path_args() if use_search_path else []
self(
'pack', 'download',
'--format', 'json',
*search_path,
packname + '@' + matchstr,
combine_std_out_err=False,
errconsumer=errgobbler,
outconsumer=rec,
env=env_with_token(),
)
j = json.loads(''.join(rec.lines))
latestv = None
for p in j.get('packs', {}):
pv = p['version']
if not(
p['name'] == packname and \
match_version(pv, matchstr)
):
return None
if latestv is None or compare_version(pv, latestv) >= 0:
latestv = pv
# TODO: Remove this once 'codeql download' always returns json
# data with information about the downloaded packages
# for now this is necessary, unfortunately.
if latestv:
return self.get_pack(
packname,
latestv,
use_search_path=True,
use_pack_cache=True
)
else:
return self.get_pack(
packname,
matchstr,
use_search_path=True,
use_pack_cache=False
)
except CalledProcessError as e:
if not_found:
return None
else:
raise
def is_dist(directory):
return (
isfile(join(directory, codeql_exec_name())) and
isdir(join(directory, 'tools'))
)
def copy2dir(srcroot, srcpattern, dstroot, dstdirpattern, hidden=False):
executed = False
dstdirs = list(rglob(dstroot, dstdirpattern, hidden=hidden))
if dstdirpattern in ['', '.']:
dstdirs.append(dstroot)
for srcfile in rglob(srcroot, srcpattern, hidden=hidden):
for dstdir in dstdirs:
if not isdir(dstdir):
error(f'"{dstdir}" is not a directory!')
dstpath = join(dstdir, basename(srcfile))
if isfile(srcfile):
shutil.copyfile(srcfile, dstpath)
else:
shutil.copytree(srcfile, dstpath, dirs_exist_ok=True)
executed = True
return executed
def delete_ql_meta(qlfile, key):
before, metadata, after = dissect_query(file2str(qlfile))
metadata = list(
filter(
lambda el: el[0] != key,
metadata
)
)
str2file(
qlfile,
assemble_query(before, metadata, after),
)
info(f'Deleted metadata key "{key}" in "{qlfile}".')
def set_ql_meta(qlfile, key, value):
before, metadata, after = dissect_query(file2str(qlfile))
modified = False
for i, (k, v) in enumerate(metadata):
if k == key:
metadata[i] = (key, value)
modified = True
break
if not modified:
metadata.append((key, value))
str2file(
qlfile,
assemble_query(before, metadata, after),
)
info(f'Set metadata key "{key}" to value "{value}" in "{qlfile}".')
def assemble_query(before, metadata, after):
return \
before \
+ '\n'.join(['/**'] + [f' * @{k} {v}' for k, v in metadata] + [' */']) \
+ after
def dissect_query(qlstr):
# extract metadata section
m = re.match(
'^(.*?)(/\*\*(.*?)\*/)?(.*)$',
qlstr,
flags=re.DOTALL
)
if not m: # there should ALWAYS be a match!
raise Exception('Internal Error')
before, metadata_section, after = m.group(1), m.group(3), m.group(4)
metadata_section = metadata_section or ''
lines = re.split(
'^\s*\*?\s*@',
metadata_section,
flags=re.MULTILINE,
)[1:]
result = []
for l in lines:
key, value = re.split('\s', l, maxsplit=1)
value = re.sub(
'^\s*\*?\s*',
'',
value,
flags=re.MULTILINE
)
value = re.sub('\s+', ' ', value).strip()
result.append((key, value))
return before, result, after
def is_qlfile(path):
return isfile(path) and splitext(path)[1] == '.ql'