annotate python/tree.py @ 378:0e99d3a68066

notes to self....that didnt quite do what we wanted :(
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 24 Jul 2013 19:36:46 -0700
parents b1c43c980b05
children 397d0ac832b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 tree in python
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 """
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import optparse
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import os
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import sys
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 here = os.path.dirname(os.path.realpath(__file__))
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 def depth(directory):
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 directory = os.path.abspath(directory)
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 level = 0
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 while True:
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 directory, remainder = os.path.split(directory)
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 level += 1
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 if not remainder:
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 break
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 return level
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 def tree(directory):
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 retval = []
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 level = depth(directory)
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 directories = {}
376
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
28 lvlndctr = []
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
29 last = {}
377
b1c43c980b05 nottin to see here
Jeff Hammel <jhammel@mozilla.com>
parents: 376
diff changeset
30 passed_last = {}
376
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
31 columns = []
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
32 lastdepth = depth
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 for dirpath, dirnames, filenames in os.walk(directory, topdown=True):
376
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
34 basename = os.path.basename(dirpath)
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
35 parent = os.path.abspath(os.path.dirname(dirpath))
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 indent = depth(dirpath) - level
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 dirnames[:] = sorted(dirnames, key=lambda x: x.lower())
376
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
38 last[os.path.abspath(dirpath)] = dirnames and dirnames[-1] or None
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 directories[dirpath] = dirnames
377
b1c43c980b05 nottin to see here
Jeff Hammel <jhammel@mozilla.com>
parents: 376
diff changeset
40
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 retval.append('%s%s%s' % ('│' * (indent-1),
376
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
42 ('├' if basename == basename else '└') if indent else '',
3f84a96cccf6 minor improvements; need a thought or two for the real answer
Jeff Hammel <jhammel@mozilla.com>
parents: 375
diff changeset
43 basename))
375
9314c1008189 aslightly better; still trouble at da end
Jeff Hammel <jhammel@mozilla.com>
parents: 374
diff changeset
44 filenames = sorted(filenames, key=lambda x: x.lower())
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 retval.extend(['%s%s%s' % ('│' * (indent),
375
9314c1008189 aslightly better; still trouble at da end
Jeff Hammel <jhammel@mozilla.com>
parents: 374
diff changeset
46 '├' if (((index < len(filenames) -1)) or dirnames) else '└',
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 name)
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 for index, name in
375
9314c1008189 aslightly better; still trouble at da end
Jeff Hammel <jhammel@mozilla.com>
parents: 374
diff changeset
49 enumerate(filenames)
374
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 ])
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 return '\n'.join(retval)
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 def main(args=sys.argv[1:]):
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 usage = '%prog [options]'
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 parser = optparse.OptionParser(usage=usage, description=__doc__)
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 options, args = parser.parse_args(args)
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 if not args:
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 args = ['.']
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 not_directory = [arg for arg in args
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 if not os.path.isdir(arg)]
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 if not_directory:
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 parser.error("Not a directory: %s" % (', '.join(not_directory)))
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 for arg in args:
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 print (tree(arg))
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 if __name__ == '__main__':
6e0853b16457 initial tree prog
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 main()