Skip to content

Commit

Permalink
store: send snapcraft version in a header (#894)
Browse files Browse the repository at this point in the history
LP: #1640501

Signed-off-by: Sergio Schvezov <[email protected]>
  • Loading branch information
sergiusens authored Nov 9, 2016
1 parent 9d6f953 commit 26d0752
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
11 changes: 11 additions & 0 deletions snapcraft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
"""

from collections import OrderedDict # noqa
import pkg_resources # noqa
import yaml # noqa

from snapcraft._baseplugin import BasePlugin # noqa
Expand Down Expand Up @@ -253,6 +254,16 @@
from snapcraft.internal import repo # noqa


def _get_version():
try:
return pkg_resources.require('snapcraft')[0].version
except pkg_resources.DistributionNotFound:
return 'devel'


__version__ = _get_version()


# Setup yaml module globally
# yaml OrderedDict loading and dumping
# from http://stackoverflow.com/a/21048064 Wed Jun 22 16:05:34 UTC 2016
Expand Down
10 changes: 1 addition & 9 deletions snapcraft/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@

import logging
import os
import pkg_resources
import pkgutil
import shutil
import sys
Expand All @@ -159,13 +158,6 @@
_SNAPCRAFT_TOUR_DIR = "./snapcraft-tour/"


def _get_version():
try:
return pkg_resources.require('snapcraft')[0].version
except pkg_resources.DistributionNotFound:
return 'devel'


def _scaffold_examples(directory):
logger.debug("Copying examples tour to {}".format(directory))
dest_dir = os.path.abspath(directory)
Expand Down Expand Up @@ -217,7 +209,7 @@ def _get_project_options(args):

def main(argv=None):
doc = __doc__.format(DEFAULT_SERIES=DEFAULT_SERIES)
args = docopt(doc, version=_get_version(), argv=argv)
args = docopt(doc, version=snapcraft.__version__, argv=argv)

# Default log level is INFO unless --debug is specified
log_level = logging.INFO
Expand Down
9 changes: 9 additions & 0 deletions snapcraft/storeapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,20 @@ def __init__(self, conf, root_url):
self.conf = conf
self.root_url = root_url
self.session = requests.Session()
self._snapcraft_headers = {
'X-SNAPCRAFT-VERSION': snapcraft.__version__
}

def request(self, method, url, params=None, headers=None, **kwargs):
"""Overriding base class to handle the root url."""
# Note that url may be absolute in which case 'root_url' is ignored by
# urljoin.

if headers:
headers.update(self._snapcraft_headers)
else:
headers = self._snapcraft_headers

final_url = urllib.parse.urljoin(self.root_url, url)
response = self.session.request(
method, final_url, headers=headers,
Expand Down
14 changes: 0 additions & 14 deletions snapcraft/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import io
import logging
import pkg_resources
import sys
from unittest import mock

import fixtures
Expand Down Expand Up @@ -105,14 +102,3 @@ def test_command_with_target_deb_arch(self, mock_cmd):
mock_project_options.assert_called_once_with(
debug=False, parallel_builds=True, target_deb_arch='arm64',
use_geoip=False)

@mock.patch('pkg_resources.require')
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_devel_version(self, mock_stdout, mock_resources):
mock_resources.side_effect = pkg_resources.DistributionNotFound()
sys.argv = ['/usr/bin/snapcraft', '--version']

with self.assertRaises(SystemExit):
snapcraft.main.main()

self.assertEqual(mock_stdout.getvalue(), 'devel\n')

0 comments on commit 26d0752

Please sign in to comment.