Skip to content

Commit

Permalink
make 'black' and 'flake8' happy at the same time
Browse files Browse the repository at this point in the history
and also 'isort' and 'mypy'
  • Loading branch information
lomnido committed Feb 29, 2024
1 parent 68f3689 commit 245614f
Showing 1 changed file with 68 additions and 70 deletions.
138 changes: 68 additions & 70 deletions tsrc/cli/manifest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
""" Entry point for `tsrc manifest`. """

import argparse
from typing import Dict, List, Union
from pathlib import Path
from typing import Dict, List, Tuple, Union

import cli_ui as ui

from pathlib import Path
from tsrc.cli import (
add_repos_selection_args,
add_workspace_arg,
Expand Down Expand Up @@ -48,58 +48,60 @@ def run(args: argparse.Namespace) -> None:
"""'static' as it cannot be changed (till the 'sync');
"manifest_manifest" = Manifest repo in Manifest.yml;
this may not exist but if it does, it will be the 'dest'intion == 'direcory_name'"""
static_manifest_manifest_dest, static_manifest_manifest_branch = \
is_manifest_in_workspace(repos, workspace.config.manifest_url)
print("DEBUG: type= ", type(statuses))
(
static_manifest_manifest_dest,
static_manifest_manifest_branch,
) = is_manifest_in_workspace(repos, workspace.config.manifest_url)

"""current: Workspace's > Manifest_repo's > branch"""
current_workspace_manifest_repo_branch = None

if static_manifest_manifest_dest:
ui.info_2("Integrated into Workspace as another repository:")

statuses_items = statuses.items()
# statuses_items = statuses.items()
current_workspace_manifest_repo_branch = workspace_integration_summary(
statuses_items,
statuses,
static_manifest_manifest_dest,
static_manifest_manifest_branch,
workspace.config.manifest_branch
workspace.config.manifest_branch,
)

mi = ManifestInfo(
workspace_config,
cfg_path,
workspace.root_path,
statuses_items,
statuses,
args.manifest_branch,
static_manifest_manifest_dest,
static_manifest_manifest_branch,
current_workspace_manifest_repo_branch
current_workspace_manifest_repo_branch,
)

mi.info()


class ManifestInfo:

def __init__(
self,
workspace_config: WorkspaceConfig,
cfg_path: Path,
workspace_root_path: Path,
statuses_items: Dict[str, Union[Status, Exception]],
statuses: Dict[str, Union[Status, Exception]],
set_manifest_branch: str,
static_manifest_manifest_dest: str,
static_manifest_manifest_branch: str,
current_workspace_manifest_repo_branch: str
current_workspace_manifest_repo_branch: str,
):
self.workspace_config = workspace_config
self.w_c = workspace_config
self.cfg_path = cfg_path
self.workspace_root_path = workspace_root_path
self.statuses_items = statuses_items
self.statuses = statuses
self.set_manifest_branch = set_manifest_branch
self.static_manifest_manifest_dest = static_manifest_manifest_dest
self.s_m_m_dest = static_manifest_manifest_dest
self.static_manifest_manifest_branch = static_manifest_manifest_branch
self.current_workspace_manifest_repo_branch = current_workspace_manifest_repo_branch
self.c_w_m_r_branch = current_workspace_manifest_repo_branch

def info(self) -> None:
if self.set_manifest_branch:
Expand All @@ -111,59 +113,57 @@ def on_set_branch(self) -> None:
"""first we need to check if such branch exists in order this to work on 'sync'"""
rc_is_on_remote = -1
found_local_branch = False
if self.static_manifest_manifest_dest:
if self.s_m_m_dest:
rc_is_on_remote, found_local_branch = manifest_branch_exist(
self.workspace_config.manifest_url,
self.workspace_root_path / self.static_manifest_manifest_dest,
self.static_manifest_manifest_dest,
self.set_manifest_branch
self.w_c.manifest_url,
self.workspace_root_path / self.s_m_m_dest,
self.s_m_m_dest,
self.set_manifest_branch,
)
else:
rc_is_on_remote = manifest_remote_branch_exist(
self.workspace_config.manifest_url, self.set_manifest_branch
self.w_c.manifest_url, self.set_manifest_branch
)
if rc_is_on_remote == 0 or found_local_branch:
"""we are good to set new branch"""
self.uip_using_new_branch(self.set_manifest_branch)
if self.workspace_config.manifest_branch == self.set_manifest_branch:
if self.w_c.manifest_branch == self.set_manifest_branch:
self.uip_skip_set_branch()
else:
self.workspace_config.manifest_branch = self.set_manifest_branch
self.workspace_config.save_to_file(self.cfg_path)
self.w_c.manifest_branch = self.set_manifest_branch
self.w_c.save_to_file(self.cfg_path)
"""workspace is now updated"""
self.uip_workspace_updated()
workspace_integration_summary(
self.statuses_items,
self.static_manifest_manifest_dest,
self.statuses,
self.s_m_m_dest,
self.static_manifest_manifest_branch,
self.workspace_config.manifest_branch,
True
self.w_c.manifest_branch,
True,
)
if self.static_manifest_manifest_dest:
if self.s_m_m_dest:
"""when there is Manifest repository in the Workspace"""
if rc_is_on_remote == 0:
if self.current_workspace_manifest_repo_branch != \
self.workspace_config.manifest_branch:
if self.c_w_m_r_branch != self.w_c.manifest_branch:
self.uip_after_sync_branch_change()
else:
self.uip_ok_after_sync_same_branch()
else:
self.uip_push_first_for_sync_to_work()
else:
"""use 'manifest_branch_0' to determine if brach will change"""
if self.workspace_config.manifest_branch != \
self.workspace_config.manifest_branch_0:
if self.w_c.manifest_branch != self.w_c.manifest_branch_0:
self.uip_branch_will_change_after_sync(
self.workspace_config.manifest_branch,
self.workspace_config.manifest_branch_0
self.w_c.manifest_branch,
self.w_c.manifest_branch_0,
)
else:
self.uip_branch_will_stay_the_same_after_sync(
self.workspace_config.manifest_branch
self.w_c.manifest_branch
)
else:
"""branch is nowhere to be found"""
if self.static_manifest_manifest_dest:
if self.s_m_m_dest:
"""when there is Manifest repository in the Workspace"""
self.uie_cannot_set_branch_create_first(self.set_manifest_branch)
else:
Expand All @@ -172,46 +172,42 @@ def on_set_branch(self) -> None:

def on_default_display(self) -> None:
"""just report final status of current state, do not update anything"""
if self.static_manifest_manifest_dest and \
self.current_workspace_manifest_repo_branch != \
self.workspace_config.manifest_branch:
self.uip_branch_will_change_after_sync(self.workspace_config.manifest_branch)
if self.s_m_m_dest and self.c_w_m_r_branch != self.w_c.manifest_branch:
self.uip_branch_will_change_after_sync(self.w_c.manifest_branch)
else:
"""use 'manifest_branch_0' to determine if brach will change"""
if self.workspace_config.manifest_branch != self.workspace_config.manifest_branch_0:
if self.w_c.manifest_branch != self.w_c.manifest_branch_0:
self.uip_branch_will_change_after_sync(
self.workspace_config.manifest_branch,
self.workspace_config.manifest_branch_0
self.w_c.manifest_branch,
self.w_c.manifest_branch_0,
)
else:
self.uip_branch_will_stay_the_same_after_sync(
self.workspace_config.manifest_branch
)
self.uip_branch_will_stay_the_same_after_sync(self.w_c.manifest_branch)

def uip_skip_set_branch(self):
def uip_skip_set_branch(self) -> None:
ui.info_1("Skipping configuring the same branch")

def uip_using_new_branch(self, branch: str):
def uip_using_new_branch(self, branch: str) -> None:
ui.info_2("Using new branch: ", ui.green, branch, ui.reset)

def uip_workspace_updated(self):
def uip_workspace_updated(self) -> None:
ui.info_1("Workspace updated")

def uip_after_sync_branch_change(self):
def uip_after_sync_branch_change(self) -> None:
ui.info_2(
"After 'sync' the branch will",
ui.red,
"(differ)",
ui.reset,
)

def uip_ok_after_sync_same_branch(self):
def uip_ok_after_sync_same_branch(self) -> None:
ui.info_2(
ui.blue,
"OK: After 'sync' the repository will stays on the same branch",
)

def uip_push_first_for_sync_to_work(self):
def uip_push_first_for_sync_to_work(self) -> None:
ui.info_2(
"You need to",
ui.red,
Expand All @@ -224,19 +220,21 @@ def uip_push_first_for_sync_to_work(self):
"to work",
)

def uie_cannot_set_branch_create_first(self, branch: str):
def uie_cannot_set_branch_create_first(self, branch: str) -> None:
ui.error(
f"Cannot set branch '{branch}' as a new branch as it does not exist. "
"You need to create it first."
)

def uie_cannot_set_branch_git_push_first(self, branch: str):
def uie_cannot_set_branch_git_push_first(self, branch: str) -> None:
ui.error(
f"Cannot set branch '{branch}' as a new branch as it does not exist. "
"You need to 'git push' it first."
)

def uip_branch_will_change_after_sync(self, branch: str, branch_0: str = ""):
def uip_branch_will_change_after_sync(
self, branch: str, branch_0: str = ""
) -> None:
message = [
"Currently configured branch on next 'sync' will",
ui.red,
Expand All @@ -253,7 +251,7 @@ def uip_branch_will_change_after_sync(self, branch: str, branch_0: str = ""):
]
ui.info_2(*message)

def uip_branch_will_stay_the_same_after_sync(self, branch: str):
def uip_branch_will_stay_the_same_after_sync(self, branch: str) -> None:
ui.info_2(
"Currently configured branch on next 'sync' will",
ui.blue,
Expand All @@ -269,7 +267,9 @@ def uip_branch_will_stay_the_same_after_sync(self, branch: str):
StatusOrError = Union[Status, Exception]


def is_manifest_in_workspace(repos: List[Repo], workspace_config_manifest_url: str):
def is_manifest_in_workspace(
repos: List[Repo], workspace_config_manifest_url: str
) -> Tuple[str, str]:
static_manifest_manifest_dest = None
static_manifest_manifest_branch = None
for x in repos:
Expand All @@ -283,22 +283,20 @@ def is_manifest_in_workspace(repos: List[Repo], workspace_config_manifest_url: s


def workspace_integration_summary(
statuses_items: Dict[str, StatusOrError],
statuses: Dict[str, StatusOrError],
st_m_m_dest: str,
st_m_m_branch: str,
w_c_m_branch: str,
do_update: bool = False
):
do_update: bool = False,
) -> str:
"""prints a summary of Manifest repository status.
the same output should be used when using 'tsrc status'
but other repositories included"""
cur_w_m_r_branch = None
for dest, status in statuses_items:
for dest, status in statuses.items():
if dest == st_m_m_dest:
if do_update:
ui.info_2(
"Updating configured Manifest branch. See new overall state:"
)
ui.info_2("Updating configured Manifest branch. See new overall state:")
cur_w_m_r_branch = status.git.branch
message = [ui.green, "*", ui.reset, dest]
message += describe_status(status)
Expand All @@ -315,7 +313,7 @@ def workspace_integration_summary(
return cur_w_m_r_branch


def manifest_remote_branch_exist(url: str, branch: str):
def manifest_remote_branch_exist(url: str, branch: str) -> int:
"""check for manifest remote branch, as only if it exist,
we should allow to set it. as you can see, there is
no real path to repository out there"""
Expand All @@ -327,7 +325,7 @@ def manifest_remote_branch_exist(url: str, branch: str):
"--heads",
url,
f"refs/heads/{branch}",
check=False
check=False,
)
return rc

Expand All @@ -336,8 +334,8 @@ def manifest_branch_exist(
manifest_url: str,
full_path: Path,
static_manifest_manifest_dest: str,
provided_branch: str
):
provided_branch: str,
) -> Tuple[int, bool]:
"""if there is Manifest repository in the workspace, we can
check for both remote and local branch if it is exist.
as only if it is exist, we should allow to set it.
Expand Down

0 comments on commit 245614f

Please sign in to comment.