annotate python/tree2.py @ 383:8d1ad56761b0

this still, somehow, eludes my tired brain
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 26 Jul 2013 05:56:18 -0700
parents 9ac9561b6574
children 5ae5ada91ac8
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
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 here = os.path.dirname(os.path.realpath(__file__))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 def depth(directory):
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 directory = os.path.abspath(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 level = 0
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 while True:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 directory, remainder = os.path.split(directory)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 level += 1
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 if not remainder:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 break
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 return level
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 tree(directory):
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
25
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
26 sort_key=lambda x: x.lower())
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
27 retval = [directory]
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
28 top = depth(directory)
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
29 indent = []
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 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
31
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
32 abspath = os.path.abspath(dirpath)
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
33 parent = os.path.dirname(abspath))
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
34 level = depth(abspath) - top
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
35
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
36 # sort articles of interest
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
37 for resource in (dirnames, filenames):
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
38 resource[:] = sorted(resource, key=sort_key)
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39
383
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
40 # set last for current dirpath
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
41 if dirnames:
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
42 last[os.path.abspath(dirpath)] = dirnames[-1]
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
43 if last.get(parent) == os.path.basename(abspath):
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
44 pass
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
45
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
46 # retval.append('%s%s%s %s' % ('│' * (indent-1),
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
47 # ('├' if basename == basename else '└') if indent else '',
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
48 # basename))
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
49 # filenames = sorted(filenames, key=lambda x: x.lower())
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
50 # retval.extend(['%s%s%s' % ('│' * (indent),
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
51 # '├' if (((index < len(filenames) -1)) or dirnames) else '└',
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
52 # name)
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
53 # for index, name in
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
54 # enumerate(filenames)
8d1ad56761b0 this still, somehow, eludes my tired brain
Jeff Hammel <jhammel@mozilla.com>
parents: 382
diff changeset
55 # ])
382
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 return '\n'.join(retval)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 def main(args=sys.argv[1:]):
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 usage = '%prog [options]'
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 parser = optparse.OptionParser(usage=usage, description=__doc__)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 options, args = parser.parse_args(args)
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 if not args:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 args = ['.']
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 not_directory = [arg for arg in args
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 if not os.path.isdir(arg)]
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 if not_directory:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 parser.error("Not a directory: %s" % (', '.join(not_directory)))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 for arg in args:
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 print (tree(arg))
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 if __name__ == '__main__':
9ac9561b6574 copy a thing
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 main()