summaryrefslogtreecommitdiff
path: root/pbr/cmd/main.py
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2014-12-18 15:09:33 -0800
committerJeremy Stanley <fungi@yuggoth.org>2014-12-21 22:20:49 +0000
commitc01b8dae1e5ad91d30756140d9b591818444db2d (patch)
tree520bfbc3314e71ccc06e1d72aa2b9b6789607490 /pbr/cmd/main.py
parent1f5c9f71f99944ecf6e120efdb677f44fbe3b654 (diff)
downloadpbr-c01b8dae1e5ad91d30756140d9b591818444db2d.tar.gz
Port in git sha changes from 0.10 line
Stop including git sha in version strings We include it in pbr.json now. Including it is contentious in the world of python, and it's up for debate as to whether or not it provides value. Write and read more complex git sha info Instead of encoding the git sha into the version string, add it to a metadata file. This will allow us to get out of the business of arguing with pip and setuptools about version info. In order to make this really nice, provide a command line utility called "pbr" that has subcommands to print out the metadata that we're now including in the egg-info dir. Only import sphinx during hook processing When pbr is imported to handle writing the egg_info file because of the entry point, it's causing sphinx to get imported. This has a cascading effect once docutils is trying to be installed on a system with pbr installed. If some of the imports fail along the way, allow pbr to continue usefully but without the Sphinx extensions available. Eventually, when everything is installed, those extensions will work again when the commands for build_sphinx, etc. are run separately. Also slip in a change to reorder the default list of environments run by tox so the testr database is created using a dbm format available to all python versions. Integration test PBR commits Make sure that if a PBR commit is being tested then we install and use that source rather than the latest PBR release. Change-Id: Ie121e795be2eef30822daaa5fe8ab1c2315577ae (cherry picked from commit 65f4fafd907a16ea1952ab7072676db2e9e0c51d) (cherry picked from commit cd7da23937b66fea3ec42fa2f5a128f363a97e7e) Closes-Bug: #1403510 Co-Authored-By: Clark Boylan <clark.boylan@gmail.com> Co-Authored-By: Doug Hellmann <doug@doughellmann.com> Co-Authored-By: Jeremy Stanley <fungi@yuggoth.org>
Diffstat (limited to 'pbr/cmd/main.py')
-rw-r--r--pbr/cmd/main.py110
1 files changed, 110 insertions, 0 deletions
diff --git a/pbr/cmd/main.py b/pbr/cmd/main.py
new file mode 100644
index 0000000..de189c4
--- /dev/null
+++ b/pbr/cmd/main.py
@@ -0,0 +1,110 @@
+# Copyright 2014 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 argparse
+import json
+import sys
+
+import pkg_resources
+
+import pbr.version
+
+
+def _get_metadata(package_name):
+ try:
+ return json.loads(
+ pkg_resources.get_distribution(
+ package_name).get_metadata('pbr.json'))
+ except pkg_resources.DistributionNotFound:
+ raise Exception('Package {0} not installed'.format(package_name))
+ except Exception:
+ return None
+
+
+def get_sha(args):
+ sha = _get_info(args.name)['sha']
+ if sha:
+ print(sha)
+
+
+def get_info(args):
+ print("{name}\t{version}\t{released}\t{sha}".format(
+ **_get_info(args.name)))
+
+
+def _get_info(name):
+ metadata = _get_metadata(name)
+ version = pkg_resources.get_distribution(name).version
+ if metadata:
+ if metadata['is_release']:
+ released = 'released'
+ else:
+ released = 'pre-release'
+ sha = metadata['git_version']
+ else:
+ version_parts = version.split('.')
+ if version_parts[-1].startswith('g'):
+ sha = version_parts[-1][1:]
+ released = 'pre-release'
+ else:
+ sha = ""
+ released = "released"
+ for part in version_parts:
+ if not part.isdigit():
+ released = "pre-release"
+ return dict(name=name, version=version, sha=sha, released=released)
+
+
+def freeze(args):
+ for dist in pkg_resources.working_set:
+ info = _get_info(dist.project_name)
+ output = "{name}=={version}".format(**info)
+ if info['sha']:
+ output += " # git sha {sha}".format(**info)
+ print(output)
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description='pbr: Python Build Reasonableness')
+ parser.add_argument(
+ '-v', '--version', action='version',
+ version=str(pbr.version.VersionInfo('pbr')))
+
+ subparsers = parser.add_subparsers(
+ title='commands', description='valid commands', help='additional help')
+
+ cmd_sha = subparsers.add_parser('sha', help='print sha of package')
+ cmd_sha.set_defaults(func=get_sha)
+ cmd_sha.add_argument('name', help='package to print sha of')
+
+ cmd_sha = subparsers.add_parser(
+ 'info', help='print version info for package')
+ cmd_sha.set_defaults(func=get_info)
+ cmd_sha.add_argument('name', help='package to print info of')
+
+ cmd_sha = subparsers.add_parser(
+ 'freeze', help='print version info for all installed packages')
+ cmd_sha.set_defaults(func=freeze)
+
+ args = parser.parse_args()
+ try:
+ args.func(args)
+ except Exception as e:
+ print(e)
+
+
+if __name__ == '__main__':
+ sys.exit(main())