-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino_cli_interface.py
136 lines (107 loc) · 4.59 KB
/
arduino_cli_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import subprocess
import logging
import shutil
import platform
from pathlib import Path
THIS_PATH = Path(__file__).parent
def get_module_logger():
return logging.getLogger(__name__)
def modify_cygwin_path(sketch_path):
if (str(sketch_path).startswith("/cygdrive/")):
# Pretty hacky way of fixing this, and probably fails under a lot of scenarios.
# Replaces "/cygdrive/c" with "c:" in the path
drive = sketch_path.parts[2]
sketch_path = Path(drive + ":", *sketch_path.parts[3:])
return sketch_path
class ArduinoCLIInterface:
def __init__(self):
self.location = None
self.cygwin = "CYGWIN" in platform.system()
if self.cygwin:
get_module_logger().info("Cygwin detected!")
def find(self):
if self.location is None:
self.location = shutil.which(
"arduino-cli") or shutil.which("arduino-cli.exe")
get_module_logger().info("CLI Location: {}".format(self.location))
get_module_logger().info("CLI Version: {}".format(self.cli_version()))
return self.location
def cli_version(self):
self.find()
args = [self.location, "version"]
result = subprocess.run(args, stdout=subprocess.PIPE)
return result.stdout.decode("utf-8").strip()
def lib_is_installed(self, library):
args = [self.location, "lib", "list"]
result = subprocess.run(args, stdout=subprocess.PIPE)
for l in result.stdout.splitlines():
if l.decode("utf-8").strip().startswith(library):
return True
return False
def core_is_installed(self, core):
args = [self.location, "core", "list"]
result = subprocess.run(args, stdout=subprocess.PIPE)
for l in result.stdout.splitlines():
try:
installed, core_id, latest, name = l.decode("utf-8").split(maxsplit=3)
if installed.strip() == core:
get_module_logger().info("Core {} is installed".format(core))
return True
except:
pass
return False
def install_lib(self, library):
self.find()
if not self.lib_is_installed(library):
args = [self.location, "lib", "install", library]
try:
result = subprocess.run(args)
get_module_logger().info("Lib Install Success: '{}'".format(" ".join(result.args)))
except: # noqa: disable=bare-except
get_module_logger().info("Lib Install Failed: '{}'".format(" ".join(result.args)))
raise
def install_core(self, core):
self.find()
if not self.core_is_installed(core):
args = [self.location, "core", "install", core]
try:
result = subprocess.run(args)
get_module_logger().info("Core Install Success: '{}'".format(" ".join(result.args)))
except: # noqa: disable=bare-except
get_module_logger().info("Core Install Failed: '{}'".format(" ".join(args)))
raise
def verify(self, board, sketch_path):
success = False
self.find()
for library in board.required_libraries():
self.install_lib(library)
self.install_core(board.required_core)
if self.cygwin:
sketch_path = modify_cygwin_path(sketch_path)
args = [self.location, "compile", "--fqbn", board.fqbn,
str(sketch_path)]
try:
result = subprocess.run(args)
success = result.returncode == 0
if success:
get_module_logger().info("Verify Success: '{}'".format(" ".join(result.args)))
else:
get_module_logger().info("Verify Failed: '{}'".format(" ".join(args)))
except: # noqa: disable=bare-except
get_module_logger().info("Verify Command Exception: '{}'".format(" ".join(args)))
raise
return success
def upload(self, board, sketch_path, port):
self.find()
args = [self.location, "upload", "-p", port,
"--fqbn", board.fqbn, str(sketch_path)]
try:
result = subprocess.run(args)
success = result.returncode == 0
if success:
get_module_logger().info("Upload Success: '{}'".format(" ".join(result.args)))
else:
get_module_logger().info("Upload Failed: '{}'".format(" ".join(args)))
except: # noqa: disable=bare-except
get_module_logger().info("Upload Failed: '{}'".format(" ".join(args)))
raise