[skeleton] do not import the __pkginfo__.py module to make cubes installable via setuptools
authorDavid Douard <david.douard@logilab.fr>
Tue, 03 Mar 2015 12:26:47 +0100
changeset 11152 fccc6ddd975c
parent 11151 4259c55df3e7
child 11153 e48db902af71
[skeleton] do not import the __pkginfo__.py module to make cubes installable via setuptools When using a simple "import" statement, running 'python setup.py install' for a cube fails in a fresh virtualenv due to the fact the install of the dependencies (here cubicweb iteself) is done by the python process doing the cube's installation, so the __pkginfo__ module from the cube is already loaded (thus used) when installing cubicweb... So we apply the third technique from: https://packaging.python.org/en/latest/single_source_version.html
cubicweb/skeleton/setup.py
--- a/cubicweb/skeleton/setup.py	Tue Feb 23 12:18:47 2016 +0100
+++ b/cubicweb/skeleton/setup.py	Tue Mar 03 12:26:47 2015 +0100
@@ -1,10 +1,10 @@
 #!/usr/bin/env python
 # pylint: disable=W0142,W0403,W0404,W0613,W0622,W0622,W0704,R0904,C0103,E0611
 #
-# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
-# This file is part of CubicWeb.
+# This file is part of a CubicWeb cube.
 #
 # CubicWeb is free software: you can redistribute it and/or modify it under the
 # terms of the GNU Lesser General Public License as published by the Free
@@ -25,7 +25,7 @@
 import os
 import sys
 import shutil
-from os.path import exists, join
+from os.path import exists, join, dirname
 
 try:
     if os.environ.get('NO_SETUPTOOLS'):
@@ -39,34 +39,45 @@
     USE_SETUPTOOLS = False
 from distutils.command import install_data
 
-# import required features
-from __pkginfo__ import modname, version, license, description, web, \
-    author, author_email, classifiers
+
+# load metadata from the __pkginfo__.py file so there is no risk of conflict
+# see https://packaging.python.org/en/latest/single_source_version.html
+base_dir = dirname(__file__)
+pkginfo = {}
+with open(join(base_dir, "__pkginfo__.py")) as f:
+    exec(f.read(), pkginfo)
 
-if exists('README'):
-    long_description = open('README').read()
-else:
-    long_description = ''
+# get required metadatas
+modname = pkginfo['modname']
+version = pkginfo['version']
+license = pkginfo['license']
+description = pkginfo['description']
+web = pkginfo['web']
+author = pkginfo['author']
+author_email = pkginfo['author_email']
+classifiers = pkginfo['classifiers']
 
-# import optional features
-import __pkginfo__
+with open(join(base_dir, 'README')) as f:
+    long_description = f.read()
+
+# get optional metadatas
+distname = pkginfo.get('distname', modname)
+scripts = pkginfo.get('scripts', ())
+include_dirs = pkginfo.get('include_dirs', ())
+data_files = pkginfo.get('data_files', None)
+ext_modules = pkginfo.get('ext_modules', None)
+dependency_links = pkginfo.get('dependency_links', ())
+
 if USE_SETUPTOOLS:
     requires = {}
     for entry in ("__depends__",):  # "__recommends__"):
-        requires.update(getattr(__pkginfo__, entry, {}))
+        requires.update(pkginfo.get(entry, {}))
     install_requires = [("%s %s" % (d, v and v or "")).strip()
                         for d, v in requires.items()]
 else:
     install_requires = []
 
-distname = getattr(__pkginfo__, 'distname', modname)
-scripts = getattr(__pkginfo__, 'scripts', ())
-include_dirs = getattr(__pkginfo__, 'include_dirs', ())
-data_files = getattr(__pkginfo__, 'data_files', None)
-ext_modules = getattr(__pkginfo__, 'ext_modules', None)
-dependency_links = getattr(__pkginfo__, 'dependency_links', ())
-
-BASE_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
+BASE_BLACKLIST = ('CVS', '.svn', '.hg', '.git', 'debian', 'dist', 'build')
 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~')
 
 
@@ -100,8 +111,8 @@
                 dirnames.remove(norecurs)
             except ValueError:
                 pass
-        for dirname in dirnames:
-            dest = join(to_dir, dirname)
+        for dir_name in dirnames:
+            dest = join(to_dir, dir_name)
             if not exists(dest):
                 os.mkdir(dest)
         for filename in filenames:
@@ -133,6 +144,7 @@
                 dest = join(self.install_dir, base, directory)
                 export(directory, dest, verbose=False)
 
+
 # re-enable copying data files in sys.prefix
 old_install_data = install_data.install_data
 if USE_SETUPTOOLS: