move_mrs: Fix coding style using autopep8

And some small edits for:
- E741 ambiguous variable name 'l'
- E722 do not use bare 'except'
- W504 line break after binary operator

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1017>
This commit is contained in:
Nicolas Dufresne 2021-10-01 15:47:44 -04:00 committed by GStreamer Marge Bot
parent 2e6cd5c2e4
commit 3d25ff10a9

View file

@ -50,7 +50,8 @@ PARSER.add_argument("--skip-branch", action="store", nargs="*",
help="Ignore MRs for branches which match those names.", dest="skipped_branches") help="Ignore MRs for branches which match those names.", dest="skipped_branches")
PARSER.add_argument("--skip-on-failure", action="store_true", default=False) PARSER.add_argument("--skip-on-failure", action="store_true", default=False)
PARSER.add_argument("--dry-run", "-n", action="store_true", default=False) PARSER.add_argument("--dry-run", "-n", action="store_true", default=False)
PARSER.add_argument("--use-branch-if-exists", action="store_true", default=False) PARSER.add_argument("--use-branch-if-exists",
action="store_true", default=False)
PARSER.add_argument( PARSER.add_argument(
"-c", "-c",
"--config-file", "--config-file",
@ -135,6 +136,7 @@ os.environ["GIT_LFS_SKIP_SMUDGE"] = "1"
log_depth = [] # type: T.List[str] log_depth = [] # type: T.List[str]
@contextmanager @contextmanager
def nested(name=''): def nested(name=''):
global log_depth global log_depth
@ -144,18 +146,23 @@ def nested(name=''):
finally: finally:
log_depth.pop() log_depth.pop()
def bold(text: str): def bold(text: str):
return f"\033[1m{text}\033[0m" return f"\033[1m{text}\033[0m"
def green(text: str): def green(text: str):
return f"\033[1;32m{text}\033[0m" return f"\033[1;32m{text}\033[0m"
def red(text: str): def red(text: str):
return f"\033[1;31m{text}\033[0m" return f"\033[1;31m{text}\033[0m"
def yellow(text: str): def yellow(text: str):
return f"\033[1;33m{text}\033[0m" return f"\033[1;33m{text}\033[0m"
def fprint(msg, nested=True): def fprint(msg, nested=True):
if log_depth: if log_depth:
prepend = log_depth[-1] + ' | ' if nested else '' prepend = log_depth[-1] + ' | ' if nested else ''
@ -196,8 +203,8 @@ class GstMRMover:
session = requests.Session() session = requests.Session()
sign_in_page = session.get(SIGN_IN_URL).content.decode() sign_in_page = session.get(SIGN_IN_URL).content.decode()
for l in sign_in_page.split('\n'): for line in sign_in_page.split('\n'):
m = re.search('name="authenticity_token" value="([^"]+)"', l) m = re.search('name="authenticity_token" value="([^"]+)"', line)
if m: if m:
break break
@ -209,7 +216,6 @@ class GstMRMover:
fprint(f"{red('Unable to find the authenticity token')}\n") fprint(f"{red('Unable to find the authenticity token')}\n")
sys.exit(1) sys.exit(1)
for data, url in [ for data, url in [
({'user[login]': 'login_or_email', ({'user[login]': 'login_or_email',
'user[password]': 'SECRET', 'user[password]': 'SECRET',
@ -246,9 +252,10 @@ class GstMRMover:
return subprocess.check_output(["git"] + list(args), cwd=cwd, return subprocess.check_output(["git"] + list(args), cwd=cwd,
stdin=subprocess.DEVNULL, stdin=subprocess.DEVNULL,
stderr=subprocess.STDOUT).decode() stderr=subprocess.STDOUT).decode()
except: except subprocess.CalledProcessError:
if not can_fail: if not can_fail:
fprint(f"\n\n{bold(red('ERROR'))}: `git {' '.join(args)}` failed" + "\n", nested=False) fprint(
f"\n\n{bold(red('ERROR'))}: `git {' '.join(args)}` failed" + "\n", nested=False)
raise raise
else: else:
subprocess.call(["git"] + list(args), cwd=cwd) subprocess.call(["git"] + list(args), cwd=cwd)
@ -292,7 +299,7 @@ class GstMRMover:
if revert_operation: if revert_operation:
self.git(*revert_operation, can_fail=True) self.git(*revert_operation, can_fail=True)
sys.exit(3) sys.exit(3)
except: except Exception:
# Result of subshell does not really matter # Result of subshell does not really matter
pass pass
@ -315,7 +322,8 @@ class GstMRMover:
for m in self.modules: for m in self.modules:
if m not in VALID_PROJECTS: if m not in VALID_PROJECTS:
projects = '\n- '.join(VALID_PROJECTS) projects = '\n- '.join(VALID_PROJECTS)
sys.exit(f"{red(f'Unknown module {m}')}\nModules are:\n- {projects}") sys.exit(
f"{red(f'Unknown module {m}')}\nModules are:\n- {projects}")
if self.mr and len(self.modules) > 1: if self.mr and len(self.modules) > 1:
sys.exit(f"{red(f'Merge request #{self.mr} specified but several modules where specified')}\n\n" sys.exit(f"{red(f'Merge request #{self.mr} specified but several modules where specified')}\n\n"
f"{bold(' -> Use `--module` only once to specify an merge request.')}") f"{bold(' -> Use `--module` only once to specify an merge request.')}")
@ -327,8 +335,9 @@ class GstMRMover:
self.gl.auth() self.gl.auth()
try: try:
prevbranch = self.git("rev-parse", "--abbrev-ref", "HEAD", can_fail=True).strip() prevbranch = self.git(
except: "rev-parse", "--abbrev-ref", "HEAD", can_fail=True).strip()
except Exception:
fprint(bold(yellow("Not on a branch?\n")), indent=False) fprint(bold(yellow("Not on a branch?\n")), indent=False)
prevbranch = None prevbranch = None
@ -341,7 +350,8 @@ class GstMRMover:
self.move_mrs(from_projects, to_project) self.move_mrs(from_projects, to_project)
finally: finally:
if self.git_rename_limit is not None: if self.git_rename_limit is not None:
self.git("config", "merge.renameLimit", str(self.git_rename_limit)) self.git("config", "merge.renameLimit",
str(self.git_rename_limit))
if prevbranch: if prevbranch:
fprint(f'Back to {prevbranch}\n') fprint(f'Back to {prevbranch}\n')
self.git("checkout", prevbranch) self.git("checkout", prevbranch)
@ -356,13 +366,15 @@ class GstMRMover:
if p.namespace['path'] == self.gl.user.username if p.namespace['path'] == self.gl.user.username
and p.name == MONOREPO_NAME] and p.name == MONOREPO_NAME]
except ValueError: except ValueError:
fprint(f"{red(f'ERROR')}\n\nCould not find repository {self.gl.user.name}/{MONOREPO_NAME}") fprint(
f"{red(f'ERROR')}\n\nCould not find repository {self.gl.user.name}/{MONOREPO_NAME}")
fprint(f"{red(f'Got to https://gitlab.freedesktop.org/gstreamer/gstreamer/ and create a fork so we can move your Merge requests.')}") fprint(f"{red(f'Got to https://gitlab.freedesktop.org/gstreamer/gstreamer/ and create a fork so we can move your Merge requests.')}")
sys.exit(1) sys.exit(1)
fprint(f"{green(' OK')}\n", nested=False) fprint(f"{green(' OK')}\n", nested=False)
from_projects = [] from_projects = []
user_projects_name = [proj.name for proj in self.all_projects if proj.namespace['path'] == self.gl.user.username and proj.name in GST_PROJECTS] user_projects_name = [proj.name for proj in self.all_projects if proj.namespace['path']
== self.gl.user.username and proj.name in GST_PROJECTS]
for project, id in GST_PROJECTS_ID.items(): for project, id in GST_PROJECTS_ID.items():
if project not in user_projects_name or project == 'gstreamer': if project not in user_projects_name or project == 'gstreamer':
continue continue
@ -441,9 +453,10 @@ class GstMRMover:
note = discussion.notes.get(note['id']) note = discussion.notes.get(note['id'])
note_url = f"{mr_url}#note_{note.id}" note_url = f"{mr_url}#note_{note.id}"
when = dateparse.parse(note.created_at).strftime('on %d, %b %Y') when = dateparse.parse(
note.created_at).strftime('on %d, %b %Y')
body = f"**{note.author['name']} - {PING_SIGN}{note.author['username']} wrote [here]({note_url})** {when}:\n\n" body = f"**{note.author['name']} - {PING_SIGN}{note.author['username']} wrote [here]({note_url})** {when}:\n\n"
body += '\n'.join([l for l in note.body.split('\n')]) body += '\n'.join([line for line in note.body.split('\n')])
obj = { obj = {
'body': body, 'body': body,
@ -467,7 +480,8 @@ class GstMRMover:
return new_mr return new_mr
def push_branch(self, branch): def push_branch(self, branch):
fprint(f"-> Pushing branch {branch} to remote {self.gl.user.username}...") fprint(
f"-> Pushing branch {branch} to remote {self.gl.user.username}...")
if self.git("push", "--no-verify", self.gl.user.username, branch, if self.git("push", "--no-verify", self.gl.user.username, branch,
interaction_message=f"pushing {branch} to {self.gl.user.username} with:\n " interaction_message=f"pushing {branch} to {self.gl.user.username} with:\n "
f" `$git push {self.gl.user.username} {branch}`") == "SKIP": f" `$git push {self.gl.user.username} {branch}`") == "SKIP":
@ -503,7 +517,8 @@ class GstMRMover:
if self.git("checkout", remote_branch, "-b", branch, if self.git("checkout", remote_branch, "-b", branch,
interaction_message=f"checking out branch with `git checkout {remote_branch} -b {branch}`") == "SKIP": interaction_message=f"checking out branch with `git checkout {remote_branch} -b {branch}`") == "SKIP":
fprint(bold(f"{red('SKIPPED')} (couldn't checkout)\n"), nested=False) fprint(
bold(f"{red('SKIPPED')} (couldn't checkout)\n"), nested=False)
return False return False
for commit in reversed([c for c in mr.commits()]): for commit in reversed([c for c in mr.commits()]):
@ -511,7 +526,8 @@ class GstMRMover:
interaction_message=f"cherry-picking {commit.id} onto {branch} with:\n " interaction_message=f"cherry-picking {commit.id} onto {branch} with:\n "
f" `$ git cherry-pick {commit.id}`", f" `$ git cherry-pick {commit.id}`",
revert_operation=["cherry-pick", "--abort"]) == "SKIP": revert_operation=["cherry-pick", "--abort"]) == "SKIP":
fprint(f"{yellow('SKIPPED')} (couldn't cherry-pick).", nested=False) fprint(
f"{yellow('SKIPPED')} (couldn't cherry-pick).", nested=False)
return False return False
self.git("show", remote_branch + "..", call=True) self.git("show", remote_branch + "..", call=True)
@ -544,7 +560,8 @@ class GstMRMover:
if self.mr != mr.iid: if self.mr != mr.iid:
continue continue
found_mr = True found_mr = True
fprint(f'Moving {mr.source_branch} "{mr.title}": {URL}{from_project.path_with_namespace}/merge_requests/{mr.iid}... ') fprint(
f'Moving {mr.source_branch} "{mr.title}": {URL}{from_project.path_with_namespace}/merge_requests/{mr.iid}... ')
if mr.source_branch in self.skipped_branches: if mr.source_branch in self.skipped_branches:
print(f"{yellow('SKIPPED')} (blacklisted branch)") print(f"{yellow('SKIPPED')} (blacklisted branch)")
failed_mrs.append( failed_mrs.append(
@ -562,10 +579,12 @@ class GstMRMover:
self.close_mr(from_project, to_project, mr, new_mr) self.close_mr(from_project, to_project, mr, new_mr)
fprint(f"\n{yellow('DONE')} with {from_project.path_with_namespace}\n\n", nested=False) fprint(
f"\n{yellow('DONE')} with {from_project.path_with_namespace}\n\n", nested=False)
if self.mr and not found_mr: if self.mr and not found_mr:
sys.exit(bold(red(f"\n==> Couldn't find MR {self.mr} in {self.modules[0]}\n"))) sys.exit(
bold(red(f"\n==> Couldn't find MR {self.mr} in {self.modules[0]}\n")))
for mr in failed_mrs: for mr in failed_mrs:
fprint(f"Didn't move MR: {mr}\n") fprint(f"Didn't move MR: {mr}\n")
@ -584,7 +603,8 @@ class GstMRMover:
if new_mr_url: if new_mr_url:
obj = {'body': f"Moved to: {new_mr_url}"} obj = {'body': f"Moved to: {new_mr_url}"}
else: else:
ret = input(f"Write a comment to add while closing MR {mr.iid} '{bold(mr.title)}':\n\n").strip() ret = input(
f"Write a comment to add while closing MR {mr.iid} '{bold(mr.title)}':\n\n").strip()
if ret: if ret:
obj = {'body': ret} obj = {'body': ret}
@ -595,7 +615,8 @@ class GstMRMover:
mr.discussions.create(obj) mr.discussions.create(obj)
mr.state_event = 'close' mr.state_event = 'close'
mr.save() mr.save()
fprint(f'Old MR {mr_url} "{bold(mr.title)}" {yellow("CLOSED")}\n') fprint(
f'Old MR {mr_url} "{bold(mr.title)}" {yellow("CLOSED")}\n')
def setup_repo(self): def setup_repo(self):
fprint(f"Setting up '{bold(ROOT_DIR)}'...") fprint(f"Setting up '{bold(ROOT_DIR)}'...")
@ -603,7 +624,8 @@ class GstMRMover:
try: try:
out = self.git("status", "--porcelain") out = self.git("status", "--porcelain")
if out: if out:
fprint("\n" + red('Git repository is not clean:') + "\n```\n" + out + "\n```\n") fprint("\n" + red('Git repository is not clean:')
+ "\n```\n" + out + "\n```\n")
sys.exit(1) sys.exit(1)
except Exception as e: except Exception as e:
@ -626,7 +648,8 @@ class GstMRMover:
git_rename_limit = 0 git_rename_limit = 0
if int(git_rename_limit) < 999999: if int(git_rename_limit) < 999999:
self.git_rename_limit = git_rename_limit self.git_rename_limit = git_rename_limit
fprint("-> Setting git rename limit to 999999 so we can properly cherry-pick between repos\n") fprint(
"-> Setting git rename limit to 999999 so we can properly cherry-pick between repos\n")
self.git("config", "merge.renameLimit", "999999") self.git("config", "merge.renameLimit", "999999")