~elementary-apps/noise/trunk

« back to all changes in this revision

Viewing changes to .waf-1.6.2-ad4cc42bd7d347f7e283789e711b993f/waflib/Tools/d_scan.py

  • Committer: Scott Ringwelski
  • Date: 2011-02-10 21:30:53 UTC
  • Revision ID: sgringwe@mtu.edu-20110210213053-d3c7mnexeref3cwj
sexy icons, sexy waf

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# encoding: utf-8
 
3
# WARNING! All changes made to this file will be lost!
 
4
 
 
5
import re
 
6
from waflib import Utils,Logs
 
7
def filter_comments(filename):
 
8
        txt=Utils.readf(filename)
 
9
        i=0
 
10
        buf=[]
 
11
        max=len(txt)
 
12
        begin=0
 
13
        while i<max:
 
14
                c=txt[i]
 
15
                if c=='"'or c=="'":
 
16
                        buf.append(txt[begin:i])
 
17
                        delim=c
 
18
                        i+=1
 
19
                        while i<max:
 
20
                                c=txt[i]
 
21
                                if c==delim:break
 
22
                                elif c=='\\':
 
23
                                        i+=1
 
24
                                i+=1
 
25
                        i+=1
 
26
                        begin=i
 
27
                elif c=='/':
 
28
                        buf.append(txt[begin:i])
 
29
                        i+=1
 
30
                        if i==max:break
 
31
                        c=txt[i]
 
32
                        if c=='+':
 
33
                                i+=1
 
34
                                nesting=1
 
35
                                c=None
 
36
                                while i<max:
 
37
                                        prev=c
 
38
                                        c=txt[i]
 
39
                                        if prev=='/'and c=='+':
 
40
                                                nesting+=1
 
41
                                                c=None
 
42
                                        elif prev=='+'and c=='/':
 
43
                                                nesting-=1
 
44
                                                if nesting==0:break
 
45
                                                c=None
 
46
                                        i+=1
 
47
                        elif c=='*':
 
48
                                i+=1
 
49
                                c=None
 
50
                                while i<max:
 
51
                                        prev=c
 
52
                                        c=txt[i]
 
53
                                        if prev=='*'and c=='/':break
 
54
                                        i+=1
 
55
                        elif c=='/':
 
56
                                i+=1
 
57
                                while i<max and txt[i]!='\n':
 
58
                                        i+=1
 
59
                        else:
 
60
                                begin=i-1
 
61
                                continue
 
62
                        i+=1
 
63
                        begin=i
 
64
                        buf.append(' ')
 
65
                else:
 
66
                        i+=1
 
67
        buf.append(txt[begin:])
 
68
        return buf
 
69
class d_parser(object):
 
70
        def __init__(self,env,incpaths):
 
71
                self.allnames=[]
 
72
                self.re_module=re.compile("module\s+([^;]+)")
 
73
                self.re_import=re.compile("import\s+([^;]+)")
 
74
                self.re_import_bindings=re.compile("([^:]+):(.*)")
 
75
                self.re_import_alias=re.compile("[^=]+=(.+)")
 
76
                self.env=env
 
77
                self.nodes=[]
 
78
                self.names=[]
 
79
                self.incpaths=incpaths
 
80
        def tryfind(self,filename):
 
81
                found=0
 
82
                for n in self.incpaths:
 
83
                        found=n.find_resource(filename.replace('.','/')+'.d')
 
84
                        if found:
 
85
                                self.nodes.append(found)
 
86
                                self.waiting.append(found)
 
87
                                break
 
88
                if not found:
 
89
                        if not filename in self.names:
 
90
                                self.names.append(filename)
 
91
        def get_strings(self,code):
 
92
                self.module=''
 
93
                lst=[]
 
94
                mod_name=self.re_module.search(code)
 
95
                if mod_name:
 
96
                        self.module=re.sub('\s+','',mod_name.group(1))
 
97
                import_iterator=self.re_import.finditer(code)
 
98
                if import_iterator:
 
99
                        for import_match in import_iterator:
 
100
                                import_match_str=re.sub('\s+','',import_match.group(1))
 
101
                                bindings_match=self.re_import_bindings.match(import_match_str)
 
102
                                if bindings_match:
 
103
                                        import_match_str=bindings_match.group(1)
 
104
                                matches=import_match_str.split(',')
 
105
                                for match in matches:
 
106
                                        alias_match=self.re_import_alias.match(match)
 
107
                                        if alias_match:
 
108
                                                match=alias_match.group(1)
 
109
                                        lst.append(match)
 
110
                return lst
 
111
        def start(self,node):
 
112
                self.waiting=[node]
 
113
                while self.waiting:
 
114
                        nd=self.waiting.pop(0)
 
115
                        self.iter(nd)
 
116
        def iter(self,node):
 
117
                path=node.abspath()
 
118
                code="".join(filter_comments(path))
 
119
                names=self.get_strings(code)
 
120
                for x in names:
 
121
                        if x in self.allnames:continue
 
122
                        self.allnames.append(x)
 
123
                        self.tryfind(x)
 
124
def scan(self):
 
125
        env=self.env
 
126
        gruik=d_parser(env,self.generator.includes_nodes)
 
127
        node=self.inputs[0]
 
128
        gruik.start(node)
 
129
        nodes=gruik.nodes
 
130
        names=gruik.names
 
131
        if Logs.verbose:
 
132
                Logs.debug('deps: deps for %s: %r; unresolved %r'%(str(node),nodes,names))
 
133
        return(nodes,names)