source: debug/debug_build.py @ 1345:ff8ef9c6bbb7

Revision 1345:ff8ef9c6bbb7, 7.4 KB checked in by Arc Riley <arcriley@…>, 11 years ago (diff)

Happy New Year! (copyright string update)

Line 
1# debug_build.py  Danger! Danger! Danger!
2#
3# Copyright (C) 2006,2007,2008,2009 PySoy Group
4#
5#  This program is free software; you can redistribute it and/or modify
6#  it under the terms of the GNU Affero General Public License as published
7#  by the Free Software Foundation, either version 3 of the License, or
8#  (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU Affero General Public License for more details.
14#
15#  You should have received a copy of the GNU Affero General Public License
16#  along with this program; if not, see http://www.gnu.org/licenses
17#
18# $Id: debug_build.py 1393 2008-12-31 23:51:25Z ArcRiley $
19
20import os
21
22class PXIfile(object):
23  def __init__(self, aPXI):
24    self.filePath = aPXI
25    self.lines    = iter(open(aPXI))
26    self.outText  = ''
27    self.buffer   = []
28    self.curScope = None  # 'cdef' or 'def'
29
30    ##setup
31    self.make_lines()
32    self.commit()   
33
34  def set_cur_scope_cdef(self): self.curScope = 'cdef'
35  def set_cur_scope_def(self):  self.curScope = 'def'
36
37  def make_lines(self):
38    self.buffer.append("cimport stdio\n")
39    self.buffer.append("cimport py\n")
40    lineNum  = 0
41    for line in self.lines:
42      lineNum += 1
43      nextLine  = Line(line, lineNum, self.filePath, self.curScope)
44      self.buffer.append(nextLine)
45   
46      if nextLine.get_line_type() == False: continue
47      if nextLine.get_line_type() == 'cdef':  self.set_cur_scope_cdef()
48      if nextLine.get_line_type() == 'def' :  self.set_cur_scope_def()
49      nextLine  = Line(line, lineNum, self.filePath, self.curScope)
50
51      self.buffer.append(nextLine.make_thread_line())       
52      lineType  = nextLine.get_line_type()
53
54      if lineType == 'cond':
55        self.buffer.append(nextLine.make_cond_line())
56      if lineType == 'def' or lineType == 'cdef':
57        self.buffer.append(nextLine.make_func_line())
58      if lineType == 'lock':
59        self.buffer.append(nextLine.make_lock_line())
60         
61  def build_outText(self):
62    self.buffer   = map(str, self.buffer)
63    self.outText  = ''.join(self.buffer)
64   
65  def commit(self):
66    self.build_outText()
67    outfile = open(self.filePath, 'w')
68    outfile.write(self.outText)
69    print self.outText
70   
71   
72class Line(object):
73  def __init__(self, aLine, lineNum, filePath, curScope=None):
74    self.text       = aLine
75    self.stripped   = self.text.strip()
76    self.whitespace = ''
77    self.lineNum    = "line: %05d, " % lineNum
78    self.filePath   = filePath
79    self.curScope   = curScope
80    ##setup
81    self.make_whitespace()
82 
83  def __repr__(self): return self.text
84                               
85  def make_whitespace(self):
86    for char in self.text:
87      if char != ' ':
88        if self.is_lock(): return
89        self.whitespace += '  '
90        return
91      self.whitespace += ' '
92
93  def line_valid(self):           # can't print funcs with multiline args... yet
94    if ":" not in self.text:     return False
95    if "self" not in self.text:  return False
96    if "class" in self.text:     return False
97    return True
98
99  def get_line_type(self):
100    if self.is_lock():        return 'lock'
101    if not self.line_valid(): return False
102    if self.is_cond():        return 'cond'
103    if self.is_cdef():        return 'cdef'
104    if self.is_def():         return 'def'
105    return False
106   
107  def is_cond(self):
108    if self.stripped[0:2]   == 'if':    return True
109    if self.stripped[0:3]   == 'for':   return True
110    if self.stripped[0:4]   == 'elif':  return True
111    if self.stripped[0:5]   == 'while': return True
112    return False
113 
114  def is_lock(self):
115    if "py.PyThread_acquire_lock" in self.stripped: return True
116    if "py.PyThread_release_lock" in self.stripped: return True 
117    return False
118   
119  def is_cdef(self):
120    if self.stripped[0:4] == "cdef": return True
121    return False
122   
123  def is_def(self):
124    if self.stripped[0:3] == "def":  return True
125    return False
126 
127  def make_thread_line(self):
128    threadingMask = 'stdio.printf("%sThread ID: Python GIL, ")#autogen'
129    threadingInfo = threadingMask % self.lineNum
130    if self.curScope  == 'cdef':               #Print threading information also
131      threadingMask = r'stdio.printf("%sThread ID:%s ,",<long>py.PyThread_get_thread_ident())#autogen'
132      threadingInfo = threadingMask % (self.lineNum, '%d')   # snuck the '%d' in
133    newLine = self.whitespace + threadingInfo + '\n'
134    return newLine
135   
136  def make_func_line(self):
137    lineText  = self.stripped.split('(')[0]
138    return self.build_line(lineText)   
139
140  def make_cond_line(self):
141    lineText  = self.stripped
142    return self.build_line(lineText)
143   
144  def make_lock_line(self):
145    lineText  = self.stripped
146    return self.build_line(lineText)
147   
148  def build_line(self, lineText):
149    mask = '%sstdio.printf("%s: %s\\n")#autogen\n'   
150    newLine = mask % (self.whitespace, self.filePath, lineText)   
151    return newLine
152   
153       
154class Augmentator(object):
155  def __init__(self, rootDir):
156    self.rootDir  = rootDir
157    self.pxiFiles = []
158    ##setup
159    self.get_pxi_files()
160   
161  def valid_fileName(self, fileName):
162    if fileName[-4:]  == '.pxi': return True
163    return False
164   
165  def get_pxi_files(self):
166    tree = os.walk(self.rootDir)
167    for group in tree:
168      curDir, ignore, fileNames = group
169      for aFile in fileNames:
170        if not self.valid_fileName(aFile): continue
171        path    = os.path.join(curDir, aFile)
172        newFile = PXIfile(path)       
173        self.pxiFiles.append(newFile)
174
175
176
177def WARNING():
178  import time, sys
179  msg = """\
180+------------------------------------------------------------------------------+
181| debug_build.py                                                               |
182+------------------------------------------------------------------------------|
183|  This program inserts printf statements in *.pxi files at the following      |
184|  locations:                                                                  |
185|  @ the def and cdef function heads                                           |
186|  @ if, for, elif, while                                                      |
187|  @ if the function scope is a cdef the Thread ID will be printed as well.    |
188|                                                                              |
189|  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    |
190|  << Warning >>  << Danger >>  << Warning >>  << Danger >>  << Warning >>     |
191|                                                                              |
192|  This program will utterly mangle your ./src directory.                      |
193|  It will not be commitable afterwards.                                       |
194|  Again. By using this program, you may cause Global Thermonuclear War,       |
195|  black plague or rabies in small animals within a radius of two miles.       |
196|  You have been warned.                                                       |
197|                                                                              |
198+------------------------------------------------------------------------------+
199"""
200  print msg
201  cin = raw_input("Do you wish to proceed [(Y)es, (N)o]: ")
202  if cin != 'Y':
203    print "--\nPerhaps we can play again sometime. Bye. [process terminated]\n"
204    sys.exit()
205  for i in range(5,0,-1):
206    time.sleep(.5)
207    print i
208   
209if __name__ == "__main__":
210  WARNING()
211  Augmentator('../src') 
212 
213 
214 
215 
Note: See TracBrowser for help on using the repository browser.