summaryrefslogtreecommitdiff
path: root/distutils2
diff options
context:
space:
mode:
authorGael Pasgrimaud <gael@gawel.org>2011-01-29 14:03:03 +0100
committerGael Pasgrimaud <gael@gawel.org>2011-01-29 14:03:03 +0100
commit761946c352cead905ef58da338ee2f358060146c (patch)
tree4f21b948704c0d960941347630b4dde6eb0241db /distutils2
parent4aa91e6aeb12e093c297f5cc439407ffa59ea156 (diff)
parent606e658869b0fa8f17da5a30df14dd5f95527250 (diff)
downloaddisutils2-761946c352cead905ef58da338ee2f358060146c.tar.gz
merge to lastest head
Diffstat (limited to 'distutils2')
-rw-r--r--distutils2/config.py11
-rw-r--r--distutils2/metadata.py6
-rw-r--r--distutils2/util.py115
3 files changed, 126 insertions, 6 deletions
diff --git a/distutils2/config.py b/distutils2/config.py
index f6f78f6..a684330 100644
--- a/distutils2/config.py
+++ b/distutils2/config.py
@@ -76,10 +76,9 @@ class Config(object):
return value
def _multiline(self, value):
- if '\n' in value:
- value = [v for v in
- [v.strip() for v in value.split('\n')]
- if v != '']
+ value = [v for v in
+ [v.strip() for v in value.split('\n')]
+ if v != '']
return value
def _read_setup_cfg(self, parser):
@@ -100,7 +99,9 @@ class Config(object):
if 'metadata' in content:
for key, value in content['metadata'].iteritems():
key = key.replace('_', '-')
- value = self._multiline(value)
+ if metadata.is_multi_field(key):
+ value = self._multiline(value)
+
if key == 'project-url':
value = [(label.strip(), url.strip())
for label, url in
diff --git a/distutils2/metadata.py b/distutils2/metadata.py
index 0fa04b9..44522fe 100644
--- a/distutils2/metadata.py
+++ b/distutils2/metadata.py
@@ -174,7 +174,7 @@ _VERSION_FIELDS = ('Version',)
_LISTFIELDS = ('Platform', 'Classifier', 'Obsoletes',
'Requires', 'Provides', 'Obsoletes-Dist',
'Provides-Dist', 'Requires-Dist', 'Requires-External',
- 'Project-URL')
+ 'Project-URL', 'Supported-Platform')
_LISTTUPLEFIELDS = ('Project-URL',)
_ELEMENTSFIELD = ('Keywords',)
@@ -308,6 +308,10 @@ class DistributionMetadata(object):
name = self._convert_name(name)
return name in _ALL_FIELDS
+ def is_multi_field(self, name):
+ name = self._convert_name(name)
+ return name in _LISTFIELDS
+
def read(self, filepath):
"""Read the metadata values from a file path."""
self.read_file(open(filepath))
diff --git a/distutils2/util.py b/distutils2/util.py
index 9a0e714..d831d55 100644
--- a/distutils2/util.py
+++ b/distutils2/util.py
@@ -15,6 +15,7 @@ import zipfile
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,117 @@ class Mixin2to3:
""" 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
+ if arg == "long_description":
+ filename = has_get_option(config, section, "description_file")
+ print "We have a filename", filename
+ if filename:
+ in_cfg_value = open(filename).read()
+ else:
+ continue
+
+ 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()