Skip to content

Commit e28bf11

Browse files
committed
Added service to download the Snap app if needed
1 parent e50d2d3 commit e28bf11

File tree

5 files changed

+95
-9
lines changed

5 files changed

+95
-9
lines changed

pypot/creatures/services_launcher.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,20 @@ def main():
185185
snap_static_port = 8888
186186
snap_static_server = HTTPServer(("0.0.0.0", snap_static_port), SimpleHTTPRequestHandler)
187187

188-
os.chdir(os.path.join(os.path.dirname(__file__), "..", "snap"))
189-
snap_static_server_process = Process(target=snap_static_server.serve_forever, args=())
190-
static_server_started = True
191-
snap_static_server_process.start()
192-
193-
snap_url = 'http://127.0.0.1:{}/snap.html'.format(snap_static_port)
194-
block_url = 'http://{}:{}/snap-blocks.xml'.format(
195-
find_local_ip(), args.snap_port)
196-
url = '{}#open:{}'.format(snap_url, block_url)
188+
from pypot.vpl.snap import download_snap_interactively
189+
static_app = download_snap_interactively()
190+
if static_app is None:
191+
print("The static server was not started because the VPL app has not been downloaded")
192+
else:
193+
os.chdir(static_app)
194+
snap_static_server_process = Process(target=snap_static_server.serve_forever, args=())
195+
static_server_started = True
196+
snap_static_server_process.start()
197+
198+
snap_url = 'http://127.0.0.1:{}/snap.html'.format(snap_static_port)
199+
block_url = 'http://{}:{}/snap-blocks.xml'.format(
200+
find_local_ip(), args.snap_port)
201+
url = '{}#open:{}'.format(snap_url, block_url)
197202

198203
with closing(start_poppy_with_services(args)):
199204

pypot/vpl/__init__.py

Whitespace-only changes.

pypot/vpl/download.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import sys
2+
import tempfile
3+
from wget import download
4+
from urllib.error import URLError
5+
from zipfile import ZipFile
6+
from pathlib import Path
7+
8+
def get_pypot_datadir(app_name="pypot"):
9+
"""
10+
Returns pypot's directory for peristent data.
11+
Attempt creation if create==True.
12+
13+
# linux: ~/.local/share
14+
# macOS: ~/Library/Application Support
15+
# windows: C:/Users/<USER>/AppData/Roaming
16+
"""
17+
home = Path.home()
18+
19+
if sys.platform == "win32":
20+
data_dir = home / "AppData/Roaming"
21+
elif sys.platform == "linux":
22+
data_dir = home / ".local/share"
23+
elif sys.platform == "darwin":
24+
data_dir = home / "Library/Application Support"
25+
else:
26+
raise ValueError("Can't find the user data directory of your platform '{}'".format(sys.platform))
27+
28+
#app_name = app_name if version is None else app_name + "-" + str(version)
29+
pypot_dir = data_dir / app_name
30+
return pypot_dir
31+
32+
33+
def download_vpl_interactively(vpl_app_name, vpl_app_url, extract=False):
34+
"""
35+
Download the specified Visual Programming langage web app and returns its path.
36+
If it couldn't be downloaded, return None
37+
"""
38+
pypot_datadir = get_pypot_datadir()
39+
vpl_dir = pypot_datadir / vpl_app_name
40+
actual_vpl_dir = vpl_dir / vpl_app_name if extract else vpl_dir
41+
42+
if vpl_dir.is_dir():
43+
return actual_vpl_dir
44+
else:
45+
while True:
46+
response = input("This is the first time you are launching {}, it needs to be downloaded first. Proceed? [Y/n] ".format(vpl_app_name))
47+
if response.lower() in ["y", ""]:
48+
try:
49+
vpl_dir.mkdir(parents=True)
50+
except FileExistsError:
51+
pass
52+
print("Downloading...")
53+
try:
54+
downloaded_app = download(vpl_app_url, tempfile.gettempdir())
55+
except URLError as e:
56+
print("Cannot download the {] app from {}: {}".format(vpl_app_name, vpl_app_url, str(e)), file=sys.stderr)
57+
else:
58+
try:
59+
with ZipFile(downloaded_app, 'r') as archive:
60+
archive.extractall(vpl_dir)
61+
except FileNotFoundError:
62+
print("Couldn't extract {} from zipfile".format(vpl_app_name))
63+
else:
64+
return actual_vpl_dir
65+
else:
66+
print("Download aborted by user", file=sys.stderr)
67+
return None

pypot/vpl/snap.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from .download import download_vpl_interactively
2+
3+
# Snap 5.4.5 application download to local data directory in userspace
4+
VPL_APP_URL = "https://codeload.github.com/jmoenig/Snap/zip/v5.4.5"
5+
VPL_APP_NAME = "Snap-5.4.5"
6+
VPL_EXTRACT_ZIP_ROOT = True
7+
8+
def download_snap_interactively():
9+
"""
10+
Download the Snap 5.4.5 Programming langage web app and returns its path.
11+
If it couldn't be downloaded, return None
12+
"""
13+
return download_vpl_interactively(VPL_APP_NAME, VPL_APP_URL, VPL_EXTRACT_ZIP_ROOT)

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def version():
2020
'bottle',
2121
'requests',
2222
'opencv-contrib-python',
23+
'wget',
2324
]
2425

2526
if sys.version_info < (3, 5):

0 commit comments

Comments
 (0)