Rolled setuptools support back into setup.py but made it much more robust.

This commit is contained in:
Joe Gregorio
2011-03-28 16:41:55 -04:00
parent 071ca7e3df
commit 5cdaa51eec
2 changed files with 41 additions and 61 deletions

View File

@@ -17,10 +17,14 @@
Also installs included versions of third party libraries, if those libraries
are not already installed.
"""
import setup_utils
# Modules, not packages, that might need to be installed.
has_setuptools = False
try:
from setuptools import setup
has_setuptools = True
except ImportError:
from distutils.core import setup
packages = [
'apiclient',
@@ -32,29 +36,41 @@ packages = [
'apiclient.contrib.moderator',
'uritemplate',
]
install_requires = []
py_modules = []
third_party_packages = ['httplib2', 'oauth2']
third_party_modules = ['gflags', 'gflags_validators']
# Don't clobber installed versions of third party libraries
# with what we include.
packages.extend(setup_utils.get_missing_requirements(third_party_packages))
py_modules.extend(setup_utils.get_missing_requirements(third_party_modules))
# (module to test for, install_requires to add if missing, packages to add if missing, py_modules to add if missing)
REQUIREMENTS = [
('httplib2', 'httplib2', 'httplib2', None),
('oauth2', 'oauth2', 'oauth2', None),
('gflags', 'python-gflags', None, ['gflags', 'gflags_validators']),
(['json', 'simplejson', 'django.utils'], 'simplejson', 'simplejson', None)
]
for import_name, requires, package, modules in REQUIREMENTS:
if setup_utils.is_missing(import_name):
if has_setuptools:
install_requires.append(requires)
else:
if package is not None:
packages.append(package)
else:
py_modules.extend(modules)
# It appears setuptools can't have packages and py_modules, but this project does.
from distutils.core import setup
long_desc = """The Google API Client for Python is a client library for
accessing the Buzz, Moderator, and Latitude APIs."""
setup(name="google-api-python-client",
version="1.0alpha9",
version="1.0alpha10",
description="Google API Client Library for Python",
long_description=long_desc,
author="Joe Gregorio",
author_email="jcgregorio@google.com",
url="http://code.google.com/p/google-api-python-client/",
install_requires=install_requires,
packages=packages,
py_modules=py_modules,
package_data={

View File

@@ -17,11 +17,13 @@
__author__ = 'tom.h.miller@gmail.com (Tom Miller)'
import sys
def get_missing_requirements(third_party_reqs):
"""Return a list of missing third party packages."""
import sys
def is_missing(packages):
"""Return True if a package can't be imported."""
retval = True
sys_path_original = sys.path[:]
# Remove the current directory from the list of paths to check when
# importing modules.
@@ -34,55 +36,17 @@ def get_missing_requirements(third_party_reqs):
sys.path.remove(os.path.abspath(os.path.curdir))
except ValueError:
pass
missing_pkgs = []
for pkg in third_party_reqs:
if not isinstance(packages, type([])):
packages = [packages]
for name in packages:
try:
__import__(pkg)
__import__(name)
retval = False
except ImportError:
missing_pkgs.append(pkg)
# JSON support gets its own special logic
try:
import_json(sys.path)
except ImportError:
missing_pkgs.append('simplejson')
retval = True
if retval == False:
break
sys.path = sys_path_original
return missing_pkgs
def import_json(import_path=None):
"""Return a package that will provide JSON support.
Args:
import_path: list Value to assing to sys.path before checking for packages.
Default None for default sys.path.
Return:
A package, one of 'json' (if running python 2.6),
'django.utils.simplejson' (if django is installed)
'simplejson' (if third party library simplejson is found)
Raises:
ImportError if none of those packages are found.
"""
import sys
sys_path_orig = sys.path[:]
if import_path is not None:
sys.path = import_path
try:
# Should work for Python 2.6.
pkg = __import__('json')
except ImportError:
try:
pkg = __import__('django.utils').simplejson
except ImportError:
try:
pkg = __import__('simplejson')
except ImportError:
pkg = None
if import_path is not None:
sys.path = sys_path_orig
if pkg:
return pkg
else:
raise ImportError('Cannot find json support')
return retval