blob: 05443b6bc08b04e488748d3779eadd1606e5d526 [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
20
21def get_missing_requirements():
22 """Return a list of missing third party packages."""
23 import sys
24
25 sys_path_original = sys.path[:]
26 # Remove the current directory from the list of paths to check when
27 # importing modules.
28 try:
29 # Sometimes it's represented by an empty string?
30 sys.path.remove('')
31 except ValueError:
32 import os.path
33 sys.path.remove(os.path.abspath(os.path.curdir))
34 missing_pkgs = []
35 third_party_reqs = ['oauth2', 'httplib2']
36 for pkg in third_party_reqs:
37 try:
38 __import__(pkg)
39 except ImportError:
40 missing_pkgs.append(pkg)
41 # JSON support gets its own special logic
42 try:
43 import_json(sys.path)
44 except ImportError:
45 missing_pkgs.append('simplejson')
46
47 sys.path = sys_path_original
48 return missing_pkgs
49
50
51def import_json(import_path=None):
52 """Return a package that will provide JSON support.
53
54 Args:
55 import_path: list Value to assing to sys.path before checking for packages.
56 Default None for default sys.path.
57 Return:
58 A package, one of 'json' (if running python 2.6),
59 'django.utils.simplejson' (if django is installed)
60 'simplejson' (if third party library simplejson is found)
61 Raises:
62 ImportError if none of those packages are found.
63 """
64 import sys
65 sys_path_orig = sys.path[:]
66 if import_path is not None:
67 sys.path = import_path
68
69 try:
70 # Should work for Python 2.6.
71 pkg = __import__('json')
72 except ImportError:
73 try:
74 pkg = __import__('django.utils').simplejson
75 except ImportError:
76 try:
77 pkg = __import__('simplejson')
78 except ImportError:
79 pkg = None
80
81 if import_path is not None:
82 sys.path = sys_path_orig
83 if pkg:
84 return pkg
85 else:
86 raise ImportError('Cannot find json support')