def checkBugs(bugs, tmpdir, checkTraceback=True): path = os.path.join(tmpdir, 'test.sc') for bug in bugs: for program, line in programsWithBug(bug): try: # write program to file so we can check SyntaxError line correction with open(path, 'w') as f: f.write(program) scenic.scenarioFromFile(path) print(f'FAILING PROGRAM:\n{program}') pytest.fail( f'Program with buggy statement "{bug}" did not raise error' ) except Exception as e: if isinstance(e, (ParseError, SyntaxError)): assert e.lineno == line, program if isinstance(e, SyntaxError): assert e.text.strip() == bug assert e.offset <= len(e.text) # in Python 3.7+, when we can modify tracebacks, check that the # last frame of the traceback has the correct line number if checkTraceback and sys.version_info >= (3, 7): tb = sys.exc_info()[2] while tb.tb_next is not None: tb = tb.tb_next assert tb.tb_lineno == line, f'\n{program}'
def checkBug(bug, template, tmpdir, checkTraceback=True, generate=False): path = os.path.join(tmpdir, 'test.sc') line, program = template program = program.format(bug=bug) print(f'TRYING PROGRAM:\n{program}') try: # write program to file so we can check SyntaxError line correction with open(path, 'w') as f: f.write(program) scenario = scenic.scenarioFromFile(path) if generate: scenario.generate(maxIterations=1) pytest.fail( f'Program with buggy statement "{bug}" did not raise error') except Exception as e: if isinstance(e, (ParseError, SyntaxError)): assert e.lineno == line, program if isinstance(e, SyntaxError): assert e.text.strip() == bug assert e.offset <= len(e.text) # in Python 3.7+, when we can modify tracebacks, check that the # last frame of the traceback has the correct line number if checkTraceback and sys.version_info >= (3, 7): tb = sys.exc_info()[2] while tb.tb_next is not None: tb = tb.tb_next assert tb.tb_lineno == line
def test_import_top_absolute(request): base = os.path.dirname(request.fspath) fullpathImports = os.path.join(base, 'imports.sc') fullpathHelper = os.path.join(base, 'helper.sc') scenario = scenarioFromFile(fullpathImports) assert len(scenario.requirements) == 0 scene, iterations = scenario.generate(maxIterations=1) # Top-level and inherited Objects assert len(scene.objects) == 2 ego = scene.egoObject assert ego.species == 'killer' assert scene.objects[1].species == 'helpful' # Parameter depending on imported Python module assert scene.params['thingy'] == 42 # Parameters depending on import circumstances assert scene.params['imports_name'] == '__main__' assert scene.params['imports_file'] == fullpathImports # Inherited parameters as above assert scene.params['helper_name'] == 'helper' assert scene.params['helper_file'] == fullpathHelper
def test_import_top_relative(request): base = os.path.dirname(request.fspath) fullpathHelper = os.path.join(base, 'helper.sc') oldDirectory = os.getcwd() os.chdir(base) try: scenario = scenarioFromFile('imports.sc') assert len(scenario.requirements) == 0 scene, iterations = scenario.generate(maxIterations=1) assert len(scene.objects) == 2 ego = scene.egoObject assert ego.species == 'killer' assert scene.objects[1].species == 'helpful' assert scene.params['thingy'] == 42 assert scene.params['imports_name'] == '__main__' assert scene.params['imports_file'] == 'imports.sc' assert scene.params['helper_name'] == 'helper' assert scene.params['helper_file'] == fullpathHelper finally: os.chdir(oldDirectory)
def test_bad_extension(tmpdir): path = os.path.join(tmpdir, 'blah.py') with pytest.raises(RuntimeError), open(path, 'w'): scenic.scenarioFromFile(path)
def test_missing_file(): with pytest.raises(FileNotFoundError): scenic.scenarioFromFile('____baloney-file-2342905_.sc')
def fromScenario(cls, path, maxIterations=None, ignoredProperties=None): """Create a sampler corresponding to a Scenic program.""" scenario = scenic.scenarioFromFile(path) return cls(scenario, maxIterations=maxIterations, ignoredProperties=ignoredProperties)
def loader(relpath): path = os.path.join(base, relpath) return scenic.scenarioFromFile(path)
help='Place to store the dataset') parser.add_argument('-i', '--index', default='5') parser.add_argument('-m', '--l_r', default='0') parser.add_argument('-n', '--f_b', default='10') parser.add_argument('-end', '--end', default='100') parser.add_argument('-start', '--start', default='0') parser.add_argument('-sc', '--sc_file', default='examples/gazebo/test.sc') parser.add_argument('-world', '--outFile', default='examples/gazebo/outputs/out.world') parser.add_argument('-template', '--world_template', default='basic_world') args = parser.parse_args() # name = "examples/gazebo/simpleBox.sc" scenic_source_file = args.sc_file outFile = args.outFile # construct scenario scenario = scenic.scenarioFromFile(scenic_source_file) scene, _ = scenario.generate() my_cfg = interface.Gazebo.parse(scenario) print(my_cfg) interface.Gazebo.write(scenario) output = interface.Gazebo.config(scene, "basic_world") with open(outFile, 'w+') as fileObj: fileObj.write(output)
def runFile(path): scenario = scenic.scenarioFromFile(path) scene, _ = scenario.generate(maxIterations=1) sampleActionsFromScene(scene, maxSteps=2)