Skip to content

Commit

Permalink
Bugfix envision resource path (#1682)
Browse files Browse the repository at this point in the history
* Fix envision resource path

* Format log message better

* Change log to debug

* Update changelog

* Remove duplicate file

* Fix directory resolution in scenario construction
  • Loading branch information
Gamenot committed Oct 26, 2022
1 parent e5548f8 commit 8b7a09e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Copy and pasting the git commit messages is __NOT__ enough.
- Fixed a bug with the `TripMeterSensor` that was not using a unit direction vector to calculate trip distance against current route.
- Fixed issues with Envision. The playback bar and realtime mode now work as expected.
- Fixed a bug where traffic history vehicles would not get traffic signal observations
- Fixed a bug where envision would not work in some versions of python due to nuances of `importlib.resource.path()`.

## [0.6.1]
### Added
Expand Down
4 changes: 2 additions & 2 deletions cli/studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def scenario_cli():
def build_scenario(clean: bool, allow_offset_map: bool, scenario: str, seed: int):
click.echo(f"build-scenario {scenario}")

from smarts.sstudio.build_scenario import build_single_scenario
from smarts.sstudio.scenario_construction import build_single_scenario

assert seed == None or isinstance(seed, (int))

Expand All @@ -72,7 +72,7 @@ def _build_single_scenario_proc(
semaphore: synchronize.Semaphore,
seed: int,
):
from smarts.sstudio.build_scenario import build_single_scenario
from smarts.sstudio.scenario_construction import build_single_scenario

semaphore.acquire()
try:
Expand Down
46 changes: 24 additions & 22 deletions envision/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import json
import logging
import math
import os
import random
import signal
import sys
Expand Down Expand Up @@ -489,27 +490,28 @@ def get(self):
def make_app(scenario_dirs: Sequence, max_capacity_mb: float, debug: bool):
"""Create the envision web server application through composition of services."""

with pkg_resources.path(web_dist, ".") as dist_path:
return tornado.web.Application(
[
(r"/", MainHandler),
(r"/simulations", SimulationListHandler),
(r"/simulations/(?P<simulation_id>\w+)/state", StateWebSocket),
(
r"/simulations/(?P<simulation_id>\w+)/broadcast",
BroadcastWebSocket,
dict(max_capacity_mb=max_capacity_mb),
),
(
r"/assets/maps/(.*)",
MapFileHandler,
dict(scenario_dirs=scenario_dirs),
),
(r"/assets/models/(.*)", ModelFileHandler),
(r"/(.*)", tornado.web.StaticFileHandler, dict(path=str(dist_path))),
],
debug=debug,
)
dist_path = Path(os.path.dirname(web_dist.__file__)).parent
logging.debug("Creating app with resources at: `%s`", dist_path)
return tornado.web.Application(
[
(r"/", MainHandler),
(r"/simulations", SimulationListHandler),
(r"/simulations/(?P<simulation_id>\w+)/state", StateWebSocket),
(
r"/simulations/(?P<simulation_id>\w+)/broadcast",
BroadcastWebSocket,
dict(max_capacity_mb=max_capacity_mb),
),
(
r"/assets/maps/(.*)",
MapFileHandler,
dict(scenario_dirs=scenario_dirs),
),
(r"/assets/models/(.*)", ModelFileHandler),
(r"/(.*)", tornado.web.StaticFileHandler, dict(path=str(dist_path))),
],
debug=debug,
)


def on_shutdown():
Expand All @@ -527,7 +529,7 @@ def run(
"""Create and run an envision web server."""
app = make_app(scenario_dirs, max_capacity_mb, debug=debug)
app.listen(port)
logging.debug(f"Envision listening on port={port}")
logging.debug("Envision listening on port=%s", port)

ioloop = tornado.ioloop.IOLoop.current()
signal.signal(
Expand Down
2 changes: 1 addition & 1 deletion examples/env/figure_eight_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def entry_point(*args, **kwargs):

scenario = str((Path(__file__).parent / "../../scenarios/figure_eight").resolve())
## Note: can build the scenario here
from smarts.sstudio.build_scenario import build_single_scenario
from smarts.sstudio.scenario_construction import build_single_scenario

build_single_scenario(clean=True, allow_offset_map=True, scenario=scenario)
hiwayenv = HiWayEnv(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,38 +103,38 @@ def clean_scenario(scenario: str):


def _install_requirements(scenario_root, log: Optional[Callable[[Any], None]] = None):
import importlib.resources as pkg_resources
import os

requirements_txt = scenario_root / "requirements.txt"
if requirements_txt.exists():
import zoo.policies

with pkg_resources.path(zoo.policies, "") as path:
# Serve policies through the static file server, then kill after
# we've installed scenario requirements
pip_index_proc = subprocess.Popen(
["twistd", "-n", "web", "--path", path],
# Hide output to keep display simple
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
)
path = Path(os.path.dirname(zoo.policies.__file__)).parent
# Serve policies through the static file server, then kill after
# we've installed scenario requirements
pip_index_proc = subprocess.Popen(
["twistd", "-n", "web", "--path", path],
# Hide output to keep display simple
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
)

pip_install_cmd = [
sys.executable,
"-m",
"pip",
"install",
"-r",
str(requirements_txt),
]

if log is not None:
log(
f"Installing scenario dependencies via '{' '.join(pip_install_cmd)}'"
)
pip_install_cmd = [
sys.executable,
"-m",
"pip",
"install",
"-r",
str(requirements_txt),
]

if log is not None:
log(
f"Installing scenario dependencies via '{' '.join(pip_install_cmd)}'"
)

try:
subprocess.check_call(pip_install_cmd, stdout=subprocess.DEVNULL)
finally:
pip_index_proc.terminate()
pip_index_proc.wait()
try:
subprocess.check_call(pip_install_cmd, stdout=subprocess.DEVNULL)
finally:
pip_index_proc.terminate()
pip_index_proc.wait()

0 comments on commit 8b7a09e

Please sign in to comment.