Debian Codegraph @

cmake/tribits/python_utils/tree.py

raw
1 #! /usr/bin/env python3
2
3 # tree.py
4 #
5 # Written by Doug Dahms
6 #
7 # Modified by Ross Bartlett
8 #
9 # Prints the tree structure for the path specified on the command line
10
11 import sys
12 from os import listdir, sep
13 from os.path import abspath, basename, isdir
14 from sys import argv, exit
15 from optparse import OptionParser
16
17 def tree(dir, padding, options, depth, top_level=False):
18
19 if depth != None:
20 if depth == 0:
21 return
22 else:
23 depth = depth - 1
24
25 print_files = options.printFiles
26 print_compact = options.printCompact
27
28 if options.noDirectorySep:
29 verticalLineChar = ' '
30 fileDirPrefix = ' '
31 else:
32 verticalLineChar = '|'
33 fileDirPrefix = '+-'
34
35 init_prefix = padding[:-1]
36 if top_level: init_prefix += ' '
37 else: init_prefix += fileDirPrefix
38
39 print(init_prefix + basename(abspath(dir)) + '/')
40
41 padding = padding + ' '
42
43 raw_listdir = listdir(dir)
44 files = []
45 if print_files:
46 files = raw_listdir
47 else:
48 files = [x for x in raw_listdir if isdir(dir + sep + x)]
49 files.sort()
50 for file in files:
51 if (file[0] == '.') and (options.hideHiddenFiles):
52 continue
53 if not print_compact:
54 print(padding + verticalLineChar)
55 path = dir + sep + file
56 if isdir(path):
57 if (depth == None) or (depth > 0):
58 tree(path, padding + verticalLineChar, options, depth)
59 else:
60 print(padding + fileDirPrefix + file + "/")
61 else:
62 print(padding + fileDirPrefix + file)
63
64
65 usageHelp = r""" tree.py [-f] [-c] [-x] [-n] <PATH>
66 Print tree structure of specified <PATH> to given depth.
67 """
68
69 def main():
70
71 # Get command-line arguments
72
73 clp = OptionParser(usage=usageHelp)
74
75 clp.add_option(
76 "-f", dest="printFiles", action="store_true",
77 help="Show files in addition to just directoires.",
78 default=False )
79
80 clp.add_option(
81 "-c", dest="printCompact", action="store_true",
82 help="Make output more compact.",
83 default=False )
84
85 clp.add_option(
86 "-x", dest="noDirectorySep", action="store_true",
87 help="Remove the directory separators and continuation lines.",
88 default=False )
89
90 clp.add_option(
91 "-n", dest="hideHiddenFiles", action="store_true",
92 help="Hide hidden files.",
93 default=False )
94
95 clp.add_option(
96 "--depth", dest="depth", type="int", default=None,
97 help="Depth (integer) to recurse into. Default = '' or unbounded.")
98
99 (options, args) = clp.parse_args()
100
101 if len(args) != 1:
102 print("Error: Need to specify path a single path argument. See, see --help")
103 sys.exit(1)
104 path = args[0]
105
106 if not isdir(path):
107 print("ERROR: \'" + path + "\' is not a directory!")
108 print("See --help!")
109 exit(1)
110
111 depth = options.depth
112 tree(path, ' ', options, depth, True)
113
114
115 if __name__ == '__main__':
116 main()