Mercurial > hg > config
annotate python/tree.py @ 379:61a3c07e9f7a
kill reject files
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 25 Jul 2013 03:36:24 -0700 |
parents | b1c43c980b05 |
children | 397d0ac832b6 |
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) | |
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 | 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 | 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 | 36 indent = depth(dirpath) - level |
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 | 39 directories[dirpath] = dirnames |
377 | 40 |
374 | 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 | 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 | 47 name) |
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 | 50 ]) |
51 return '\n'.join(retval) | |
52 | |
53 def main(args=sys.argv[1:]): | |
54 | |
55 usage = '%prog [options]' | |
56 parser = optparse.OptionParser(usage=usage, description=__doc__) | |
57 options, args = parser.parse_args(args) | |
58 if not args: | |
59 args = ['.'] | |
60 | |
61 not_directory = [arg for arg in args | |
62 if not os.path.isdir(arg)] | |
63 if not_directory: | |
64 parser.error("Not a directory: %s" % (', '.join(not_directory))) | |
65 | |
66 for arg in args: | |
67 print (tree(arg)) | |
68 | |
69 if __name__ == '__main__': | |
70 main() |