Skip to content

Commit

Permalink
WestCommand: lift die_if_no_git from project.py
Browse files Browse the repository at this point in the history
This allows us to reuse the same '_git' attribute already
added to WestCommand.git_version_info.

Signed-off-by: Martí Bolívar <[email protected]>
  • Loading branch information
mbolivar-nordic committed Apr 7, 2021
1 parent 54156b5 commit 7983cba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
22 changes: 7 additions & 15 deletions src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,7 @@
'''West project commands'''

import argparse
from functools import partial, lru_cache
from functools import partial
import logging
import os
from os.path import abspath, relpath, exists
Expand Down Expand Up @@ -188,7 188,7 @@ def do_run(self, args, ignored):
if args.local and (args.manifest_url or args.manifest_rev):
log.die('-l cannot be combined with -m or --mr')

die_if_no_git()
self.die_if_no_git()

self._setup_logging(args)

Expand Down Expand Up @@ -447,7 447,7 @@ def do_add_parser(self, parser_adder):

def do_run(self, args, user_args):
def sha_thunk(project):
die_if_no_git()
self.die_if_no_git()

if not project.is_cloned():
log.die(f'cannot get sha for uncloned project {project.name}; '
Expand All @@ -458,7 458,7 @@ def sha_thunk(project):
return f'{"N/A":40}'

def cloned_thunk(project):
die_if_no_git()
self.die_if_no_git()

return "cloned" if project.is_cloned() else "not-cloned"

Expand Down Expand Up @@ -638,7 638,7 @@ def do_add_parser(self, parser_adder):
return parser

def do_run(self, args, ignored):
die_if_no_git()
self.die_if_no_git()
self._setup_logging(args)

failed = []
Expand Down Expand Up @@ -684,7 684,7 @@ def do_add_parser(self, parser_adder):
return parser

def do_run(self, args, user_args):
die_if_no_git()
self.die_if_no_git()
self._setup_logging(args)

failed = []
Expand Down Expand Up @@ -769,7 769,7 @@ def do_add_parser(self, parser_adder):
return parser

def do_run(self, args, user_args):
die_if_no_git()
self.die_if_no_git()
self._setup_logging(args)

self.args = args
Expand Down Expand Up @@ -1470,14 1470,6 @@ def die_unknown(unknown):
log.die(f'unknown project name{s}/path{s}: {names}\n'
' Hint: use "west list" to list all projects.')

@lru_cache(maxsize=1)
def die_if_no_git():
# Using an LRU cache means this only calls shutil.which() once.
# This is useful when the function is called multiple times, e.g.
# from the west list thunk for computing a SHA.
if shutil.which('git') is None:
log.die("can't find git; install it or ensure it's on your PATH")

#
# Special files and directories in the west workspace.
#
Expand Down
13 changes: 9 additions & 4 deletions src/west/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 229,23 @@ def check_output(args, cwd=None):
level=log.VERBOSE_VERY)
return subprocess.check_output(args, cwd=cwd)

def die_if_no_git(self):
'''Abort if git is not installed on PATH.
'''
if not hasattr(self, '_git'):
self._git = shutil.which('git')
if self._git is None:
log.die("can't find git; install it or ensure it's on your PATH")

@property
def git_version_info(self):
'''Returns git version info as a tuple of ints in (major,
minor, patch) format, like (2, 31, 1) for git version 2.31.1.
Aborts the program if there is no git installed.
'''
if not hasattr(self, '_git'):
self._git = shutil.which('git')
if self._git is None:
log.die("can't find git; install it or ensure it's on your PATH")
if not hasattr(self, '_git_ver'):
self.die_if_no_git()
# raw_ver usually looks like '2.31.1'
raw_ver = self.check_output(
[self._git, '--version']).decode().strip().split()[-1]
Expand Down

0 comments on commit 7983cba

Please sign in to comment.