From 26d0752fc4baeb134e23c6d9fad3adee0f24768f Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Wed, 9 Nov 2016 16:45:15 -0300 Subject: [PATCH] store: send snapcraft version in a header (#894) LP: #1640501 Signed-off-by: Sergio Schvezov --- snapcraft/__init__.py | 11 +++++++++++ snapcraft/main.py | 10 +--------- snapcraft/storeapi/__init__.py | 9 +++++++++ snapcraft/tests/test_main.py | 14 -------------- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/snapcraft/__init__.py b/snapcraft/__init__.py index f5f3ece32d..eeb651b8cc 100644 --- a/snapcraft/__init__.py +++ b/snapcraft/__init__.py @@ -224,6 +224,7 @@ """ from collections import OrderedDict # noqa +import pkg_resources # noqa import yaml # noqa from snapcraft._baseplugin import BasePlugin # noqa @@ -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 diff --git a/snapcraft/main.py b/snapcraft/main.py index cba77301cb..f1942178ea 100755 --- a/snapcraft/main.py +++ b/snapcraft/main.py @@ -139,7 +139,6 @@ import logging import os -import pkg_resources import pkgutil import shutil import sys @@ -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) @@ -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 diff --git a/snapcraft/storeapi/__init__.py b/snapcraft/storeapi/__init__.py index 96a361ced5..f8833ef148 100644 --- a/snapcraft/storeapi/__init__.py +++ b/snapcraft/storeapi/__init__.py @@ -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, diff --git a/snapcraft/tests/test_main.py b/snapcraft/tests/test_main.py index d68d908102..1c5c6b7135 100644 --- a/snapcraft/tests/test_main.py +++ b/snapcraft/tests/test_main.py @@ -14,10 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import io import logging -import pkg_resources -import sys from unittest import mock import fixtures @@ -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')