annotate python/tree2.py @ 385:6ef0ea2f10e3

its alive
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 26 Jul 2013 13:14:19 -0700
parents 5ae5ada91ac8
children bea5f2fe4ea4
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
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
12 LINE = '|'
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
13 ITEM = '+'
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
14 END = '\\'
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
15 #
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
16 LINE = '│'
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
17 ITEM = '├'
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
18 END = '└'
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 def depth(directory):
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 directory = os.path.abspath(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 level = 0
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 while True:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 directory, remainder = os.path.split(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 level += 1
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 if not remainder:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 break
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 return level
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 def tree(directory):
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
31
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
32 sort_key=lambda x: x.lower()
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
33 retval = []
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
34 top = depth(directory)
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
35 indent = []
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
36 last = {}
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 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
38
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
39 abspath = os.path.abspath(dirpath)
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
40 basename = os.path.basename(abspath)
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
41 parent = os.path.dirname(abspath)
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
42 level = depth(abspath) - top
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
43
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
44 # sort articles of interest
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
45 for resource in (dirnames, filenames):
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
46 resource[:] = sorted(resource, key=sort_key)
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
48 files_end = ITEM
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
49 dirpath_marker = ITEM
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
50
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
51 if level > len(indent):
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
52 indent.append(LINE)
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
53 indent = indent[:level]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
54
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
55 if dirnames:
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
56 files_end = ITEM
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
57
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
58 last[abspath] = dirnames[-1]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
59 else:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
60 files_end = END
384
5ae5ada91ac8 simple numbering
Jeff Hammel <jhammel@mozilla.com>
parents: 383
diff changeset
61
385
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
62 if last.get(parent) == os.path.basename(abspath):
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
63 # last directory of parent
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
64 dirpath_mark = END
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
65 indent[-1] = ' '
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
66 elif not indent:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
67 dirpath_mark = ''
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
68 else:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
69 dirpath_mark = ITEM
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
70
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
71
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
72 # if basename == 'bin':
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
73 # import pdb; pdb.set_trace()
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
74
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
75 str_indent = ''.join(indent)
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
76 retval.append('%s%s%s'% (''.join(indent[:-1]), dirpath_mark, basename))
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
77 if filenames:
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
78 last_file = filenames[-1]
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
79 retval.extend([('%s%s%s' % (str_indent,
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
80 files_end if filename == last_file else ITEM,
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
81 filename))
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
82 for index, filename in enumerate(filenames)])
6ef0ea2f10e3 its alive
Jeff Hammel <jhammel@mozilla.com>
parents: 384
diff changeset
83
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 return '\n'.join(retval)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 def main(args=sys.argv[1:]):
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 usage = '%prog [options]'
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 parser = optparse.OptionParser(usage=usage, description=__doc__)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 options, args = parser.parse_args(args)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 if not args:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 args = ['.']
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 not_directory = [arg for arg in args
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95 if not os.path.isdir(arg)]
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 if not_directory:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 parser.error("Not a directory: %s" % (', '.join(not_directory)))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 for arg in args:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 print (tree(arg))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102 if __name__ == '__main__':
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103 main()