summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2013-05-30 02:03:39 -0400
committerMonty Taylor <mordred@inaugust.com>2013-06-07 21:47:38 -0400
commit04984a51b1a6f933531063b53b9efa5bf3edfb23 (patch)
tree51855a88660d977936e34d4251b6d88d7614c1d3
parenta65e8ee654c623fa360ef9e2b4c9b72039a32b39 (diff)
downloadpbr-04984a51b1a6f933531063b53b9efa5bf3edfb23.tar.gz
Refactor hooks file.
It was long and rambly. It's also in need of unittesting. So split it into some classes so that we can test the inputs and outputs more sensibly. Change-Id: I3d28f5771e38b819f98a9af06aeb06529be7b302
-rw-r--r--pbr/find_package.py30
-rw-r--r--pbr/hooks.py92
-rw-r--r--pbr/hooks/__init__.py30
-rw-r--r--pbr/hooks/backwards.py34
-rw-r--r--pbr/hooks/base.py36
-rw-r--r--pbr/hooks/commands.py48
-rw-r--r--pbr/hooks/files.py80
-rw-r--r--pbr/hooks/metadata.py34
-rw-r--r--pbr/packaging.py21
9 files changed, 302 insertions, 103 deletions
diff --git a/pbr/find_package.py b/pbr/find_package.py
new file mode 100644
index 0000000..2319c06
--- /dev/null
+++ b/pbr/find_package.py
@@ -0,0 +1,30 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import os
+import setuptools
+
+
+def smart_find_packages(package_list):
+ """Run find_packages the way we intend."""
+ packages = []
+ for pkg in package_list.strip().split("\n"):
+ pkg_path = pkg.replace('.', os.path.sep)
+ packages.append(pkg)
+ packages.extend(['%s.%s' % (pkg, f)
+ for f in setuptools.find_packages(pkg_path)])
+ return "\n".join(set(packages))
diff --git a/pbr/hooks.py b/pbr/hooks.py
deleted file mode 100644
index 7a61ece..0000000
--- a/pbr/hooks.py
+++ /dev/null
@@ -1,92 +0,0 @@
-# vim: tabstop=4 shiftwidth=4 softtabstop=4
-
-# Copyright 2013 Hewlett-Packard Development Company, L.P.
-# All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-import os
-import setuptools
-
-from pbr import packaging
-
-
-def smart_find_packages(package_list):
- """Run find_packages the way we intend."""
- packages = []
- for pkg in package_list.strip().split("\n"):
- pkg_path = pkg.replace('.', os.path.sep)
- packages.append(pkg)
- packages.extend(['%s.%s' % (pkg, f)
- for f in setuptools.find_packages(pkg_path)])
- return "\n".join(set(packages))
-
-
-def setup_hook(config):
- """Filter config parsed from a setup.cfg to inject our defaults."""
- metadata = config['metadata']
- metadata['version'] = packaging.get_version(metadata['name'],
- metadata.get('version', None))
- metadata['requires_dist'] = "\n".join(packaging.parse_requirements())
- config['metadata'] = metadata
-
- config['global'] = config.get('global', dict())
- config['global']['commands'] = config['global'].get('commands', "") + """
-pbr.packaging.LocalSDist
-"""
- if packaging.have_sphinx():
- config['global']['commands'] = config['global']['commands'] + """
-pbr.packaging.LocalBuildDoc
-pbr.packaging.LocalBuildLatex
-"""
-
- pbr_config = config.get('pbr', dict())
- use_egg = packaging.get_boolean_option(
- pbr_config, 'use-egg', 'PBR_USE_EGG')
- # We always want non-egg install unless explicitly requested
- if 'manpages' in pbr_config or not use_egg:
- config['global']['commands'] = config['global']['commands'] + """
-pbr.packaging.DistutilsInstall
-"""
-
- backwards_compat = config.get('backwards_compat', dict())
- backwards_compat['dependency_links'] = "\n".join(
- packaging.parse_dependency_links())
- backwards_compat['include_package_data'] = 'True'
- backwards_compat['tests_require'] = "\n".join(
- packaging.parse_requirements(
- ["test-requirements.txt", "tools/test-requires"]))
- config['backwards_compat'] = backwards_compat
-
- files = config.get('files', dict())
- package = files.get('packages', metadata['name']).strip()
- if os.path.isdir(package):
- files['packages'] = smart_find_packages(package)
-
- if 'manpages' in pbr_config:
- man_sections = dict()
- manpages = pbr_config['manpages']
- data_files = files.get('data_files', '')
- for manpage in manpages.split():
- section_number = manpage.strip()[-1]
- section = man_sections.get(section_number, list())
- section.append(manpage.strip())
- man_sections[section_number] = section
- for (section, pages) in man_sections.items():
- manpath = os.path.join(packaging.get_manpath(), 'man%s' % section)
- data_files = "%s\n%s" % (data_files, "%s =" % manpath)
- for page in pages:
- data_files = "%s\n%s" % (data_files, page)
- files['data_files'] = data_files
-
- config['files'] = files
diff --git a/pbr/hooks/__init__.py b/pbr/hooks/__init__.py
new file mode 100644
index 0000000..b35cd42
--- /dev/null
+++ b/pbr/hooks/__init__.py
@@ -0,0 +1,30 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from pbr.hooks import backwards
+from pbr.hooks import commands
+from pbr.hooks import files
+from pbr.hooks import metadata
+
+
+def setup_hook(config):
+ """Filter config parsed from a setup.cfg to inject our defaults."""
+ metadata_config = metadata.MetadataConfig(config)
+ metadata_config.run()
+ backwards.BackwardsCompatConfig(config).run()
+ commands.CommandsConfig(config).run()
+ files.FilesConfig(config, metadata_config.get_name()).run()
diff --git a/pbr/hooks/backwards.py b/pbr/hooks/backwards.py
new file mode 100644
index 0000000..bcc69a5
--- /dev/null
+++ b/pbr/hooks/backwards.py
@@ -0,0 +1,34 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from pbr.hooks import base
+from pbr import packaging
+
+
+class BackwardsCompatConfig(base.BaseConfig):
+
+ section = 'backwards_compat'
+
+ def hook(self):
+ self.config['include_package_data'] = 'True'
+ packaging.append_text_list(
+ self.config, 'dependency_links',
+ packaging.parse_dependency_links())
+ packaging.append_text_list(
+ self.config, 'tests_require',
+ packaging.parse_requirements(
+ ["test-requirements.txt", "tools/test-requires"]))
diff --git a/pbr/hooks/base.py b/pbr/hooks/base.py
new file mode 100644
index 0000000..925573a
--- /dev/null
+++ b/pbr/hooks/base.py
@@ -0,0 +1,36 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+
+class BaseConfig(object):
+
+ section = None
+
+ def __init__(self, config):
+ self._global_config = config
+ self.config = self._global_config.get(self.section, dict())
+ self.pbr_config = config.get('pbr', dict())
+
+ def run(self):
+ self.hook()
+ self.save()
+
+ def hook(self):
+ pass
+
+ def save(self):
+ self._global_config[self.section] = self.config
diff --git a/pbr/hooks/commands.py b/pbr/hooks/commands.py
new file mode 100644
index 0000000..99fa34c
--- /dev/null
+++ b/pbr/hooks/commands.py
@@ -0,0 +1,48 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from pbr.hooks import base
+from pbr import packaging
+
+
+class CommandsConfig(base.BaseConfig):
+
+ section = 'global'
+
+ def __init__(self, config):
+ super(CommandsConfig, self).__init__(config)
+ self.commands = self.config.get('commands', "")
+
+ def save(self):
+ self.config['commands'] = self.commands
+ super(CommandsConfig, self).save()
+
+ def add_command(self, command):
+ self.commands = "%s\n%s" % (self.commands, command)
+
+ def hook(self):
+ self.add_command('pbr.packaging.LocalSDist')
+
+ if packaging.have_sphinx():
+ self.add_command('pbr.packaging.LocalBuildDoc')
+ self.add_command('pbr.packaging.LocalBuildLatex')
+
+ use_egg = packaging.get_boolean_option(
+ self.pbr_config, 'use-egg', 'PBR_USE_EGG')
+ # We always want non-egg install unless explicitly requested
+ if 'manpages' in self.pbr_config or not use_egg:
+ self.add_command('pbr.packaging.DistutilsInstall')
diff --git a/pbr/hooks/files.py b/pbr/hooks/files.py
new file mode 100644
index 0000000..fb6a9bf
--- /dev/null
+++ b/pbr/hooks/files.py
@@ -0,0 +1,80 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import os
+import sys
+
+from pbr import find_package
+from pbr.hooks import base
+
+
+def get_manpath():
+ manpath = 'share/man'
+ if os.path.exists(os.path.join(sys.prefix, 'man')):
+ # This works around a bug with install where it expects every node
+ # in the relative data directory to be an actual directory, since at
+ # least Debian derivatives (and probably other platforms as well)
+ # like to symlink Unixish /usr/local/man to /usr/local/share/man.
+ manpath = 'man'
+ return manpath
+
+
+def get_man_section(section):
+ return os.path.join(get_manpath(), 'man%s' % section)
+
+
+class FilesConfig(base.BaseConfig):
+
+ section = 'files'
+
+ def __init__(self, config, name):
+ super(FilesConfig, self).__init__(config)
+ self.name = name
+ self.data_files = self.config.get('data_files', '')
+
+ def save(self):
+ self.config['data_files'] = self.data_files
+ super(FilesConfig, self).save()
+
+ def add_man_path(self, man_path):
+ self.data_files = "%s\n%s =" % (self.data_files, man_path)
+
+ def add_man_page(self, man_page):
+ self.data_files = "%s\n %s" % (self.data_files, man_page)
+
+ def get_man_sections(self):
+ man_sections = dict()
+ manpages = self.pbr_config['manpages']
+ for manpage in manpages.split():
+ section_number = manpage.strip()[-1]
+ section = man_sections.get(section_number, list())
+ section.append(manpage.strip())
+ man_sections[section_number] = section
+ return man_sections
+
+ def hook(self):
+ package = self.config.get('packages', self.name).strip()
+ if os.path.isdir(package):
+ self.config['packages'] = find_package.smart_find_packages(package)
+
+ if 'manpages' in self.pbr_config:
+ man_sections = self.get_man_sections()
+ for (section, pages) in man_sections.items():
+ manpath = get_man_section(section)
+ self.add_man_path(manpath)
+ for page in pages:
+ self.add_man_page(page)
diff --git a/pbr/hooks/metadata.py b/pbr/hooks/metadata.py
new file mode 100644
index 0000000..687e149
--- /dev/null
+++ b/pbr/hooks/metadata.py
@@ -0,0 +1,34 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from pbr.hooks import base
+from pbr import packaging
+
+
+class MetadataConfig(base.BaseConfig):
+
+ section = 'metadata'
+
+ def hook(self):
+ self.config['version'] = packaging.get_version(
+ self.config['name'], self.config.get('version', None))
+ packaging.append_text_list(
+ self.config, 'requires_dist',
+ packaging.parse_requirements())
+
+ def get_name(self):
+ return self.config['name']
diff --git a/pbr/packaging.py b/pbr/packaging.py
index 4cccffa..b23e2fb 100644
--- a/pbr/packaging.py
+++ b/pbr/packaging.py
@@ -38,6 +38,16 @@ log.set_verbosity(log.INFO)
TRUE_VALUES = ['true', '1', 'yes']
+def append_text_list(config, key, text_list):
+ """Append a \n separated list to possibly existing value."""
+ new_value = []
+ current_value = config.get(key, "")
+ if current_value:
+ new_value.append(current_value)
+ new_value.extend(text_list)
+ config[key] = '\n'.join(new_value)
+
+
def _parse_mailmap(mailmap_info):
mapping = dict()
for l in mailmap_info:
@@ -491,14 +501,3 @@ def get_version(package_name, pre_version=None):
return version
raise Exception("Versioning for this project requires either an sdist"
" tarball, or access to an upstream git repository.")
-
-
-def get_manpath():
- manpath = 'share/man'
- if os.path.exists(os.path.join(sys.prefix, 'man')):
- # This works around a bug with install where it expects every node
- # in the relative data directory to be an actual directory, since at
- # least Debian derivatives (and probably other platforms as well)
- # like to symlink Unixish /usr/local/man to /usr/local/share/man.
- manpath = 'man'
- return manpath