Skip to content

Commit

Permalink
Added service to download the Snap app if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ymollard committed Jun 25, 2020
1 parent e50d2d3 commit e28bf11
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pypot/creatures/services_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,20 @@ def main():
snap_static_port = 8888
snap_static_server = HTTPServer(("0.0.0.0", snap_static_port), SimpleHTTPRequestHandler)

os.chdir(os.path.join(os.path.dirname(__file__), "..", "snap"))
snap_static_server_process = Process(target=snap_static_server.serve_forever, args=())
static_server_started = True
snap_static_server_process.start()

snap_url = 'http://127.0.0.1:{}/snap.html'.format(snap_static_port)
block_url = 'http://{}:{}/snap-blocks.xml'.format(
find_local_ip(), args.snap_port)
url = '{}#open:{}'.format(snap_url, block_url)
from pypot.vpl.snap import download_snap_interactively
static_app = download_snap_interactively()
if static_app is None:
print("The static server was not started because the VPL app has not been downloaded")
else:
os.chdir(static_app)
snap_static_server_process = Process(target=snap_static_server.serve_forever, args=())
static_server_started = True
snap_static_server_process.start()

snap_url = 'http://127.0.0.1:{}/snap.html'.format(snap_static_port)
block_url = 'http://{}:{}/snap-blocks.xml'.format(
find_local_ip(), args.snap_port)
url = '{}#open:{}'.format(snap_url, block_url)

with closing(start_poppy_with_services(args)):

Expand Down
Empty file added pypot/vpl/__init__.py
Empty file.
67 changes: 67 additions & 0 deletions pypot/vpl/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import sys
import tempfile
from wget import download
from urllib.error import URLError
from zipfile import ZipFile
from pathlib import Path

def get_pypot_datadir(app_name="pypot"):
"""
Returns pypot's directory for peristent data.
Attempt creation if create==True.
# linux: ~/.local/share
# macOS: ~/Library/Application Support
# windows: C:/Users/<USER>/AppData/Roaming
"""
home = Path.home()

if sys.platform == "win32":
data_dir = home / "AppData/Roaming"
elif sys.platform == "linux":
data_dir = home / ".local/share"
elif sys.platform == "darwin":
data_dir = home / "Library/Application Support"
else:
raise ValueError("Can't find the user data directory of your platform '{}'".format(sys.platform))

#app_name = app_name if version is None else app_name + "-" + str(version)
pypot_dir = data_dir / app_name
return pypot_dir


def download_vpl_interactively(vpl_app_name, vpl_app_url, extract=False):
"""
Download the specified Visual Programming langage web app and returns its path.
If it couldn't be downloaded, return None
"""
pypot_datadir = get_pypot_datadir()
vpl_dir = pypot_datadir / vpl_app_name
actual_vpl_dir = vpl_dir / vpl_app_name if extract else vpl_dir

if vpl_dir.is_dir():
return actual_vpl_dir
else:
while True:
response = input("This is the first time you are launching {}, it needs to be downloaded first. Proceed? [Y/n] ".format(vpl_app_name))
if response.lower() in ["y", ""]:
try:
vpl_dir.mkdir(parents=True)
except FileExistsError:
pass
print("Downloading...")
try:
downloaded_app = download(vpl_app_url, tempfile.gettempdir())
except URLError as e:
print("Cannot download the {] app from {}: {}".format(vpl_app_name, vpl_app_url, str(e)), file=sys.stderr)
else:
try:
with ZipFile(downloaded_app, 'r') as archive:
archive.extractall(vpl_dir)
except FileNotFoundError:
print("Couldn't extract {} from zipfile".format(vpl_app_name))
else:
return actual_vpl_dir
else:
print("Download aborted by user", file=sys.stderr)
return None
13 changes: 13 additions & 0 deletions pypot/vpl/snap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .download import download_vpl_interactively

# Snap 5.4.5 application download to local data directory in userspace
VPL_APP_URL = "https://codeload.github.com/jmoenig/Snap/zip/v5.4.5"
VPL_APP_NAME = "Snap-5.4.5"
VPL_EXTRACT_ZIP_ROOT = True

def download_snap_interactively():
"""
Download the Snap 5.4.5 Programming langage web app and returns its path.
If it couldn't be downloaded, return None
"""
return download_vpl_interactively(VPL_APP_NAME, VPL_APP_URL, VPL_EXTRACT_ZIP_ROOT)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def version():
'bottle',
'requests',
'opencv-contrib-python',
'wget',
]

if sys.version_info < (3, 5):
Expand Down

0 comments on commit e28bf11

Please sign in to comment.