Ejemplo n.º 1
0
 def testGetSubTestDescriptionWithoutDocstring(self):
     with self.subTest(foo=1, bar=2):
         result = unittest.TextTestResult(None, True, 1)
         self.assertEqual(
             result.getDescription(self._subtest),
             'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
             '.Test_TextTestResult) (foo=1, bar=2)')
     with self.subTest('some message'):
         result = unittest.TextTestResult(None, True, 1)
         self.assertEqual(
             result.getDescription(self._subtest),
             'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
             '.Test_TextTestResult) [some message]')
Ejemplo n.º 2
0
    def run_test_without_subtest(self, fixture, name, *, raises=None, failures_contain=[], errors_contain=[]):
        tc = fixture(methodName=name)
        stream = stream_for_unittest()
        tr = unittest.TextTestResult(stream=stream, descriptions=True, verbosity=1)
        tc.run(tr)

        failures = '\n'.join(
            f"{test}\n{indent(stack_trace)}"
            for test, stack_trace in tr.failures
        )
        for text in failures_contain:
            self.assertIn(text, failures)
        errors = '\n'.join(
            f"{test}\n{indent(stack_trace)}"
            for test, stack_trace in tr.errors
        )
        for text in errors_contain:
            self.assertIn(text, errors)

        if raises is AssertionError:
            self.assertNotEqual(len(tr.failures), 0, "Expected failures but got none")
        elif raises is not None:
            self.assertNotEqual(len(tr.errors), 0, "Expected errors but got none")
            self.assertIn(
                raises.__name__,
                '\n'.join(
                    stack_trace
                    for _, stack_trace in tr.errors
                ),
            )
        else:
            self.assertEqual(tr.testsRun, 1, "Expected one test to be run")
            self.assertEqual(len(tr.failures), 0, f"Expected no failures, but got:\n\n{indent(failures)}")
            self.assertEqual(len(tr.errors), 0, f"Expected no errors, but got:\n\n{indent(errors)}")
Ejemplo n.º 3
0
def _make_result(verbose, failfast):
    """Creates a TextTestResult object that writes a stream to a StringIO"""
    stream = _WritelnDecorator(StringIO())
    result = unittest.TextTestResult(stream, True, verbose)
    result.buffer = False
    result.failfast = failfast
    return result
Ejemplo n.º 4
0
 def testGetDescriptionWithMultiLineDocstring(self):
     result = unittest.TextTestResult(None, True, 1)
     self.assertEqual(
         result.getDescription(self),
         'testGetDescriptionWithMultiLineDocstring (' + __name__ +
         '.Test_TestResult)\nTests getDescription() for a method with a longer docstring.'
     )
Ejemplo n.º 5
0
	def run(self):
		self.run_command("build")
		import glob
		lib=glob.glob(os.getcwd()+"/build/*")[0]
		sys.path.insert(0,lib)
		print >>sys.stderr,"Using %s for tested modules"%(lib)
		import unittest
		result=unittest.TextTestResult(sys.stdout,True,True)
		suite= unittest.defaultTestLoader.discover("./tests")
		print "Discovered %d test cases"%suite.countTestCases()
		result.buffer=True
		suite.run(result)
		print ""
		if not result.wasSuccessful():
			if len(result.errors):
				print "============ Errors disovered ================="
				for r in result.errors:
					print r[0],":",r[1]
			
			if len(result.failures):
				print "============ Failures disovered ================="
				for r in result.failures:
					print r[0],":",r[1]
			sys.exit(1)
		else:
			print "All tests successful"
Ejemplo n.º 6
0
 def testGetSubTestDescriptionForFalsyValues(self):
     expected = 'testGetSubTestDescriptionForFalsyValues (%s.Test_TextTestResult) [%s]'
     result = unittest.TextTestResult(None, True, 1)
     for arg in [0, None, []]:
         with self.subTest(arg):
             self.assertEqual(result.getDescription(self._subtest),
                              expected % (__name__, arg))
Ejemplo n.º 7
0
def test():
    '''
    Runnig automated tests
    '''
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestResult(verbosity=2).run(tests)
Ejemplo n.º 8
0
 def testGetSubTestDescriptionWithoutDocstringAndParams(self):
     with self.subTest():
         result = unittest.TextTestResult(None, True, 1)
         self.assertEqual(
             result.getDescription(self._subtest),
             'testGetSubTestDescriptionWithoutDocstringAndParams '
             '(' + __name__ + '.Test_TextTestResult) (<subtest>)')
Ejemplo n.º 9
0
def testinstall():
    """
    Performs aperforms basic tests to make sure grid2op is properly installed and working.

    It's not because these tests pass that grid2op will be fully functional however.
    """
    test_loader = unittest.TestLoader()
    this_directory = os.path.abspath(os.path.dirname(__file__))
    test_suite = test_loader.discover(os.path.join(this_directory, 'tests'),
                                      pattern=__LI_FILENAME_TESTS[0])
    for file_name in __LI_FILENAME_TESTS[1:]:
        test_suite.addTest(test_loader.discover(os.path.join(this_directory, 'tests'),
                                                pattern=file_name)
                           )
    results = unittest.TextTestResult(stream=sys.stderr, descriptions=True, verbosity=1)
    test_suite.run(results)
    if results.wasSuccessful():
        sys.exit(0)
    else:
        for _, str_ in results.errors:
            print(str_)
            print("-------------------------\n")
        for _, str_ in results.failures:
            print(str_)
            print("-------------------------\n")
        raise RuntimeError("Test not successful !")
