Merge ~blake-rouse/maas:fix-snap-version-git into maas:master

Proposed by Blake Rouse
Status: Merged
Approved by: Blake Rouse
Approved revision: 6d7d7fe725ac9c371ccf1a655c953c2a35a3735c
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~blake-rouse/maas:fix-snap-version-git
Merge into: maas:master
Diff against target: 154 lines (+56/-42)
5 files modified
.gitignore (+4/-0)
dev/null (+0/-34)
snap/snapcraft.yaml (+2/-7)
src/maasserver/__init__.py (+1/-1)
utilities/calc-snap-version (+49/-0)
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+326181@code.launchpad.net

Commit message

Use new version-script in snapcraft YAML to calculate a nice version string for the MAAS snap.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Self-approving as this just fixes the version string.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.gitignore b/.gitignore
2index f75bc00..789a2e1 100644
3--- a/.gitignore
4+++ b/.gitignore
5@@ -2,6 +2,7 @@
6 *.egg
7 *.egg-info
8 *.log
9+*.snap
10 /.bzr
11 /.coverage
12 /.coverage.*
13@@ -38,8 +39,11 @@
14 /media/demo/*
15 /media/development
16 /parts
17+/prime
18 /services/*/supervise
19+/snap/.snapcraft
20 /src/maasserver/static/js/enums.js
21+/stage
22 /TAGS
23 /tags
24 /xunit.*.xml
25diff --git a/snap/plugins/bzr_version.py b/snap/plugins/bzr_version.py
26deleted file mode 100644
27index 630bc84..0000000
28--- a/snap/plugins/bzr_version.py
29+++ /dev/null
30@@ -1,34 +0,0 @@
31-# Copyright 2017 Canonical Ltd. This software is licensed under the
32-# GNU Affero General Public License version 3 (see the file LICENSE).
33-
34-import os
35-import subprocess
36-
37-import snapcraft
38-
39-
40-class BZRVersionPlugin(snapcraft.BasePlugin):
41- """Plugin that patches snapcraft to append the version of the branch
42- to the version string in the snapcraft.yaml."""
43-
44- def __init__(self, *args, **kwargs):
45- super(BZRVersionPlugin, self).__init__(*args, **kwargs)
46- # Patch snapcraft so the bzr revno is attached to the version
47- # of the snap. This really should be built inside of snapcraft.
48- create_packaging = snapcraft.internal.meta.create_snap_packaging
49-
50- def wrap_create_packaging(config_data, project_options):
51- try:
52- revno = subprocess.check_output([
53- 'bzr', 'revno',
54- os.path.abspath(os.path.dirname(self.project.parts_dir))])
55- except subprocess.CalledProcessError:
56- # This can fail when using 'cleanbuild'. This is because the
57- # bazaar branch can depend on a parent that is not copied into
58- # the container.
59- revno = b'UNKNOWN'
60- config_data['version'] += (
61- '+bzr%s-snap' % revno.decode('utf-8').strip())
62- return create_packaging(config_data, project_options)
63-
64- snapcraft.internal.meta.create_snap_packaging = wrap_create_packaging
65diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
66index d059db1..8e72e25 100644
67--- a/snap/snapcraft.yaml
68+++ b/snap/snapcraft.yaml
69@@ -1,5 +1,6 @@
70 name: maas
71-version: trunk
72+version: git
73+version-script: utilities/calc-snap-version
74 summary: Metal as a Service
75 description: |
76 Total automation of you physical servers for amazing data center
77@@ -132,9 +133,3 @@ parts:
78 - -conf
79 - -plugins
80 - -snapcraft.yaml
81- version:
82- plugin: bzr-version
83- stage:
84- - -*
85- prime:
86- - -*
87diff --git a/src/maasserver/__init__.py b/src/maasserver/__init__.py
88index fb0a0fb..cd6b4d6 100644
89--- a/src/maasserver/__init__.py
90+++ b/src/maasserver/__init__.py
91@@ -14,7 +14,7 @@ import logging
92 from os import environ
93
94
95-__version__ = '2.2.0'
96+__version__ = '2.3.0'
97
98 default_app_config = 'maasserver.apps.MAASConfig'
99
100diff --git a/utilities/calc-snap-version b/utilities/calc-snap-version
101new file mode 100755
102index 0000000..5c73d48
103--- /dev/null
104+++ b/utilities/calc-snap-version
105@@ -0,0 +1,49 @@
106+#!/usr/bin/env python3
107+# Copyright 2017 Canonical Ltd. This software is licensed under the
108+# GNU Affero General Public License version 3 (see the file LICENSE).
109+
110+"""
111+Calculates the version of the snap using git and the version defined in
112+src/maasserver/__init__.py.
113+"""
114+
115+import ast
116+import os
117+import sys
118+import subprocess
119+
120+
121+# Get the path to the src/maasserver/__init__.py.
122+repo_path = os.path.dirname(
123+ os.path.dirname(os.path.abspath(os.path.realpath(__file__))))
124+init_path = os.path.join(repo_path, 'src', 'maasserver', '__init__.py')
125+
126+# Get the syntax tree for the file, since we don't want to actually import
127+# that file into this script.
128+with open(init_path, 'r') as stream:
129+ init_ast = ast.parse(stream.read())
130+
131+# Get the version from the parsed ast tree.
132+version = None
133+for node in ast.walk(init_ast):
134+ if isinstance(node, ast.Assign):
135+ if node.targets:
136+ if node.targets[0].id == '__version__':
137+ version = node.value.s
138+ break
139+
140+# Unable to get the version then just set it to 'master'.
141+if not version:
142+ version = 'master'
143+
144+# Describe number of commits in git.
145+revno = subprocess.check_output(
146+ ['git', '-C', repo_path, 'rev-list', '--count', 'HEAD'],
147+ stderr=subprocess.DEVNULL).decode(sys.getfilesystemencoding()).strip()
148+
149+# Describe commit hash from git.
150+commit = subprocess.check_output(
151+ ['git', '-C', repo_path, 'describe', '--abbrev=7', '--always', '--dirty'],
152+ stderr=subprocess.DEVNULL).decode(sys.getfilesystemencoding()).strip()
153+
154+print('%s-%s-%s-snap' % (version, revno, commit))

Subscribers

People subscribed via source and target branches