-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
245 lines (203 loc) · 6.74 KB
/
main.py
File metadata and controls
245 lines (203 loc) · 6.74 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import click
import docker
from typing import Optional
import subprocess
import docker_commands
import dros_utils
from consts import ROS_IMAGE_REPOSITORY, ROS_IMAGE_DEFAULT_TAG, DROS_BASE_IMAGE_NAME
import os
@click.group()
def cli() -> None:
"""
A tool to create ROS-Workspaces as docker-containers and to interact with them.
Only compatible with docker-images of ROS-Versions pre ROS2.
"""
pass
@click.command()
@click.option(
'-r',
'--ros-version',
default='melodic',
help='ROS version you want to use (e.g. "melodic")'
)
@click.option(
'-p',
'--path',
default=None,
help='path of the catkin_ws on host environment'
)
@click.argument('workspace')
def new(workspace: str, ros_version: str, path: Optional[str]) -> None:
"""
Creates and initializes the given workspace with the given ros_version, and creates a catkin_ws folder at the given path.
\b
Parameters
----------
workspace : str
Name of the workspace you want to create.
ros_version : str
The ROS-Version to build from (e.g. noetic).
Defaults to melodic.
path : str
The path where the catkin_ws folder should be placed on the host environment.
e.g. ~/ros-workspaces/ws-1 -> /home/ros-workspaces/ws-1/catkin_ws
/home/path/to/workspaces/ws-x -> /home/path/to/workspaces/ws-x/catkin_ws
/home/ws-x/catkin_ws -> /home/ws-x/catkin_ws
Defaults to current working directory (./catkin_ws)
"""
if not dros_utils.workspace_exists(workspace):
click.echo(f"Creating '{workspace}'. This might take a while")
dros_utils.new(workspace, ros_version, path)
click.echo(f"'{workspace}' created")
else:
click.echo(f"'{workspace}' already exists")
@click.command()
@click.option(
'-p',
'--path',
default=None,
required=True,
help='path of the catkin_ws on host environment'
)
@click.argument('workspace')
def reinit(workspace: str, path: str) -> None:
"""
Clears the catkin_ws folder of the given workspace and initializes it again.
\b
Parameters
----------
workspace : str
Workspace reinitialize.
"""
confirm_message = "This will clear all files in your persisted workspace. Continue?"
if dros_utils.shout_if_workspace_exists(workspace) and click.confirm(confirm_message, abort=True):
click.echo(f"Clearing workspace '{workspace}' ...")
dros_utils.clear_workspace(workspace)
click.echo(f"Initializing workspace '{workspace}' ...")
dros_utils.init_workspace(workspace, dros_utils.catkin_ws_path_from(path))
@click.command()
@click.option(
'--select/--no-select',
default=False,
help='Gives you a list with all workspaces to connect to, and asks which one to connect you to.'
)
@click.argument('workspace', required=False)
def connect(workspace: Optional[str], select: bool) -> None:
"""
Connects you to the given workspace by opening a shell.
\b
Parameters
----------
workspace : str
Workspace to connect to.
"""
if select:
workspaces = dros_utils.get_workspaces()
indices = range(len(workspaces))
for (index, workspace) in zip(indices, workspaces):
click.echo(f"[{index}] {workspace}")
click.echo()
selector = click.prompt("Select workspace (index or name)")
for (index, workspace) in zip(indices, workspaces):
try:
indx = int(selector)
if indx == index:
dros_utils.connect(workspace)
break
except:
if selector == workspace:
dros_utils.connect(workspace)
break
elif workspace != None and dros_utils.shout_if_workspace_exists(workspace):
dros_utils.connect(workspace)
# @click.command()
# def select() -> None:
# """
# Gives you a list with all workspaces to connect to, and asks which one to connect you to.
# """
# workspaces = dros_utils.get_workspaces()
# indices = range(len(workspaces))
# for (index, workspace) in zip(indices, workspaces):
# click.echo(f"[{index}] {workspace}")
# click.echo()
# selector = click.prompt("Select workspace (index or name)")
# for (index, workspace) in zip(indices, workspaces):
# try:
# indx = int(selector)
# if indx == index:
# dros_utils.connect(workspace)
# break
# except:
# if selector == workspace:
# dros_utils.connect(workspace)
# break
@click.command()
@click.argument('workspace')
def rename(workspace: str) -> None:
"""
Renames the given workspace
\b
Parameters
----------
workspace : str
Workspace to rename.
"""
if dros_utils.shout_if_workspace_exists(workspace):
new_name = click.prompt("New name").strip()
while new_name == "" or (dros_utils.workspace_exists(new_name)):
if new_name == "":
click.echo("Workspace name can't be empty")
else:
click.echo(f"A workspace with name '{new_name}' already exists")
new_name = click.prompt("New name").strip()
if new_name != workspace:
dros_utils.rename_workspace(workspace, new_name)
@click.command()
def list() -> None:
"""
List all DROS workspaces.
"""
workspaces = dros_utils.get_workspaces()
indices = range(len(workspaces))
for (index, workspace) in zip(indices, workspaces):
click.echo(f"[{index}] {workspace}")
# @click.command()
# @click.argument('workspace')
# def start(workspace: str) -> None:
# """
# Starts the given workspace.
# """
# if dros_utils.shout_if_workspace_exists(workspace):
# dros_utils.start(workspace)
@click.command()
@click.option(
'--persist/--no-persist',
default=True,
help='keeps the content of the catkin_ws folder on host environment.'
)
@click.argument('workspace')
def remove(workspace: str, persist: bool) -> None:
"""
Removes the given workspace.
\b
Parameters
----------
workspace : str
Workspace to remove.
persist : bool
Keeps folder content of the 'catkin_ws' folder, corresponding to 'workspace', on host environment.
Defaults to True.
"""
if dros_utils.shout_if_workspace_exists(workspace):
click.echo(f"Removing '{workspace}'. This might take a while")
dros_utils.remove(workspace, persist)
cli.add_command(new)
# cli.add_command(start)
cli.add_command(connect)
# cli.add_command(select)
cli.add_command(reinit)
cli.add_command(rename)
cli.add_command(list)
cli.add_command(remove)
if __name__ == '__main__':
cli()