Ejemplo n.º 10
0
    def run(cls):
        global result
        requests.packages.urllib3.disable_warnings()
        try:
            cls.print_symbol()
            usage = """
                syntribos <config> <input_file> --test-types=TEST_TYPES
                syntribos <config> <input_file> -t TEST_TYPE TEST_TYPE ...
                syntribos <config> <input_file>
                """
            args, unknown = syntribos.arguments.SyntribosCLI(
                usage=usage).parse_known_args()
            test_env_manager = TestEnvManager(
                "", args.config, test_repo_package_name="os")
            test_env_manager.finalize()
            cls.set_env()
            init_root_log_handler()

            cls.print_log()
            result = unittest.TextTestResult(
                unittest.runner._WritelnDecorator(sys.stdout),
                True, 2 if args.verbose else 1)
            start_time = time.time()
            for file_path, req_str in args.input:
                for test_name, test_class in cls.get_tests(args.test_types):
                    for test in test_class.get_test_cases(file_path, req_str):
                        cls.run_test(test, result, args.dry_run)
            cls.print_result(result, start_time)
        except KeyboardInterrupt:
            cafe.drivers.base.print_exception(
                "Runner",
                "run",
                "Keyboard Interrupt, exiting...")
            exit(0)
Ejemplo n.º 11
0
 def testGetDescriptionWithoutDocstring(self):
     result = unittest.TextTestResult(None, True, 1)
     self.assertEqual(
         result.getDescription(self),
         'testGetDescriptionWithoutDocstring (' + __name__ +
         '.Test_TestResult)')
     return
Ejemplo n.º 12
0
def main(argv=None):
    """Main function.
    :param argv: command line arguments
    """
    if argv is None:
        argv = sys.argv[1:]

    if len(argv) > 0:
        if argv[0] == 'install':
            from .. import setup
            setup.install()
            return
        elif argv[0] == 'info':
            from problog.core import list_transformations
            list_transformations()
            return
        elif argv[0] == 'unittest':
            import unittest
            test_results = unittest.TextTestResult(sys.stderr, False, 1)
            unittest.TestLoader().discover(os.path.join(os.path.dirname(__file__), '..')).run(test_results)
            return
        elif argv[0] == '--version':
            print (version.version)
            return
        else:
            return run_task(argv)
    else:
        return run_task(argv)
Ejemplo n.º 13
0
 def testGetDescriptionWithOneLineDocstring(self):
     """Tests getDescription() for a method with a docstring."""
     result = unittest.TextTestResult(None, True, 1)
     self.assertEqual(
             result.getDescription(self),
            ('testGetDescriptionWithOneLineDocstring '
             '(' + __name__ + '.Test_TestResult)\n'
             'Tests getDescription() for a method with a docstring.'))
Ejemplo n.º 14
0
 def testGetNestedSubTestDescriptionWithoutDocstring(self):
     with self.subTest(foo=1):
         with self.subTest(bar=2):
             result = unittest.TextTestResult(None, True, 1)
             self.assertEqual(
                 result.getDescription(self._subtest),
                 'testGetNestedSubTestDescriptionWithoutDocstring '
                 '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)')
Ejemplo n.º 15
0
 def _run_test(self, test_name, verbosity, tearDownError=None):
     stream = io.StringIO()
     stream = unittest.runner._WritelnDecorator(stream)
     result = unittest.TextTestResult(stream, True, verbosity)
     test = self.Test(test_name)
     test.tearDownError = tearDownError
     test.run(result)
     return stream.getvalue()
Ejemplo n.º 16
0
 def run(self, result, *args, **kwargs):
     self.result = result
     ori = unittest.TestCase.run
     _result = unittest.TextTestResult(
         unittest.runner._WritelnDecorator(sys.stderr), True, 1)
     ori(self, _result, *args, **kwargs)
     _result.stopTestRun()
     _result.printErrors()
Ejemplo n.º 17
0
 def testGetSubTestDescriptionWithOneLineDocstring(self):
     """Tests getDescription() for a method with a docstring."""
     result = unittest.TextTestResult(None, True, 1)
     with self.subTest(foo=1, bar=2):
         self.assertEqual(
             result.getDescription(self._subtest),
             ('testGetSubTestDescriptionWithOneLineDocstring '
              '(' + __name__ + '.Test_TextTestResult) (foo=1, bar=2)\n'
              'Tests getDescription() for a method with a docstring.'))
Ejemplo n.º 18
0
        def main(_):
            # pybullet imports multiprocessing in their setup.py, which causes an
            # issue when we import multiprocessing.pool.dummy down the line because
            # the PYTHONPATH has changed.
            for module in [
                    'multiprocessing', 'multiprocessing.pool',
                    'multiprocessing.dummy', 'multiprocessing.pool.dummy'
            ]:
                if module in sys.modules:
                    del sys.modules[module]
            # Reimport multiprocessing to avoid spurious error printouts. See
            # https://bugs.python.org/issue15881.
            import multiprocessing as _  # pylint: disable=g-import-not-at-top

            run_separately = [
                x.rstrip()
                for x in open('test_individually.txt', 'r').readlines() if x
            ]
            test_loader = TestLoader(blacklist=run_separately)
            test_suite = test_loader.discover('tf_agents', pattern='*_test.py')
            stderr = StderrWrapper()
            result = unittest.TextTestResult(stderr,
                                             descriptions=True,
                                             verbosity=2)
            test_suite.run(result)

            external_test_failures = []

            for test in run_separately:
                filename = 'tf_agents/%s.py' % test.replace('.', '/')
                try:
                    subprocess.check_call([sys.executable, filename])
                except subprocess.CalledProcessError as e:
                    external_test_failures.append(e)

            result.printErrors()

            for failure in external_test_failures:
                stderr.writeln(str(failure))

            final_output = (
                'Tests run: {} grouped and {} external.  '.format(
                    result.testsRun, len(run_separately)) +
                'Errors: {}  Failures: {}  External failures: {}.'.format(
                    len(result.errors), len(result.failures),
                    len(external_test_failures)))

            header = '=' * len(final_output)
            stderr.writeln(header)
            stderr.writeln(final_output)
            stderr.writeln(header)

            if result.wasSuccessful() and not external_test_failures:
                return 0
            else:
                return 1
