Skip to content

Commit

Permalink
Add --export-json flag. #347
Browse files Browse the repository at this point in the history
  • Loading branch information
kraigher committed Aug 26, 2018
1 parent 0eb0f3a commit dd5b1c0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
33 changes: 32 additions & 1 deletion vunit/test/unit/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
from string import Template
import os
from os.path import join, dirname, basename, exists, abspath
import json
import re
from re import MULTILINE
from shutil import rmtree
from vunit.ui import VUnit
from vunit.project import VHDL_EXTENSIONS, VERILOG_EXTENSIONS
from vunit.test.mock_2or3 import mock
from vunit.test.common import set_env
from vunit.test.common import set_env, with_tempdir
from vunit.ostools import renew_path
from vunit.builtins import add_verilog_include_dir
from vunit.simulator_interface import SimulatorInterface
Expand Down Expand Up @@ -374,6 +375,36 @@ def test_list_files_flag(self):
lib1, ent0.vhd
Listed 2 files""".splitlines()))

@with_tempdir
def test_export_json(self, tempdir):
json_file = join(tempdir, "export.json")

ui = self._create_ui("--export-json", json_file)
lib1 = ui.add_library("lib1")
lib2 = ui.add_library("lib2")
file_name1 = self.create_entity_file()
file_name2 = self.create_entity_file()
lib1.add_source_file(file_name1)
lib2.add_source_file(file_name2)

self._run_main(ui)

with open(json_file, "r") as fptr:
data = json.load(fptr)

# Check known keys
self.assertEqual(set(data.keys()),
set(["export_format_version", "files"]))

# Check that data format is an integer
self.assertEqual(type(data["export_format_version"]), int)

# Check the contents of the files section
self.assertEqual(set((item["library_name"], item["name"])
for item in data["files"]),
set([("lib1", abspath(file_name1)),
("lib2", abspath(file_name2))]))

def test_library_attributes(self):
ui = self._create_ui()
lib1 = ui.add_library("lib1")
Expand Down
31 changes: 31 additions & 0 deletions vunit/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
import sys
import traceback
import logging
import json
import os
from os.path import exists, abspath, join, basename, splitext, normpath, dirname
from glob import glob
Expand Down Expand Up @@ -790,6 +791,9 @@ def _main(self, post_run):
Base vunit main function without performing exit
"""

if self._args.export_json is not None:
return self._main_export_json(self._args.export_json)

if self._args.list:
return self._main_list_only()

Expand Down Expand Up @@ -858,6 +862,33 @@ def _main_list_only(self):
print("Listed %i tests" % test_list.num_tests())
return True

def _main_export_json(self, file_name):
"""
Main function when exporting to JSON
"""

file_objects = self.get_compile_order()
files = []
for source_file in file_objects:
files.append(dict(name=abspath(source_file.name),
library_name=source_file.library.name))

json_data = dict(
# The version of the JSON export data format
export_format_version=1,

# The set of files added to the project
files=files)

with open(file_name, "w") as fptr:
json.dump(json_data,
fptr,
sort_keys=True,
indent=4,
separators=(',', ': '))

return True

def _main_list_files_only(self):
"""
Main function when only listing files
Expand Down
4 changes: 4 additions & 0 deletions vunit/vunit_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def _create_argument_parser(description=None, for_documentation=False):
nargs="?",
help="Enable code coverage. Works with ModelSim and RivieraPRO.")

parser.add_argument("--export-json",
default=None,
help="Export project information to a JSON file. Used for IDE integration.")

parser.add_argument('--version', action='version', version=version())

SIMULATOR_FACTORY.add_arguments(parser)
Expand Down

0 comments on commit dd5b1c0

Please sign in to comment.