From 8cf2e94f610cfe9792b3d99e2b473283267f4b63 Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Fri, 10 Apr 2020 09:12:19 -0300 Subject: [PATCH 1/4] static: mypy requires __init__.py Add missing __init__.py to workaround python/mypy#1645 Fix mypy type hint issues in the code found from this. Signed-off-by: Sergio Schvezov --- snapcraft/cli/_channel_map.py | 9 +++++++-- snapcraft/internal/lxd/__init__.py | 0 snapcraft/plugins/_python/__init__.py | 0 snapcraft/plugins/v2/__init__.py | 0 snapcraft/scripts/__init__.py | 0 snapcraft/storeapi/v2/__init__.py | 0 snapcraft/storeapi/v2/_api_schema.py | 5 ++++- snapcraft/storeapi/v2/channel_map.py | 14 +++++++------- tests/unit/build_providers/lxd/__init__.py | 0 tests/unit/build_providers/lxd/test_lxd.py | 3 +++ tests/unit/review_tools/__init__.py | 0 11 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 snapcraft/internal/lxd/__init__.py create mode 100644 snapcraft/plugins/_python/__init__.py create mode 100644 snapcraft/plugins/v2/__init__.py create mode 100644 snapcraft/scripts/__init__.py create mode 100644 snapcraft/storeapi/v2/__init__.py create mode 100644 tests/unit/build_providers/lxd/__init__.py create mode 100644 tests/unit/review_tools/__init__.py diff --git a/snapcraft/cli/_channel_map.py b/snapcraft/cli/_channel_map.py index cdf9c169ee..35c5d38f9b 100644 --- a/snapcraft/cli/_channel_map.py +++ b/snapcraft/cli/_channel_map.py @@ -37,7 +37,7 @@ class _HINTS: PROGRESSING_TO: Final[str] = "→" -def _get_channel_hint(*, channel_map, fallback: str, architecture: str) -> str: +def _get_channel_hint(*, channel_map, fallback: Optional[str], architecture: str) -> str: tick = _HINTS.CLOSED for c in channel_map: if c.channel == fallback and c.architecture == architecture: @@ -137,6 +137,7 @@ def _get_channel_lines_for_channel( progressive_revision = snap_channel_map.get_revision( progressive_mapped_channel.revision ) + progressive_mapped_channel_line = _get_channel_line( mapped_channel=progressive_mapped_channel, revision=progressive_revision, @@ -144,11 +145,15 @@ def _get_channel_lines_for_channel( hint=hint, progress_string=f"{_HINTS.PROGRESSING_TO} {progressive_mapped_channel.progressive.percentage:.0f}%", ) + if progressive_mapped_channel.progressive.percentage is None: + percentage = 0.0 + else: + percentage = progressive_mapped_channel.progressive.percentage # Setup progress for the actually released revision, this needs to be # calculated. But only show it if the channel is open. progress_string = "{} {:.0f}%".format( _HINTS.PROGRESSING_TO, - 100 - progressive_mapped_channel.progressive.percentage, + 100 - percentage, ) else: progress_string = _HINTS.NO_PROGRESS diff --git a/snapcraft/internal/lxd/__init__.py b/snapcraft/internal/lxd/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/snapcraft/plugins/_python/__init__.py b/snapcraft/plugins/_python/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/snapcraft/plugins/v2/__init__.py b/snapcraft/plugins/v2/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/snapcraft/scripts/__init__.py b/snapcraft/scripts/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/snapcraft/storeapi/v2/__init__.py b/snapcraft/storeapi/v2/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/snapcraft/storeapi/v2/_api_schema.py b/snapcraft/storeapi/v2/_api_schema.py index 8e77bdf4da..d1423ae3b5 100644 --- a/snapcraft/storeapi/v2/_api_schema.py +++ b/snapcraft/storeapi/v2/_api_schema.py @@ -18,7 +18,10 @@ # Snapcraft does not have been commented out from this schema originally # imported from # https://dashboard.snapcraft.io/docs/v2/en/snaps.html#snap-channel-map -CHANNEL_MAP_JSONSCHEMA = { + +from typing import Any, Dict + +CHANNEL_MAP_JSONSCHEMA: Dict[str, Any] = { "additionalProperties": False, "properties": { "channel-map": { diff --git a/snapcraft/storeapi/v2/channel_map.py b/snapcraft/storeapi/v2/channel_map.py index 0b0d813edf..78ec5ccb2c 100644 --- a/snapcraft/storeapi/v2/channel_map.py +++ b/snapcraft/storeapi/v2/channel_map.py @@ -35,7 +35,7 @@ class Progressive: """ @classmethod - def unmarshal(cls, payload: Dict[str, Union[str, Optional[bool]]]) -> "Progressive": + def unmarshal(cls, payload: Dict[str, Any]) -> "Progressive": jsonschema.validate( payload, CHANNEL_MAP_JSONSCHEMA["properties"]["channel-map"]["items"]["properties"][ @@ -48,7 +48,7 @@ def unmarshal(cls, payload: Dict[str, Union[str, Optional[bool]]]) -> "Progressi percentage=payload["percentage"], ) - def marshal(self) -> Dict[str, Union[str, Optional[bool]]]: + def marshal(self) -> Dict[str, Any]: return {"key": self.key, "paused": self.paused, "percentage": self.percentage} def __repr__(self) -> str: @@ -114,7 +114,7 @@ class Revision: """ @classmethod - def unmarshal(cls, payload: Dict[str, Union[int, str, List[str]]]) -> "Revision": + def unmarshal(cls, payload: Dict[str, Any]) -> "Revision": jsonschema.validate( payload, CHANNEL_MAP_JSONSCHEMA["properties"]["revisions"]["items"] ) @@ -124,7 +124,7 @@ def unmarshal(cls, payload: Dict[str, Union[int, str, List[str]]]) -> "Revision" architectures=payload["architectures"], ) - def marshal(self) -> Dict[str, Union[int, str, List[str]]]: + def marshal(self) -> Dict[str, Any]: return { "revision": self.revision, "version": self.version, @@ -148,7 +148,7 @@ class SnapChannel: """ @classmethod - def unmarshal(cls, payload: Dict[str, Optional[str]]) -> "SnapChannel": + def unmarshal(cls, payload: Dict[str, Any]) -> "SnapChannel": jsonschema.validate( payload, CHANNEL_MAP_JSONSCHEMA["properties"]["snap"]["properties"]["channels"][ @@ -163,7 +163,7 @@ def unmarshal(cls, payload: Dict[str, Optional[str]]) -> "SnapChannel": fallback=payload["fallback"], ) - def marshal(self) -> Dict[str, Optional[str]]: + def marshal(self) -> Dict[str, Any]: return { "name": self.name, "track": self.track, @@ -235,7 +235,7 @@ def marshal(self) -> Dict[str, Any]: } def __repr__(self) -> str: - return "<{self.__class__.__name__}: {!r}>".format(self.snap.name) + return f"<{self.__class__.__name__}: {self.snap.name!r}>" def __init__( self, *, channel_map: List[MappedChannel], revisions: List[Revision], snap: Snap diff --git a/tests/unit/build_providers/lxd/__init__.py b/tests/unit/build_providers/lxd/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/build_providers/lxd/test_lxd.py b/tests/unit/build_providers/lxd/test_lxd.py index 1e08e1ea0f..df662b8c32 100644 --- a/tests/unit/build_providers/lxd/test_lxd.py +++ b/tests/unit/build_providers/lxd/test_lxd.py @@ -59,13 +59,16 @@ def __init__(self, config: Dict[str, Any], wait: bool) -> None: class FakeContainerFiles: delete_available = True + @staticmethod def get(file_name) -> bytes: self.files_get_mock(file_name=file_name) return b"fake-pull" + @staticmethod def put(destination: str, contents: bytes) -> None: self.files_put_mock(destination=destination, contents=contents) + @staticmethod def delete(file_name) -> None: self.files_delete_mock(file_name=file_name) diff --git a/tests/unit/review_tools/__init__.py b/tests/unit/review_tools/__init__.py new file mode 100644 index 0000000000..e69de29bb2 From 8fa938abf6b26823743c105e2f72ab32f595def2 Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Fri, 10 Apr 2020 09:32:01 -0300 Subject: [PATCH 2/4] reformat --- snapcraft/cli/_channel_map.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/snapcraft/cli/_channel_map.py b/snapcraft/cli/_channel_map.py index 35c5d38f9b..7b1cf6f114 100644 --- a/snapcraft/cli/_channel_map.py +++ b/snapcraft/cli/_channel_map.py @@ -37,7 +37,9 @@ class _HINTS: PROGRESSING_TO: Final[str] = "→" -def _get_channel_hint(*, channel_map, fallback: Optional[str], architecture: str) -> str: +def _get_channel_hint( + *, channel_map, fallback: Optional[str], architecture: str +) -> str: tick = _HINTS.CLOSED for c in channel_map: if c.channel == fallback and c.architecture == architecture: @@ -151,10 +153,7 @@ def _get_channel_lines_for_channel( percentage = progressive_mapped_channel.progressive.percentage # Setup progress for the actually released revision, this needs to be # calculated. But only show it if the channel is open. - progress_string = "{} {:.0f}%".format( - _HINTS.PROGRESSING_TO, - 100 - percentage, - ) + progress_string = "{} {:.0f}%".format(_HINTS.PROGRESSING_TO, 100 - percentage) else: progress_string = _HINTS.NO_PROGRESS From b1f0df3c9fc0367ea68f53f272fdae993206cdf0 Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Fri, 10 Apr 2020 09:47:26 -0300 Subject: [PATCH 3/4] flake8!!!!! --- snapcraft/storeapi/v2/channel_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snapcraft/storeapi/v2/channel_map.py b/snapcraft/storeapi/v2/channel_map.py index 78ec5ccb2c..2065b6fdd8 100644 --- a/snapcraft/storeapi/v2/channel_map.py +++ b/snapcraft/storeapi/v2/channel_map.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from typing import Any, Dict, List, Optional, Set, Union +from typing import Any, Dict, List, Optional, Set import jsonschema From 10c8405d47c5f7669c616525ff4a337fe84ca286 Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Fri, 10 Apr 2020 10:00:08 -0300 Subject: [PATCH 4/4] getuid --- tests/unit/build_providers/lxd/test_lxd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/build_providers/lxd/test_lxd.py b/tests/unit/build_providers/lxd/test_lxd.py index df662b8c32..0daeb69dac 100644 --- a/tests/unit/build_providers/lxd/test_lxd.py +++ b/tests/unit/build_providers/lxd/test_lxd.py @@ -159,7 +159,7 @@ def test_create(self): self.fake_pylxd_client.containers.create_mock.assert_called_once_with( config={ "name": "snapcraft-project-name", - "raw.idmap": "both 1000 0", + "raw.idmap": f"both {os.getuid()} 0", "source": { "mode": "pull", "type": "image", @@ -327,7 +327,7 @@ def test_create_for_type_base(self): self.fake_pylxd_client.containers.create_mock.assert_called_once_with( config={ "name": "snapcraft-core18", - "raw.idmap": "both 1000 0", + "raw.idmap": f"both {os.getuid()} 0", "source": { "mode": "pull", "type": "image",