Ejemplo n.º 19
0
 def testGetDescriptionWithMultiLineDocstring(self):
     """Tests getDescription() for a method with a longer docstring.
     The second line of the docstring.
     """
     result = unittest.TextTestResult(None, True, 1)
     self.assertEqual(result.getDescription(self),
                      ('testGetDescriptionWithMultiLineDocstring '
                       '(' + __name__ + '.Test_TextTestResult)\n'
                       'Tests getDescription() for a method with a longer '
                       'docstring.'))
Ejemplo n.º 20
0
def python():
    if sys.gettrace() is None:
        import pip._internal
        pip._internal.main(
            ["install", "-I", "-e", str(CURDIR.parent.joinpath(r"Include\Roffild\RoffildPython"))])
    with open(CURDIR.joinpath("python_unittests.log"), "w") as log:
        log.writeln = lambda text="": log.write(text + "\n")
        testresult = unittest.TextTestResult(log, "", 100)
        unittest.defaultTestLoader.discover(
            CURDIR.parent.joinpath(r"Include\Roffild\RoffildPython\roffild\test")).run(testresult)
        testresult.printErrors()
Ejemplo n.º 21
0
    def testGetSubTestDescriptionWithMultiLineDocstring(self):
        """Tests getDescription() for a method with a longer docstring.
        The second line of the docstring.
        """
        result = unittest.TextTestResult(None, True, 1)
        with self.subTest(foo=1, bar=2):
            self.assertEqual(
                result.getDescription(self._subtest),
                'testGetSubTestDescriptionWithMultiLineDocstring (' +
                __name__ + """.Test_TestResult) (bar=2, foo=1)
Tests getDescription() for a method with a longer docstring.""")
Ejemplo n.º 22
0
 def _do_tests(self, dirpath):
     print('\nRunning tests in directory', dirpath)
     self.result = unittest.TextTestResult(sys.stdout , 1, 3)
     loader = unittest.TestLoader().discover(dirpath, self.test_glob)
     loader.run(self.result)
     print("errors:", len(self.result.errors),\
           "failures:", len(self.result.failures),\
           "skipped:", len(self.result.skipped),\
           "expectedFailures:", len(self.result.expectedFailures),\
           "unexpected successes:", len(self.result.unexpectedSuccesses))
     sys.stdout.flush()
     if not self.result.wasSuccessful():
         self.failed_dirs.append(dirpath)
     self.success = self.success and self.result.wasSuccessful()
Ejemplo n.º 23
0
def test_case_run():
    """
    对testCase 和 testsuite的run方法进行验证
    :return:
    """
    # testCase的执行
    result = unittest.TextTestResult(sys.stdout, 'test result', 1)
    unittest_case = unittest_demo.UnitTestCase("test_add")
    unittest_case.run(result)

    # testsuite的执行
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    suite.addTest(loader.loadTestsFromTestCase(unittest_demo.UnitTestCase))
    suite.run(result)
Ejemplo n.º 24
0
def run_scenarios():
    metadata = {
        'network_config': str({'content_path': 'random_value_test_random'})}
    userdata = util.get_resource('windows/multipart_metadata')
    test_result = unittest.TextTestResult(
        _WritelnDecorator(sys.stderr), None, 0)

    scenarios = [
        scenario.BaseWindowsScenario(
            test_class=test_smoke.TestWindowsSmoke,
            recipee=windows.WindowsCloudbaseinitRecipee,
            userdata=userdata,
            metadata=metadata,
            image=CONF.images[0],
            result=test_result),
    ]
    Runner(scenarios).run()
Ejemplo n.º 25
0
        def main(_):
            run_separately = load_test_list('test_individually.txt')
            test_loader = TestLoader(blacklist=run_separately)
            test_suite = test_loader.discover('rl_reliability_metrics',
                                              pattern='*_test.py')

            external_test_failures = []

            for test in run_separately:
                filename = 'rl_reliability_metrics.%s' % test
                try:
                    # Using stderr=subprocess.STDOUT results in an exception in some
                    # environments.
                    subprocess.check_call(
                        ['python3', '-m', 'unittest', '-v', filename])
                except subprocess.CalledProcessError as e:
                    external_test_failures.append(e)

            stderr = StderrWrapper()
            result = unittest.TextTestResult(stderr,
                                             descriptions=True,
                                             verbosity=2)
            test_suite.run(result)
            result.printErrors()

            for failure in external_test_failures:
                stderr.writeln('Individually run test failure:{}'.format(
                    str(failure)))

            final_output = (
                'Tests run: {} grouped and {} external.  '.format(
                    result.testsRun, len(run_separately)) +
                'Errors: {}  Failures: {}  External failures: {}.'.format(
                    len(result.errors), len(result.failures),
                    len(external_test_failures)))

            header = '=' * len(final_output)
            stderr.writeln(header)
            stderr.writeln(final_output)
            stderr.writeln(header)

            if result.wasSuccessful():
                return 0
            else:
                return 1
Ejemplo n.º 26
0
    def main(_):
      test_loader = unittest.TestLoader()
      test_suite = test_loader.discover('tensorflow_gan', pattern='*_test.py')
      stderr = StderrWrapper()
      result = unittest.TextTestResult(stderr, descriptions=True, verbosity=2)
      test_suite.run(result)

      result.printErrors()

      final_output = ('Tests run: {}. Errors: {}  Failures: {}.'.format(
          result.testsRun, len(result.errors), len(result.failures)))

      header = '=' * len(final_output)
      stderr.writeln(header)
      stderr.writeln(final_output)
      stderr.writeln(header)

      if result.wasSuccessful():
        return 0
      else:
        return 1
