blob: cc8b1c9aa6e021378ddc99e05d45db7363c74a44 [file] [log] [blame]
Tom Miller7c95d812010-10-11 11:50:52 -07001# 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 Gregorio5cdaa512011-03-28 16:41:55 -040020import sys
Tom Miller7c95d812010-10-11 11:50:52 -070021
Tom Miller7c95d812010-10-11 11:50:52 -070022
Joe Gregorio5cdaa512011-03-28 16:41:55 -040023def is_missing(packages):
24 """Return True if a package can't be imported."""
25
26 retval = True
Tom Miller7c95d812010-10-11 11:50:52 -070027 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 Gregorio09aa3f52011-03-21 15:08:18 -040035 try:
36 sys.path.remove(os.path.abspath(os.path.curdir))
37 except ValueError:
38 pass
Joe Gregorio5cdaa512011-03-28 16:41:55 -040039 if not isinstance(packages, type([])):
40 packages = [packages]
41 for name in packages:
Tom Miller7c95d812010-10-11 11:50:52 -070042 try:
Joe Gregorio5cdaa512011-03-28 16:41:55 -040043 __import__(name)
44 retval = False
Tom Miller7c95d812010-10-11 11:50:52 -070045 except ImportError:
Joe Gregorio5cdaa512011-03-28 16:41:55 -040046 retval = True
47 if retval == False:
48 break
Tom Miller7c95d812010-10-11 11:50:52 -070049
50 sys.path = sys_path_original
Tom Miller7c95d812010-10-11 11:50:52 -070051
Joe Gregorio5cdaa512011-03-28 16:41:55 -040052 return retval