Skip to content

Commit 5e579aa

Browse files
committed
Move query_yes_no implementation to utils.py
This is needed in order to avoid cyclic imports, and it removes a runtime import from DeprecatedTargetAlias.
1 parent 666ddf0 commit 5e579aa

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

Diff for: pycheribuild/config/chericonfig.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ def __init__(self, loader, action_class):
220220
# Work around circular dependencies
221221
from .loader import ConfigLoaderBase
222222
# noinspection PyTypeChecker
223-
super().__init__(pretend=DoNotUseInIfStmt(), verbose=DoNotUseInIfStmt(), quiet=DoNotUseInIfStmt())
223+
super().__init__(pretend=DoNotUseInIfStmt(), verbose=DoNotUseInIfStmt(), quiet=DoNotUseInIfStmt(),
224+
force=DoNotUseInIfStmt())
224225
self._cached_deps = collections.defaultdict(dict)
225226

226227
assert isinstance(loader, ConfigLoaderBase)

Diff for: pycheribuild/projects/project.py

+3-22
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
from ..targets import MultiArchTarget, MultiArchTargetAlias, Target, target_manager
5858
from ..utils import (AnsiColour, cached_property, classproperty, coloured, fatal_error, include_local_file,
5959
InstallInstructions, is_jenkins_build, OSInfo, remove_prefix, replace_one, status_update,
60-
ThreadJoiner, remove_duplicates)
60+
ThreadJoiner, remove_duplicates, query_yes_no)
6161