Ejemplo n.º 27
0
    def run(self):
        sys.path.insert(0, os.getcwd())
        import unittest
        result = unittest.TextTestResult(sys.stdout, True, True)
        suite = unittest.defaultTestLoader.discover("./tests")
        print("Discovered %d test cases" % suite.countTestCases())
        result.buffer = True
        suite.run(result)
        print("")
        if not result.wasSuccessful():
            if len(result.errors):
                print("============ Errors disovered =================")
                for res in result.errors:
                    print(res[0], ":", res[1])

            if len(result.failures):
                print("============ Failures disovered =================")
                for res in result.failures:
                    print(res[0], ":", res[1])
            sys.exit(1)
        else:
            print("All tests successful")
Ejemplo n.º 28
0
        set(
            x.split(':')[0].strip() + '.html'
            for x in args.load_list.readlines()))
else:
    tests = []

# Collect summary of all the individual test runs
summary = testtools.StreamSummary()

# Output information to stdout
if not args.subunit:
    # Human readable test output
    pertest = testtools.StreamToExtendedDecorator(
        testtools.MultiTestResult(
            # Individual test progress
            unittest.TextTestResult(
                unittest.runner._WritelnDecorator(sys.stdout), False, 2),
            # End of run, summary of failures.
            testtools.TextTestResult(sys.stdout),
        ))
else:
    from subunit.v2 import StreamResultToBytes
    pertest = StreamResultToBytes(sys.stdout)

    if args.list:
        output = subunit.CopyStreamResult([summary, pertest])
        output.startTestRun()
        for test in re.compile("(?<=').+(?=')").findall(
                file("test/testcases.js").read()):
            output.status(test_status='exists', test_id=test[:-5])

        output.stopTestRun()
