Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit e0e8bd8

Browse files
committed
Fixes gRPC device registration in Python 2.7
The built-in path mkdir function recently added a parameter that we are using. This change adds a backport of this feature where applicable. Another test was added to verify this change works. Bug: 72382392 Change-Id: I2d3016fc018e6ebd423ee4bdbe099e2135e0f698
1 parent edf5598 commit e0e8bd8

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

google-assistant-sdk/googlesamples/assistant/grpc/devicetool.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525

2626
ASSISTANT_API_VERSION = 'v1alpha2'
27+
logging.basicConfig(format='', level=logging.INFO)
2728

2829

2930
def failed_request_exception(message, r):
@@ -80,8 +81,8 @@ def build_client_from_context(ctx):
8081

8182
def pretty_print_model(devicemodel):
8283
"""Prints out a device model in the terminal by parsing dict."""
83-
PRETTY_PRINT_MODEL = """Device Model Id: %(deviceModelId)s
84-
Project Id: %(projectId)s
84+
PRETTY_PRINT_MODEL = """Device Model ID: %(deviceModelId)s
85+
Project ID: %(projectId)s
8586
Device Type: %(deviceType)s"""
8687
logging.info(PRETTY_PRINT_MODEL % devicemodel)
8788
if 'traits' in devicemodel:
@@ -94,7 +95,7 @@ def pretty_print_model(devicemodel):
9495

9596
def pretty_print_device(device):
9697
"""Prints out a device instance in the terminal by parsing dict."""
97-
logging.info('Device Instance Id: %s' % device['id'])
98+
logging.info('Device Instance ID: %s' % device['id'])
9899
if 'nickname' in device:
99100
logging.info(' Nickname: %s' % device['nickname'])
100101
if 'modelId' in device:
@@ -152,8 +153,8 @@ def cli(ctx, project_id, client_secrets, verbose, api_endpoint, credentials):
152153
ctx.obj['PROJECT_ID'] = project_id
153154
ctx.obj['CREDENTIALS'] = c
154155
ctx.obj['CLIENT_SECRETS'] = client_secrets
155-
logging.basicConfig(format='',
156-
level=logging.DEBUG if verbose else logging.INFO)
156+
if verbose:
157+
logging.getLogger().setLevel(logging.DEBUG)
157158

158159

159160
@cli.command()

google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020
import os
2121
import os.path
22+
import pathlib2 as pathlib
2223
import sys
2324
import uuid
2425

@@ -399,7 +400,7 @@ def onoff(on):
399400
logging.error('Failed to register device: %s', r.text)
400401
sys.exit(-1)
401402
logging.info('Device registered: %s', device_id)
402-
os.makedirs(os.path.dirname(device_config), exist_ok=True)
403+
pathlib.Path(os.path.dirname(device_config)).mkdir(exist_ok=True)
403404
with open(device_config, 'w') as f:
404405
json.dump(payload, f)
405406

google-assistant-sdk/googlesamples/assistant/grpc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ sounddevice>=0.3.7,<0.4
55
click>=6.7,<7
66
tenacity>=4.1.0,<5
77
futures>=3.1.1,<4
8-
8+
pathlib2==2.3.0

google-assistant-sdk/tests/test_endtoend.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,50 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import json
1617
import tempfile
18+
import os
1719
import os.path
20+
import pytest
1821
import subprocess
22+
import uuid
1923

2024

2125
import builtins
26+
PROJECT_ID = os.environ.get('PROJECT_ID', 'dummy-project-id')
27+
28+
29+
@pytest.fixture(scope='session')
30+
def device_model():
31+
device_model_id = 'assistant-sdk-test-model-%s' % str(uuid.uuid1())
32+
subprocess.check_call(['python', '-m',
33+
'googlesamples.assistant.grpc.devicetool',
34+
'--project-id', PROJECT_ID,
35+
'register-model', '--model', device_model_id,
36+
'--type', 'LIGHT',
37+
'--manufacturer', 'assistant-sdk-test',
38+
'--product-name', 'assistant-sdk-test'])
39+
yield device_model_id
40+
subprocess.check_call(['python', '-m',
41+
'googlesamples.assistant.grpc.devicetool',
42+
'--project-id', PROJECT_ID,
43+
'delete', '--model', device_model_id])
44+
45+
46+
@pytest.fixture(scope='session')
47+
def device_instance(device_model):
48+
device_instance_id = 'assistant-sdk-test-device-%s' % str(uuid.uuid1())
49+
subprocess.check_call(['python', '-m',
50+
'googlesamples.assistant.grpc.devicetool',
51+
'--project-id', PROJECT_ID,
52+
'register-device', '--model', device_model,
53+
'--client-type', 'SERVICE',
54+
'--device', device_instance_id])
55+
yield device_instance_id
56+
subprocess.check_call(['python', '-m',
57+
'googlesamples.assistant.grpc.devicetool',
58+
'--project-id', PROJECT_ID,
59+
'delete', '--device', device_instance_id])
2260

2361

2462
def test_endtoend_pushtotalk():
@@ -36,12 +74,48 @@ def test_endtoend_pushtotalk():
3674
assert os.path.getsize(audio_out_file) > 0
3775

3876

39-
def test_endtoend_textinput():
77+
def test_registration_pushtotalk(device_model):
78+
temp_dir = tempfile.mkdtemp()
79+
audio_out_file = os.path.join(temp_dir, 'out.raw')
80+
# Use an non-existing device config file intentionally
81+
# to force device registration.
82+
device_config = os.path.join(temp_dir, 'device_config.json')
83+
out = subprocess.check_output(['python', '-m',
84+
'googlesamples.assistant.grpc.pushtotalk',
85+
'--verbose',
86+
'--project-id', PROJECT_ID,
87+
'--device-model-id', device_model,
88+
'--device-config', device_config,
89+
'-i', 'tests/data/whattimeisit.riff',
90+
'-o', audio_out_file],
91+
stderr=subprocess.STDOUT)
92+
assert 'what time is it' in builtins.str(out).lower()
93+
assert os.path.getsize(audio_out_file) > 0
94+
with open(device_config) as f:
95+
config = json.load(f)
96+
assert ('device registered: %s' % config['id']
97+
in builtins.str(out).lower())
98+
out = subprocess.check_output(
99+
['python', '-m',
100+
'googlesamples.assistant.grpc.devicetool',
101+
'--project-id', PROJECT_ID,
102+
'get', '--device', config['id']],
103+
stderr=subprocess.STDOUT
104+
)
105+
assert ('device instance id: %s' % config['id']
106+
in builtins.str(out).lower())
107+
subprocess.check_call(['python', '-m',
108+
'googlesamples.assistant.grpc.devicetool',
109+
'--project-id', PROJECT_ID,
110+
'delete', '--device', config['id']])
111+
112+
113+
def test_endtoend_textinput(device_model, device_instance):
40114
p = subprocess.Popen(['python', '-m',
41115
'googlesamples.assistant.grpc.textinput',
42116
'--verbose',
43-
'--device-model-id', 'test-device-model',
44-
'--device-id', 'test-device'],
117+
'--device-model-id', device_model,
118+
'--device-id', device_instance],
45119
stdin=subprocess.PIPE,
46120
stdout=subprocess.PIPE)
47121
out, err = p.communicate(b'how do you say grapefruit in French?')

0 commit comments

Comments
 (0)