annotate python/tree2.py @ 388:3678770e8c52

changes
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 27 Jul 2013 18:45:43 -0700
parents 0aee0da6b06b
children 2d4dfe1dc632
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 tree in python
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 """
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import optparse
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import os
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import sys
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
12 # ASCII delimeters
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
13 ascii_delimeters = {
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
14 VERTICAL_LINE = '|'
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
15 ITEM = '+'
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
16 END = '\\'
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
17 }
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
18
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
19 # unicode delimiters
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
20 VERTICAL_LINE = '│'
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
21 ITEM = '├'
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
22 END = '└'
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 def depth(directory):
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
25 """returns the integer depth of a directory or path relative to '/' """
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
26
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 directory = os.path.abspath(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 level = 0
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 while True:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 directory, remainder = os.path.split(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 level += 1
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if not remainder:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 break
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 return level
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
36 def tree(directory,
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
37 item_marker='',
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
38 sort_key=lambda x: x.lower()):
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
39 """
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
40 display tree directory structure for `directory`
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
41 """
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
42
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
43 retval = []
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
44 indent = []
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
45 last = {}
386
bea5f2fe4ea4 it works even more!
Jeff Hammel <jhammel@mozilla.com>
parents: 385
diff changeset
46 top = depth(directory)
bea5f2fe4ea4 it works even more!
Jeff Hammel <jhammel@mozilla.com>
parents: 385
diff changeset
47
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 for dirpath, dirnames, filenames in os.walk(directory, topdown=True):
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
49
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
50 abspath = os.path.abspath(dirpath)
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
51 basename = os.path.basename(abspath)
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
52 parent = os.path.dirname(abspath)
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
53 level = depth(abspath) - top
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
54
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
55 # sort articles of interest
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
56 for resource in (dirnames, filenames):
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
57 resource[:] = sorted(resource, key=sort_key)
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
59 files_end = ITEM
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
60 dirpath_marker = ITEM
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
61
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
62 if level > len(indent):
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
63 indent.append(VERTICAL_LINE)
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
64 indent = indent[:level]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
65
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
66 if dirnames:
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
67 files_end = ITEM
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
68 last[abspath] = dirnames[-1]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
69 else:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
70 files_end = END
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
71
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
72 if last.get(parent) == os.path.basename(abspath):
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
73 # last directory of parent
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
74 dirpath_mark = END
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
75 indent[-1] = ' '
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
76 elif not indent:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
77 dirpath_mark = ''
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
78 else:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
79 dirpath_mark = ITEM
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
80
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
81 # append the directory and piece of tree structure
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
82 # if the top-level entry directory, print as passed
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
83 retval.append('%s%s%s'% (''.join(indent[:-1]),
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
84 dirpath_mark,
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
85 basename if retval else directory))
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
86 # add the files
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
87 if filenames:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
88 last_file = filenames[-1]
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
89 retval.extend([('%s%s%s' % (''.join(indent),
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
90 files_end if filename == last_file else ITEM,
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
91 filename))
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
92 for index, filename in enumerate(filenames)])
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
93
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 return '\n'.join(retval)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 def main(args=sys.argv[1:]):
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 usage = '%prog [options]'
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 parser = optparse.OptionParser(usage=usage, description=__doc__)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 options, args = parser.parse_args(args)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101 if not args:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102 args = ['.']
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104 not_directory = [arg for arg in args
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 if not os.path.isdir(arg)]
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 if not_directory:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107 parser.error("Not a directory: %s" % (', '.join(not_directory)))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 for arg in args:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 print (tree(arg))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 if __name__ == '__main__':
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113 main()