Ejemplo n.º 29
0
async def _main():
    args = get_argparser().parse_args()
    create_logger(args)

    device = None
    try:
        # TODO(py3.7): use importlib.resources
        firmware_filename = os.path.join(os.path.dirname(__file__),
                                         "glasgow.ihex")
        if args.action in ("build", "test", "tool"):
            pass
        elif args.action == "factory":
            device = GlasgowHardwareDevice(args.serial,
                                           firmware_filename,
                                           _factory_rev=args.factory_rev)
        else:
            device = GlasgowHardwareDevice(args.serial, firmware_filename)

        if args.action == "voltage":
            if args.voltage is not None:
                await device.reset_alert(args.ports)
                await device.poll_alert()  # clear any remaining alerts
                try:
                    await device.set_voltage(args.ports, args.voltage)
                except:
                    await device.set_voltage(args.ports, 0.0)
                    raise
                if args.set_alert and args.voltage != 0.0:
                    await asyncio.sleep(
                        0.050)  # let the output capacitor discharge a bit
                    await device.set_alert_tolerance(args.ports, args.voltage,
                                                     args.tolerance / 100)

            print("Port\tVio\tVlimit\tVsense\tMonitor")
            alerts = await device.poll_alert()
            for port in args.ports:
                vio = await device.get_voltage(port)
                vlimit = await device.get_voltage_limit(port)
                vsense = await device.measure_voltage(port)
                alert = await device.get_alert(port)
                notice = ""
                if port in alerts:
                    notice += " (ALERT)"
                print("{}\t{:.2}\t{:.2}\t{:.3}\t{:.2}-{:.2}\t{}".format(
                    port, vio, vlimit, vsense, alert[0], alert[1], notice))

        if args.action == "safe":
            await device.reset_alert("AB")
            await device.set_voltage("AB", 0.0)
            await device.poll_alert()  # clear any remaining alerts
            logger.info("all ports safe")

        if args.action == "voltage-limit":
            if args.voltage is not None:
                await device.set_voltage_limit(args.ports, args.voltage)

            print("Port\tVio\tVlimit")
            for port in args.ports:
                vio = await device.get_voltage(port)
                vlimit = await device.get_voltage_limit(port)
                print("{}\t{:.2}\t{:.2}".format(port, vio, vlimit))

        if args.action in ("run", "run-repl", "run-prebuilt"):
            target, applet = _applet(device.revision, args)
            device.demultiplexer = DirectDemultiplexer(
                device, target.multiplexer.pipe_count)
            plan = target.build_plan()

            if args.action in ("run", "run-repl"):
                await device.download_target(plan, rebuild=args.rebuild)
            if args.action == "run-prebuilt":
                bitstream_file = args.bitstream or open(
                    "{}.bin".format(args.applet), "rb")
                with bitstream_file:
                    logger.warn("downloading prebuilt bitstream from %r",
                                bitstream_file.name)
                    await device.download_bitstream(bitstream_file.read())

            do_trace = hasattr(args, "trace") and args.trace
            if do_trace:
                logger.info("starting applet analyzer")
                await device.write_register(target.analyzer.addr_done, 0)
                analyzer_iface = await device.demultiplexer.claim_interface(
                    target.analyzer, target.analyzer.mux_interface, args=None)
                trace_decoder = TraceDecoder(target.analyzer.event_sources)
                vcd_writer = VCDWriter(
                    args.trace,
                    timescale="1 ns",
                    check_values=False,
                    comment='Generated by Glasgow for bitstream ID %s' %
                    plan.bitstream_id.hex())

            async def run_analyzer():
                signals = {}
                strobes = set()
                for field_name, field_trigger, field_width in trace_decoder.events(
                ):
                    if field_trigger == "throttle":
                        var_type = "wire"
                        var_init = 0
                    elif field_trigger == "change":
                        var_type = "wire"
                        var_init = "x"
                    elif field_trigger == "strobe":
                        if field_width > 0:
                            var_type = "tri"
                            var_init = "z"
                        else:
                            var_type = "event"
                            var_init = ""
                    else:
                        assert False
                    signals[field_name] = vcd_writer.register_var(
                        scope="",
                        name=field_name,
                        var_type=var_type,
                        size=field_width,
                        init=var_init)
                    if field_trigger == "strobe":
                        strobes.add(field_name)

                init = True
                while not trace_decoder.is_done():
                    trace_decoder.process(await analyzer_iface.read())
                    for cycle, events in trace_decoder.flush():
                        if events == "overrun":
                            target.analyzer.logger.error(
                                "FIFO overrun, shutting down")

                            for name in signals:
                                vcd_writer.change(signals[name],
                                                  next_timestamp, "x")
                            timestamp += 1e3  # 1us
                            break

                        event_repr = " ".join("{}={}".format(n, v)
                                              for n, v in events.items())
                        target.analyzer.logger.trace("cycle %d: %s", cycle,
                                                     event_repr)

                        timestamp = 1e9 * (cycle + 0) // target.sys_clk_freq
                        next_timestamp = 1e9 * (cycle +
                                                1) // target.sys_clk_freq
                        if init:
                            init = False
                            vcd_writer._timestamp = timestamp
                        for name, value in events.items():
                            vcd_writer.change(signals[name], timestamp, value)
                        for name, _value in events.items():
                            if name in strobes:
                                vcd_writer.change(signals[name],
                                                  next_timestamp, "z")
                        vcd_writer.flush()

                vcd_writer.close(timestamp)

            async def run_applet():
                logger.info("running handler for applet %r", args.applet)
                if applet.preview:
                    logger.warn(
                        "applet %r is PREVIEW QUALITY and may CORRUPT DATA",
                        args.applet)
                try:
                    iface = await applet.run(device, args)
                    if args.action in ("run", "run-prebuilt"):
                        await applet.interact(device, args, iface)
                    if args.action == "run-repl":
                        if applet.has_custom_repl:
                            logger.warn(
                                "applet provides customized REPL(s); consider using `run "
                                "{} ...-repl` subcommands".format(applet.name))
                        logger.info(
                            "dropping to REPL; use 'help(iface)' to see available APIs"
                        )
                        await AsyncInteractiveConsole(locals={
                            "iface": iface
                        }).interact()
                except GlasgowAppletError as e:
                    applet.logger.error(str(e))
                except asyncio.CancelledError:
                    pass  # terminate gracefully
                finally:
                    await device.demultiplexer.flush()

            async def wait_for_sigint():
                await wait_for_signal(signal.SIGINT)
                logger.debug("Ctrl+C pressed, terminating")

            if do_trace:
                analyzer_task = asyncio.ensure_future(run_analyzer())

            applet_task = asyncio.ensure_future(run_applet())
            sigint_task = asyncio.ensure_future(wait_for_sigint())

            tasks = [applet_task, sigint_task]
            done, pending = await asyncio.wait(
                tasks, return_when=asyncio.FIRST_COMPLETED)
            for task in pending:
                task.cancel()
            for task in tasks:
                try:
                    await task
                except asyncio.CancelledError:
                    pass

            if do_trace:
                await device.write_register(target.analyzer.addr_done, 1)
                await analyzer_task

            await device.demultiplexer.cancel()

        if args.action == "tool":
            tool = GlasgowApplet.all_applets[args.applet].tool_cls()
            try:
                await tool.run(args)
            except GlasgowAppletError as e:
                tool.logger.error(e)
                raise SystemExit()

        if args.action == "flash":
            logger.info("reading device configuration")
            header = await device.read_eeprom("fx2", 0,
                                              8 + 4 + GlasgowConfig.size)
            header[0] = 0xC2  # see below

            fx2_config = FX2Config.decode(header, partial=True)
            if (len(fx2_config.firmware) != 1
                    or fx2_config.firmware[0][0] != 0x4000 - GlasgowConfig.size
                    or len(fx2_config.firmware[0][1]) != GlasgowConfig.size):
                raise SystemExit(
                    "Unrecognized or corrupted configuration block")
            glasgow_config = GlasgowConfig.decode(fx2_config.firmware[0][1])

            logger.info("device has serial %s-%s", glasgow_config.revision,
                        glasgow_config.serial)
            if fx2_config.disconnect:
                logger.info("device has flashed firmware")
            else:
                logger.info("device does not have flashed firmware")
            if glasgow_config.bitstream_size:
                logger.info("device has flashed bitstream ID %s",
                            glasgow_config.bitstream_id.hex())
            else:
                logger.info("device does not have flashed bitstream")

            new_bitstream = b""
            if args.remove_bitstream:
                logger.info("removing bitstream")
                glasgow_config.bitstream_size = 0
                glasgow_config.bitstream_id = b"\x00" * 16
            elif args.bitstream:
                logger.info("using bitstream from %s", args.bitstream.name)
                with args.bitstream as f:
                    new_bitstream = f.read()
                    glasgow_config.bitstream_size = len(new_bitstream)
                    glasgow_config.bitstream_id = b"\xff" * 16
            elif args.applet:
                logger.info("building bitstream for applet %s", args.applet)
                target, applet = _applet(device.revision, args)
                plan = target.build_plan()
                new_bitstream_id = plan.bitstream_id
                new_bitstream = plan.execute()

                # We always build and reflash the bitstream in case the one currently
                # in EEPROM is corrupted. If we only compared the ID, there would be
                # no easy way to recover from that case. There's also no point in
                # storing the bitstream hash (as opposed to Verilog hash) in the ID,
                # as building the bitstream takes much longer than flashing it.
                logger.info("built bitstream ID %s", new_bitstream_id.hex())
                glasgow_config.bitstream_size = len(new_bitstream)
                glasgow_config.bitstream_id = new_bitstream_id

            fx2_config.firmware[0] = (0x4000 - GlasgowConfig.size,
                                      glasgow_config.encode())

            if args.remove_firmware:
                logger.info("removing firmware")
                fx2_config.disconnect = False
                new_image = fx2_config.encode()
                new_image[0] = 0xC0  # see below
            else:
                logger.info(
                    "using firmware from %r",
                    args.firmware.name if args.firmware else firmware_filename)
                with (args.firmware or open(firmware_filename, "rb")) as f:
                    for (addr, chunk) in input_data(f, fmt="ihex"):
                        fx2_config.append(addr, chunk)
                fx2_config.disconnect = True
                new_image = fx2_config.encode()

            if new_bitstream:
                logger.info("programming bitstream")
                old_bitstream = await device.read_eeprom(
                    "ice", 0, len(new_bitstream))
                if old_bitstream != new_bitstream:
                    for (addr, chunk) in diff_data(old_bitstream,
                                                   new_bitstream):
                        await device.write_eeprom("ice", addr, chunk)

                    logger.info("verifying bitstream")
                    if await device.read_eeprom(
                            "ice", 0, len(new_bitstream)) != new_bitstream:
                        logger.critical("bitstream programming failed")
                        return 1
                else:
                    logger.info("bitstream identical")

            logger.info("programming configuration and firmware")
            old_image = await device.read_eeprom("fx2", 0, len(new_image))
            if old_image != new_image:
                for (addr, chunk) in diff_data(old_image, new_image):
                    await device.write_eeprom("fx2", addr, chunk)

                logger.info("verifying configuration and firmware")
                if await device.read_eeprom("fx2", 0,
                                            len(new_image)) != new_image:
                    logger.critical(
                        "configuration/firmware programming failed")
                    return 1
            else:
                logger.info("configuration and firmware identical")

        if args.action == "build":
            target, applet = _applet(args.rev, args)
            plan = target.build_plan()
            if args.type in ("il", "rtlil"):
                logger.info("building RTLIL for applet %r", args.applet)
                with open(args.filename or args.applet + ".il", "wt") as f:
                    f.write(plan.rtlil)
            if args.type in ("bin", "bitstream"):
                logger.info("building bitstream for applet %r", args.applet)
                with open(args.filename or args.applet + ".bin", "wb") as f:
                    f.write(plan.execute())
            if args.type in ("zip", "archive"):
                logger.info("building archive for applet %r", args.applet)
                plan.archive(args.filename or args.applet + ".zip")

        if args.action == "test":
            logger.info("testing applet %r", args.applet)
            applet = GlasgowApplet.all_applets[args.applet]()
            loader = unittest.TestLoader()
            stream = unittest.runner._WritelnDecorator(sys.stderr)
            result = unittest.TextTestResult(stream=stream,
                                             descriptions=True,
                                             verbosity=2)
            result.failfast = True

            def startTest(test):
                unittest.TextTestResult.startTest(result, test)
                result.stream.write("\n")

            result.startTest = startTest
            if args.tests == []:
                suite = loader.loadTestsFromTestCase(applet.test_cls)
                suite.run(result)
            else:
                for test in args.tests:
                    suite = loader.loadTestsFromName(test,
                                                     module=applet.test_cls)
                    suite.run(result)
            if not result.wasSuccessful():
                for _, traceback in result.errors + result.failures:
                    print(traceback, end="", file=sys.stderr)
                return 1

        if args.action == "factory":
            logger.info("reading device configuration")
            header = await device.read_eeprom("fx2", 0,
                                              8 + 4 + GlasgowConfig.size)
            if not re.match(rb"^\xff+$", header):
                if args.force:
                    logger.warning(
                        "device already factory-programmed, proceeding anyway")
                else:
                    logger.error("device already factory-programmed")
                    return 1

            fx2_config = FX2Config(vendor_id=VID_QIHW,
                                   product_id=PID_GLASGOW,
                                   device_id=GlasgowConfig.encode_revision(
                                       args.rev),
                                   i2c_400khz=True)
            glasgow_config = GlasgowConfig(args.factory_rev,
                                           args.factory_serial)
            fx2_config.append(0x4000 - GlasgowConfig.size,
                              glasgow_config.encode())

            image = fx2_config.encode()
            # Let FX2 hardware enumerate. This won't load the configuration block
            # into memory automatically, but the firmware has code that does that
            # if it detects a C0 load.
            image[0] = 0xC0

            logger.info("programming device configuration")
            await device.write_eeprom("fx2", 0, image)

            logger.info("verifying device configuration")
            if await device.read_eeprom("fx2", 0, len(image)) != image:
                logger.critical("factory programming failed")
                return 1

    except GlasgowDeviceError as e:
        logger.error(e)
        return 1

    except GatewareBuildError as e:
        applet.logger.error(e)
        return 1

    finally:
        if device is not None:
            device.close()

    return 0
