1 | #!/usr/bin/env python
|
---|
2 | #!/usr/local/bin/python
|
---|
3 | #
|
---|
4 | # Version 1.0
|
---|
5 | #
|
---|
6 | # Copyright (C) 2006 Ulrich Langenbach
|
---|
7 | #
|
---|
8 | # for any question, improvements and comments send an email to
|
---|
9 | #
|
---|
10 | # ulrich@falaba.de
|
---|
11 | #
|
---|
12 | # with 'dirs.py' as subject
|
---|
13 | #
|
---|
14 | # This program is free software; you can redistribute it and/or modify
|
---|
15 | # it under the terms of the GNU General Public License as published by
|
---|
16 | # the Free Software Foundation; either version 2 of the License, or
|
---|
17 | # (at your option) any later version.
|
---|
18 | #
|
---|
19 | # This program is distributed in the hope that it will be useful,
|
---|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | # GNU General Public License for more details.
|
---|
23 | #
|
---|
24 | # You should have received a copy of the GNU General Public License
|
---|
25 | # along with this program; if not, write to the Free Software
|
---|
26 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
27 |
|
---|
28 | import sys
|
---|
29 | import os
|
---|
30 |
|
---|
31 | def printHelp():
|
---|
32 | print ""
|
---|
33 | print "optional arguments:"
|
---|
34 | print ""
|
---|
35 | print "-max-depth specifies the maximum recursive depth"
|
---|
36 | print ""
|
---|
37 | print "-comments with this argument the script looks in .comment files"
|
---|
38 | print " in every directory and the first line of this file is"
|
---|
39 | print " as an comment of the directory the file lies in"
|
---|
40 | print ""
|
---|
41 | print "-excl-dirs list the directories specified with this comma seperated list"
|
---|
42 | print " will be excluded on every level of the recursive search"
|
---|
43 | print ""
|
---|
44 | print "-help prints this (hopefully) helping lines"
|
---|
45 | print ""
|
---|
46 | print "example: ./dirs.py -max-depth=2 -excl-dirs=proc -comments=commentFile /path"
|
---|
47 | print ""
|
---|
48 | print "prints the file structure for the dirtree package in tex format"
|
---|
49 | print ""
|
---|
50 | print "for the tex dirtree package look at "
|
---|
51 | print "http://tug.ctan.org/cgi-bin/ctanPackageInformation.py?id=dirtree"
|
---|
52 | print ""
|
---|
53 |
|
---|
54 | def isInt(str):
|
---|
55 | try:int(str)
|
---|
56 | except ValueError:return False
|
---|
57 | else:return True
|
---|
58 |
|
---|
59 | def parseDir(root,directory,deepness,maxDepth,skipDirs,comments):
|
---|
60 | deepness=deepness+1
|
---|
61 | dirs = os.listdir(directory)
|
---|
62 | dirs.sort()
|
---|
63 | for curr in dirs:
|
---|
64 | do = True
|
---|
65 |
|
---|
66 | for currDir in skipDirs:
|
---|
67 | if currDir in curr:
|
---|
68 | do = False
|
---|
69 |
|
---|
70 | commentFilePath = directory + curr + commentFile
|
---|
71 |
|
---|
72 | if (comments == True) and os.path.isfile(commentFilePath):
|
---|
73 | file = open(commentFile,'r')
|
---|
74 | datInhalt = file.readline()
|
---|
75 | file.close()
|
---|
76 | datInhalt=datInhalt.strip()
|
---|
77 | comment = "\\DTcomment{%(datInhalt)s}" % vars()
|
---|
78 | else:
|
---|
79 | comment = ""
|
---|
80 |
|
---|
81 | nextDir = directory + curr + "/"
|
---|
82 | #print nextDir
|
---|
83 | if os.path.isdir(nextDir) and do:
|
---|
84 | printDirectory = curr.replace("_","\_")
|
---|
85 | print ".%(deepness)u %(printDirectory)s %(comment)s." % vars()
|
---|
86 | if (maxDepth > deepness):
|
---|
87 | parseDir(root,nextDir,deepness,maxDepth,skipDirs,comments)
|
---|
88 |
|
---|
89 | # parsing arguments
|
---|
90 |
|
---|
91 | if (len(sys.argv) > 1):
|
---|
92 | if (sys.argv[1] != ""):
|
---|
93 | if (sys.argv[1] == "help") or (sys.argv[1] == "--help"):
|
---|
94 | printHelp()
|
---|
95 | else:
|
---|
96 | maxDepth = 100000
|
---|
97 | skipDirs = []
|
---|
98 | commentFile = ""
|
---|
99 | comments = False
|
---|
100 | directory = "./"
|
---|
101 | for i in sys.argv:
|
---|
102 | if (i.find("=") > 0):
|
---|
103 | i = i.split("=")
|
---|
104 | if (i[0] == "-excl-dirs"):
|
---|
105 | if i[1].find(","):
|
---|
106 | skipDirs = i[1].split(",")
|
---|
107 | else:
|
---|
108 | skipDirs.append(i[1])
|
---|
109 | elif (i[0] == "-max-depth"):
|
---|
110 | maxDepth = long(i[1])
|
---|
111 | elif (i[0] == "-comments"):
|
---|
112 | comments = True
|
---|
113 | commentFile = i[1]
|
---|
114 | elif (i == "-comments"):
|
---|
115 | comments = True
|
---|
116 | commentFile = ".comment"
|
---|
117 | else:
|
---|
118 | directory = i
|
---|
119 |
|
---|
120 | # parsing directories
|
---|
121 |
|
---|
122 | print "\\dirtree{%"
|
---|
123 |
|
---|
124 | top = directory.split("/")
|
---|
125 | if (top[len(top)-1] == ""):
|
---|
126 | top = top[len(top)-1]
|
---|
127 | else:
|
---|
128 | top = top[len(top)-2]
|
---|
129 | if (top == ".") or (top == "..") or (top==""):
|
---|
130 | top = "/"
|
---|
131 | print ".1 %(top)s." % vars()
|
---|
132 |
|
---|
133 | parseDir(directory,directory,1,maxDepth,skipDirs,comments)
|
---|
134 |
|
---|
135 | print "}"
|
---|
136 |
|
---|
137 | else:
|
---|
138 | printHelp()
|
---|
139 | else:
|
---|
140 | printHelp()
|
---|
141 |
|
---|