forked from catboost/catboost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxs.py
More file actions
74 lines (53 loc) · 1.95 KB
/
Copy pathxs.py
File metadata and controls
74 lines (53 loc) · 1.95 KB
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
import os
import re
import _import_wrapper as iw
class XsParser(object):
def __init__(self, path, unit):
self._path = path
retargeted = os.path.join(unit.path(), os.path.basename(path))
with open(path, 'rb') as f:
includes, induced = XsParser.parse_includes(f.readlines())
self._includes = unit.resolve_include([retargeted] + includes) if includes else []
self._induced = unit.resolve_include([retargeted] + induced) if induced else []
@staticmethod
def parse_includes(lines):
includes = []
induced = []
include_pattern = re.compile(r'INCLUDE\s*:\s*(?P<path>\S*)')
induced_pattern = re.compile(r'\#include\s*["<](?P<path>[^">]*)')
for line in lines:
line = line.lstrip()
comment_pos = line.find('//')
if comment_pos != -1:
line = line[:comment_pos] # assumes there are no cases like #include "a//b/c.h"
if line.startswith('#include'):
m = induced_pattern.match(line)
if m:
induced.append(m.group('path'))
elif line.startswith('INCLUDE'):
m = include_pattern.match(line)
if m:
includes.append(m.group('path'))
return includes, induced
def includes(self):
return self._includes
def induced_deps(self):
return {'cpp': self._induced}
def init():
if 1:
iw.addparser('xs', XsParser, {'xs': 'use', 'xscpp': 'pass'})
# ----------------Plugin test------------------ #
def test_include_parser():
text = '''
aaaaa
#include <induced1>
# include <not_induced>
#include<induced2>
INCLUDE: included1
INCLUDE : included2
INCLUDE: included3 // asdasd
//INCLUDE : not_included
'''
includes, induced = XsParser.parse_includes(text.split('\n'))
assert includes == ['included1', 'included2', 'included3', ]
assert induced == ['induced1', 'induced2']