Skip to content

Commit b604f53

Browse files
jfrimmelolofk
authored andcommitted
Perform an initial checkout if library is missing
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.
1 parent da35177 commit b604f53

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

fusesoc/fusesoc.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ def _register_libraries(self):
3737
try:
3838
self.add_library(library)
3939
except (RuntimeError, OSError) as e:
40-
_s = "Failed to register library '{}'"
41-
logger.warning(_s.format(str(e)))
40+
try:
41+
temporary_lm = LibraryManager(self.config.library_root)
42+
# try to initialize library
43+
temporary_lm.add_library(library)
44+
temporary_lm.update([library.name])
45+
# the initialization worked, now register it properly
46+
self.add_library(library)
47+
except (RuntimeError, OSError) as e:
48+
_s = "Failed to register library '{}'"
49+
logger.warning(_s.format(str(e)))
4250

4351
@staticmethod
4452
def init_logging(verbose, monochrome, log_file=None):

fusesoc/librarymanager.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,23 @@ def l(s):
5050
logger.info(l("sync-type is local. Ignoring update"))
5151
return
5252

53-
# FIXME: Do an initial checkout if missing
54-
if not os.path.exists(self.location):
55-
logger.warning(l(f"{self.location} does not exist. Ignoring update"))
56-
return
57-
5853
if not (self.auto_sync or force):
5954
logger.info(l("auto-sync disabled. Ignoring update"))
6055
return
6156

6257
provider = get_provider(self.sync_type)
58+
59+
if not os.path.exists(self.location):
60+
logger.info(l(f"{self.location} does not exist. Trying a checkout"))
61+
try:
62+
provider.init_library(self)
63+
except RuntimeError as e:
64+
# Keep old behavior of logging a warning if there is a library
65+
# in `fusesoc.conf`, but the directory does not exist for some
66+
# reason and it could not be initialized.
67+
logger.warning(l(f"{self.location} does not exist. Ignoring update"))
68+
return
69+
6370
try:
6471
logger.info(l("Updating..."))
6572
provider.update_library(self)

tests/test_libraries.py

+33
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,36 @@ def test_library_update(caplog):
214214
fs.update_libraries([])
215215

216216
assert "test_lib : sync-type is local. Ignoring update" in caplog.text
217+
218+
219+
def test_library_update_with_initialize(caplog):
220+
with tempfile.TemporaryDirectory() as library:
221+
222+
with tempfile.NamedTemporaryFile(mode="w+") as tcf:
223+
tcf.write(
224+
f"""[main]
225+
library_root = {library}
226+
227+
[library.vlog_tb_utils]
228+
location = fusesoc_libraries/vlog_tb_utils
229+
sync-uri = https://github.com/fusesoc/vlog_tb_utils
230+
sync-type = git
231+
auto-sync = true
232+
233+
"""
234+
)
235+
tcf.flush()
236+
237+
conf = Config(tcf.name)
238+
239+
args = Namespace()
240+
241+
Fusesoc.init_logging(False, False)
242+
fs = Fusesoc(conf)
243+
244+
with caplog.at_level(logging.INFO):
245+
fs.update_libraries([])
246+
247+
assert "vlog_tb_utils does not exist. Trying a checkout" in caplog.text
248+
assert "Cloning library into fusesoc_libraries/vlog_tb_utils" in caplog.text
249+
assert "Updating..." in caplog.text

0 commit comments

Comments
 (0)