OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 # This Source Code Form is subject to the terms of the Mozilla Public |
| 4 # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 # You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
| 7 """ |
| 8 utility functions for mozrunner |
| 9 """ |
| 10 |
| 11 __all__ = ['findInPath', 'get_metadata_from_egg'] |
| 12 |
| 13 import mozinfo |
| 14 import os |
| 15 import sys |
| 16 |
| 17 |
| 18 ### python package method metadata by introspection |
| 19 try: |
| 20 import pkg_resources |
| 21 def get_metadata_from_egg(module): |
| 22 ret = {} |
| 23 try: |
| 24 dist = pkg_resources.get_distribution(module) |
| 25 except pkg_resources.DistributionNotFound: |
| 26 return {} |
| 27 if dist.has_metadata("PKG-INFO"): |
| 28 key = None |
| 29 for line in dist.get_metadata("PKG-INFO").splitlines(): |
| 30 # see http://www.python.org/dev/peps/pep-0314/ |
| 31 if key == 'Description': |
| 32 # descriptions can be long |
| 33 if not line or line[0].isspace(): |
| 34 value += '\n' + line |
| 35 continue |
| 36 else: |
| 37 key = key.strip() |
| 38 value = value.strip() |
| 39 ret[key] = value |
| 40 |
| 41 key, value = line.split(':', 1) |
| 42 key = key.strip() |
| 43 value = value.strip() |
| 44 ret[key] = value |
| 45 if dist.has_metadata("requires.txt"): |
| 46 ret["Dependencies"] = "\n" + dist.get_metadata("requires.txt") |
| 47 return ret |
| 48 except ImportError: |
| 49 # package resources not avaialable |
| 50 def get_metadata_from_egg(module): |
| 51 return {} |
| 52 |
| 53 |
| 54 def findInPath(fileName, path=os.environ['PATH']): |
| 55 """python equivalent of which; should really be in the stdlib""" |
| 56 dirs = path.split(os.pathsep) |
| 57 for dir in dirs: |
| 58 if os.path.isfile(os.path.join(dir, fileName)): |
| 59 return os.path.join(dir, fileName) |
| 60 if mozinfo.isWin: |
| 61 if os.path.isfile(os.path.join(dir, fileName + ".exe")): |
| 62 return os.path.join(dir, fileName + ".exe") |
| 63 |
| 64 if __name__ == '__main__': |
| 65 for i in sys.argv[1:]: |
| 66 print findInPath(i) |
OLD | NEW |