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