Skip to content

Commit

Permalink
Perform an initial checkout if library is missing
Browse files Browse the repository at this point in the history
As mentioned in #603, it is tempting to check in the `fusesoc.conf`-file
into a version control system. This way all the necessary libraries are
written down explicitly and changes are tracked as well. Note, that this
is not the intended workflow, but it _almost_ works with fusesoc version
2.4.2, except for one usecase:
in a clean checkout of a repository, there is the `fusesoc.conf`, but
crucially not the `fusesoc_library`. In this case, almost all `fusesoc`-
commands will fail with an error like this:
```shell
$ ls fusesoc_libraries fusesoc.conf
ls: cannot access 'fusesoc_libraries': No such file or directory
fusesoc.conf
$ cat fusesoc.conf
[library.vlog_tb_utils]
location = fusesoc_libraries/vlog_tb_utils
sync-uri = https://github.com/fusesoc/vlog_tb_utils
sync-type = git
auto-sync = true

$ fusesoc library update
WARNING: Failed to register library 'fusesoc_libraries/vlog_tb_utils is not a directory'
```
When looking into the code, the error can be traced to a FIXME, which
exactly describes the initialization of missing libraries that this com-
mit tries to add.

All it does is to move th check for a non-existing directory downwards
after there is the correct provider. If the path doesn't exist, it tries
to initialize the library using said provider, failing with the old
error output as before this commit. This way, missing libraries get
initialized gracefully and the usecase described above is supported.

The new testcase is simple: it creates an empty library directory, which
gets updated. Since the core directory does not exist, an initial check-
out should be performed.
  • Loading branch information
jfrimmel authored and olofk committed Jan 16, 2025
1 parent da35177 commit b604f53
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
12 changes: 10 additions & 2 deletions fusesoc/fusesoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ def _register_libraries(self):
try:
self.add_library(library)
except (RuntimeError, OSError) as e:
_s = "Failed to register library '{}'"
logger.warning(_s.format(str(e)))
try:
temporary_lm = LibraryManager(self.config.library_root)
# try to initialize library
temporary_lm.add_library(library)
temporary_lm.update([library.name])
# the initialization worked, now register it properly
self.add_library(library)
except (RuntimeError, OSError) as e:
_s = "Failed to register library '{}'"
logger.warning(_s.format(str(e)))

@staticmethod
def init_logging(verbose, monochrome, log_file=None):
Expand Down
17 changes: 12 additions & 5 deletions fusesoc/librarymanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,23 @@ def l(s):
logger.info(l("sync-type is local. Ignoring update"))
return

# FIXME: Do an initial checkout if missing
if not os.path.exists(self.location):
logger.warning(l(f"{self.location} does not exist. Ignoring update"))
return

if not (self.auto_sync or force):
logger.info(l("auto-sync disabled. Ignoring update"))
return

provider = get_provider(self.sync_type)

if not os.path.exists(self.location):
logger.info(l(f"{self.location} does not exist. Trying a checkout"))
try:
provider.init_library(self)
except RuntimeError as e:
# Keep old behavior of logging a warning if there is a library
# in `fusesoc.conf`, but the directory does not exist for some
# reason and it could not be initialized.
logger.warning(l(f"{self.location} does not exist. Ignoring update"))
return

try:
logger.info(l("Updating..."))
provider.update_library(self)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,36 @@ def test_library_update(caplog):
fs.update_libraries([])

assert "test_lib : sync-type is local. Ignoring update" in caplog.text


def test_library_update_with_initialize(caplog):
with tempfile.TemporaryDirectory() as library:

with tempfile.NamedTemporaryFile(mode="w+") as tcf:
tcf.write(
f"""[main]
library_root = {library}
[library.vlog_tb_utils]
location = fusesoc_libraries/vlog_tb_utils
sync-uri = https://github.com/fusesoc/vlog_tb_utils
sync-type = git
auto-sync = true
"""
)
tcf.flush()

conf = Config(tcf.name)

args = Namespace()

Fusesoc.init_logging(False, False)
fs = Fusesoc(conf)

with caplog.at_level(logging.INFO):
fs.update_libraries([])

assert "vlog_tb_utils does not exist. Trying a checkout" in caplog.text
assert "Cloning library into fusesoc_libraries/vlog_tb_utils" in caplog.text
assert "Updating..." in caplog.text

0 comments on commit b604f53

Please sign in to comment.