6262
__all__ = ["Project", "CMakeProject", "AutotoolsProject", "TargetAlias", "TargetAliasWithDependencies", # no-combine
6363
"SimpleProject", "CheriConfig", "flush_stdio", "MakeOptions", "MakeCommandKind", # no-combine
@@ -809,29 +809,10 @@ def add_required_system_header(self, header: str, install_instructions: str = No
809809
apt=apt, homebrew=homebrew, cheribuild_target=cheribuild_target)
810810
self.__required_system_headers[header] = instructions
811811

812-
@staticmethod
813-
def _query_yes_no(config: CheriConfig, message: str = "", *, default_result=False, force_result=True,
814-
yes_no_str: str = None) -> bool:
815-
if yes_no_str is None:
816-
yes_no_str = " [Y]/n " if default_result else " y/[N] "
817-
if config.pretend:
818-
print(message + yes_no_str, coloured(AnsiColour.green, "y" if force_result else "n"), sep="", flush=True)
819-
return force_result # in pretend mode we always return true
820-
if config.force:
821-
# in force mode we always return the forced result without prompting the user
822-
print(message + yes_no_str, coloured(AnsiColour.green, "y" if force_result else "n"), sep="", flush=True)
823-
return force_result
824-
if not sys.__stdin__.isatty():
825-
return default_result # can't get any input -> return the default
826-
result = input(message + yes_no_str)
827-
if default_result:
828-
return not result.startswith("n") # if default is yes accept anything other than strings starting with "n"
829-
return str(result).lower().startswith("y") # anything but y will be treated as false
830-
831812
def query_yes_no(self, message: str = "", *, default_result=False, force_result=True,
832813
yes_no_str: str = None) -> bool:
833-
return self._query_yes_no(self.config, message, default_result=default_result, force_result=force_result,
834-
yes_no_str=yes_no_str)
814+
return query_yes_no(self.config, message, default_result=default_result, force_result=force_result,
815+
yes_no_str=yes_no_str)
835816

836817
def ask_for_confirmation(self, message: str, error_message="Cannot continue.", default_result=True, **kwargs):
837818
if not self.query_yes_no(message, default_result=default_result, **kwargs):

Diff for: pycheribuild/targets.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from .config.chericonfig import CheriConfig
3737
from .config.target_info import CrossCompileTarget
3838
from .processutils import set_env
39-
from .utils import add_error_context, AnsiColour, coloured, fatal_error, status_update, warning_message
39+
from .utils import add_error_context, AnsiColour, coloured, fatal_error, status_update, warning_message, query_yes_no
4040

4141
if typing.TYPE_CHECKING: # no-combine
4242
from .projects.project import SimpleProject # no-combine
@@ -327,9 +327,7 @@ def get_real_target(self, cross_target: typing.Optional[CrossCompileTarget], con
327327
coloured(AnsiColour.magenta, ". Please use "),
328328
coloured(AnsiColour.yellow, self.real_target_name),
329329
coloured(AnsiColour.magenta, " instead."), sep="")
330-
from .projects.project import SimpleProject
331-
# noinspection PyProtectedMember
332-
if not SimpleProject._query_yes_no(config, "Continue?", default_result=True):
330+
if not query_yes_no(config, "Continue?", default_result=True):
333331
fatal_error("Cannot continue.")
334332
return self._real_target
335333

Diff for: pycheribuild/utils.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
# reduce the number of import statements per project # no-combine
4747
__all__ = ["typing", "include_local_file", "Type_T", "init_global_config", # no-combine
48-
"status_update", "fatal_error", "coloured", "AnsiColour", # no-combine
48+
"status_update", "fatal_error", "coloured", "AnsiColour", "query_yes_no", # no-combine
4949
"warning_message", "DoNotUseInIfStmt", "ThreadJoiner", "InstallInstructions", # no-combine
5050
"SafeDict", "error_message", "ConfigBase", "final", "add_error_context", # no-combine
5151
"default_make_jobs_count", "OSInfo", "is_jenkins_build", "get_global_config", # no-combine
@@ -88,16 +88,18 @@ def __len__(self):
8888
class ConfigBase:
8989
TEST_MODE = False
9090

91-
def __init__(self, *, pretend: bool, verbose: bool, quiet: bool):
91+
def __init__(self, *, pretend: bool, verbose: bool, quiet: bool, force: bool):
9292
self.quiet = quiet
9393
self.verbose = verbose
9494
self.pretend = pretend
95+
self.force = force
9596
self.internet_connection_last_checked_at = None # type: typing.Optional[float]
9697
self.internet_connection_last_check_result = False
9798

9899

99100
# noinspection PyTypeChecker
100-
GlobalConfig = ConfigBase(pretend=DoNotUseInIfStmt(), verbose=DoNotUseInIfStmt(), quiet=DoNotUseInIfStmt())
101+
GlobalConfig = ConfigBase(pretend=DoNotUseInIfStmt(), verbose=DoNotUseInIfStmt(), quiet=DoNotUseInIfStmt(),
102+
force=DoNotUseInIfStmt())
101103

102104

103105
def init_global_config(config: ConfigBase, *, test_mode: bool = False):
@@ -266,6 +268,25 @@ def fatal_error(*args, sep=" ", fixit_hint=None, fatal_when_pretending=False, ex
266268
sys.exit(exit_code)
267269

268270

271+
def query_yes_no(config: ConfigBase, message: str = "", *, default_result=False, force_result=True,
272+
yes_no_str: str = None) -> bool:
273+
if yes_no_str is None:
274+
yes_no_str = " [Y]/n " if default_result else " y/[N] "
275+
if config.pretend:
276+
print(message + yes_no_str, coloured(AnsiColour.green, "y" if force_result else "n"), sep="", flush=True)
277+
return force_result # in pretend mode we always return true
278+
if config.force:
279+
# in force mode we always return the forced result without prompting the user
280+
print(message + yes_no_str, coloured(AnsiColour.green, "y" if force_result else "n"), sep="", flush=True)
281+
return force_result
282+
if not sys.__stdin__.isatty():
283+
return default_result # can't get any input -> return the default
284+
result = input(message + yes_no_str)
285+
if default_result:
286+
return not result.startswith("n") # if default is yes accept anything other than strings starting with "n"
287+
return str(result).lower().startswith("y") # anything but y will be treated as false
288+
289+
269290
@functools.lru_cache(maxsize=20)
270291
def include_local_file(path: str) -> str:
271292
file = Path(__file__).parent / path # type: Path

Diff for: vcu118-run.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def main():
416416
pass
417417
args = parser.parse_args()
418418
print(args)
419-
init_global_config(ConfigBase(pretend=args.pretend, verbose=True, quiet=False))
419+
init_global_config(ConfigBase(pretend=args.pretend, verbose=True, quiet=False, force=False))
420420
if (args.action == "all" and args.bitfile is not None) or args.action == "bitfile":
421421
if args.ltxfile is None:
422422
args.ltxfile = Path(args.bitfile).with_suffix(".ltx")

0 commit comments

Comments
 (0)