File: d_scan.py

package info (click to toggle)
lysdr 1.0~git20141206+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 832 kB
  • ctags: 1,685
  • sloc: python: 11,659; ansic: 1,335; makefile: 24
file content (133 lines) | stat: -rw-r--r-- 2,981 bytes parent folder | download | duplicates (16)
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
#! /usr/bin/env python
# encoding: utf-8
# WARNING! All changes made to this file will be lost!

import re
from waflib import Utils,Logs
def filter_comments(filename):
	txt=Utils.readf(filename)
	i=0
	buf=[]
	max=len(txt)
	begin=0
	while i<max:
		c=txt[i]
		if c=='"'or c=="'":
			buf.append(txt[begin:i])
			delim=c
			i+=1
			while i<max:
				c=txt[i]
				if c==delim:break
				elif c=='\\':
					i+=1
				i+=1
			i+=1
			begin=i
		elif c=='/':
			buf.append(txt[begin:i])
			i+=1
			if i==max:break
			c=txt[i]
			if c=='+':
				i+=1
				nesting=1
				c=None
				while i<max:
					prev=c
					c=txt[i]
					if prev=='/'and c=='+':
						nesting+=1
						c=None
					elif prev=='+'and c=='/':
						nesting-=1
						if nesting==0:break
						c=None
					i+=1
			elif c=='*':
				i+=1
				c=None
				while i<max:
					prev=c
					c=txt[i]
					if prev=='*'and c=='/':break
					i+=1
			elif c=='/':
				i+=1
				while i<max and txt[i]!='\n':
					i+=1
			else:
				begin=i-1
				continue
			i+=1
			begin=i
			buf.append(' ')
		else:
			i+=1
	buf.append(txt[begin:])
	return buf
class d_parser(object):
	def __init__(self,env,incpaths):
		self.allnames=[]
		self.re_module=re.compile("module\s+([^;]+)")
		self.re_import=re.compile("import\s+([^;]+)")
		self.re_import_bindings=re.compile("([^:]+):(.*)")
		self.re_import_alias=re.compile("[^=]+=(.+)")
		self.env=env
		self.nodes=[]
		self.names=[]
		self.incpaths=incpaths
	def tryfind(self,filename):
		found=0
		for n in self.incpaths:
			found=n.find_resource(filename.replace('.','/')+'.d')
			if found:
				self.nodes.append(found)
				self.waiting.append(found)
				break
		if not found:
			if not filename in self.names:
				self.names.append(filename)
	def get_strings(self,code):
		self.module=''
		lst=[]
		mod_name=self.re_module.search(code)
		if mod_name:
			self.module=re.sub('\s+','',mod_name.group(1))
		import_iterator=self.re_import.finditer(code)
		if import_iterator:
			for import_match in import_iterator:
				import_match_str=re.sub('\s+','',import_match.group(1))
				bindings_match=self.re_import_bindings.match(import_match_str)
				if bindings_match:
					import_match_str=bindings_match.group(1)
				matches=import_match_str.split(',')
				for match in matches:
					alias_match=self.re_import_alias.match(match)
					if alias_match:
						match=alias_match.group(1)
					lst.append(match)
		return lst
	def start(self,node):
		self.waiting=[node]
		while self.waiting:
			nd=self.waiting.pop(0)
			self.iter(nd)
	def iter(self,node):
		path=node.abspath()
		code="".join(filter_comments(path))
		names=self.get_strings(code)
		for x in names:
			if x in self.allnames:continue
			self.allnames.append(x)
			self.tryfind(x)
def scan(self):
	env=self.env
	gruik=d_parser(env,self.generator.includes_nodes)
	node=self.inputs[0]
	gruik.start(node)
	nodes=gruik.nodes
	names=gruik.names
	if Logs.verbose:
		Logs.debug('deps: deps for %s: %r; unresolved %r'%(str(node),nodes,names))
	return(nodes,names)