annotate python/path.py @ 562:8d28fce0a6d6

detect scripts; namely x-tile
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 11 Dec 2013 02:52:53 -0800
parents c461ffb7af8c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
494
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 (filesystem) path utilities
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 from http://stackoverflow.com/questions/12041525/a-system-independent-way-using-python-to-get-the-root-directory-drive-on-which-p
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import os
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 def is_root(path):
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 """is `path` the filesystem root"""
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 return not os.path.split(path)[1]
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 def root(path):
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 """return filesystem root of path"""
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 path = os.path.abspath(path)
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 while not is_root(path):
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 path, tail = os.path.split(path)
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 return path
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 if __name__ == '__main__':
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 import sys
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 for path in sys.argv[1:]:
c461ffb7af8c adding path utilities
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 print root(path)