Skip to content

Commit 391912e

Browse files
authored
Merge branch 'main' into sample-revamp
2 parents 13724e8 + 4034959 commit 391912e

File tree

8 files changed

+139
-16
lines changed

8 files changed

+139
-16
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
- 'docs'
88

99
env:
10-
BUILDER_VERSION: v0.9.56
10+
BUILDER_VERSION: v0.9.75
1111
BUILDER_SOURCE: releases
1212
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
1313
PACKAGE_NAME: aws-iot-device-sdk-python-v2

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This document provides information about the AWS IoT Device SDK v2 for Python. T
1818
## Installation
1919

2020
### Minimum Requirements
21-
* Python 3.7+
21+
* Python 3.8+
2222

2323
[Step-by-step instructions](./documents/PREREQUISITES.md)
2424

awsiot/mqtt5_client_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ def _get_metrics_str(current_username=""):
220220

221221
if _metrics_str is None:
222222
try:
223-
import pkg_resources
223+
import importlib.metadata
224224
try:
225-
version = pkg_resources.get_distribution("awsiotsdk").version
225+
version = importlib.metadata.version("awsiotsdk")
226226
_metrics_str = "SDK=PythonV2&Version={}".format(version)
227-
except pkg_resources.DistributionNotFound:
227+
except importlib.metadata.PackageNotFoundError:
228228
_metrics_str = "SDK=PythonV2&Version=dev"
229229
except BaseException:
230230
_metrics_str = ""

awsiot/mqtt_connection_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ def _get_metrics_str(current_username=""):
158158

159159
if _metrics_str is None:
160160
try:
161-
import pkg_resources
161+
import importlib.metadata
162162
try:
163-
version = pkg_resources.get_distribution("awsiotsdk").version
163+
version = importlib.metadata.version("awsiotsdk")
164164
_metrics_str = "SDK=PythonV2&Version={}".format(version)
165-
except pkg_resources.DistributionNotFound:
165+
except importlib.metadata.PackageNotFoundError:
166166
_metrics_str = "SDK=PythonV2&Version=dev"
167167
except BaseException:
168168
_metrics_str = ""

builder.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@
4242
["/opt/python/cp38-cp38/bin/python", "-m", "pip", "install", "--upgrade", "pip", "setuptools"],
4343
["/opt/python/cp38-cp38/bin/python", "-m", "pip", "install", ".", "--verbose"],
4444
["/opt/python/cp38-cp38/bin/python", "-m", "pip", "install", "boto3", "autopep8"],
45-
["/opt/python/cp38-cp38/bin/python", "-m", "unittest", "discover", "--verbose"],
46-
["echo", "------ Python 3.7 ------"],
47-
["/opt/python/cp37-cp37m/bin/python", "-m", "pip", "install", "--upgrade", "pip", "setuptools"],
48-
["/opt/python/cp37-cp37m/bin/python", "-m", "pip", "install", ".", "--verbose"],
49-
["/opt/python/cp37-cp37m/bin/python", "-m", "pip", "install", "boto3", "autopep8"],
50-
["/opt/python/cp37-cp37m/bin/python", "-m", "unittest", "discover", "--verbose"]
45+
["/opt/python/cp38-cp38/bin/python", "-m", "unittest", "discover", "--verbose"]
5146
],
5247
"run_tests": false,
5348
"_comment": "manylinux has all its own build steps, turn off 'tests' which is where normal build steps are declared. using data to program sucks"

documents/PREREQUISITES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PREREQUISITES
22

3-
## Python 3.7 or higher
3+
## Python 3.8 or higher
44

55
How you install Python varies from platform to platform. Below are the instructions for Windows, MacOS, and Linux:
66

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ def _load_version():
4242
install_requires=[
4343
'awscrt==0.27.4',
4444
],
45-
python_requires='>=3.7',
45+
python_requires='>=3.8',
4646
)

