[Release] wrt-commons_0.2.167
[framework/web/wrt-commons.git] / dir-struct.py
1 #!/usr/bin/env python
2 # Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License");
5 #    you may not use this file except in compliance with the License.
6 #    You may obtain a copy of the License at
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS,
12 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #    See the License for the specific language governing permissions and
14 #    limitations under the License.
15 #
16
17 import os
18 import re
19
20
21 def countLines(path):
22     with open(path) as f:
23         return len(f.readlines())
24
25 # RETURNS: (
26 #    short description (string or None)
27 #    long decsription (array of strings or None)
28 #    options: stop
29 def parseDescr(lines):
30     if len(lines) == 0:
31         return (None, None, False)
32     linesRest = None
33     if re.match( r"!!!options!!!", lines[0] ):
34         optStop = True
35         linesRest = lines[1:]
36     else:
37         optStop = False
38         linesRest = lines
39     if len(linesRest) == 0:
40         return(None,None,optStop)
41     short = linesRest[0].rstrip()
42     long = []
43     for l in linesRest[1:]:
44         ll = l.rstrip()
45         if re.search( r"\S", ll ):
46             long.append( ll )
47     if len(long) == 0:
48         long = None
49
50     return (short, long, optStop)
51
52 # RETURNS a tree with nodes like: (
53 #    path (string)
54 #    short description (string or None)
55 #    long decsription (array of strings or None)
56 #    LOC (integer)
57 #    list of subdirs (child nodes like this one)
58 def parseDir(path):
59     short = None
60     long = None
61     optStop = False
62     try:
63         with open( path+'/DESCRIPTION' ) as f:
64             short, long, optStop = parseDescr( f.readlines() )
65     except IOError:
66         pass
67     dirs = []
68     cntLines = 0
69     for fname in os.listdir(path):
70         if fname != '.git' and os.path.isdir(path+'/'+fname):
71             subdir = parseDir(path+'/'+fname)
72             if optStop == False:
73                 dirs.append(subdir)
74             (dummy0, dummy1, dummy2, subLines, dummy4) = subdir
75             cntLines += subLines
76   
77         if os.path.isfile(path+'/'+fname) \
78         and not os.path.islink(path+'/'+fname):
79             cntLines += countLines(path+'/'+fname)
80
81     return path, short, long, cntLines, dirs
82
83
84 ###     ##### PRINT AS TEXT
85 ###     
86 ###     def printTextSub(path,indent,withLongDesc):
87 ###         short, long, dirs, loc = parseDir(path)
88 ###         if short == None: 
89 ###             p = re.sub(r"^\./", '', path)
90 ###             print '%s%s -- ' % (indent, p)
91 ###         else:
92 ###             p = re.sub(r"^\./", '', path)
93 ###             print '%s%s -- %s' % (indent, p, short)
94 ###         if withLongDesc:
95 ###             if long != None:
96 ###                 print ''
97 ###                 for line in long:
98 ###                     print '%s%s' % (indent+'    ',line)
99 ###                 print ''
100 ###         for dir in dirs:
101 ###             printTextSub(path+'/'+dir, indent+'    ', withLongDesc)
102 ###     
103 ###     def printText(path,withLongDesc):
104 ###         printTextSub(path,'',withLongDesc)
105 ###     
106 ###     def printTextWoMain(path,withLongDesc):
107 ###         short, long, dirs, loc = parseDir(path)
108 ###         for dir in dirs:
109 ###             printTextSub(path+'/'+dir, '', withLongDesc)
110 ###     
111
112 ##### PRINT AS a sort of CSV delimited by '|'
113
114 # indent is a number (0..)
115 def printTabSub(tree,indent):
116     path, short, long, loc, subdirs = tree 
117     p = re.sub(r"^\./", '', path)
118     m = re.search(r"/([^/]*$)", p)
119     if m != None: p = m.groups()[0]
120     if short == None: 
121         print '%s%s|%d|' % ("        "*indent, p, loc)
122     else:
123         print '%s%s|%d|%s' % ("        "*indent, p, loc, short)
124     for dir in subdirs:
125         printTabSub(dir, indent+1)
126
127 def printTab(tree):
128     printTabSub(tree,0)
129
130 def printTabWoMain(tree):
131     path, short, long, loc, dirs = tree
132     for dir in dirs:
133         printTabSub(dir, 0)
134
135
136 ##### MAIN
137
138 tree = parseDir('.')
139 printTabWoMain(tree)
140