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