diff --git a/lib/fpm/package/pyfpm/get_metadata.py b/lib/fpm/package/pyfpm/get_metadata.py index cd012755f0..8110954ff5 100644 --- a/lib/fpm/package/pyfpm/get_metadata.py +++ b/lib/fpm/package/pyfpm/get_metadata.py @@ -32,13 +32,16 @@ def u(s): class get_metadata(Command): description = "get package metadata" user_options = [ + ('allow-underscores', 'a', 'allow underscores in package names/deps'), ('load-requirements-txt', 'l', "load dependencies from requirements.txt"), ("output=", "o", "output destination for metadata json") ] - boolean_options = ['load-requirements-txt'] + boolean_options = ['allow-underscores', 'load-requirements-txt'] def initialize_options(self): + # allow-underscores is handled in python_patch.py; this is ignored here + self.allow_underscores = False self.load_requirements_txt = False self.cwd = None self.output = None @@ -68,7 +71,7 @@ def process_dep(self, dep): def run(self): data = { - "name": self.distribution.get_name(), + "name": pkg_resources.safe_name(self.distribution.get_name()), "version": self.distribution.get_version(), "author": u("%s <%s>") % ( u(self.distribution.get_author()), diff --git a/lib/fpm/package/python.rb b/lib/fpm/package/python.rb index 08f9d0d052..65968f931c 100644 --- a/lib/fpm/package/python.rb +++ b/lib/fpm/package/python.rb @@ -19,6 +19,8 @@ # class FPM::Package::Python < FPM::Package # Flags '--foo' will be accessable as attributes[:python_foo] + option "--allow-underscores", :flag, "Should underscores be allowed in " \ + " package names and dependencies?", :default => true option "--bin", "PYTHON_EXECUTABLE", "The path to the python executable you wish to run.", :default => "python" option "--easyinstall", "EASYINSTALL_EXECUTABLE", @@ -233,6 +235,10 @@ def load_package_info(setup_py) setup_cmd = "env PYTHONPATH=#{pylib}:$PYTHONPATH #{attributes[:python_bin]} " \ "#{pylib}/python_patch.py --command-packages=pyfpm get_metadata --output=#{tmp}" + if attributes[:python_allow_underscores?] + setup_cmd += " --allow-underscores" + end + if attributes[:python_obey_requirements_txt?] setup_cmd += " --load-requirements-txt" end diff --git a/lib/fpm/package/python_patch.py b/lib/fpm/package/python_patch.py index a8f3e39013..134ea05389 100755 --- a/lib/fpm/package/python_patch.py +++ b/lib/fpm/package/python_patch.py @@ -1,4 +1,27 @@ +import pkg_resources +import re +import sys from setuptools.dist import Distribution + + +def gen_safe_name(allow_underscores): + if allow_underscores: + regex = '[^A-Za-z0-9_.]+' + else: + # This matches the original verion in pkg_resources. + regex = '[^A-Za-z0-9.]+' + + def safe_name(name): + return re.sub(regex, '-', name) + + return safe_name + + +# Use simple argument parsing--older python lacks argparse and optparse has no +# good way to ignore unrecognized options (which need to be passed through). +pkg_resources.safe_name = gen_safe_name('--allow-underscores' in sys.argv) + + try: # Many older modules include a setup.py that uses distutils.core, which # does not support the install_requires attribute. Monkey-patch