diff options
author | Jason R. Coombs <jaraco@google.com> | 2023-09-29 11:04:49 -0400 |
---|---|---|
committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-10-31 16:03:54 +0000 |
commit | b32ccbb66bb16965ecb8b4e266c4e45186636c1b (patch) | |
tree | 1c1eda32af709f0cbf822de56f696ccd531ce6de /repo | |
parent | b99272c601bc5f466c3cfc782bb852c2c967ad27 (diff) | |
download | git-repo-b32ccbb66bb16965ecb8b4e266c4e45186636c1b.tar.gz |
cleanup: Update codebase to expect Python 3.6
- Bump minimum version to Python 3.6.
- Use f-strings in a lot of places.
Change-Id: I2aa70197230fcec2eff8e7c8eb754f20c08075bb
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/389034
Tested-by: Jason R. Coombs <jaraco@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Commit-Queue: Jason R. Coombs <jaraco@google.com>
Diffstat (limited to 'repo')
-rwxr-xr-x | repo | 94 |
1 files changed, 46 insertions, 48 deletions
@@ -33,7 +33,7 @@ import sys | |||
33 | # bit more flexible with older systems. See that file for more details on the | 33 | # bit more flexible with older systems. See that file for more details on the |
34 | # versions we select. | 34 | # versions we select. |
35 | MIN_PYTHON_VERSION_SOFT = (3, 6) | 35 | MIN_PYTHON_VERSION_SOFT = (3, 6) |
36 | MIN_PYTHON_VERSION_HARD = (3, 5) | 36 | MIN_PYTHON_VERSION_HARD = (3, 6) |
37 | 37 | ||
38 | 38 | ||
39 | # Keep basic logic in sync with repo_trace.py. | 39 | # Keep basic logic in sync with repo_trace.py. |
@@ -96,7 +96,7 @@ def check_python_version(): | |||
96 | # bridge the gap. This is the fallback anyways so perf isn't critical. | 96 | # bridge the gap. This is the fallback anyways so perf isn't critical. |
97 | min_major, min_minor = MIN_PYTHON_VERSION_SOFT | 97 | min_major, min_minor = MIN_PYTHON_VERSION_SOFT |
98 | for inc in range(0, 10): | 98 | for inc in range(0, 10): |
99 | reexec("python{}.{}".format(min_major, min_minor + inc)) | 99 | reexec(f"python{min_major}.{min_minor + inc}") |
100 | 100 | ||
101 | # Fallback to older versions if possible. | 101 | # Fallback to older versions if possible. |
102 | for inc in range( | 102 | for inc in range( |
@@ -105,7 +105,7 @@ def check_python_version(): | |||
105 | # Don't downgrade, and don't reexec ourselves (which would infinite loop). | 105 | # Don't downgrade, and don't reexec ourselves (which would infinite loop). |
106 | if (min_major, min_minor - inc) <= (major, minor): | 106 | if (min_major, min_minor - inc) <= (major, minor): |
107 | break | 107 | break |
108 | reexec("python{}.{}".format(min_major, min_minor - inc)) | 108 | reexec(f"python{min_major}.{min_minor - inc}") |
109 | 109 | ||
110 | # Try the generic Python 3 wrapper, but only if it's new enough. If it | 110 | # Try the generic Python 3 wrapper, but only if it's new enough. If it |
111 | # isn't, we want to just give up below and make the user resolve things. | 111 | # isn't, we want to just give up below and make the user resolve things. |
@@ -566,8 +566,7 @@ def run_command(cmd, **kwargs): | |||
566 | return output.decode("utf-8") | 566 | return output.decode("utf-8") |
567 | except UnicodeError: | 567 | except UnicodeError: |
568 | print( | 568 | print( |
569 | "repo: warning: Invalid UTF-8 output:\ncmd: %r\n%r" | 569 | f"repo: warning: Invalid UTF-8 output:\ncmd: {cmd!r}\n{output}", |
570 | % (cmd, output), | ||
571 | file=sys.stderr, | 570 | file=sys.stderr, |
572 | ) | 571 | ) |
573 | return output.decode("utf-8", "backslashreplace") | 572 | return output.decode("utf-8", "backslashreplace") |
@@ -590,20 +589,17 @@ def run_command(cmd, **kwargs): | |||
590 | # If things failed, print useful debugging output. | 589 | # If things failed, print useful debugging output. |
591 | if check and ret.returncode: | 590 | if check and ret.returncode: |
592 | print( | 591 | print( |
593 | 'repo: error: "%s" failed with exit status %s' | 592 | f'repo: error: "{cmd[0]}" failed with exit status {ret.returncode}', |
594 | % (cmd[0], ret.returncode), | ||
595 | file=sys.stderr, | ||
596 | ) | ||
597 | print( | ||
598 | " cwd: %s\n cmd: %r" % (kwargs.get("cwd", os.getcwd()), cmd), | ||
599 | file=sys.stderr, | 593 | file=sys.stderr, |
600 | ) | 594 | ) |
595 | cwd = kwargs.get("cwd", os.getcwd()) | ||
596 | print(f" cwd: {cwd}\n cmd: {cmd!r}", file=sys.stderr) | ||
601 | 597 | ||
602 | def _print_output(name, output): | 598 | def _print_output(name, output): |
603 | if output: | 599 | if output: |
604 | print( | 600 | print( |
605 | " %s:\n >> %s" | 601 | f" {name}:" |
606 | % (name, "\n >> ".join(output.splitlines())), | 602 | + "".join(f"\n >> {x}" for x in output.splitlines()), |
607 | file=sys.stderr, | 603 | file=sys.stderr, |
608 | ) | 604 | ) |
609 | 605 | ||
@@ -719,7 +715,7 @@ def _Init(args, gitc_init=False): | |||
719 | except OSError as e: | 715 | except OSError as e: |
720 | if e.errno != errno.EEXIST: | 716 | if e.errno != errno.EEXIST: |
721 | print( | 717 | print( |
722 | "fatal: cannot make %s directory: %s" % (repodir, e.strerror), | 718 | f"fatal: cannot make {repodir} directory: {e.strerror}", |
723 | file=sys.stderr, | 719 | file=sys.stderr, |
724 | ) | 720 | ) |
725 | # Don't raise CloneFailure; that would delete the | 721 | # Don't raise CloneFailure; that would delete the |
@@ -817,7 +813,7 @@ def _CheckGitVersion(): | |||
817 | if ver_act < MIN_GIT_VERSION: | 813 | if ver_act < MIN_GIT_VERSION: |
818 | need = ".".join(map(str, MIN_GIT_VERSION)) | 814 | need = ".".join(map(str, MIN_GIT_VERSION)) |
819 | print( | 815 | print( |
820 | "fatal: git %s or later required; found %s" % (need, ver_act.full), | 816 | f"fatal: git {need} or later required; found {ver_act.full}", |
821 | file=sys.stderr, | 817 | file=sys.stderr, |
822 | ) | 818 | ) |
823 | raise CloneFailure() | 819 | raise CloneFailure() |
@@ -836,7 +832,8 @@ def SetGitTrace2ParentSid(env=None): | |||
836 | KEY = "GIT_TRACE2_PARENT_SID" | 832 | KEY = "GIT_TRACE2_PARENT_SID" |
837 | 833 | ||
838 | now = datetime.datetime.now(datetime.timezone.utc) | 834 | now = datetime.datetime.now(datetime.timezone.utc) |
839 | value = "repo-%s-P%08x" % (now.strftime("%Y%m%dT%H%M%SZ"), os.getpid()) | 835 | timestamp = now.strftime("%Y%m%dT%H%M%SZ") |
836 | value = f"repo-{timestamp}-P{os.getpid():08x}" | ||
840 | 837 | ||
841 | # If it's already set, then append ourselves. | 838 | # If it's already set, then append ourselves. |
842 | if KEY in env: | 839 | if KEY in env: |
@@ -880,8 +877,7 @@ def SetupGnuPG(quiet): | |||
880 | except OSError as e: | 877 | except OSError as e: |
881 | if e.errno != errno.EEXIST: | 878 | if e.errno != errno.EEXIST: |
882 | print( | 879 | print( |
883 | "fatal: cannot make %s directory: %s" | 880 | f"fatal: cannot make {home_dot_repo} directory: {e.strerror}", |
884 | % (home_dot_repo, e.strerror), | ||
885 | file=sys.stderr, | 881 | file=sys.stderr, |
886 | ) | 882 | ) |
887 | sys.exit(1) | 883 | sys.exit(1) |
@@ -891,15 +887,15 @@ def SetupGnuPG(quiet): | |||
891 | except OSError as e: | 887 | except OSError as e: |
892 | if e.errno != errno.EEXIST: | 888 | if e.errno != errno.EEXIST: |
893 | print( | 889 | print( |
894 | "fatal: cannot make %s directory: %s" % (gpg_dir, e.strerror), | 890 | f"fatal: cannot make {gpg_dir} directory: {e.strerror}", |
895 | file=sys.stderr, | 891 | file=sys.stderr, |
896 | ) | 892 | ) |
897 | sys.exit(1) | 893 | sys.exit(1) |
898 | 894 | ||
899 | if not quiet: | 895 | if not quiet: |
900 | print( | 896 | print( |
901 | "repo: Updating release signing keys to keyset ver %s" | 897 | "repo: Updating release signing keys to keyset ver " |
902 | % (".".join(str(x) for x in KEYRING_VERSION),) | 898 | + ".".join(str(x) for x in KEYRING_VERSION), |
903 | ) | 899 | ) |
904 | # NB: We use --homedir (and cwd below) because some environments (Windows) do | 900 | # NB: We use --homedir (and cwd below) because some environments (Windows) do |
905 | # not correctly handle full native paths. We avoid the issue by changing to | 901 | # not correctly handle full native paths. We avoid the issue by changing to |
@@ -951,7 +947,7 @@ def _GetRepoConfig(name): | |||
951 | return None | 947 | return None |
952 | else: | 948 | else: |
953 | print( | 949 | print( |
954 | "repo: error: git %s failed:\n%s" % (" ".join(cmd), ret.stderr), | 950 | f"repo: error: git {' '.join(cmd)} failed:\n{ret.stderr}", |
955 | file=sys.stderr, | 951 | file=sys.stderr, |
956 | ) | 952 | ) |
957 | raise RunError() | 953 | raise RunError() |
@@ -1064,7 +1060,7 @@ def _Clone(url, cwd, clone_bundle, quiet, verbose): | |||
1064 | os.mkdir(cwd) | 1060 | os.mkdir(cwd) |
1065 | except OSError as e: | 1061 | except OSError as e: |
1066 | print( | 1062 | print( |
1067 | "fatal: cannot make %s directory: %s" % (cwd, e.strerror), | 1063 | f"fatal: cannot make {cwd} directory: {e.strerror}", |
1068 | file=sys.stderr, | 1064 | file=sys.stderr, |
1069 | ) | 1065 | ) |
1070 | raise CloneFailure() | 1066 | raise CloneFailure() |
@@ -1104,7 +1100,7 @@ def resolve_repo_rev(cwd, committish): | |||
1104 | ret = run_git( | 1100 | ret = run_git( |
1105 | "rev-parse", | 1101 | "rev-parse", |
1106 | "--verify", | 1102 | "--verify", |
1107 | "%s^{commit}" % (committish,), | 1103 | f"{committish}^{{commit}}", |
1108 | cwd=cwd, | 1104 | cwd=cwd, |
1109 | check=False, | 1105 | check=False, |
1110 | ) | 1106 | ) |
@@ -1117,7 +1113,7 @@ def resolve_repo_rev(cwd, committish): | |||
1117 | rev = resolve("refs/remotes/origin/%s" % committish) | 1113 | rev = resolve("refs/remotes/origin/%s" % committish) |
1118 | if rev is None: | 1114 | if rev is None: |
1119 | print( | 1115 | print( |
1120 | 'repo: error: unknown branch "%s"' % (committish,), | 1116 | f'repo: error: unknown branch "{committish}"', |
1121 | file=sys.stderr, | 1117 | file=sys.stderr, |
1122 | ) | 1118 | ) |
1123 | raise CloneFailure() | 1119 | raise CloneFailure() |
@@ -1130,7 +1126,8 @@ def resolve_repo_rev(cwd, committish): | |||
1130 | rev = resolve(remote_ref) | 1126 | rev = resolve(remote_ref) |
1131 | if rev is None: | 1127 | if rev is None: |
1132 | print( | 1128 | print( |
1133 | 'repo: error: unknown tag "%s"' % (committish,), file=sys.stderr | 1129 | f'repo: error: unknown tag "{committish}"', |
1130 | file=sys.stderr, | ||
1134 | ) | 1131 | ) |
1135 | raise CloneFailure() | 1132 | raise CloneFailure() |
1136 | return (remote_ref, rev) | 1133 | return (remote_ref, rev) |
@@ -1138,12 +1135,12 @@ def resolve_repo_rev(cwd, committish): | |||
1138 | # See if it's a short branch name. | 1135 | # See if it's a short branch name. |
1139 | rev = resolve("refs/remotes/origin/%s" % committish) | 1136 | rev = resolve("refs/remotes/origin/%s" % committish) |
1140 | if rev: | 1137 | if rev: |
1141 | return ("refs/heads/%s" % (committish,), rev) | 1138 | return (f"refs/heads/{committish}", rev) |
1142 | 1139 | ||
1143 | # See if it's a tag. | 1140 | # See if it's a tag. |
1144 | rev = resolve("refs/tags/%s" % committish) | 1141 | rev = resolve(f"refs/tags/{committish}") |
1145 | if rev: | 1142 | if rev: |
1146 | return ("refs/tags/%s" % (committish,), rev) | 1143 | return (f"refs/tags/{committish}", rev) |
1147 | 1144 | ||
1148 | # See if it's a commit. | 1145 | # See if it's a commit. |
1149 | rev = resolve(committish) | 1146 | rev = resolve(committish) |
@@ -1152,7 +1149,8 @@ def resolve_repo_rev(cwd, committish): | |||
1152 | 1149 | ||
1153 | # Give up! | 1150 | # Give up! |
1154 | print( | 1151 | print( |
1155 | 'repo: error: unable to resolve "%s"' % (committish,), file=sys.stderr | 1152 | f'repo: error: unable to resolve "{committish}"', |
1153 | file=sys.stderr, | ||
1156 | ) | 1154 | ) |
1157 | raise CloneFailure() | 1155 | raise CloneFailure() |
1158 | 1156 | ||
@@ -1168,8 +1166,8 @@ def verify_rev(cwd, remote_ref, rev, quiet): | |||
1168 | if not quiet: | 1166 | if not quiet: |
1169 | print(file=sys.stderr) | 1167 | print(file=sys.stderr) |
1170 | print( | 1168 | print( |
1171 | "warning: '%s' is not signed; falling back to signed release '%s'" | 1169 | f"warning: '{remote_ref}' is not signed; " |
1172 | % (remote_ref, cur), | 1170 | f"falling back to signed release '{cur}'", |
1173 | file=sys.stderr, | 1171 | file=sys.stderr, |
1174 | ) | 1172 | ) |
1175 | print(file=sys.stderr) | 1173 | print(file=sys.stderr) |
@@ -1222,7 +1220,7 @@ def _ExpandAlias(name): | |||
1222 | if name in {"gitc-init", "help", "init"}: | 1220 | if name in {"gitc-init", "help", "init"}: |
1223 | return name, [] | 1221 | return name, [] |
1224 | 1222 | ||
1225 | alias = _GetRepoConfig("alias.%s" % (name,)) | 1223 | alias = _GetRepoConfig(f"alias.{name}") |
1226 | if alias is None: | 1224 | if alias is None: |
1227 | return name, [] | 1225 | return name, [] |
1228 | 1226 | ||
@@ -1318,18 +1316,20 @@ class Requirements: | |||
1318 | hard_ver = tuple(self._get_hard_ver(pkg)) | 1316 | hard_ver = tuple(self._get_hard_ver(pkg)) |
1319 | if curr_ver < hard_ver: | 1317 | if curr_ver < hard_ver: |
1320 | print( | 1318 | print( |
1321 | 'repo: error: Your version of "%s" (%s) is unsupported; ' | 1319 | f'repo: error: Your version of "{pkg}" ' |
1322 | "Please upgrade to at least version %s to continue." | 1320 | f"({self._format_ver(curr_ver)}) is unsupported; " |
1323 | % (pkg, self._format_ver(curr_ver), self._format_ver(soft_ver)), | 1321 | "Please upgrade to at least version " |
1322 | f"{self._format_ver(soft_ver)} to continue.", | ||
1324 | file=sys.stderr, | 1323 | file=sys.stderr, |
1325 | ) | 1324 | ) |
1326 | sys.exit(1) | 1325 | sys.exit(1) |
1327 | 1326 | ||
1328 | if curr_ver < soft_ver: | 1327 | if curr_ver < soft_ver: |
1329 | print( | 1328 | print( |
1330 | 'repo: warning: Your version of "%s" (%s) is no longer supported; ' | 1329 | f'repo: error: Your version of "{pkg}" ' |
1331 | "Please upgrade to at least version %s to avoid breakage." | 1330 | f"({self._format_ver(curr_ver)}) is no longer supported; " |
1332 | % (pkg, self._format_ver(curr_ver), self._format_ver(soft_ver)), | 1331 | "Please upgrade to at least version " |
1332 | f"{self._format_ver(soft_ver)} to continue.", | ||
1333 | file=sys.stderr, | 1333 | file=sys.stderr, |
1334 | ) | 1334 | ) |
1335 | 1335 | ||
@@ -1390,20 +1390,18 @@ def _Help(args): | |||
1390 | def _Version(): | 1390 | def _Version(): |
1391 | """Show version information.""" | 1391 | """Show version information.""" |
1392 | print("<repo not installed>") | 1392 | print("<repo not installed>") |
1393 | print("repo launcher version %s" % (".".join(str(x) for x in VERSION),)) | 1393 | print(f"repo launcher version {'.'.join(str(x) for x in VERSION)}") |
1394 | print(" (from %s)" % (__file__,)) | 1394 | print(f" (from {__file__})") |
1395 | print("git %s" % (ParseGitVersion().full,)) | 1395 | print(f"git {ParseGitVersion().full}") |
1396 | print("Python %s" % sys.version) | 1396 | print(f"Python {sys.version}") |
1397 | uname = platform.uname() | 1397 | uname = platform.uname() |
1398 | if sys.version_info.major < 3: | 1398 | if sys.version_info.major < 3: |
1399 | # Python 3 returns a named tuple, but Python 2 is simpler. | 1399 | # Python 3 returns a named tuple, but Python 2 is simpler. |
1400 | print(uname) | 1400 | print(uname) |
1401 | else: | 1401 | else: |
1402 | print("OS %s %s (%s)" % (uname.system, uname.release, uname.version)) | 1402 | print(f"OS {uname.system} {uname.release} ({uname.version})") |
1403 | print( | 1403 | processor = uname.processor if uname.processor else "unknown" |
1404 | "CPU %s (%s)" | 1404 | print(f"CPU {uname.machine} ({processor})") |
1405 | % (uname.machine, uname.processor if uname.processor else "unknown") | ||
1406 | ) | ||
1407 | print("Bug reports:", BUG_URL) | 1405 | print("Bug reports:", BUG_URL) |
1408 | sys.exit(0) | 1406 | sys.exit(0) |
1409 | 1407 | ||