Ejemplo n.º 30
0
async def _main():
    args = get_argparser().parse_args()
    create_logger(args)

    try:
        firmware_file = os.path.join(os.path.dirname(__file__), "glasgow.ihex")
        if args.action in ("build", "test", "tool"):
            pass
        elif args.action == "factory":
            device = GlasgowHardwareDevice(firmware_file, VID_CYPRESS, PID_FX2)
        else:
            device = GlasgowHardwareDevice(firmware_file)

        if args.action == "voltage":
            if args.voltage is not None:
                await device.reset_alert(args.ports)
                await device.poll_alert()  # clear any remaining alerts
                try:
                    await device.set_voltage(args.ports, args.voltage)
                except:
                    await device.set_voltage(args.ports, 0.0)
                    raise
                if args.set_alert and args.voltage != 0.0:
                    await asyncio.sleep(
                        0.050)  # let the output capacitor discharge a bit
                    await device.set_alert_tolerance(args.ports, args.voltage,
                                                     args.tolerance / 100)

            print("Port\tVio\tVlimit\tVsense\tMonitor")
            alerts = await device.poll_alert()
            for port in args.ports:
                vio = await device.get_voltage(port)
                vlimit = await device.get_voltage_limit(port)
                vsense = await device.measure_voltage(port)
                alert = await device.get_alert(port)
                notice = ""
                if port in alerts:
                    notice += " (ALERT)"
                print("{}\t{:.2}\t{:.2}\t{:.3}\t{:.2}-{:.2}\t{}".format(
                    port, vio, vlimit, vsense, alert[0], alert[1], notice))

        if args.action == "voltage-limit":
            if args.voltage is not None:
                await device.set_voltage_limit(args.ports, args.voltage)

            print("Port\tVio\tVlimit")
            for port in args.ports:
                vio = await device.get_voltage(port)
                vlimit = await device.get_voltage_limit(port)
                print("{}\t{:.2}\t{:.2}".format(port, vio, vlimit))

        if args.action == "run":
            if args.applet:
                target, applet = _applet(device.revision, args)
                device.demultiplexer = DirectDemultiplexer(device)

                await device.download_target(
                    target,
                    rebuild=args.rebuild,
                    toolchain_opts=_toolchain_opts(args))

                if args.trace:
                    logger.info("starting applet analyzer")
                    await device.write_register(target.analyzer.addr_done, 0)
                    analyzer_iface = await device.demultiplexer.claim_interface(
                        target.analyzer,
                        target.analyzer.mux_interface,
                        args=None)
                    trace_decoder = TraceDecoder(target.analyzer.event_sources)
                    vcd_writer = VCDWriter(
                        args.trace,
                        timescale="1 ns",
                        check_values=False,
                        comment='Generated by Glasgow for bitstream ID %s' %
                        target.get_bitstream_id().hex())

                async def run_analyzer():
                    if not args.trace:
                        return

                    signals = {}
                    strobes = set()
                    for field_name, field_trigger, field_width in trace_decoder.events(
                    ):
                        if field_trigger == "throttle":
                            var_type = "wire"
                            var_init = 0
                        elif field_trigger == "change":
                            var_type = "wire"
                            var_init = "x"
                        elif field_trigger == "strobe":
                            if field_width > 0:
                                var_type = "tri"
                                var_init = "z"
                            else:
                                var_type = "event"
                                var_init = ""
                        else:
                            assert False
                        signals[field_name] = vcd_writer.register_var(
                            scope="",
                            name=field_name,
                            var_type=var_type,
                            size=field_width,
                            init=var_init)
                        if field_trigger == "strobe":
                            strobes.add(field_name)

                    init = True
                    while not trace_decoder.is_done():
                        trace_decoder.process(await analyzer_iface.read())
                        for cycle, events in trace_decoder.flush():
                            if events == "overrun":
                                target.analyzer.logger.error(
                                    "FIFO overrun, shutting down")

                                for name in signals:
                                    vcd_writer.change(signals[name],
                                                      next_timestamp, "x")
                                timestamp += 1e3  # 1us
                                break

                            event_repr = " ".join("{}={}".format(n, v)
                                                  for n, v in events.items())
                            target.analyzer.logger.trace(
                                "cycle %d: %s", cycle, event_repr)

                            timestamp = 1e9 * (cycle +
                                               0) // target.sys_clk_freq
                            next_timestamp = 1e9 * (cycle +
                                                    1) // target.sys_clk_freq
                            if init:
                                init = False
                                vcd_writer._timestamp = timestamp
                            for name, value in events.items():
                                vcd_writer.change(signals[name], timestamp,
                                                  value)
                            for name, _value in events.items():
                                if name in strobes:
                                    vcd_writer.change(signals[name],
                                                      next_timestamp, "z")
                            vcd_writer.flush()

                    vcd_writer.close(timestamp)

                async def run_applet():
                    logger.info("running handler for applet %r", args.applet)
                    try:
                        iface = await applet.run(device, args)
                        await applet.interact(device, args, iface)
                    except GlasgowAppletError as e:
                        applet.logger.error(str(e))
                    finally:
                        if args.trace:
                            await device.write_register(
                                target.analyzer.addr_done, 1)

                done, pending = await asyncio.wait(
                    [run_analyzer(), run_applet()],
                    return_when=asyncio.FIRST_EXCEPTION)
                for task in done:
                    await task

                # Work around bugs in python-libusb1 that cause segfaults on interpreter shutdown.
                await device.demultiplexer.flush()

            else:
                with args.bitstream as f:
                    logger.info("downloading bitstream from %r", f.name)
                    await device.download_bitstream(f.read())

        if args.action == "tool":
            tool = GlasgowApplet.all_applets[args.applet].tool_cls()
            await tool.run(args)

        if args.action == "flash":
            logger.info("reading device configuration")
            header = await device.read_eeprom("fx2", 0,
                                              8 + 4 + GlasgowConfig.size)
            header[0] = 0xC2  # see below

            fx2_config = FX2Config.decode(header, partial=True)
            if (len(fx2_config.firmware) != 1
                    or fx2_config.firmware[0][0] != 0x4000 - GlasgowConfig.size
                    or len(fx2_config.firmware[0][1]) != GlasgowConfig.size):
                raise SystemExit(
                    "Unrecognized or corrupted configuration block")
            glasgow_config = GlasgowConfig.decode(fx2_config.firmware[0][1])

            logger.info("device has serial %s-%s", glasgow_config.revision,
                        glasgow_config.serial)
            if fx2_config.disconnect:
                logger.info("device has flashed firmware")
            else:
                logger.info("device does not have flashed firmware")
            if glasgow_config.bitstream_size:
                logger.info("device has flashed bitstream ID %s",
                            glasgow_config.bitstream_id.hex())
            else:
                logger.info("device does not have flashed bitstream")

            new_bitstream = b""
            if args.remove_bitstream:
                logger.info("removing bitstream")
                glasgow_config.bitstream_size = 0
                glasgow_config.bitstream_id = b"\x00" * 16
            elif args.bitstream:
                logger.info("using bitstream from %s", args.bitstream.name)
                with args.bitstream as f:
                    new_bitstream = f.read()
                    glasgow_config.bitstream_size = len(new_bitstream)
                    glasgow_config.bitstream_id = b"\xff" * 16
            elif args.applet:
                logger.info("building bitstream for applet %s", args.applet)
                target, applet = _applet(device.revision, args)
                new_bitstream_id = target.get_bitstream_id()
                new_bitstream = target.get_bitstream(**_toolchain_opts(args))

                # We always build and reflash the bitstream in case the one currently
                # in EEPROM is corrupted. If we only compared the ID, there would be
                # no easy way to recover from that case. There's also no point in
                # storing the bitstream hash (as opposed to Verilog hash) in the ID,
                # as building the bitstream takes much longer than flashing it.
                logger.info("built bitstream ID %s", new_bitstream_id.hex())
                glasgow_config.bitstream_size = len(new_bitstream)
                glasgow_config.bitstream_id = new_bitstream_id

            fx2_config.firmware[0] = (0x4000 - GlasgowConfig.size,
                                      glasgow_config.encode())

            if args.remove_firmware:
                logger.info("removing firmware")
                fx2_config.disconnect = False
                new_image = fx2_config.encode()
                new_image[0] = 0xC0  # see below
            else:
                logger.info(
                    "using firmware from %r",
                    args.firmware.name if args.firmware else firmware_file)
                with (args.firmware or open(firmware_file, "rb")) as f:
                    for (addr, chunk) in input_data(f, fmt="ihex"):
                        fx2_config.append(addr, chunk)
                fx2_config.disconnect = True
                new_image = fx2_config.encode()

            if new_bitstream:
                logger.info("programming bitstream")
                old_bitstream = await device.read_eeprom(
                    "ice", 0, len(new_bitstream))
                if old_bitstream != new_bitstream:
                    for (addr, chunk) in diff_data(old_bitstream,
                                                   new_bitstream):
                        await device.write_eeprom("ice", addr, chunk)

                    logger.info("verifying bitstream")
                    if await device.read_eeprom(
                            "ice", 0, len(new_bitstream)) != new_bitstream:
                        logger.critical("bitstream programming failed")
                        return 1
                else:
                    logger.info("bitstream identical")

            logger.info("programming configuration and firmware")
            old_image = await device.read_eeprom("fx2", 0, len(new_image))
            if old_image != new_image:
                for (addr, chunk) in diff_data(old_image, new_image):
                    await device.write_eeprom("fx2", addr, chunk)

                logger.info("verifying configuration and firmware")
                if await device.read_eeprom("fx2", 0,
                                            len(new_image)) != new_image:
                    logger.critical(
                        "configuration/firmware programming failed")
                    return 1
            else:
                logger.info("configuration and firmware identical")

        if args.action == "build":
            target, applet = _applet(args.rev, args)
            if args.type in ("v", "verilog"):
                logger.info("building Verilog for applet %r", args.applet)
                target.get_verilog().write(args.filename or args.applet + ".v")
            if args.type in ("bin", "bitstream"):
                logger.info("building bitstream for applet %r", args.applet)
                with open(args.filename or args.applet + ".bin", "wb") as f:
                    f.write(target.get_bitstream(**_toolchain_opts(args)))
            if args.type in ("zip", "archive"):
                logger.info("building archive for applet %r", args.applet)
                with target.get_build_tree() as tree:
                    if args.filename:
                        basename, = os.path.splitext(args.filename)
                    else:
                        basename = args.applet
                    shutil.make_archive(basename,
                                        format="zip",
                                        root_dir=tree,
                                        logger=logger)

        if args.action == "test":
            logger.info("testing applet %r", args.applet)
            applet = GlasgowApplet.all_applets[args.applet]()
            loader = unittest.TestLoader()
            stream = unittest.runner._WritelnDecorator(sys.stderr)
            result = unittest.TextTestResult(stream=stream,
                                             descriptions=True,
                                             verbosity=2)
            result.failfast = True

            def startTest(test):
                unittest.TextTestResult.startTest(result, test)
                result.stream.write("\n")

            result.startTest = startTest
            if args.tests == []:
                suite = loader.loadTestsFromTestCase(applet.test_cls)
                suite.run(result)
            else:
                for test in args.tests:
                    suite = loader.loadTestsFromName(test,
                                                     module=applet.test_cls)
                    suite.run(result)
            if not result.wasSuccessful():
                for _, traceback in result.errors + result.failures:
                    print(traceback, end="", file=sys.stderr)
                return 1

        if args.action == "factory":
            logger.info("reading device configuration")
            header = await device.read_eeprom("fx2", 0,
                                              8 + 4 + GlasgowConfig.size)
            if not re.match(rb"^\xff+$", header):
                if args.force:
                    logger.warning(
                        "device already factory-programmed, proceeding anyway")
                else:
                    logger.error("device already factory-programmed")
                    return 1

            fx2_config = FX2Config(vendor_id=VID_QIHW,
                                   product_id=PID_GLASGOW,
                                   device_id=1 + ord(args.rev) - ord('A'),
                                   i2c_400khz=True)
            glasgow_config = GlasgowConfig(args.rev, args.serial)
            fx2_config.append(0x4000 - GlasgowConfig.size,
                              glasgow_config.encode())

            image = fx2_config.encode()
            # Let FX2 hardware enumerate. This won't load the configuration block
            # into memory automatically, but the firmware has code that does that
            # if it detects a C0 load.
            image[0] = 0xC0

            logger.info("programming device configuration")
            await device.write_eeprom("fx2", 0, image)

            logger.info("verifying device configuration")
            if await device.read_eeprom("fx2", 0, len(image)) != image:
                logger.critical("factory programming failed")
                return 1

    except GlasgowDeviceError as e:
        logger.error(e)
        return 1

    except GatewareBuildError as e:
        applet.logger.error(e)
        return 1

    return 0