[Python-checkins] distutils2: Adding methods to generate a distutils setup.py file reading the setup.cfg.
tarek.ziade
python-checkins at python.org
Sat Jan 29 14:21:48 CET 2011
tarek.ziade pushed 0b44306763cf to distutils2:
http://hg.python.org/distutils2/rev/0b44306763cf
changeset: 908:0b44306763cf
user: Julien Miotte <miotte.julien at gmail.com>
date: Fri Jan 28 17:53:42 2011 +0100
summary:
Adding methods to generate a distutils setup.py file reading the setup.cfg.
That way, distribution maintainers will be able to offer distutils & distutils2
compatibility, without having to maintain the same information in two files.
The only that needs to be maintained is the setup.cfg. Indeed, the setup.py
reads the setup.cfg file every time it is called, to determine which are the
correct args to pass on to distutils.core.setup().
files:
distutils2/util.py
diff --git a/distutils2/util.py b/distutils2/util.py
--- a/distutils2/util.py
+++ b/distutils2/util.py
@@ -15,6 +15,7 @@
from copy import copy
from fnmatch import fnmatchcase
from ConfigParser import RawConfigParser
+from inspect import getsource
from distutils2.errors import (DistutilsPlatformError, DistutilsFileError,
DistutilsByteCompileError, DistutilsExecError)
@@ -1127,3 +1128,116 @@
""" Issues a call to util.run_2to3. """
return run_2to3(files, doctests_only, self.fixer_names,
self.options, self.explicit)
+
+
+def generate_distutils_kwargs_from_setup_cfg(file='setup.cfg'):
+ """ Distutils2 to distutils1 compatibility util.
+
+ This method uses an existing setup.cfg to generate a dictionnary of
+ keywords that can be used by distutils.core.setup(kwargs**).
+
+ :param file:
+ The setup.cfg path.
+ :raises DistutilsFileError:
+ When the setup.cfg file is not found.
+
+ """
+ # We need to declare the following constants here so that it's easier to
+ # generate the setup.py afterwards, using inspect.getsource.
+ D1_D2_SETUP_ARGS = {
+ # D1 name : (D2_section, D2_name)
+ "name" : ("metadata",),
+ "version" : ("metadata",),
+ "author" : ("metadata",),
+ "author_email" : ("metadata",),
+ "maintainer" : ("metadata",),
+ "maintainer_email" : ("metadata",),
+ "url" : ("metadata", "home_page"),
+ "description" : ("metadata", "summary"),
+ "long_description" : ("metadata", "description"),
+ "download-url" : ("metadata",),
+ "classifiers" : ("metadata", "classifier"),
+ "platforms" : ("metadata", "platform"), # Needs testing
+ "license" : ("metadata",),
+ "requires" : ("metadata", "requires_dist"),
+ "provides" : ("metadata", "provides_dist"), # Needs testing
+ "obsoletes" : ("metadata", "obsoletes_dist"), # Needs testing
+
+ "packages" : ("files",),
+ "scripts" : ("files",),
+ "py_modules" : ("files", "modules"), # Needs testing
+ }
+
+ MULTI_FIELDS = ("classifiers",
+ "requires",
+ "platforms",
+ "packages",
+ "scripts")
+
+ def has_get_option(config, section, option):
+ if config.has_option(section, option):
+ return config.get(section, option)
+ elif config.has_option(section, option.replace('_', '-')):
+ return config.get(section, option.replace('_', '-'))
+ else:
+ return False
+
+ # The method source code really starts here.
+ config = RawConfigParser()
+ if not os.path.exists(file):
+ raise DistutilsFileError("file '%s' does not exist" %
+ os.path.abspath(file))
+ config.read(file)
+
+ kwargs = {}
+ for arg in D1_D2_SETUP_ARGS:
+ if len(D1_D2_SETUP_ARGS[arg]) == 2:
+ # The distutils field name is different than distutils2's.
+ section, option = D1_D2_SETUP_ARGS[arg]
+
+ elif len(D1_D2_SETUP_ARGS[arg]) == 1:
+ # The distutils field name is the same thant distutils2's.
+ section = D1_D2_SETUP_ARGS[arg][0]
+ option = arg
+
+ in_cfg_value = has_get_option(config, section, option)
+ if not in_cfg_value:
+ # There is no such option in the setup.cfg
+ continue
+
+ if arg == "long_description":
+ filename = has_get_option("description_file")
+ if filename:
+ in_cfg_value = open(filename).read()
+
+ if arg in MULTI_FIELDS:
+ # Special behaviour when we have a multi line option
+ if "\n" in in_cfg_value:
+ in_cfg_value = in_cfg_value.strip().split('\n')
+ else:
+ in_cfg_value = list((in_cfg_value,))
+
+ kwargs[arg] = in_cfg_value
+
+ return kwargs
+
+
+def generate_distutils_setup_py():
+ """ Generate a distutils compatible setup.py using an existing setup.cfg.
+
+ :raises DistutilsFileError:
+ When a setup.py already exists.
+ """
+ if os.path.exists("setup.py"):
+ raise DistutilsFileError("A pre existing setup.py file exists")
+
+ handle = open("setup.py", "w")
+ handle.write("# Distutils script using distutils2 setup.cfg to call the\n")
+ handle.write("# distutils.core.setup() with the right args.\n\n\n")
+ handle.write("import os\n")
+ handle.write("from distutils.core import setup\n")
+ handle.write("from ConfigParser import RawConfigParser\n\n")
+ handle.write(getsource(generate_distutils_kwargs_from_setup_cfg))
+ handle.write("\n\nkwargs = generate_distutils_kwargs_from_setup_cfg()\n")
+ handle.write("setup(**kwargs)")
+ handle.close()
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list