summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2014-12-15 18:59:30 -0500
committerMonty Taylor <mordred@inaugust.com>2014-12-16 14:59:13 -0500
commitb3678714aabd9c82a4ad389036e160a0819fd45c (patch)
tree696dc916240379867a3bedbfec794f2664d8591f
parent1c89d18b6beecade6b7c9007b324e8cf0f486e22 (diff)
downloadpbr-b3678714aabd9c82a4ad389036e160a0819fd45c.tar.gz
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. Change-Id: I35a1489c9ab637769218d1e6a26d1115a52a7051
-rw-r--r--pbr/cmd/__init__.py0
-rw-r--r--pbr/cmd/main.py110
-rw-r--r--pbr/packaging.py50
-rw-r--r--setup.cfg4
4 files changed, 161 insertions, 3 deletions
diff --git a/pbr/cmd/__init__.py b/pbr/cmd/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/pbr/cmd/__init__.py
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())
diff --git a/pbr/packaging.py b/pbr/packaging.py
index 44c14cc..4f5897c 100644
--- a/pbr/packaging.py
+++ b/pbr/packaging.py
@@ -24,6 +24,7 @@ from __future__ import unicode_literals
import email
import io
+import json
import os
import re
import subprocess
@@ -776,6 +777,49 @@ def have_sphinx():
return _have_sphinx
+def _get_raw_tag_info(git_dir):
+ describe = _run_git_command(['describe', '--always'], git_dir)
+ if "-" in describe:
+ return describe.rsplit("-", 2)[-2]
+ if "." in describe:
+ return 0
+ return None
+
+
+def get_is_release(git_dir):
+ return _get_raw_tag_info(git_dir) == 0
+
+
+def _run_git_functions():
+ git_dir = _get_git_directory()
+ if git_dir and _git_is_installed():
+ return git_dir
+ return None
+
+
+def get_git_short_sha(git_dir=None):
+ """Return the short sha for this repo, if it exists."""
+ if not git_dir:
+ git_dir = _run_git_functions()
+ if git_dir:
+ return _run_git_command(
+ ['log', '-n1', '--pretty=format:%h'], git_dir)
+ return None
+
+
+def write_pbr_json(cmd, basename, filename):
+ git_dir = _run_git_functions()
+ if not git_dir:
+ return
+ values = dict()
+ git_version = get_git_short_sha(git_dir)
+ is_release = get_is_release(git_dir)
+ if git_version is not None:
+ values['git_version'] = git_version
+ values['is_release'] = is_release
+ cmd.write_file('pbr', filename, json.dumps(values))
+
+
def _get_revno(git_dir):
"""Return the number of commits since the most recent tag.
@@ -783,9 +827,9 @@ def _get_revno(git_dir):
tags then we fall back to counting commits since the beginning
of time.
"""
- describe = _run_git_command(['describe', '--always'], git_dir)
- if "-" in describe:
- return describe.rsplit("-", 2)[-2]
+ raw_tag_info = _get_raw_tag_info(git_dir)
+ if raw_tag_info:
+ return raw_tag_info
# no tags found
revlist = _run_git_command(
diff --git a/setup.cfg b/setup.cfg
index 34b88a8..8f41901 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -38,6 +38,10 @@ distutils.setup_keywords =
pbr = pbr.core:pbr
distutils.commands =
testr = pbr.testr_command:Testr
+egg_info.writers =
+ pbr.json = pbr.packaging:write_pbr_json
+console_scripts =
+ pbr = pbr.cmd.main:main
[build_sphinx]
all_files = 1