[Engine] patch for normal support of pkgmgr signal and thread problem
[framework/web/wrt-commons.git] / dir-struct.py
1 #!/usr/bin/env python
2
3 import os
4 import re
5
6
7 def countLines(path):
8     with open(path) as f:
9         return len(f.readlines())
10
11 # RETURNS: (
12 #    short description (string or None)
13 #    long decsription (array of strings or None)
14 #    options: stop
15 def parseDescr(lines):
16     if len(lines) == 0:
17         return (None, None, False)
18     linesRest = None
19     if re.match( r"!!!options!!!", lines[0] ):
20         optStop = True
21         linesRest = lines[1:]
22     else:
23         optStop = False
24         linesRest = lines
25     if len(linesRest) == 0:
26         return(None,None,optStop)
27     short = linesRest[0].rstrip()
28     long = []
29     for l in linesRest[1:]:
30         ll = l.rstrip()
31         if re.search( r"\S", ll ):
32             long.append( ll )
33     if len(long) == 0:
34         long = None
35
36     return (short, long, optStop)
37
38 # RETURNS a tree with nodes like: (
39 #    path (string)
40 #    short description (string or None)
41 #    long decsription (array of strings or None)
42 #    LOC (integer)
43 #    list of subdirs (child nodes like this one)
44 def parseDir(path):
45     short = None
46     long = None
47     optStop = False
48     try:
49         with open( path+'/DESCRIPTION' ) as f:
50             short, long, optStop = parseDescr( f.readlines() )
51     except IOError:
52         pass
53     dirs = []
54     cntLines = 0
55     for fname in os.listdir(path):
56         if fname != '.git' and os.path.isdir(path+'/'+fname):
57             subdir = parseDir(path+'/'+fname)
58             if optStop == False:
59                 dirs.append(subdir)
60             (dummy0, dummy1, dummy2, subLines, dummy4) = subdir
61             cntLines += subLines
62   
63         if os.path.isfile(path+'/'+fname) \
64         and not os.path.islink(path+'/'+fname):
65             cntLines += countLines(path+'/'+fname)
66
67     return path, short, long, cntLines, dirs
68
69
70 ###     ##### PRINT AS TEXT
71 ###     
72 ###     def printTextSub(path,indent,withLongDesc):
73 ###         short, long, dirs, loc = parseDir(path)
74 ###         if short == None: 
75 ###             p = re.sub(r"^\./", '', path)
76 ###             print '%s%s -- ' % (indent, p)
77 ###         else:
78 ###             p = re.sub(r"^\./", '', path)
79 ###             print '%s%s -- %s' % (indent, p, short)
80 ###         if withLongDesc:
81 ###             if long != None:
82 ###                 print ''
83 ###                 for line in long:
84 ###                     print '%s%s' % (indent+'    ',line)
85 ###                 print ''
86 ###         for dir in dirs:
87 ###             printTextSub(path+'/'+dir, indent+'    ', withLongDesc)
88 ###     
89 ###     def printText(path,withLongDesc):
90 ###         printTextSub(path,'',withLongDesc)
91 ###     
92 ###     def printTextWoMain(path,withLongDesc):
93 ###         short, long, dirs, loc = parseDir(path)
94 ###         for dir in dirs:
95 ###             printTextSub(path+'/'+dir, '', withLongDesc)
96 ###     
97
98 ##### PRINT AS a sort of CSV delimited by '|'
99
100 # indent is a number (0..)
101 def printTabSub(tree,indent):
102     path, short, long, loc, subdirs = tree 
103     p = re.sub(r"^\./", '', path)
104     m = re.search(r"/([^/]*$)", p)
105     if m != None: p = m.groups()[0]
106     if short == None: 
107         print '%s%s|%d|' % ("        "*indent, p, loc)
108     else:
109         print '%s%s|%d|%s' % ("        "*indent, p, loc, short)
110     for dir in subdirs:
111         printTabSub(dir, indent+1)
112
113 def printTab(tree):
114     printTabSub(tree,0)
115
116 def printTabWoMain(tree):
117     path, short, long, loc, dirs = tree
118     for dir in dirs:
119         printTabSub(dir, 0)
120
121
122 ##### MAIN
123
124 tree = parseDir('.')
125 printTabWoMain(tree)
126