Skip to content

Commit

Permalink
Update to properly use public interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamenot committed Mar 23, 2022
1 parent e01fbad commit e08cd03
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 794 deletions.
102 changes: 8 additions & 94 deletions cli/studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
# THE SOFTWARE.
import multiprocessing
import os
import subprocess
import sys
from multiprocessing import Process, Semaphore
from multiprocessing.pool import ThreadPool
from pathlib import Path
Expand Down Expand Up @@ -49,87 +47,24 @@ def scenario_cli():
)
@click.argument("scenario", type=click.Path(exists=True), metavar="<scenario>")
def build_scenario(clean: bool, allow_offset_map: bool, scenario: str):
_build_single_scenario(clean, allow_offset_map, scenario)


def _build_single_scenario(clean: bool, allow_offset_map: bool, scenario: str):
click.echo(f"build-scenario {scenario}")
if clean:
_clean(scenario)

scenario_root = Path(scenario)
scenario_root_str = str(scenario_root)

scenario_py = scenario_root / "scenario.py"
if scenario_py.exists():
_install_requirements(scenario_root)
subprocess.check_call([sys.executable, scenario_py])

from smarts.core.scenario import Scenario

traffic_histories = Scenario.discover_traffic_histories(scenario_root_str)
shift_to_origin = not allow_offset_map or bool(traffic_histories)

map_spec = Scenario.discover_map(scenario_root_str, shift_to_origin=shift_to_origin)
road_map, _ = map_spec.builder_fn(map_spec)
if not road_map:
click.echo(
"No reference to a RoadNetwork file was found in {}, or one could not be created. "
"Please make sure the path passed is a valid Scenario with RoadNetwork file required "
"(or a way to create one) for scenario building.".format(scenario_root_str)
)
return
from smarts.sstudio.build_scenario import build_single_scenario

road_map.to_glb(os.path.join(scenario_root, "map.glb"))
build_single_scenario(clean, allow_offset_map, scenario, click.echo)


def _build_single_scenario_proc(
clean: bool, allow_offset_map: bool, scenario: str, semaphore: Semaphore
):
from smarts.sstudio.build_scenario import build_single_scenario

semaphore.acquire()
try:
_build_single_scenario(clean, allow_offset_map, scenario)
build_single_scenario(clean, allow_offset_map, scenario, click.echo)
finally:
semaphore.release()


def _install_requirements(scenario_root):
import importlib.resources as pkg_resources

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,
)

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

click.echo(
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()


def _is_scenario_folder_to_build(path: str) -> bool:
if os.path.exists(os.path.join(path, "waymo.yaml")):
# for now, don't try to build Waymo scenarios...
Expand Down Expand Up @@ -189,30 +124,9 @@ def build_all_scenarios(clean: bool, allow_offset_maps: bool, scenarios: str):
@scenario_cli.command(name="clean")
@click.argument("scenario", type=click.Path(exists=True), metavar="<scenario>")
def clean_scenario(scenario: str):
_clean(scenario)


def _clean(scenario: str):
to_be_removed = [
"map.glb",
"bubbles.pkl",
"missions.pkl",
"flamegraph-perf.log",
"flamegraph.svg",
"flamegraph.html",
"*.rou.xml",
"*.rou.alt.xml",
"social_agents/*",
"traffic/*",
"history_mission.pkl",
"*.shf",
"*-AUTOGEN.net.xml",
]
p = Path(scenario)
for file_name in to_be_removed:
for f in p.glob(file_name):
# Remove file
f.unlink()
from smarts.sstudio.build_scenario import clean_scenario

clean_scenario(scenario)


@scenario_cli.command(name="replay")
Expand Down
Loading

0 comments on commit e08cd03

Please sign in to comment.