Skip to content

Commit

Permalink
✨ Auto-detect ios platform and device name from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
astariul committed Apr 5, 2024
1 parent 66bda6c commit 4cb5dde
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
35 changes: 8 additions & 27 deletions kebbie/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@

from kebbie import evaluate
from kebbie.correctors import EmulatorCorrector
from kebbie.emulator import DEFAULT_IOS_NAME, DEFAULT_IOS_PLATFORM, Emulator
from kebbie.emulator import Emulator
from kebbie.utils import get_soda_dataset


def instantiate_correctors(
keyboard: str, ios_name: str, ios_platform: str, fast_mode: bool = True, instantiate_emulator: bool = True
keyboard: str, fast_mode: bool = True, instantiate_emulator: bool = True
) -> List[EmulatorCorrector]:
"""Create the right correctors (with the right platform, etc...) given the
arguments from the command line.
Args:
keyboard (str): Name fo the keyboard to load.
ios_name (str): (For iOS emulator only) Name of the device.
ios_platform (str): (For iOS emulator only) Name of the iOS version.
fast_mode (bool, optional): If `True`, the corrector will be
instantiated in fast mode (only AC).
instantiate_emulator (bool, optional): If `True`, the emulators are
Expand All @@ -31,21 +29,19 @@ def instantiate_correctors(
The list of created Correctors.
"""
if keyboard in ["gboard", "tappa"]:
# Android keyboards can be parallel & are detected automatically
# Android keyboards
return [
EmulatorCorrector(
device=d,
platform="android",
keyboard=keyboard,
fast_mode=fast_mode,
instantiate_emulator=instantiate_emulator,
ios_name=ios_name,
ios_platform=ios_platform,
)
for d in Emulator.get_devices()
for d in Emulator.get_android_devices()
]
else:
# iOS keyboards need to specify the exact name & platform...
# iOS keyboards
return [
EmulatorCorrector(
platform="ios",
Expand All @@ -55,6 +51,7 @@ def instantiate_correctors(
ios_name=ios_name,
ios_platform=ios_platform,
)
for ios_platform, ios_name in Emulator.get_ios_devices()
]


Expand All @@ -73,20 +70,6 @@ def common_args(parser: argparse.ArgumentParser):
choices=["gboard", "ios", "tappa"],
help="Which keyboard, to be tested, is currently installed on the emulator.",
)
parser.add_argument(
"--ios_name",
dest="ios_name",
type=str,
default=DEFAULT_IOS_NAME,
help="Name of the emulated device (only for iOS).",
)
parser.add_argument(
"--ios_platform",
dest="ios_platform",
type=str,
default=DEFAULT_IOS_PLATFORM,
help="Name of the emulated platform (only for iOS).",
)


def cli():
Expand Down Expand Up @@ -145,9 +128,7 @@ def cli():
parser.print_help(sys.stderr)
sys.exit(1)
elif args.cmd == "evaluate":
correctors = instantiate_correctors(
args.keyboard, args.ios_name, args.ios_platform, fast_mode=not args.all_tasks, instantiate_emulator=False
)
correctors = instantiate_correctors(args.keyboard, fast_mode=not args.all_tasks, instantiate_emulator=False)

# Get dataset, and filter it to keep only a small number of sentences
dataset = get_soda_dataset(args.n_sentences)
Expand All @@ -162,7 +143,7 @@ def cli():
print("Overall score : ", results["overall_score"])

elif args.cmd == "show_layout":
correctors = instantiate_correctors(args.keyboard, args.ios_name, args.ios_platform)
correctors = instantiate_correctors(args.keyboard)
for c in correctors:
c.emulator.show_keyboards()
print(f"Predictions : {c.emulator.get_predictions()}")
37 changes: 32 additions & 5 deletions kebbie/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
"bundleId": "com.apple.MobileSMS",
"newCommandTimeout": 3600,
}
DEFAULT_IOS_NAME = "iPhone 15 Pro"
DEFAULT_IOS_PLATFORM = "17.4"
BROWSER_PAD_URL = "https://www.justnotepad.com"
ANDROID_TYPING_FIELD_CLASS_NAME = "android.widget.EditText"
DUMMY_RECIPIENT = "0"
Expand Down Expand Up @@ -234,8 +232,8 @@ def __init__(
device: str = None,
host: str = "127.0.0.1",
port: str = "4723",
ios_name: str = DEFAULT_IOS_NAME,
ios_platform: str = DEFAULT_IOS_PLATFORM,
ios_name: str = None,
ios_platform: str = None,
):
super().__init__()

Expand Down Expand Up @@ -306,7 +304,7 @@ def _access_typing_field(self):
self.typing_field.click()
self.typing_field.clear()

def get_devices() -> List[str]:
def get_android_devices() -> List[str]:
"""Static method that uses the `adb devices` command to retrieve the
list of devices running.
Expand All @@ -318,6 +316,35 @@ def get_devices() -> List[str]:
devices = [d.split()[0] for d in devices if not (d.startswith("List of devices attached") or len(d) == 0)]
return devices

def get_ios_devices() -> List[Tuple[str, str]]:
"""Static method that uses the `xcrun simctl` command to retrieve the
list of booted devices.
Returns:
List of booted device platform and device name.
"""
devices = []

result = subprocess.run(["xcrun", "simctl", "list", "devices"], stdout=subprocess.PIPE)
out = result.stdout.decode().split("\n")

curr_platform = ""
for line in out:
if line.startswith("== ") and line.endswith(" =="):
continue
elif line.startswith("-- ") and line.endswith(" --"):
curr_platform = line[3:-3]
else:
m = re.match(r"\s+([^\t]+)\s+\([A-Z0-9\-]+\)\s+\((Booted|Shutdown)\)", line)
if m:
device_name = m.group(1)
status = m.group(2)

if status == "Booted" and curr_platform.startswith("iOS "):
devices.append((curr_platform[4:], device_name))

return devices

def _paste(self, text: str):
"""Paste the given text into the typing field, to quickly simulate
typing a context.
Expand Down

0 comments on commit 4cb5dde

Please sign in to comment.