annotate python/tree.py @ 397:11ac996b9c49

make at least a pretend stub
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 30 Jul 2013 00:04:34 -0700
parents cccfe246452e
children 5417eb6364ee
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 = {
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
14 'vertical_line' : '|',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
15 'item_marker' : '+',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
16 'last_child' : '\\'
388
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
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
20 unicode_delimeters = {
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
21 'vertical_line' : '│',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
22 'item_marker' : '├',
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
23 'last_child' : '└'
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
24 }
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 def depth(directory):
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
27 """returns the integer depth of a directory or path relative to '/' """
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
28
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 directory = os.path.abspath(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 level = 0
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 while True:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 directory, remainder = os.path.split(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 level += 1
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 if not remainder:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 break
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 return level
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
38 def tree(directory,
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
39 item_marker=unicode_delimeters['item_marker'],
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
40 vertical_line=unicode_delimeters['vertical_line'],
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
41 last_child=unicode_delimeters['last_child'],
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
42 sort_key=lambda x: x.lower()):
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
43 """
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
44 display tree directory structure for `directory`
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
45 """
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
46
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
47 retval = []
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
48 indent = []
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
49 last = {}
386
bea5f2fe4ea4 it works even more!
Jeff Hammel <jhammel@mozilla.com>
parents: 385
diff changeset
50 top = depth(directory)
bea5f2fe4ea4 it works even more!
Jeff Hammel <jhammel@mozilla.com>
parents: 385
diff changeset
51
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 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
53
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
54 abspath = os.path.abspath(dirpath)
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
55 basename = os.path.basename(abspath)
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
56 parent = os.path.dirname(abspath)
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
57 level = depth(abspath) - top
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
58
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
59 # sort articles of interest
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
60 for resource in (dirnames, filenames):
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
61 resource[:] = sorted(resource, key=sort_key)
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
63 files_end = item_marker
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
64 dirpath_marker = item_marker
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
65
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
66 if level > len(indent):
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
67 indent.append(vertical_line)
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
68 indent = indent[:level]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
69
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
70 if dirnames:
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
71 files_end = item_marker
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
72 last[abspath] = dirnames[-1]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
73 else:
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
74 files_end = last_child
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
75
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
76 if last.get(parent) == os.path.basename(abspath):
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
77 # last directory of parent
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
78 dirpath_mark = last_child
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
79 indent[-1] = ' '
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
80 elif not indent:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
81 dirpath_mark = ''
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
82 else:
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
83 dirpath_mark = item_marker
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
84
388
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
85 # append the directory and piece of tree structure
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
86 # if the top-level entry directory, print as passed
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
87 retval.append('%s%s%s'% (''.join(indent[:-1]),
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
88 dirpath_mark,
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
89 basename if retval else directory))
3678770e8c52 changes
Jeff Hammel <jhammel@mozilla.com>
parents: 387
diff changeset
90 # add the files
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
91 if filenames:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
92 last_file = filenames[-1]
387
0aee0da6b06b even more success
Jeff Hammel <jhammel@mozilla.com>
parents: 386
diff changeset
93 retval.extend([('%s%s%s' % (''.join(indent),
389
2d4dfe1dc632 make delimeters flexible
Jeff Hammel <jhammel@mozilla.com>
parents: 388
diff changeset
94 files_end if filename == last_file else item_marker,
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
95 filename))
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
96 for index, filename in enumerate(filenames)])
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
97
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 return '\n'.join(retval)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 def main(args=sys.argv[1:]):
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
102 # parse command line options
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103 usage = '%prog [options]'
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104 parser = optparse.OptionParser(usage=usage, description=__doc__)
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
105 parser.add_option('-a', '--ascii', dest='use_ascii',
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
106 action='store_true', default=False,
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
107 help="use ascii delimeters (%s)" % ascii_delimeters)
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 options, args = parser.parse_args(args)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 if not args:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110 args = ['.']
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
112 # sanity check
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113 not_directory = [arg for arg in args
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 if not os.path.isdir(arg)]
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115 if not_directory:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 parser.error("Not a directory: %s" % (', '.join(not_directory)))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
118 delimeters = unicode_delimeters
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
119 if options.use_ascii:
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
120 delimeters = ascii_delimeters
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
121
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
122 # print the tree
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
123 for arg in args:
390
9d02187611ae make delimeters CLI switchable
Jeff Hammel <jhammel@mozilla.com>
parents: 389
diff changeset
124 print (tree(arg, **delimeters))
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
125
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
126 if __name__ == '__main__':
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
127 main()