Merge lp:~cjwatson/launchpad/version-info-script into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 19019
Proposed branch: lp:~cjwatson/launchpad/version-info-script
Merge into: lp:launchpad
Diff against target: 141 lines (+121/-0)
3 files modified
lib/lp/scripts/utilities/tests/test_versioninfo.py (+85/-0)
lib/lp/scripts/utilities/versioninfo.py (+35/-0)
setup.py (+1/-0)
To merge this branch: bzr merge lp:~cjwatson/launchpad/version-info-script
Reviewer Review Type Date Requested Status
Tom Wardill (community) Approve
Review via email: mp+370529@code.launchpad.net

Commit message

Add a script to show version information.

Description of the change

This is useful in deployment scripts. I've found a few places where deployment code is calling something like "bzr revno /path/to/launchpad", which will break when we migrate to git. This script will allow replacing that with "/path/to/launchpad/bin/version-info -a revision"; that's a bit wordier, but not unreasonably so, and it can easily be extended if we find similar but slightly different requirements.

To post a comment you must log in.
Revision history for this message
Tom Wardill (twom) wrote :

Looks pretty useful to me :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'lib/lp/scripts/utilities/tests/test_versioninfo.py'
2--- lib/lp/scripts/utilities/tests/test_versioninfo.py 1970-01-01 00:00:00 +0000
3+++ lib/lp/scripts/utilities/tests/test_versioninfo.py 2019-07-30 12:19:41 +0000
4@@ -0,0 +1,85 @@
5+# Copyright 2019 Canonical Ltd. This software is licensed under the
6+# GNU Affero General Public License version 3 (see the file LICENSE).
7+
8+"""Test the script to show version information."""
9+
10+from __future__ import absolute_import, print_function, unicode_literals
11+
12+__metaclass__ = type
13+
14+from textwrap import dedent
15+
16+from fixtures import MockPatch
17+from testtools.content import text_content
18+
19+from lp.app import versioninfo
20+from lp.scripts.utilities.versioninfo import main as versioninfo_main
21+from lp.services.utils import CapturedOutput
22+from lp.testing import TestCase
23+
24+
25+class TestVersionInfo(TestCase):
26+
27+ def runScript(self, args, expect_exit=False):
28+ try:
29+ with MockPatch('sys.argv', ['version-info'] + args):
30+ with CapturedOutput() as captured:
31+ versioninfo_main()
32+ except SystemExit:
33+ exited = True
34+ else:
35+ exited = False
36+ stdout = captured.stdout.getvalue()
37+ stderr = captured.stderr.getvalue()
38+ self.addDetail('stdout', text_content(stdout))
39+ self.addDetail('stderr', text_content(stderr))
40+ if expect_exit:
41+ if not exited:
42+ raise AssertionError('Script unexpectedly exited successfully')
43+ else:
44+ if exited:
45+ raise AssertionError(
46+ 'Script unexpectedly exited unsuccessfully')
47+ self.assertEqual('', stderr)
48+ return stdout
49+
50+ def test_attribute_revision(self):
51+ for option in '-a', '--attribute':
52+ self.assertEqual(
53+ versioninfo.revision + '\n',
54+ self.runScript([option, 'revision']))
55+
56+ def test_attribute_display_revision(self):
57+ for option in '-a', '--attribute':
58+ self.assertEqual(
59+ versioninfo.display_revision + '\n',
60+ self.runScript([option, 'display_revision']))
61+
62+ def test_attribute_date(self):
63+ for option in '-a', '--attribute':
64+ self.assertEqual(
65+ versioninfo.date + '\n',
66+ self.runScript([option, 'date']))
67+
68+ def test_attribute_branch_nick(self):
69+ for option in '-a', '--attribute':
70+ self.assertEqual(
71+ versioninfo.branch_nick + '\n',
72+ self.runScript([option, 'branch_nick']))
73+
74+ def test_attribute_nonsense(self):
75+ for option in '-a', '--attribute':
76+ self.runScript([option, 'nonsense'], expect_exit=True)
77+
78+ def test_all_attributes(self):
79+ expected_output = dedent('''\
80+ Revision: {revision}
81+ Display revision: {display_revision}
82+ Date: {date}
83+ Branch nick: {branch_nick}
84+ ''').format(
85+ revision=versioninfo.revision,
86+ display_revision=versioninfo.display_revision,
87+ date=versioninfo.date,
88+ branch_nick=versioninfo.branch_nick)
89+ self.assertEqual(expected_output, self.runScript([]))
90
91=== added file 'lib/lp/scripts/utilities/versioninfo.py'
92--- lib/lp/scripts/utilities/versioninfo.py 1970-01-01 00:00:00 +0000
93+++ lib/lp/scripts/utilities/versioninfo.py 2019-07-30 12:19:41 +0000
94@@ -0,0 +1,35 @@
95+# Copyright 2019 Canonical Ltd. This software is licensed under the
96+# GNU Affero General Public License version 3 (see the file LICENSE).
97+
98+"""Script to show version information.
99+
100+This is useful in deployment scripts.
101+"""
102+
103+from __future__ import absolute_import, print_function, unicode_literals
104+
105+__metaclass__ = type
106+__all__ = ['main']
107+
108+import argparse
109+
110+from lp.app import versioninfo
111+
112+
113+def main():
114+ parser = argparse.ArgumentParser(
115+ description=__doc__,
116+ formatter_class=argparse.RawDescriptionHelpFormatter)
117+ parser.add_argument(
118+ '-a', '--attribute',
119+ choices=['revision', 'display_revision', 'date', 'branch_nick'],
120+ help='Display a single version information attribute.')
121+ args = parser.parse_args()
122+
123+ if args.attribute:
124+ print(getattr(versioninfo, args.attribute))
125+ else:
126+ print('Revision:', versioninfo.revision)
127+ print('Display revision:', versioninfo.display_revision)
128+ print('Date:', versioninfo.date)
129+ print('Branch nick:', versioninfo.branch_nick)
130
131=== modified file 'setup.py'
132--- setup.py 2019-06-18 16:52:56 +0000
133+++ setup.py 2019-07-30 12:19:41 +0000
134@@ -309,6 +309,7 @@
135 'test = lp.scripts.utilities.test:main',
136 'tracereport = zc.zservertracelog.tracereport:main',
137 'twistd = twisted.scripts.twistd:run',
138+ 'version-info = lp.scripts.utilities.versioninfo:main',
139 'watch_jsbuild = lp.scripts.utilities.js.watchjsbuild:main',
140 'with-xvfb = lp.scripts.utilities.withxvfb:main',
141 ]