Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Utility functions for setup.py file(s).""" |
| 16 | |
| 17 | |
| 18 | __author__ = 'tom.h.miller@gmail.com (Tom Miller)' |
| 19 | |
Joe Gregorio | 5cdaa51 | 2011-03-28 16:41:55 -0400 | [diff] [blame] | 20 | import sys |
Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 21 | |
Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 22 | |
Joe Gregorio | 5cdaa51 | 2011-03-28 16:41:55 -0400 | [diff] [blame] | 23 | def is_missing(packages): |
| 24 | """Return True if a package can't be imported.""" |
| 25 | |
| 26 | retval = True |
Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 27 | sys_path_original = sys.path[:] |
| 28 | # Remove the current directory from the list of paths to check when |
| 29 | # importing modules. |
| 30 | try: |
| 31 | # Sometimes it's represented by an empty string? |
| 32 | sys.path.remove('') |
| 33 | except ValueError: |
| 34 | import os.path |
Joe Gregorio | 09aa3f5 | 2011-03-21 15:08:18 -0400 | [diff] [blame] | 35 | try: |
| 36 | sys.path.remove(os.path.abspath(os.path.curdir)) |
| 37 | except ValueError: |
| 38 | pass |
Joe Gregorio | 5cdaa51 | 2011-03-28 16:41:55 -0400 | [diff] [blame] | 39 | if not isinstance(packages, type([])): |
| 40 | packages = [packages] |
| 41 | for name in packages: |
Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 42 | try: |
Joe Gregorio | 5cdaa51 | 2011-03-28 16:41:55 -0400 | [diff] [blame] | 43 | __import__(name) |
| 44 | retval = False |
Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 45 | except ImportError: |
Joe Gregorio | 5cdaa51 | 2011-03-28 16:41:55 -0400 | [diff] [blame] | 46 | retval = True |
| 47 | if retval == False: |
| 48 | break |
Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 49 | |
| 50 | sys.path = sys_path_original |
Tom Miller | 7c95d81 | 2010-10-11 11:50:52 -0700 | [diff] [blame] | 51 | |
Joe Gregorio | 5cdaa51 | 2011-03-28 16:41:55 -0400 | [diff] [blame] | 52 | return retval |