test/test_get_metrics.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
import unittest
5+
from unittest.mock import patch
6+
7+
8+
class TestImportlibMetadata(unittest.TestCase):
9+
"""Test that importlib.metadata is used instead of pkg_resources"""
10+
11+
def setUp(self):
12+
"""Reset the metrics string cache before each test"""
13+
# Reset the cached metrics string in both modules
14+
import awsiot.mqtt5_client_builder
15+
import awsiot.mqtt_connection_builder
16+
17+
# Reset the global _metrics_str variable
18+
awsiot.mqtt_connection_builder._metrics_str = None
19+
awsiot.mqtt5_client_builder._metrics_str = None
20+
21+
def test_metrics_string_generation_mqtt_connection_builder(self):
22+
"""Test that mqtt_connection_builder uses importlib.metadata for version detection"""
23+
from awsiot import mqtt_connection_builder
24+
25+
# Mock importlib.metadata.version to return a known version
26+
with patch("importlib.metadata.version") as mock_version:
27+
mock_version.return_value = "1.2.3"
28+
29+
# Call the function that uses version detection
30+
# We need to access the private function for testing
31+
result = mqtt_connection_builder._get_metrics_str("test_username")
32+
33+
# Verify that importlib.metadata.version was called
34+
mock_version.assert_called_once_with("awsiotsdk")
35+
36+
# Verify the result contains the expected format
37+
self.assertIn("SDK=PythonV2&Version=1.2.3", result)
38+
39+
def test_metrics_string_generation_mqtt5_client_builder(self):
40+
"""Test that mqtt5_client_builder uses importlib.metadata for version detection"""
41+
from awsiot import mqtt5_client_builder
42+
43+
# Mock importlib.metadata.version to return a known version
44+
with patch("importlib.metadata.version") as mock_version:
45+
mock_version.return_value = "1.2.3"
46+
47+
# Call the function that uses version detection
48+
# We need to access the private function for testing
49+
result = mqtt5_client_builder._get_metrics_str("test_username")
50+
51+
# Verify that importlib.metadata.version was called
52+
mock_version.assert_called_once_with("awsiotsdk")
53+
54+
# Verify the result contains the expected format
55+
self.assertIn("SDK=PythonV2&Version=1.2.3", result)
56+
57+
def test_package_not_found_handling_mqtt_connection_builder(self):
58+
"""Test that PackageNotFoundError is handled correctly in mqtt_connection_builder"""
59+
import importlib.metadata
60+
61+
from awsiot import mqtt_connection_builder
62+
63+
# Mock importlib.metadata.version to raise PackageNotFoundError
64+
with patch("importlib.metadata.version") as mock_version:
65+
mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found")
66+
67+
# Call the function that uses version detection
68+
result = mqtt_connection_builder._get_metrics_str("test_username")
69+
70+
# Verify that the fallback version is used
71+
self.assertIn("SDK=PythonV2&Version=dev", result)
72+
73+
def test_package_not_found_handling_mqtt5_client_builder(self):
74+
"""Test that PackageNotFoundError is handled correctly in mqtt5_client_builder"""
75+
import importlib.metadata
76+
77+
from awsiot import mqtt5_client_builder
78+
79+
# Mock importlib.metadata.version to raise PackageNotFoundError
80+
with patch("importlib.metadata.version") as mock_version:
81+
mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found")
82+
83+
# Call the function that uses version detection
84+
result = mqtt5_client_builder._get_metrics_str("test_username")
85+
86+
# Verify that the fallback version is used
87+
self.assertIn("SDK=PythonV2&Version=dev", result)
88+
89+
def test_general_exception_handling_mqtt_connection_builder(self):
90+
"""Test that general exceptions are handled correctly in mqtt_connection_builder"""
91+
from awsiot import mqtt_connection_builder
92+
93+
# Mock importlib.metadata.version to raise a general exception
94+
with patch("importlib.metadata.version") as mock_version:
95+
mock_version.side_effect = Exception("Some other error")
96+
97+
# Call the function that uses version detection
98+
result = mqtt_connection_builder._get_metrics_str("test_username")
99+
100+
# Verify that empty string is returned on general exception
101+
self.assertEqual(result, "")
102+
103+
def test_general_exception_handling_mqtt5_client_builder(self):
104+
"""Test that general exceptions are handled correctly in mqtt5_client_builder"""
105+
from awsiot import mqtt5_client_builder
106+
107+
# Mock importlib.metadata.version to raise a general exception
108+
with patch("importlib.metadata.version") as mock_version:
109+
mock_version.side_effect = Exception("Some other error")
110+
111+
# Call the function that uses version detection
112+
result = mqtt5_client_builder._get_metrics_str("test_username")
113+
114+
# Verify that empty string is returned on general exception
115+
self.assertEqual(result, "")
116+
117+
def test_no_pkg_resources_import(self):
118+
"""Test that pkg_resources is not imported in the modified files"""
119+
import awsiot.mqtt5_client_builder
120+
import awsiot.mqtt_connection_builder
121+
122+
# Check that pkg_resources is not in the module's globals
123+
self.assertNotIn("pkg_resources", awsiot.mqtt_connection_builder.__dict__)
124+
self.assertNotIn("pkg_resources", awsiot.mqtt5_client_builder.__dict__)
125+
126+
127+
if __name__ == "__main__":
128+
unittest.main()

0 commit comments

Comments
 (0)