Skip to content

Commit

Permalink
update: add update.name-cache, update.path-cache config options
Browse files Browse the repository at this point in the history
These set default values of the --name-cache and --path-cache command
line options, respectively.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
  • Loading branch information
mbolivar-nordic committed Apr 7, 2021
1 parent 1176402 commit 4351f01
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
33 changes: 20 additions & 13 deletions src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,10 @@ def init_state(self, args):

self.narrow = args.narrow or config.getboolean('update', 'narrow',
fallback=False)
self.path_cache = args.path_cache or config.get('update', 'path-cache',
fallback=None)
self.name_cache = args.name_cache or config.get('update', 'name-cache',
fallback=None)

self.group_filter: List[str] = []

Expand Down Expand Up @@ -1157,26 +1161,29 @@ def init_project(self, project):
def project_cache(self, project):
# Find the absolute path to a pre-existing local clone of a project
# and return it. If the search fails, return None.
name_cache, path_cache = self.args.name_cache, self.args.path_cache

if name_cache is not None:
maybe = Path(name_cache) / project.name
if self.name_cache is not None:
maybe = Path(self.name_cache) / project.name
if maybe.is_dir():
log.dbg(f'found {project.name} in --name-cache {name_cache}',
level=log.VERBOSE_VERY)
log.dbg(
f'found {project.name} in --name-cache {self.name_cache}',
level=log.VERBOSE_VERY)
return os.fspath(maybe)
else:
log.dbg(f'{project.name} not in --name-cache {name_cache}',
level=log.VERBOSE_VERY)
elif self.args.path_cache is not None:
maybe = Path(self.args.path_cache) / project.path
log.dbg(
f'{project.name} not in --name-cache {self.name_cache}',
level=log.VERBOSE_VERY)
elif self.path_cache is not None:
maybe = Path(self.path_cache) / project.path
if maybe.is_dir():
log.dbg(f'found {project.path} in --path-cache {path_cache}',
level=log.VERBOSE_VERY)
log.dbg(
f'found {project.path} in --path-cache {self.path_cache}',
level=log.VERBOSE_VERY)
return os.fspath(maybe)
else:
log.dbg(f'{project.path} not in --path-cache {path_cache}',
level=log.VERBOSE_VERY)
log.dbg(
f'{project.path} not in --path-cache {self.path_cache}',
level=log.VERBOSE_VERY)

return None

Expand Down
60 changes: 40 additions & 20 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,47 +969,67 @@ def test_update_name_cache(tmpdir):
# network if it doesn't have to.

name_cache_dir = tmpdir / 'name_cache'

create_repo(name_cache_dir / 'foo')
create_repo(name_cache_dir / 'bar')

foo_head = rev_parse(name_cache_dir / 'foo', 'HEAD')
bar_head = rev_parse(name_cache_dir / 'bar', 'HEAD')

workspace = tmpdir / 'workspace'
setup_cache_workspace(workspace, foo_head, bar_head)

cmd(f'update --name-cache {name_cache_dir}', cwd=workspace)

assert (workspace / 'subdir' / 'foo').check(dir=1)
assert (workspace / 'bar').check(dir=1)

assert rev_parse(workspace / 'subdir' / 'foo', 'HEAD') == foo_head
assert rev_parse(workspace / 'bar', 'HEAD') == bar_head
workspace.chdir()
foo = workspace / 'subdir' / 'foo'
bar = workspace / 'bar'

# Test the command line option.
cmd(f'update --name-cache {name_cache_dir}')
assert foo.check(dir=1)
assert bar.check(dir=1)
assert rev_parse(foo, 'HEAD') == foo_head
assert rev_parse(bar, 'HEAD') == bar_head

# Delete the repositories and test the configuration option.
shutil.rmtree(foo)
shutil.rmtree(bar)
cmd(f'config update.name-cache {name_cache_dir}')
cmd('update')
assert foo.check(dir=1)
assert bar.check(dir=1)
assert rev_parse(foo, 'HEAD') == foo_head
assert rev_parse(bar, 'HEAD') == bar_head


def test_update_path_cache(tmpdir):
# Test that 'west update --path-cache' works and doesn't hit the
# network if it doesn't have to.

path_cache_dir = tmpdir / 'path_cache_dir'

create_repo(path_cache_dir / 'subdir' / 'foo')
create_repo(path_cache_dir / 'bar')

foo_head = rev_parse(path_cache_dir / 'subdir' / 'foo', 'HEAD')
bar_head = rev_parse(path_cache_dir / 'bar', 'HEAD')

workspace = tmpdir / 'workspace'
setup_cache_workspace(workspace, foo_head, bar_head)

cmd(f'update --path-cache {path_cache_dir}', cwd=workspace)

assert (workspace / 'subdir' / 'foo').check(dir=1)
assert (workspace / 'bar').check(dir=1)

assert rev_parse(workspace / 'subdir' / 'foo', 'HEAD') == foo_head
assert rev_parse(workspace / 'bar', 'HEAD') == bar_head
workspace.chdir()
foo = workspace / 'subdir' / 'foo'
bar = workspace / 'bar'

# Test the command line option.
cmd(f'update --path-cache {path_cache_dir}')
assert foo.check(dir=1)
assert bar.check(dir=1)
assert rev_parse(foo, 'HEAD') == foo_head
assert rev_parse(foo, 'HEAD') == bar_head

# Delete the repositories and test the configuration option.
shutil.rmtree(foo)
shutil.rmtree(bar)
cmd(f'config update.path-cache {path_cache_dir}')
cmd('update')
assert foo.check(dir=1)
assert bar.check(dir=1)
assert rev_parse(foo, 'HEAD') == foo_head
assert rev_parse(bar, 'HEAD') == bar_head


def setup_narrow(tmpdir):
Expand Down

0 comments on commit 4351f01

Please sign in to comment.