Skip to content

Commit c743503

Browse files
committed
feat: Environment variables host and port of the hdc server can be set
1 parent e0bab00 commit c743503

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,18 @@ toast = d.toast_watcher.get_toast()
738738
# output: 'testMessage'
739739
```
740740

741+
# Environment
742+
如何需要连接远端的HDC Server来实现远程设备调试,运行脚本前需要设置环境变量
743+
```bash
744+
export HDC_SERVER_HOST=127.0.0.1 # hdc server host
745+
export HDC_SERVER_PORT=8710
746+
```
747+
748+
PS 移除环境变量
749+
```
750+
unset HDC_SERVER_HOST
751+
unset HDC_SERVER_PORT
752+
```
741753

742754
# 鸿蒙Uitest协议
743755

hmdriver2/hdc.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import uuid
66
import shlex
77
import re
8+
import os
89
import subprocess
910
from typing import Union, List, Dict, Tuple
1011

@@ -37,9 +38,22 @@ def _execute_command(cmdargs: Union[str, List[str]]) -> CommandResult:
3738
return CommandResult("", str(e), -1)
3839

3940

41+
def _build_hdc_prefix() -> str:
42+
"""
43+
Construct the hdc command prefix based on environment variables.
44+
"""
45+
host = os.getenv("HDC_SERVER_HOST")
46+
port = os.getenv("HDC_SERVER_PORT")
47+
if host and port:
48+
logger.debug(f"HDC_SERVER_HOST: {host}, HDC_SERVER_PORT: {port}")
49+
return f"hdc -s {host}:{port}"
50+
return "hdc"
51+
52+
4053
def list_devices() -> List[str]:
4154
devices = []
42-
result = _execute_command('hdc list targets')
55+
hdc_prefix = _build_hdc_prefix()
56+
result = _execute_command(f"{hdc_prefix} list targets")
4357
if result.exit_code == 0 and result.output:
4458
lines = result.output.strip().split('\n')
4559
for line in lines:
@@ -56,6 +70,8 @@ def list_devices() -> List[str]:
5670
class HdcWrapper:
5771
def __init__(self, serial: str) -> None:
5872
self.serial = serial
73+
self.hdc_prefix = _build_hdc_prefix()
74+
5975
if not self.is_online():
6076
raise DeviceNotFoundError(f"Device [{self.serial}] not found")
6177

@@ -65,13 +81,13 @@ def is_online(self):
6581

6682
def forward_port(self, rport: int) -> int:
6783
lport: int = FreePort().get()
68-
result = _execute_command(f"hdc -t {self.serial} fport tcp:{lport} tcp:{rport}")
84+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} fport tcp:{lport} tcp:{rport}")
6985
if result.exit_code != 0:
7086
raise HdcError("HDC forward port error", result.error)
7187
return lport
7288

7389
def rm_forward(self, lport: int, rport: int) -> int:
74-
result = _execute_command(f"hdc -t {self.serial} fport rm tcp:{lport} tcp:{rport}")
90+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} fport rm tcp:{lport} tcp:{rport}")
7591
if result.exit_code != 0:
7692
raise HdcError("HDC rm forward error", result.error)
7793
return lport
@@ -80,32 +96,32 @@ def list_fport(self) -> List:
8096
"""
8197
eg.['tcp:10001 tcp:8012', 'tcp:10255 tcp:8012']
8298
"""
83-
result = _execute_command(f"hdc -t {self.serial} fport ls")
99+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} fport ls")
84100
if result.exit_code != 0:
85101
raise HdcError("HDC forward list error", result.error)
86102
pattern = re.compile(r"tcp:\d+ tcp:\d+")
87103
return pattern.findall(result.output)
88104

89105
def send_file(self, lpath: str, rpath: str):
90-
result = _execute_command(f"hdc -t {self.serial} file send {lpath} {rpath}")
106+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} file send {lpath} {rpath}")
91107
if result.exit_code != 0:
92108
raise HdcError("HDC send file error", result.error)
93109
return result
94110

95111
def recv_file(self, rpath: str, lpath: str):
96-
result = _execute_command(f"hdc -t {self.serial} file recv {rpath} {lpath}")
112+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} file recv {rpath} {lpath}")
97113
if result.exit_code != 0:
98114
raise HdcError("HDC receive file error", result.error)
99115
return result
100116

101117
def shell(self, cmd: str, error_raise=True) -> CommandResult:
102-
result = _execute_command(f"hdc -t {self.serial} shell {cmd}")
118+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} shell {cmd}")
103119
if result.exit_code != 0 and error_raise:
104120
raise HdcError("HDC shell error", f"{cmd}\n{result.output}\n{result.error}")
105121
return result
106122

107123
def uninstall(self, bundlename: str):
108-
result = _execute_command(f"hdc -t {self.serial} uninstall {bundlename}")
124+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} uninstall {bundlename}")
109125
if result.exit_code != 0:
110126
raise HdcError("HDC uninstall error", result.output)
111127
return result
@@ -114,7 +130,7 @@ def install(self, apkpath: str):
114130
# Ensure the path is properly quoted for Windows
115131
quoted_path = f'"{apkpath}"'
116132

117-
result = _execute_command(f"hdc -t {self.serial} install {quoted_path}")
133+
result = _execute_command(f"{self.hdc_prefix} -t {self.serial} install {quoted_path}")
118134
if result.exit_code != 0:
119135
raise HdcError("HDC install error", result.error)
120136
return result

0 commit comments

Comments
 (0)