The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 15 | import collections |
Mike Frysinger | ebf04a4 | 2021-02-23 20:48:04 -0500 | [diff] [blame] | 16 | import functools |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 17 | import http.cookiejar as cookielib |
Mike Frysinger | 7b586f2 | 2021-02-23 18:38:39 -0500 | [diff] [blame] | 18 | import io |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 19 | import json |
Mike Frysinger | ebf04a4 | 2021-02-23 20:48:04 -0500 | [diff] [blame] | 20 | import multiprocessing |
David Pursehouse | 86d973d | 2012-08-24 10:21:02 +0900 | [diff] [blame] | 21 | import netrc |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 22 | import optparse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import os |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 24 | import socket |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 25 | import sys |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 26 | import tempfile |
Shawn O. Pearce | f690687 | 2009-04-18 10:49:00 -0700 | [diff] [blame] | 27 | import time |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 28 | from typing import List, NamedTuple, Set, Union |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 29 | import urllib.error |
| 30 | import urllib.parse |
| 31 | import urllib.request |
Mike Frysinger | 5951e30 | 2022-05-20 23:34:44 -0400 | [diff] [blame] | 32 | import xml.parsers.expat |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 33 | import xmlrpc.client |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 35 | |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 36 | try: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 37 | import threading as _threading |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 38 | except ImportError: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 39 | import dummy_threading as _threading |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 40 | |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 41 | try: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 42 | import resource |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 43 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 44 | def _rlimit_nofile(): |
| 45 | return resource.getrlimit(resource.RLIMIT_NOFILE) |
| 46 | |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 47 | except ImportError: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 48 | |
| 49 | def _rlimit_nofile(): |
| 50 | return (256, 256) |
| 51 | |
Shawn O. Pearce | 97d2b2f | 2011-09-22 17:23:41 -0700 | [diff] [blame] | 52 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 53 | from command import Command |
| 54 | from command import DEFAULT_LOCAL_JOBS |
| 55 | from command import MirrorSafeCommand |
| 56 | from command import WORKER_BATCH_SIZE |
| 57 | from error import GitError |
| 58 | from error import RepoChangedException |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 59 | from error import RepoError |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 60 | from error import RepoExitError |
| 61 | from error import RepoUnhandledExceptionError |
| 62 | from error import SyncError |
| 63 | from error import UpdateManifestError |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 64 | import event_log |
Mike Frysinger | 347f9ed | 2021-03-15 14:58:52 -0400 | [diff] [blame] | 65 | from git_command import git_require |
David Pursehouse | ba7bc73 | 2015-08-20 16:55:42 +0900 | [diff] [blame] | 66 | from git_config import GetUrlCookieFile |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 67 | from git_refs import HEAD |
| 68 | from git_refs import R_HEADS |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 69 | import git_superproject |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 70 | import platform_utils |
| 71 | from progress import elapsed_str |
| 72 | from progress import jobs_str |
| 73 | from progress import Progress |
| 74 | from project import DeleteWorktreeError |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 75 | from project import Project |
| 76 | from project import RemoteSpec |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 77 | from project import SyncBuffer |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 78 | from repo_logging import RepoLogger |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 79 | from repo_trace import Trace |
Mike Frysinger | 19e409c | 2021-05-05 19:44:35 -0400 | [diff] [blame] | 80 | import ssh |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 81 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 83 | |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 84 | _ONE_DAY_S = 24 * 60 * 60 |
| 85 | |
LaMont Jones | d793553 | 2022-12-01 20:18:46 +0000 | [diff] [blame] | 86 | # Env var to implicitly turn auto-gc back on. This was added to allow a user to |
LaMont Jones | 100a214 | 2022-12-02 22:54:11 +0000 | [diff] [blame] | 87 | # revert a change in default behavior in v2.29.9. Remove after 2023-04-01. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 88 | _REPO_AUTO_GC = "REPO_AUTO_GC" |
| 89 | _AUTO_GC = os.environ.get(_REPO_AUTO_GC) == "1" |
LaMont Jones | 5ed8c63 | 2022-11-10 00:10:44 +0000 | [diff] [blame] | 90 | |
Jason Chang | 1783332 | 2023-05-23 13:06:55 -0700 | [diff] [blame] | 91 | _REPO_ALLOW_SHALLOW = os.environ.get("REPO_ALLOW_SHALLOW") |
| 92 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 93 | logger = RepoLogger(__file__) |
| 94 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 95 | |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 96 | class _FetchOneResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 97 | """_FetchOne return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 98 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 99 | Attributes: |
| 100 | success (bool): True if successful. |
| 101 | project (Project): The fetched project. |
| 102 | start (float): The starting time.time(). |
| 103 | finish (float): The ending time.time(). |
| 104 | remote_fetched (bool): True if the remote was actually queried. |
| 105 | """ |
| 106 | |
| 107 | success: bool |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 108 | errors: List[Exception] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 109 | project: Project |
| 110 | start: float |
| 111 | finish: float |
| 112 | remote_fetched: bool |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | class _FetchResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 116 | """_Fetch return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 117 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 118 | Attributes: |
| 119 | success (bool): True if successful. |
| 120 | projects (Set[str]): The names of the git directories of fetched projects. |
| 121 | """ |
| 122 | |
| 123 | success: bool |
| 124 | projects: Set[str] |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | class _FetchMainResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 128 | """_FetchMain return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 129 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 130 | Attributes: |
| 131 | all_projects (List[Project]): The fetched projects. |
| 132 | """ |
| 133 | |
| 134 | all_projects: List[Project] |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | class _CheckoutOneResult(NamedTuple): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 138 | """_CheckoutOne return value. |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 139 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 140 | Attributes: |
| 141 | success (bool): True if successful. |
| 142 | project (Project): The project. |
| 143 | start (float): The starting time.time(). |
| 144 | finish (float): The ending time.time(). |
| 145 | """ |
| 146 | |
| 147 | success: bool |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 148 | errors: List[Exception] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 149 | project: Project |
| 150 | start: float |
| 151 | finish: float |
LaMont Jones | 1eddca8 | 2022-09-01 15:15:04 +0000 | [diff] [blame] | 152 | |
| 153 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 154 | class SuperprojectError(SyncError): |
| 155 | """Superproject sync repo.""" |
| 156 | |
| 157 | |
| 158 | class SyncFailFastError(SyncError): |
| 159 | """Sync exit error when --fail-fast set.""" |
| 160 | |
| 161 | |
| 162 | class SmartSyncError(SyncError): |
| 163 | """Smart sync exit error.""" |
| 164 | |
| 165 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 166 | class ManifestInterruptError(RepoError): |
| 167 | """Aggregate Error to be logged when a user interrupts a manifest update.""" |
| 168 | |
| 169 | def __init__(self, output, **kwargs): |
| 170 | super().__init__(output, **kwargs) |
| 171 | self.output = output |
| 172 | |
| 173 | def __str__(self): |
| 174 | error_type = type(self).__name__ |
| 175 | return f"{error_type}:{self.output}" |
| 176 | |
| 177 | |
| 178 | class TeeStringIO(io.StringIO): |
| 179 | """StringIO class that can write to an additional destination.""" |
| 180 | |
| 181 | def __init__( |
| 182 | self, io: Union[io.TextIOWrapper, None], *args, **kwargs |
| 183 | ) -> None: |
| 184 | super().__init__(*args, **kwargs) |
| 185 | self.io = io |
| 186 | |
| 187 | def write(self, s: str) -> int: |
| 188 | """Write to additional destination.""" |
| 189 | super().write(s) |
| 190 | if self.io is not None: |
| 191 | self.io.write(s) |
| 192 | |
| 193 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 194 | class Sync(Command, MirrorSafeCommand): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 195 | COMMON = True |
| 196 | MULTI_MANIFEST_SUPPORT = True |
| 197 | helpSummary = "Update working tree to the latest revision" |
| 198 | helpUsage = """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 199 | %prog [...] |
| 200 | """ |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 201 | helpDescription = """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 202 | The '%prog' command synchronizes local project directories |
| 203 | with the remote repositories specified in the manifest. If a local |
| 204 | project does not yet exist, it will clone a new local directory from |
| 205 | the remote repository and set up tracking branches as specified in |
| 206 | the manifest. If the local project already exists, '%prog' |
| 207 | will update the remote branches and rebase any new local changes |
| 208 | on top of the new remote changes. |
| 209 | |
| 210 | '%prog' will synchronize all projects listed at the command |
| 211 | line. Projects can be specified either by name, or by a relative |
| 212 | or absolute path to the project's local directory. If no projects |
| 213 | are specified, '%prog' will synchronize all projects listed in |
| 214 | the manifest. |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 215 | |
| 216 | The -d/--detach option can be used to switch specified projects |
| 217 | back to the manifest revision. This option is especially helpful |
| 218 | if the project is currently on a topic branch, but the manifest |
| 219 | revision is temporarily needed. |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 220 | |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 221 | The -s/--smart-sync option can be used to sync to a known good |
| 222 | build as specified by the manifest-server element in the current |
Victor Boivie | 08c880d | 2011-04-19 10:32:52 +0200 | [diff] [blame] | 223 | manifest. The -t/--smart-tag option is similar and allows you to |
| 224 | specify a custom tag/label. |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 225 | |
David Pursehouse | cf76b1b | 2012-09-14 10:31:42 +0900 | [diff] [blame] | 226 | The -u/--manifest-server-username and -p/--manifest-server-password |
| 227 | options can be used to specify a username and password to authenticate |
| 228 | with the manifest server when using the -s or -t option. |
| 229 | |
| 230 | If -u and -p are not specified when using the -s or -t option, '%prog' |
| 231 | will attempt to read authentication credentials for the manifest server |
| 232 | from the user's .netrc file. |
| 233 | |
| 234 | '%prog' will not use authentication credentials from -u/-p or .netrc |
| 235 | if the manifest server specified in the manifest file already includes |
| 236 | credentials. |
| 237 | |
Mike Frysinger | d9e5cf0 | 2019-08-26 03:12:55 -0400 | [diff] [blame] | 238 | By default, all projects will be synced. The --fail-fast option can be used |
Mike Frysinger | 7ae210a | 2020-05-24 14:56:52 -0400 | [diff] [blame] | 239 | to halt syncing as soon as possible when the first project fails to sync. |
Andrei Warkentin | 5df6de0 | 2010-07-02 17:58:31 -0500 | [diff] [blame] | 240 | |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 241 | The --force-sync option can be used to overwrite existing git |
| 242 | directories if they have previously been linked to a different |
Roger Shimizu | ac29ac3 | 2020-06-06 02:33:40 +0900 | [diff] [blame] | 243 | object directory. WARNING: This may cause data to be lost since |
Kevin Degi | abaa7f3 | 2014-11-12 11:27:45 -0700 | [diff] [blame] | 244 | refs may be removed when overwriting. |
| 245 | |
Oleksii Okolielov | d3c0f59 | 2018-12-17 19:23:44 -0500 | [diff] [blame] | 246 | The --force-remove-dirty option can be used to remove previously used |
| 247 | projects with uncommitted changes. WARNING: This may cause data to be |
| 248 | lost since uncommitted changes may be removed with projects that no longer |
| 249 | exist in the manifest. |
| 250 | |
Shawn O. Pearce | e02ac0a | 2012-03-14 15:36:59 -0700 | [diff] [blame] | 251 | The --no-clone-bundle option disables any attempt to use |
| 252 | $URL/clone.bundle to bootstrap a new Git repository from a |
| 253 | resumeable bundle file on a content delivery network. This |
| 254 | may be necessary if there are problems with the local Python |
| 255 | HTTP client or proxy configuration, but the Git binary works. |
| 256 | |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 257 | The --fetch-submodules option enables fetching Git submodules |
| 258 | of a project from server. |
| 259 | |
David Pursehouse | f2fad61 | 2015-01-29 14:36:28 +0900 | [diff] [blame] | 260 | The -c/--current-branch option can be used to only fetch objects that |
| 261 | are on the branch specified by a project's revision. |
| 262 | |
David Pursehouse | b155354 | 2014-09-04 21:28:09 +0900 | [diff] [blame] | 263 | The --optimized-fetch option can be used to only fetch projects that |
| 264 | are fixed to a sha1 revision if the sha1 revision does not already |
| 265 | exist locally. |
| 266 | |
David Pursehouse | 74cfd27 | 2015-10-14 10:50:15 +0900 | [diff] [blame] | 267 | The --prune option can be used to remove any refs that no longer |
| 268 | exist on the remote. |
| 269 | |
LaMont Jones | 7efab53 | 2022-09-01 15:41:12 +0000 | [diff] [blame] | 270 | The --auto-gc option can be used to trigger garbage collection on all |
| 271 | projects. By default, repo does not run garbage collection. |
| 272 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 273 | # SSH Connections |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 274 | |
| 275 | If at least one project remote URL uses an SSH connection (ssh://, |
| 276 | git+ssh://, or user@host:path syntax) repo will automatically |
| 277 | enable the SSH ControlMaster option when connecting to that host. |
| 278 | This feature permits other projects in the same '%prog' session to |
| 279 | reuse the same SSH tunnel, saving connection setup overheads. |
| 280 | |
| 281 | To disable this behavior on UNIX platforms, set the GIT_SSH |
| 282 | environment variable to 'ssh'. For example: |
| 283 | |
| 284 | export GIT_SSH=ssh |
| 285 | %prog |
| 286 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 287 | # Compatibility |
Shawn O. Pearce | eb7af87 | 2009-04-21 08:02:04 -0700 | [diff] [blame] | 288 | |
| 289 | This feature is automatically disabled on Windows, due to the lack |
| 290 | of UNIX domain socket support. |
| 291 | |
| 292 | This feature is not compatible with url.insteadof rewrites in the |
| 293 | user's ~/.gitconfig. '%prog' is currently not able to perform the |
| 294 | rewrite early enough to establish the ControlMaster tunnel. |
| 295 | |
| 296 | If the remote SSH daemon is Gerrit Code Review, version 2.0.10 or |
| 297 | later is required to fix a server side protocol bug. |
| 298 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 299 | """ |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 300 | # A value of 0 means we want parallel jobs, but we'll determine the default |
| 301 | # value later on. |
| 302 | PARALLEL_JOBS = 0 |
Shawn O. Pearce | 6392c87 | 2011-09-22 17:44:31 -0700 | [diff] [blame] | 303 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 304 | def _Options(self, p, show_smart=True): |
| 305 | p.add_option( |
| 306 | "--jobs-network", |
| 307 | default=None, |
| 308 | type=int, |
| 309 | metavar="JOBS", |
| 310 | help="number of network jobs to run in parallel (defaults to " |
| 311 | "--jobs or 1)", |
| 312 | ) |
| 313 | p.add_option( |
| 314 | "--jobs-checkout", |
| 315 | default=None, |
| 316 | type=int, |
| 317 | metavar="JOBS", |
| 318 | help="number of local checkout jobs to run in parallel (defaults " |
| 319 | f"to --jobs or {DEFAULT_LOCAL_JOBS})", |
| 320 | ) |
Mike Frysinger | 49de8ef | 2021-04-09 00:21:02 -0400 | [diff] [blame] | 321 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 322 | p.add_option( |
| 323 | "-f", |
| 324 | "--force-broken", |
| 325 | dest="force_broken", |
| 326 | action="store_true", |
| 327 | help="obsolete option (to be deleted in the future)", |
| 328 | ) |
| 329 | p.add_option( |
| 330 | "--fail-fast", |
| 331 | dest="fail_fast", |
| 332 | action="store_true", |
| 333 | help="stop syncing after first error is hit", |
| 334 | ) |
| 335 | p.add_option( |
| 336 | "--force-sync", |
| 337 | dest="force_sync", |
| 338 | action="store_true", |
| 339 | help="overwrite an existing git directory if it needs to " |
| 340 | "point to a different object directory. WARNING: this " |
| 341 | "may cause loss of data", |
| 342 | ) |
| 343 | p.add_option( |
| 344 | "--force-remove-dirty", |
| 345 | dest="force_remove_dirty", |
| 346 | action="store_true", |
| 347 | help="force remove projects with uncommitted modifications if " |
| 348 | "projects no longer exist in the manifest. " |
| 349 | "WARNING: this may cause loss of data", |
| 350 | ) |
| 351 | p.add_option( |
| 352 | "-l", |
| 353 | "--local-only", |
| 354 | dest="local_only", |
| 355 | action="store_true", |
| 356 | help="only update working tree, don't fetch", |
| 357 | ) |
| 358 | p.add_option( |
| 359 | "--no-manifest-update", |
| 360 | "--nmu", |
| 361 | dest="mp_update", |
| 362 | action="store_false", |
| 363 | default="true", |
| 364 | help="use the existing manifest checkout as-is. " |
| 365 | "(do not update to the latest revision)", |
| 366 | ) |
| 367 | p.add_option( |
| 368 | "-n", |
| 369 | "--network-only", |
| 370 | dest="network_only", |
| 371 | action="store_true", |
| 372 | help="fetch only, don't update working tree", |
| 373 | ) |
| 374 | p.add_option( |
| 375 | "-d", |
| 376 | "--detach", |
| 377 | dest="detach_head", |
| 378 | action="store_true", |
| 379 | help="detach projects back to manifest revision", |
| 380 | ) |
| 381 | p.add_option( |
| 382 | "-c", |
| 383 | "--current-branch", |
| 384 | dest="current_branch_only", |
| 385 | action="store_true", |
| 386 | help="fetch only current branch from server", |
| 387 | ) |
| 388 | p.add_option( |
| 389 | "--no-current-branch", |
| 390 | dest="current_branch_only", |
| 391 | action="store_false", |
| 392 | help="fetch all branches from server", |
| 393 | ) |
| 394 | p.add_option( |
| 395 | "-m", |
| 396 | "--manifest-name", |
| 397 | dest="manifest_name", |
| 398 | help="temporary manifest to use for this sync", |
| 399 | metavar="NAME.xml", |
| 400 | ) |
| 401 | p.add_option( |
| 402 | "--clone-bundle", |
| 403 | action="store_true", |
| 404 | help="enable use of /clone.bundle on HTTP/HTTPS", |
| 405 | ) |
| 406 | p.add_option( |
| 407 | "--no-clone-bundle", |
| 408 | dest="clone_bundle", |
| 409 | action="store_false", |
| 410 | help="disable use of /clone.bundle on HTTP/HTTPS", |
| 411 | ) |
| 412 | p.add_option( |
| 413 | "-u", |
| 414 | "--manifest-server-username", |
| 415 | action="store", |
| 416 | dest="manifest_server_username", |
| 417 | help="username to authenticate with the manifest server", |
| 418 | ) |
| 419 | p.add_option( |
| 420 | "-p", |
| 421 | "--manifest-server-password", |
| 422 | action="store", |
| 423 | dest="manifest_server_password", |
| 424 | help="password to authenticate with the manifest server", |
| 425 | ) |
| 426 | p.add_option( |
| 427 | "--fetch-submodules", |
| 428 | dest="fetch_submodules", |
| 429 | action="store_true", |
| 430 | help="fetch submodules from server", |
| 431 | ) |
| 432 | p.add_option( |
| 433 | "--use-superproject", |
| 434 | action="store_true", |
| 435 | help="use the manifest superproject to sync projects; implies -c", |
| 436 | ) |
| 437 | p.add_option( |
| 438 | "--no-use-superproject", |
| 439 | action="store_false", |
| 440 | dest="use_superproject", |
| 441 | help="disable use of manifest superprojects", |
| 442 | ) |
| 443 | p.add_option("--tags", action="store_true", help="fetch tags") |
| 444 | p.add_option( |
| 445 | "--no-tags", |
| 446 | dest="tags", |
| 447 | action="store_false", |
| 448 | help="don't fetch tags (default)", |
| 449 | ) |
| 450 | p.add_option( |
| 451 | "--optimized-fetch", |
| 452 | dest="optimized_fetch", |
| 453 | action="store_true", |
| 454 | help="only fetch projects fixed to sha1 if revision does not exist " |
| 455 | "locally", |
| 456 | ) |
| 457 | p.add_option( |
| 458 | "--retry-fetches", |
| 459 | default=0, |
| 460 | action="store", |
| 461 | type="int", |
| 462 | help="number of times to retry fetches on transient errors", |
| 463 | ) |
| 464 | p.add_option( |
| 465 | "--prune", |
| 466 | action="store_true", |
| 467 | help="delete refs that no longer exist on the remote (default)", |
| 468 | ) |
| 469 | p.add_option( |
| 470 | "--no-prune", |
| 471 | dest="prune", |
| 472 | action="store_false", |
| 473 | help="do not delete refs that no longer exist on the remote", |
| 474 | ) |
| 475 | p.add_option( |
| 476 | "--auto-gc", |
| 477 | action="store_true", |
| 478 | default=None, |
| 479 | help="run garbage collection on all synced projects", |
| 480 | ) |
| 481 | p.add_option( |
| 482 | "--no-auto-gc", |
| 483 | dest="auto_gc", |
| 484 | action="store_false", |
| 485 | help="do not run garbage collection on any projects (default)", |
| 486 | ) |
| 487 | if show_smart: |
| 488 | p.add_option( |
| 489 | "-s", |
| 490 | "--smart-sync", |
| 491 | dest="smart_sync", |
| 492 | action="store_true", |
| 493 | help="smart sync using manifest from the latest known good " |
| 494 | "build", |
| 495 | ) |
| 496 | p.add_option( |
| 497 | "-t", |
| 498 | "--smart-tag", |
| 499 | dest="smart_tag", |
| 500 | action="store", |
| 501 | help="smart sync using manifest from a known tag", |
| 502 | ) |
Shawn O. Pearce | 3e768c9 | 2009-04-10 16:59:36 -0700 | [diff] [blame] | 503 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 504 | g = p.add_option_group("repo Version options") |
| 505 | g.add_option( |
| 506 | "--no-repo-verify", |
| 507 | dest="repo_verify", |
| 508 | default=True, |
| 509 | action="store_false", |
| 510 | help="do not verify repo source code", |
| 511 | ) |
| 512 | g.add_option( |
| 513 | "--repo-upgraded", |
| 514 | dest="repo_upgraded", |
| 515 | action="store_true", |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 516 | help=optparse.SUPPRESS_HELP, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 517 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 518 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 519 | def _GetBranch(self, manifest_project): |
| 520 | """Returns the branch name for getting the approved smartsync manifest. |
LaMont Jones | a46047a | 2022-04-07 21:57:06 +0000 | [diff] [blame] | 521 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 522 | Args: |
| 523 | manifest_project: The manifestProject to query. |
| 524 | """ |
| 525 | b = manifest_project.GetBranch(manifest_project.CurrentBranch) |
| 526 | branch = b.merge |
| 527 | if branch.startswith(R_HEADS): |
| 528 | branch = branch[len(R_HEADS) :] |
| 529 | return branch |
Raman Tenneti | 8d43dea | 2021-02-07 16:30:27 -0800 | [diff] [blame] | 530 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 531 | def _GetCurrentBranchOnly(self, opt, manifest): |
| 532 | """Returns whether current-branch or use-superproject options are |
| 533 | enabled. |
Daniel Andersson | d52ca42 | 2022-04-01 12:55:38 +0200 | [diff] [blame] | 534 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 535 | Args: |
| 536 | opt: Program options returned from optparse. See _Options(). |
| 537 | manifest: The manifest to use. |
LaMont Jones | a46047a | 2022-04-07 21:57:06 +0000 | [diff] [blame] | 538 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 539 | Returns: |
| 540 | True if a superproject is requested, otherwise the value of the |
| 541 | current_branch option (True, False or None). |
| 542 | """ |
| 543 | return ( |
| 544 | git_superproject.UseSuperproject(opt.use_superproject, manifest) |
| 545 | or opt.current_branch_only |
| 546 | ) |
Raman Tenneti | 2ae44d7 | 2021-03-23 15:12:27 -0700 | [diff] [blame] | 547 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 548 | def _UpdateProjectsRevisionId( |
| 549 | self, opt, args, superproject_logging_data, manifest |
| 550 | ): |
| 551 | """Update revisionId of projects with the commit from the superproject. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 552 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 553 | This function updates each project's revisionId with the commit hash |
| 554 | from the superproject. It writes the updated manifest into a file and |
| 555 | reloads the manifest from it. When appropriate, sub manifests are also |
| 556 | processed. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 557 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 558 | Args: |
| 559 | opt: Program options returned from optparse. See _Options(). |
| 560 | args: Arguments to pass to GetProjects. See the GetProjects |
| 561 | docstring for details. |
| 562 | superproject_logging_data: A dictionary of superproject data to log. |
| 563 | manifest: The manifest to use. |
| 564 | """ |
| 565 | have_superproject = manifest.superproject or any( |
| 566 | m.superproject for m in manifest.all_children |
| 567 | ) |
| 568 | if not have_superproject: |
| 569 | return |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 570 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 571 | if opt.local_only and manifest.superproject: |
| 572 | manifest_path = manifest.superproject.manifest_path |
| 573 | if manifest_path: |
| 574 | self._ReloadManifest(manifest_path, manifest) |
| 575 | return |
Raman Tenneti | ae86a46 | 2021-07-27 08:54:59 -0700 | [diff] [blame] | 576 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 577 | all_projects = self.GetProjects( |
| 578 | args, |
| 579 | missing_ok=True, |
| 580 | submodules_ok=opt.fetch_submodules, |
| 581 | manifest=manifest, |
| 582 | all_manifests=not opt.this_manifest_only, |
| 583 | ) |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 584 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 585 | per_manifest = collections.defaultdict(list) |
| 586 | if opt.this_manifest_only: |
| 587 | per_manifest[manifest.path_prefix] = all_projects |
| 588 | else: |
| 589 | for p in all_projects: |
| 590 | per_manifest[p.manifest.path_prefix].append(p) |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 591 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 592 | superproject_logging_data = {} |
| 593 | need_unload = False |
| 594 | for m in self.ManifestList(opt): |
| 595 | if m.path_prefix not in per_manifest: |
| 596 | continue |
| 597 | use_super = git_superproject.UseSuperproject( |
| 598 | opt.use_superproject, m |
| 599 | ) |
| 600 | if superproject_logging_data: |
| 601 | superproject_logging_data["multimanifest"] = True |
| 602 | superproject_logging_data.update( |
| 603 | superproject=use_super, |
| 604 | haslocalmanifests=bool(m.HasLocalManifests), |
| 605 | hassuperprojecttag=bool(m.superproject), |
| 606 | ) |
| 607 | if use_super and (m.IsMirror or m.IsArchive): |
| 608 | # Don't use superproject, because we have no working tree. |
| 609 | use_super = False |
| 610 | superproject_logging_data["superproject"] = False |
| 611 | superproject_logging_data["noworktree"] = True |
| 612 | if opt.use_superproject is not False: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 613 | logger.warning( |
| 614 | "%s: not using superproject because there is no " |
| 615 | "working tree.", |
| 616 | m.path_prefix, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 617 | ) |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 618 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 619 | if not use_super: |
| 620 | continue |
| 621 | m.superproject.SetQuiet(opt.quiet) |
| 622 | print_messages = git_superproject.PrintMessages( |
| 623 | opt.use_superproject, m |
| 624 | ) |
| 625 | m.superproject.SetPrintMessages(print_messages) |
| 626 | update_result = m.superproject.UpdateProjectsRevisionId( |
| 627 | per_manifest[m.path_prefix], git_event_log=self.git_event_log |
| 628 | ) |
| 629 | manifest_path = update_result.manifest_path |
| 630 | superproject_logging_data["updatedrevisionid"] = bool(manifest_path) |
| 631 | if manifest_path: |
| 632 | m.SetManifestOverride(manifest_path) |
| 633 | need_unload = True |
| 634 | else: |
| 635 | if print_messages: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 636 | logger.warning( |
| 637 | "%s: warning: Update of revisionId from superproject " |
| 638 | "has failed, repo sync will not use superproject to " |
| 639 | "fetch the source. Please resync with the " |
| 640 | "--no-use-superproject option to avoid this repo " |
| 641 | "warning.", |
| 642 | m.path_prefix, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 643 | ) |
| 644 | if update_result.fatal and opt.use_superproject is not None: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 645 | raise SuperprojectError() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 646 | if need_unload: |
| 647 | m.outer_client.manifest.Unload() |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 648 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 649 | def _FetchProjectList(self, opt, projects): |
| 650 | """Main function of the fetch worker. |
Mike Frysinger | b2fa30a | 2021-02-24 00:15:32 -0500 | [diff] [blame] | 651 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 652 | The projects we're given share the same underlying git object store, so |
| 653 | we have to fetch them in serial. |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 654 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 655 | Delegates most of the work to _FetchOne. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 656 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 657 | Args: |
| 658 | opt: Program options returned from optparse. See _Options(). |
| 659 | projects: Projects to fetch. |
| 660 | """ |
| 661 | return [self._FetchOne(opt, x) for x in projects] |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 662 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 663 | def _FetchOne(self, opt, project): |
| 664 | """Fetch git objects for a single project. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 665 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 666 | Args: |
| 667 | opt: Program options returned from optparse. See _Options(). |
| 668 | project: Project object for the project to fetch. |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 669 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 670 | Returns: |
| 671 | Whether the fetch was successful. |
| 672 | """ |
| 673 | start = time.time() |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 674 | k = f"{project.name} @ {project.relpath}" |
| 675 | self._sync_dict[k] = start |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 676 | success = False |
| 677 | remote_fetched = False |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 678 | errors = [] |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 679 | buf = TeeStringIO(sys.stdout if opt.verbose else None) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 680 | try: |
| 681 | sync_result = project.Sync_NetworkHalf( |
| 682 | quiet=opt.quiet, |
| 683 | verbose=opt.verbose, |
| 684 | output_redir=buf, |
| 685 | current_branch_only=self._GetCurrentBranchOnly( |
| 686 | opt, project.manifest |
| 687 | ), |
| 688 | force_sync=opt.force_sync, |
| 689 | clone_bundle=opt.clone_bundle, |
| 690 | tags=opt.tags, |
| 691 | archive=project.manifest.IsArchive, |
| 692 | optimized_fetch=opt.optimized_fetch, |
| 693 | retry_fetches=opt.retry_fetches, |
| 694 | prune=opt.prune, |
| 695 | ssh_proxy=self.ssh_proxy, |
| 696 | clone_filter=project.manifest.CloneFilter, |
| 697 | partial_clone_exclude=project.manifest.PartialCloneExclude, |
Jason Chang | 1783332 | 2023-05-23 13:06:55 -0700 | [diff] [blame] | 698 | clone_filter_for_depth=project.manifest.CloneFilterForDepth, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 699 | ) |
| 700 | success = sync_result.success |
| 701 | remote_fetched = sync_result.remote_fetched |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 702 | if sync_result.error: |
| 703 | errors.append(sync_result.error) |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 704 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 705 | output = buf.getvalue() |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 706 | if output and buf.io is None and not success: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 707 | print("\n" + output.rstrip()) |
Doug Anderson | fc06ced | 2011-03-16 15:49:18 -0700 | [diff] [blame] | 708 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 709 | if not success: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 710 | logger.error( |
| 711 | "error: Cannot fetch %s from %s", |
| 712 | project.name, |
| 713 | project.remote.url, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 714 | ) |
| 715 | except KeyboardInterrupt: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 716 | logger.error("Keyboard interrupt while processing %s", project.name) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 717 | except GitError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 718 | logger.error("error.GitError: Cannot fetch %s", e) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 719 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 720 | except Exception as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 721 | logger.error( |
| 722 | "error: Cannot fetch %s (%s: %s)", |
| 723 | project.name, |
| 724 | type(e).__name__, |
| 725 | e, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 726 | ) |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 727 | del self._sync_dict[k] |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 728 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 729 | raise |
Mike Frysinger | 7b586f2 | 2021-02-23 18:38:39 -0500 | [diff] [blame] | 730 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 731 | finish = time.time() |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 732 | del self._sync_dict[k] |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 733 | return _FetchOneResult( |
| 734 | success, errors, project, start, finish, remote_fetched |
| 735 | ) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 736 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 737 | @classmethod |
| 738 | def _FetchInitChild(cls, ssh_proxy): |
| 739 | cls.ssh_proxy = ssh_proxy |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 740 | |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 741 | def _GetSyncProgressMessage(self): |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 742 | earliest_time = float("inf") |
| 743 | earliest_proj = None |
Gavin Mak | 945c006 | 2023-05-30 20:04:07 +0000 | [diff] [blame] | 744 | items = self._sync_dict.items() |
| 745 | for project, t in items: |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 746 | if t < earliest_time: |
| 747 | earliest_time = t |
| 748 | earliest_proj = project |
| 749 | |
Josip Sokcevic | 71122f9 | 2023-05-26 02:44:37 +0000 | [diff] [blame] | 750 | if not earliest_proj: |
Gavin Mak | 945c006 | 2023-05-30 20:04:07 +0000 | [diff] [blame] | 751 | # This function is called when sync is still running but in some |
| 752 | # cases (by chance), _sync_dict can contain no entries. Return some |
| 753 | # text to indicate that sync is still working. |
| 754 | return "..working.." |
Josip Sokcevic | 71122f9 | 2023-05-26 02:44:37 +0000 | [diff] [blame] | 755 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 756 | elapsed = time.time() - earliest_time |
Gavin Mak | 945c006 | 2023-05-30 20:04:07 +0000 | [diff] [blame] | 757 | jobs = jobs_str(len(items)) |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 758 | return f"{jobs} | {elapsed_str(elapsed)} {earliest_proj}" |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 759 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 760 | def _Fetch(self, projects, opt, err_event, ssh_proxy, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 761 | ret = True |
Mike Frysinger | b2fa30a | 2021-02-24 00:15:32 -0500 | [diff] [blame] | 762 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 763 | jobs = opt.jobs_network |
| 764 | fetched = set() |
| 765 | remote_fetched = set() |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 766 | pm = Progress( |
| 767 | "Fetching", |
| 768 | len(projects), |
| 769 | delay=False, |
| 770 | quiet=opt.quiet, |
| 771 | show_elapsed=True, |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 772 | elide=True, |
Gavin Mak | edcaa94 | 2023-04-27 05:58:57 +0000 | [diff] [blame] | 773 | ) |
Roy Lee | 18afd7f | 2010-05-09 04:32:08 +0800 | [diff] [blame] | 774 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 775 | self._sync_dict = multiprocessing.Manager().dict() |
| 776 | sync_event = _threading.Event() |
| 777 | |
| 778 | def _MonitorSyncLoop(): |
| 779 | while True: |
Gavin Mak | 04cba4a | 2023-05-24 21:28:28 +0000 | [diff] [blame] | 780 | pm.update(inc=0, msg=self._GetSyncProgressMessage()) |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 781 | if sync_event.wait(timeout=1): |
| 782 | return |
| 783 | |
| 784 | sync_progress_thread = _threading.Thread(target=_MonitorSyncLoop) |
| 785 | sync_progress_thread.daemon = True |
| 786 | sync_progress_thread.start() |
| 787 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 788 | objdir_project_map = dict() |
| 789 | for project in projects: |
| 790 | objdir_project_map.setdefault(project.objdir, []).append(project) |
| 791 | projects_list = list(objdir_project_map.values()) |
David James | 8d20116 | 2013-10-11 17:03:19 -0700 | [diff] [blame] | 792 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 793 | def _ProcessResults(results_sets): |
| 794 | ret = True |
| 795 | for results in results_sets: |
| 796 | for result in results: |
| 797 | success = result.success |
| 798 | project = result.project |
| 799 | start = result.start |
| 800 | finish = result.finish |
| 801 | self._fetch_times.Set(project, finish - start) |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 802 | self._local_sync_state.SetFetchTime(project) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 803 | self.event_log.AddSync( |
| 804 | project, |
| 805 | event_log.TASK_SYNC_NETWORK, |
| 806 | start, |
| 807 | finish, |
| 808 | success, |
| 809 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 810 | if result.errors: |
| 811 | errors.extend(result.errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 812 | if result.remote_fetched: |
| 813 | remote_fetched.add(project) |
| 814 | # Check for any errors before running any more tasks. |
| 815 | # ...we'll let existing jobs finish, though. |
| 816 | if not success: |
| 817 | ret = False |
| 818 | else: |
| 819 | fetched.add(project.gitdir) |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 820 | pm.update() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 821 | if not ret and opt.fail_fast: |
| 822 | break |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 823 | return ret |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 824 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 825 | # We pass the ssh proxy settings via the class. This allows |
| 826 | # multiprocessing to pickle it up when spawning children. We can't pass |
| 827 | # it as an argument to _FetchProjectList below as multiprocessing is |
| 828 | # unable to pickle those. |
| 829 | Sync.ssh_proxy = None |
Mike Frysinger | ebf04a4 | 2021-02-23 20:48:04 -0500 | [diff] [blame] | 830 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 831 | # NB: Multiprocessing is heavy, so don't spin it up for one job. |
| 832 | if len(projects_list) == 1 or jobs == 1: |
| 833 | self._FetchInitChild(ssh_proxy) |
| 834 | if not _ProcessResults( |
| 835 | self._FetchProjectList(opt, x) for x in projects_list |
| 836 | ): |
| 837 | ret = False |
LaMont Jones | fa8d939 | 2022-11-02 22:01:29 +0000 | [diff] [blame] | 838 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 839 | # Favor throughput over responsiveness when quiet. It seems that |
| 840 | # imap() will yield results in batches relative to chunksize, so |
| 841 | # even as the children finish a sync, we won't see the result until |
| 842 | # one child finishes ~chunksize jobs. When using a large --jobs |
| 843 | # with large chunksize, this can be jarring as there will be a large |
| 844 | # initial delay where repo looks like it isn't doing anything and |
| 845 | # sits at 0%, but then suddenly completes a lot of jobs all at once. |
| 846 | # Since this code is more network bound, we can accept a bit more |
| 847 | # CPU overhead with a smaller chunksize so that the user sees more |
| 848 | # immediate & continuous feedback. |
| 849 | if opt.quiet: |
| 850 | chunksize = WORKER_BATCH_SIZE |
| 851 | else: |
| 852 | pm.update(inc=0, msg="warming up") |
| 853 | chunksize = 4 |
| 854 | with multiprocessing.Pool( |
| 855 | jobs, initializer=self._FetchInitChild, initargs=(ssh_proxy,) |
| 856 | ) as pool: |
| 857 | results = pool.imap_unordered( |
| 858 | functools.partial(self._FetchProjectList, opt), |
| 859 | projects_list, |
| 860 | chunksize=chunksize, |
| 861 | ) |
| 862 | if not _ProcessResults(results): |
| 863 | ret = False |
| 864 | pool.close() |
LaMont Jones | fa8d939 | 2022-11-02 22:01:29 +0000 | [diff] [blame] | 865 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 866 | # Cleanup the reference now that we're done with it, and we're going to |
| 867 | # release any resources it points to. If we don't, later |
| 868 | # multiprocessing usage (e.g. checkouts) will try to pickle and then |
| 869 | # crash. |
| 870 | del Sync.ssh_proxy |
LaMont Jones | 7efab53 | 2022-09-01 15:41:12 +0000 | [diff] [blame] | 871 | |
Gavin Mak | 551285f | 2023-05-04 04:48:43 +0000 | [diff] [blame] | 872 | sync_event.set() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 873 | pm.end() |
| 874 | self._fetch_times.Save() |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 875 | self._local_sync_state.Save() |
LaMont Jones | 43549d8 | 2022-11-30 19:55:30 +0000 | [diff] [blame] | 876 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 877 | if not self.outer_client.manifest.IsArchive: |
| 878 | self._GCProjects(projects, opt, err_event) |
Mike Frysinger | 65af260 | 2021-04-08 22:47:44 -0400 | [diff] [blame] | 879 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 880 | return _FetchResult(ret, fetched) |
LaMont Jones | fa8d939 | 2022-11-02 22:01:29 +0000 | [diff] [blame] | 881 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 882 | def _FetchMain( |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 883 | self, opt, args, all_projects, err_event, ssh_proxy, manifest, errors |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 884 | ): |
| 885 | """The main network fetch loop. |
Mike Frysinger | 65af260 | 2021-04-08 22:47:44 -0400 | [diff] [blame] | 886 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 887 | Args: |
| 888 | opt: Program options returned from optparse. See _Options(). |
| 889 | args: Command line args used to filter out projects. |
| 890 | all_projects: List of all projects that should be fetched. |
| 891 | err_event: Whether an error was hit while processing. |
| 892 | ssh_proxy: SSH manager for clients & masters. |
| 893 | manifest: The manifest to use. |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 894 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 895 | Returns: |
| 896 | List of all projects that should be checked out. |
| 897 | """ |
| 898 | rp = manifest.repoProject |
LaMont Jones | 891e8f7 | 2022-09-08 20:17:58 +0000 | [diff] [blame] | 899 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 900 | to_fetch = [] |
| 901 | now = time.time() |
| 902 | if _ONE_DAY_S <= (now - rp.LastFetch): |
| 903 | to_fetch.append(rp) |
| 904 | to_fetch.extend(all_projects) |
| 905 | to_fetch.sort(key=self._fetch_times.Get, reverse=True) |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 906 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 907 | result = self._Fetch(to_fetch, opt, err_event, ssh_proxy, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 908 | success = result.success |
| 909 | fetched = result.projects |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 910 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 911 | if not success: |
| 912 | err_event.set() |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 913 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 914 | _PostRepoFetch(rp, opt.repo_verify) |
| 915 | if opt.network_only: |
| 916 | # Bail out now; the rest touches the working tree. |
| 917 | if err_event.is_set(): |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 918 | e = SyncError( |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 919 | "error: Exited sync due to fetch errors.", |
| 920 | aggregate_errors=errors, |
| 921 | ) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 922 | |
| 923 | logger.error(e) |
| 924 | raise e |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 925 | return _FetchMainResult([]) |
Dave Borowitz | 1885721 | 2012-10-23 17:02:59 -0700 | [diff] [blame] | 926 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 927 | # Iteratively fetch missing and/or nested unregistered submodules. |
| 928 | previously_missing_set = set() |
| 929 | while True: |
| 930 | self._ReloadManifest(None, manifest) |
| 931 | all_projects = self.GetProjects( |
| 932 | args, |
| 933 | missing_ok=True, |
| 934 | submodules_ok=opt.fetch_submodules, |
LaMont Jones | a46047a | 2022-04-07 21:57:06 +0000 | [diff] [blame] | 935 | manifest=manifest, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 936 | all_manifests=not opt.this_manifest_only, |
| 937 | ) |
| 938 | missing = [] |
| 939 | for project in all_projects: |
| 940 | if project.gitdir not in fetched: |
| 941 | missing.append(project) |
| 942 | if not missing: |
| 943 | break |
| 944 | # Stop us from non-stopped fetching actually-missing repos: If set |
| 945 | # of missing repos has not been changed from last fetch, we break. |
| 946 | missing_set = set(p.name for p in missing) |
| 947 | if previously_missing_set == missing_set: |
| 948 | break |
| 949 | previously_missing_set = missing_set |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 950 | result = self._Fetch(missing, opt, err_event, ssh_proxy, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 951 | success = result.success |
| 952 | new_fetched = result.projects |
| 953 | if not success: |
| 954 | err_event.set() |
| 955 | fetched.update(new_fetched) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 956 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 957 | return _FetchMainResult(all_projects) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 958 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 959 | def _CheckoutOne(self, detach_head, force_sync, project): |
| 960 | """Checkout work tree for one project |
jiajia tang | a590e64 | 2021-04-25 20:02:02 +0800 | [diff] [blame] | 961 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 962 | Args: |
| 963 | detach_head: Whether to leave a detached HEAD. |
| 964 | force_sync: Force checking out of the repo. |
| 965 | project: Project object for the project to checkout. |
jiajia tang | a590e64 | 2021-04-25 20:02:02 +0800 | [diff] [blame] | 966 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 967 | Returns: |
| 968 | Whether the fetch was successful. |
| 969 | """ |
| 970 | start = time.time() |
| 971 | syncbuf = SyncBuffer( |
| 972 | project.manifest.manifestProject.config, detach_head=detach_head |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 973 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 974 | success = False |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 975 | errors = [] |
David Pursehouse | 59b4174 | 2015-05-07 14:36:09 +0900 | [diff] [blame] | 976 | try: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 977 | project.Sync_LocalHalf( |
| 978 | syncbuf, force_sync=force_sync, errors=errors |
| 979 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 980 | success = syncbuf.Finish() |
| 981 | except GitError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 982 | logger.error( |
| 983 | "error.GitError: Cannot checkout %s: %s", project.name, e |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 984 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 985 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 986 | except Exception as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 987 | logger.error( |
| 988 | "error: Cannot checkout %s: %s: %s", |
| 989 | project.name, |
| 990 | type(e).__name__, |
| 991 | e, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 992 | ) |
| 993 | raise |
Nico Sallembien | a1bfd2c | 2010-04-06 10:40:01 -0700 | [diff] [blame] | 994 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 995 | if not success: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 996 | logger.error("error: Cannot checkout %s", project.name) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 997 | finish = time.time() |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 998 | return _CheckoutOneResult(success, errors, project, start, finish) |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 999 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1000 | def _Checkout(self, all_projects, opt, err_results, checkout_errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1001 | """Checkout projects listed in all_projects |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1002 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1003 | Args: |
| 1004 | all_projects: List of all projects that should be checked out. |
| 1005 | opt: Program options returned from optparse. See _Options(). |
| 1006 | err_results: A list of strings, paths to git repos where checkout |
| 1007 | failed. |
| 1008 | """ |
| 1009 | # Only checkout projects with worktrees. |
| 1010 | all_projects = [x for x in all_projects if x.worktree] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1011 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1012 | def _ProcessResults(pool, pm, results): |
| 1013 | ret = True |
| 1014 | for result in results: |
| 1015 | success = result.success |
| 1016 | project = result.project |
| 1017 | start = result.start |
| 1018 | finish = result.finish |
| 1019 | self.event_log.AddSync( |
| 1020 | project, event_log.TASK_SYNC_LOCAL, start, finish, success |
| 1021 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1022 | |
| 1023 | if result.errors: |
| 1024 | checkout_errors.extend(result.errors) |
| 1025 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1026 | # Check for any errors before running any more tasks. |
| 1027 | # ...we'll let existing jobs finish, though. |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 1028 | if success: |
| 1029 | self._local_sync_state.SetCheckoutTime(project) |
| 1030 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1031 | ret = False |
| 1032 | err_results.append( |
| 1033 | project.RelPath(local=opt.this_manifest_only) |
| 1034 | ) |
| 1035 | if opt.fail_fast: |
| 1036 | if pool: |
| 1037 | pool.close() |
| 1038 | return ret |
| 1039 | pm.update(msg=project.name) |
| 1040 | return ret |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 1041 | |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 1042 | proc_res = self.ExecuteInParallel( |
| 1043 | opt.jobs_checkout, |
| 1044 | functools.partial( |
| 1045 | self._CheckoutOne, opt.detach_head, opt.force_sync |
| 1046 | ), |
| 1047 | all_projects, |
| 1048 | callback=_ProcessResults, |
| 1049 | output=Progress("Checking out", len(all_projects), quiet=opt.quiet), |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1050 | ) |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1051 | |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 1052 | self._local_sync_state.Save() |
| 1053 | return proc_res and not err_results |
| 1054 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1055 | @staticmethod |
| 1056 | def _GetPreciousObjectsState(project: Project, opt): |
| 1057 | """Get the preciousObjects state for the project. |
Mike Frysinger | 355f439 | 2022-07-20 17:15:29 -0400 | [diff] [blame] | 1058 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1059 | Args: |
| 1060 | project (Project): the project to examine, and possibly correct. |
| 1061 | opt (optparse.Values): options given to sync. |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 1062 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1063 | Returns: |
| 1064 | Expected state of extensions.preciousObjects: |
| 1065 | False: Should be disabled. (not present) |
| 1066 | True: Should be enabled. |
| 1067 | """ |
| 1068 | if project.use_git_worktrees: |
| 1069 | return False |
| 1070 | projects = project.manifest.GetProjectsWithName( |
| 1071 | project.name, all_manifests=True |
| 1072 | ) |
| 1073 | if len(projects) == 1: |
| 1074 | return False |
| 1075 | if len(projects) > 1: |
| 1076 | # Objects are potentially shared with another project. |
| 1077 | # See the logic in Project.Sync_NetworkHalf regarding UseAlternates. |
| 1078 | # - When False, shared projects share (via symlink) |
| 1079 | # .repo/project-objects/{PROJECT_NAME}.git as the one-and-only |
| 1080 | # objects directory. All objects are precious, since there is no |
| 1081 | # project with a complete set of refs. |
| 1082 | # - When True, shared projects share (via info/alternates) |
| 1083 | # .repo/project-objects/{PROJECT_NAME}.git as an alternate object |
| 1084 | # store, which is written only on the first clone of the project, |
| 1085 | # and is not written subsequently. (When Sync_NetworkHalf sees |
| 1086 | # that it exists, it makes sure that the alternates file points |
| 1087 | # there, and uses a project-local .git/objects directory for all |
| 1088 | # syncs going forward. |
| 1089 | # We do not support switching between the options. The environment |
| 1090 | # variable is present for testing and migration only. |
| 1091 | return not project.UseAlternates |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1092 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1093 | return False |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 1094 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1095 | def _SetPreciousObjectsState(self, project: Project, opt): |
| 1096 | """Correct the preciousObjects state for the project. |
| 1097 | |
| 1098 | Args: |
| 1099 | project: the project to examine, and possibly correct. |
| 1100 | opt: options given to sync. |
| 1101 | """ |
| 1102 | expected = self._GetPreciousObjectsState(project, opt) |
| 1103 | actual = ( |
| 1104 | project.config.GetBoolean("extensions.preciousObjects") or False |
| 1105 | ) |
| 1106 | relpath = project.RelPath(local=opt.this_manifest_only) |
| 1107 | |
| 1108 | if expected != actual: |
| 1109 | # If this is unexpected, log it and repair. |
| 1110 | Trace( |
| 1111 | f"{relpath} expected preciousObjects={expected}, got {actual}" |
| 1112 | ) |
| 1113 | if expected: |
| 1114 | if not opt.quiet: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1115 | logger.info( |
| 1116 | "%s: Shared project %s found, disabling pruning.", |
| 1117 | relpath, |
| 1118 | project.name, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1119 | ) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1120 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1121 | if git_require((2, 7, 0)): |
| 1122 | project.EnableRepositoryExtension("preciousObjects") |
| 1123 | else: |
| 1124 | # This isn't perfect, but it's the best we can do with old |
| 1125 | # git. |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1126 | logger.warning( |
| 1127 | "%s: WARNING: shared projects are unreliable when " |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1128 | "using old versions of git; please upgrade to " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1129 | "git-2.7.0+.", |
| 1130 | relpath, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1131 | ) |
| 1132 | project.config.SetString("gc.pruneExpire", "never") |
| 1133 | else: |
| 1134 | if not opt.quiet: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1135 | logger.info("%s: not shared, disabling pruning.", relpath) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1136 | project.config.SetString("extensions.preciousObjects", None) |
| 1137 | project.config.SetString("gc.pruneExpire", None) |
| 1138 | |
| 1139 | def _GCProjects(self, projects, opt, err_event): |
| 1140 | """Perform garbage collection. |
| 1141 | |
| 1142 | If We are skipping garbage collection (opt.auto_gc not set), we still |
| 1143 | want to potentially mark objects precious, so that `git gc` does not |
| 1144 | discard shared objects. |
| 1145 | """ |
| 1146 | if not opt.auto_gc: |
| 1147 | # Just repair preciousObjects state, and return. |
| 1148 | for project in projects: |
| 1149 | self._SetPreciousObjectsState(project, opt) |
| 1150 | return |
| 1151 | |
| 1152 | pm = Progress( |
| 1153 | "Garbage collecting", len(projects), delay=False, quiet=opt.quiet |
| 1154 | ) |
| 1155 | pm.update(inc=0, msg="prescan") |
| 1156 | |
| 1157 | tidy_dirs = {} |
| 1158 | for project in projects: |
| 1159 | self._SetPreciousObjectsState(project, opt) |
| 1160 | |
| 1161 | project.config.SetString("gc.autoDetach", "false") |
| 1162 | # Only call git gc once per objdir, but call pack-refs for the |
| 1163 | # remainder. |
| 1164 | if project.objdir not in tidy_dirs: |
| 1165 | tidy_dirs[project.objdir] = ( |
| 1166 | True, # Run a full gc. |
| 1167 | project.bare_git, |
| 1168 | ) |
| 1169 | elif project.gitdir not in tidy_dirs: |
| 1170 | tidy_dirs[project.gitdir] = ( |
| 1171 | False, # Do not run a full gc; just run pack-refs. |
| 1172 | project.bare_git, |
| 1173 | ) |
| 1174 | |
| 1175 | jobs = opt.jobs |
| 1176 | |
| 1177 | if jobs < 2: |
| 1178 | for run_gc, bare_git in tidy_dirs.values(): |
| 1179 | pm.update(msg=bare_git._project.name) |
| 1180 | |
| 1181 | if run_gc: |
| 1182 | bare_git.gc("--auto") |
| 1183 | else: |
| 1184 | bare_git.pack_refs() |
| 1185 | pm.end() |
| 1186 | return |
| 1187 | |
| 1188 | cpu_count = os.cpu_count() |
| 1189 | config = {"pack.threads": cpu_count // jobs if cpu_count > jobs else 1} |
| 1190 | |
| 1191 | threads = set() |
| 1192 | sem = _threading.Semaphore(jobs) |
| 1193 | |
| 1194 | def tidy_up(run_gc, bare_git): |
| 1195 | pm.start(bare_git._project.name) |
| 1196 | try: |
| 1197 | try: |
| 1198 | if run_gc: |
| 1199 | bare_git.gc("--auto", config=config) |
| 1200 | else: |
| 1201 | bare_git.pack_refs(config=config) |
| 1202 | except GitError: |
| 1203 | err_event.set() |
| 1204 | except Exception: |
| 1205 | err_event.set() |
| 1206 | raise |
| 1207 | finally: |
| 1208 | pm.finish(bare_git._project.name) |
| 1209 | sem.release() |
| 1210 | |
| 1211 | for run_gc, bare_git in tidy_dirs.values(): |
| 1212 | if err_event.is_set() and opt.fail_fast: |
| 1213 | break |
| 1214 | sem.acquire() |
| 1215 | t = _threading.Thread( |
| 1216 | target=tidy_up, |
| 1217 | args=( |
| 1218 | run_gc, |
| 1219 | bare_git, |
| 1220 | ), |
| 1221 | ) |
| 1222 | t.daemon = True |
| 1223 | threads.add(t) |
| 1224 | t.start() |
| 1225 | |
| 1226 | for t in threads: |
| 1227 | t.join() |
| 1228 | pm.end() |
| 1229 | |
| 1230 | def _ReloadManifest(self, manifest_name, manifest): |
| 1231 | """Reload the manfiest from the file specified by the |manifest_name|. |
| 1232 | |
| 1233 | It unloads the manifest if |manifest_name| is None. |
| 1234 | |
| 1235 | Args: |
| 1236 | manifest_name: Manifest file to be reloaded. |
| 1237 | manifest: The manifest to use. |
| 1238 | """ |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 1239 | if manifest_name: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1240 | # Override calls Unload already. |
| 1241 | manifest.Override(manifest_name) |
Dan Willemsen | 5ea32d1 | 2015-09-08 13:27:20 -0700 | [diff] [blame] | 1242 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1243 | manifest.Unload() |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 1244 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1245 | def UpdateProjectList(self, opt, manifest): |
| 1246 | """Update the cached projects list for |manifest| |
LaMont Jones | bdcba7d | 2022-04-11 22:50:11 +0000 | [diff] [blame] | 1247 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1248 | In a multi-manifest checkout, each manifest has its own project.list. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1249 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1250 | Args: |
| 1251 | opt: Program options returned from optparse. See _Options(). |
| 1252 | manifest: The manifest to use. |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 1253 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1254 | Returns: |
| 1255 | 0: success |
| 1256 | 1: failure |
| 1257 | """ |
| 1258 | new_project_paths = [] |
| 1259 | for project in self.GetProjects( |
| 1260 | None, missing_ok=True, manifest=manifest, all_manifests=False |
| 1261 | ): |
| 1262 | if project.relpath: |
| 1263 | new_project_paths.append(project.relpath) |
| 1264 | file_name = "project.list" |
| 1265 | file_path = os.path.join(manifest.subdir, file_name) |
| 1266 | old_project_paths = [] |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 1267 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1268 | if os.path.exists(file_path): |
| 1269 | with open(file_path, "r") as fd: |
| 1270 | old_project_paths = fd.read().split("\n") |
| 1271 | # In reversed order, so subfolders are deleted before parent folder. |
| 1272 | for path in sorted(old_project_paths, reverse=True): |
| 1273 | if not path: |
| 1274 | continue |
| 1275 | if path not in new_project_paths: |
| 1276 | # If the path has already been deleted, we don't need to do |
| 1277 | # it. |
| 1278 | gitdir = os.path.join(manifest.topdir, path, ".git") |
| 1279 | if os.path.exists(gitdir): |
| 1280 | project = Project( |
| 1281 | manifest=manifest, |
| 1282 | name=path, |
| 1283 | remote=RemoteSpec("origin"), |
| 1284 | gitdir=gitdir, |
| 1285 | objdir=gitdir, |
| 1286 | use_git_worktrees=os.path.isfile(gitdir), |
| 1287 | worktree=os.path.join(manifest.topdir, path), |
| 1288 | relpath=path, |
| 1289 | revisionExpr="HEAD", |
| 1290 | revisionId=None, |
| 1291 | groups=None, |
| 1292 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1293 | project.DeleteWorktree( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1294 | quiet=opt.quiet, force=opt.force_remove_dirty |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1295 | ) |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 1296 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1297 | new_project_paths.sort() |
| 1298 | with open(file_path, "w") as fd: |
| 1299 | fd.write("\n".join(new_project_paths)) |
| 1300 | fd.write("\n") |
| 1301 | return 0 |
Che-Liang Chiou | b2bd91c | 2012-01-11 11:28:42 +0800 | [diff] [blame] | 1302 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1303 | def UpdateCopyLinkfileList(self, manifest): |
| 1304 | """Save all dests of copyfile and linkfile, and update them if needed. |
Shawn O. Pearce | cd1d7ff | 2009-06-04 16:15:53 -0700 | [diff] [blame] | 1305 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1306 | Returns: |
| 1307 | Whether update was successful. |
| 1308 | """ |
| 1309 | new_paths = {} |
| 1310 | new_linkfile_paths = [] |
| 1311 | new_copyfile_paths = [] |
| 1312 | for project in self.GetProjects( |
| 1313 | None, missing_ok=True, manifest=manifest, all_manifests=False |
| 1314 | ): |
| 1315 | new_linkfile_paths.extend(x.dest for x in project.linkfiles) |
| 1316 | new_copyfile_paths.extend(x.dest for x in project.copyfiles) |
Jaikumar Ganesh | 4f2517f | 2009-06-01 21:10:33 -0700 | [diff] [blame] | 1317 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1318 | new_paths = { |
| 1319 | "linkfile": new_linkfile_paths, |
| 1320 | "copyfile": new_copyfile_paths, |
| 1321 | } |
jiajia tang | a590e64 | 2021-04-25 20:02:02 +0800 | [diff] [blame] | 1322 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1323 | copylinkfile_name = "copy-link-files.json" |
| 1324 | copylinkfile_path = os.path.join(manifest.subdir, copylinkfile_name) |
| 1325 | old_copylinkfile_paths = {} |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1326 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1327 | if os.path.exists(copylinkfile_path): |
| 1328 | with open(copylinkfile_path, "rb") as fp: |
| 1329 | try: |
| 1330 | old_copylinkfile_paths = json.load(fp) |
| 1331 | except Exception: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1332 | logger.error( |
| 1333 | "error: %s is not a json formatted file.", |
| 1334 | copylinkfile_path, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1335 | ) |
| 1336 | platform_utils.remove(copylinkfile_path) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1337 | raise |
Doug Anderson | 2b8db3c | 2010-11-01 15:08:06 -0700 | [diff] [blame] | 1338 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1339 | need_remove_files = [] |
| 1340 | need_remove_files.extend( |
| 1341 | set(old_copylinkfile_paths.get("linkfile", [])) |
| 1342 | - set(new_linkfile_paths) |
| 1343 | ) |
| 1344 | need_remove_files.extend( |
| 1345 | set(old_copylinkfile_paths.get("copyfile", [])) |
| 1346 | - set(new_copyfile_paths) |
| 1347 | ) |
Mike Frysinger | 5a03308 | 2019-09-23 19:21:20 -0400 | [diff] [blame] | 1348 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1349 | for need_remove_file in need_remove_files: |
| 1350 | # Try to remove the updated copyfile or linkfile. |
| 1351 | # So, if the file is not exist, nothing need to do. |
| 1352 | platform_utils.remove(need_remove_file, missing_ok=True) |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 1353 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1354 | # Create copy-link-files.json, save dest path of "copyfile" and |
| 1355 | # "linkfile". |
| 1356 | with open(copylinkfile_path, "w", encoding="utf-8") as fp: |
| 1357 | json.dump(new_paths, fp) |
| 1358 | return True |
Raman Tenneti | 7954de1 | 2021-07-28 14:36:49 -0700 | [diff] [blame] | 1359 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1360 | def _SmartSyncSetup(self, opt, smart_sync_manifest_path, manifest): |
| 1361 | if not manifest.manifest_server: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1362 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1363 | "error: cannot smart sync: no manifest server defined in " |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1364 | "manifest" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1365 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1366 | |
| 1367 | manifest_server = manifest.manifest_server |
| 1368 | if not opt.quiet: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1369 | logger.info("Using manifest server %s", manifest_server) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1370 | |
| 1371 | if "@" not in manifest_server: |
| 1372 | username = None |
| 1373 | password = None |
| 1374 | if opt.manifest_server_username and opt.manifest_server_password: |
| 1375 | username = opt.manifest_server_username |
| 1376 | password = opt.manifest_server_password |
| 1377 | else: |
| 1378 | try: |
| 1379 | info = netrc.netrc() |
| 1380 | except IOError: |
| 1381 | # .netrc file does not exist or could not be opened. |
| 1382 | pass |
| 1383 | else: |
| 1384 | try: |
| 1385 | parse_result = urllib.parse.urlparse(manifest_server) |
| 1386 | if parse_result.hostname: |
| 1387 | auth = info.authenticators(parse_result.hostname) |
| 1388 | if auth: |
| 1389 | username, _account, password = auth |
| 1390 | else: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1391 | logger.error( |
| 1392 | "No credentials found for %s in .netrc", |
| 1393 | parse_result.hostname, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1394 | ) |
| 1395 | except netrc.NetrcParseError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1396 | logger.error("Error parsing .netrc file: %s", e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1397 | |
| 1398 | if username and password: |
| 1399 | manifest_server = manifest_server.replace( |
| 1400 | "://", "://%s:%s@" % (username, password), 1 |
| 1401 | ) |
| 1402 | |
| 1403 | transport = PersistentTransport(manifest_server) |
| 1404 | if manifest_server.startswith("persistent-"): |
| 1405 | manifest_server = manifest_server[len("persistent-") :] |
| 1406 | |
| 1407 | try: |
| 1408 | server = xmlrpc.client.Server(manifest_server, transport=transport) |
| 1409 | if opt.smart_sync: |
| 1410 | branch = self._GetBranch(manifest.manifestProject) |
| 1411 | |
| 1412 | if "SYNC_TARGET" in os.environ: |
| 1413 | target = os.environ["SYNC_TARGET"] |
| 1414 | [success, manifest_str] = server.GetApprovedManifest( |
| 1415 | branch, target |
| 1416 | ) |
| 1417 | elif ( |
| 1418 | "TARGET_PRODUCT" in os.environ |
| 1419 | and "TARGET_BUILD_VARIANT" in os.environ |
| 1420 | ): |
| 1421 | target = "%s-%s" % ( |
| 1422 | os.environ["TARGET_PRODUCT"], |
| 1423 | os.environ["TARGET_BUILD_VARIANT"], |
| 1424 | ) |
| 1425 | [success, manifest_str] = server.GetApprovedManifest( |
| 1426 | branch, target |
| 1427 | ) |
| 1428 | else: |
| 1429 | [success, manifest_str] = server.GetApprovedManifest(branch) |
| 1430 | else: |
| 1431 | assert opt.smart_tag |
| 1432 | [success, manifest_str] = server.GetManifest(opt.smart_tag) |
| 1433 | |
| 1434 | if success: |
| 1435 | manifest_name = os.path.basename(smart_sync_manifest_path) |
| 1436 | try: |
| 1437 | with open(smart_sync_manifest_path, "w") as f: |
| 1438 | f.write(manifest_str) |
| 1439 | except IOError as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1440 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1441 | "error: cannot write manifest to %s:\n%s" |
| 1442 | % (smart_sync_manifest_path, e), |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1443 | aggregate_errors=[e], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1444 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1445 | self._ReloadManifest(manifest_name, manifest) |
| 1446 | else: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1447 | raise SmartSyncError( |
| 1448 | "error: manifest server RPC call failed: %s" % manifest_str |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1449 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1450 | except (socket.error, IOError, xmlrpc.client.Fault) as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1451 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1452 | "error: cannot connect to manifest server %s:\n%s" |
| 1453 | % (manifest.manifest_server, e), |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1454 | aggregate_errors=[e], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1455 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1456 | except xmlrpc.client.ProtocolError as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1457 | raise SmartSyncError( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1458 | "error: cannot connect to manifest server %s:\n%d %s" |
| 1459 | % (manifest.manifest_server, e.errcode, e.errmsg), |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1460 | aggregate_errors=[e], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1461 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1462 | |
| 1463 | return manifest_name |
| 1464 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 1465 | def _UpdateAllManifestProjects(self, opt, mp, manifest_name, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1466 | """Fetch & update the local manifest project. |
| 1467 | |
| 1468 | After syncing the manifest project, if the manifest has any sub |
| 1469 | manifests, those are recursively processed. |
| 1470 | |
| 1471 | Args: |
| 1472 | opt: Program options returned from optparse. See _Options(). |
| 1473 | mp: the manifestProject to query. |
| 1474 | manifest_name: Manifest file to be reloaded. |
| 1475 | """ |
| 1476 | if not mp.standalone_manifest_url: |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 1477 | self._UpdateManifestProject(opt, mp, manifest_name, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1478 | |
| 1479 | if mp.manifest.submanifests: |
| 1480 | for submanifest in mp.manifest.submanifests.values(): |
| 1481 | child = submanifest.repo_client.manifest |
| 1482 | child.manifestProject.SyncWithPossibleInit( |
| 1483 | submanifest, |
| 1484 | current_branch_only=self._GetCurrentBranchOnly(opt, child), |
| 1485 | verbose=opt.verbose, |
| 1486 | tags=opt.tags, |
| 1487 | git_event_log=self.git_event_log, |
| 1488 | ) |
| 1489 | self._UpdateAllManifestProjects( |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 1490 | opt, child.manifestProject, None, errors |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1491 | ) |
| 1492 | |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 1493 | def _UpdateManifestProject(self, opt, mp, manifest_name, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1494 | """Fetch & update the local manifest project. |
| 1495 | |
| 1496 | Args: |
| 1497 | opt: Program options returned from optparse. See _Options(). |
| 1498 | mp: the manifestProject to query. |
| 1499 | manifest_name: Manifest file to be reloaded. |
| 1500 | """ |
| 1501 | if not opt.local_only: |
| 1502 | start = time.time() |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 1503 | buf = TeeStringIO(sys.stdout) |
| 1504 | try: |
| 1505 | result = mp.Sync_NetworkHalf( |
| 1506 | quiet=opt.quiet, |
| 1507 | output_redir=buf, |
| 1508 | verbose=opt.verbose, |
| 1509 | current_branch_only=self._GetCurrentBranchOnly( |
| 1510 | opt, mp.manifest |
| 1511 | ), |
| 1512 | force_sync=opt.force_sync, |
| 1513 | tags=opt.tags, |
| 1514 | optimized_fetch=opt.optimized_fetch, |
| 1515 | retry_fetches=opt.retry_fetches, |
| 1516 | submodules=mp.manifest.HasSubmodules, |
| 1517 | clone_filter=mp.manifest.CloneFilter, |
| 1518 | partial_clone_exclude=mp.manifest.PartialCloneExclude, |
| 1519 | clone_filter_for_depth=mp.manifest.CloneFilterForDepth, |
| 1520 | ) |
| 1521 | if result.error: |
| 1522 | errors.append(result.error) |
| 1523 | except KeyboardInterrupt: |
| 1524 | errors.append( |
| 1525 | ManifestInterruptError(buf.getvalue(), project=mp.name) |
| 1526 | ) |
| 1527 | raise |
| 1528 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1529 | finish = time.time() |
| 1530 | self.event_log.AddSync( |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1531 | mp, event_log.TASK_SYNC_NETWORK, start, finish, result.success |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1532 | ) |
| 1533 | |
| 1534 | if mp.HasChanges: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1535 | errors = [] |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1536 | syncbuf = SyncBuffer(mp.config) |
| 1537 | start = time.time() |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1538 | mp.Sync_LocalHalf( |
| 1539 | syncbuf, submodules=mp.manifest.HasSubmodules, errors=errors |
| 1540 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1541 | clean = syncbuf.Finish() |
| 1542 | self.event_log.AddSync( |
| 1543 | mp, event_log.TASK_SYNC_LOCAL, start, time.time(), clean |
| 1544 | ) |
| 1545 | if not clean: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1546 | raise UpdateManifestError( |
| 1547 | aggregate_errors=errors, project=mp.name |
| 1548 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1549 | self._ReloadManifest(manifest_name, mp.manifest) |
| 1550 | |
| 1551 | def ValidateOptions(self, opt, args): |
| 1552 | if opt.force_broken: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1553 | logger.warning( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1554 | "warning: -f/--force-broken is now the default behavior, and " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1555 | "the options are deprecated" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1556 | ) |
| 1557 | if opt.network_only and opt.detach_head: |
| 1558 | self.OptionParser.error("cannot combine -n and -d") |
| 1559 | if opt.network_only and opt.local_only: |
| 1560 | self.OptionParser.error("cannot combine -n and -l") |
| 1561 | if opt.manifest_name and opt.smart_sync: |
| 1562 | self.OptionParser.error("cannot combine -m and -s") |
| 1563 | if opt.manifest_name and opt.smart_tag: |
| 1564 | self.OptionParser.error("cannot combine -m and -t") |
| 1565 | if opt.manifest_server_username or opt.manifest_server_password: |
| 1566 | if not (opt.smart_sync or opt.smart_tag): |
| 1567 | self.OptionParser.error( |
| 1568 | "-u and -p may only be combined with -s or -t" |
| 1569 | ) |
| 1570 | if None in [ |
| 1571 | opt.manifest_server_username, |
| 1572 | opt.manifest_server_password, |
| 1573 | ]: |
| 1574 | self.OptionParser.error("both -u and -p must be given") |
| 1575 | |
| 1576 | if opt.prune is None: |
| 1577 | opt.prune = True |
| 1578 | |
| 1579 | if opt.auto_gc is None and _AUTO_GC: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1580 | logger.error( |
| 1581 | "Will run `git gc --auto` because %s is set. %s is deprecated " |
| 1582 | "and will be removed in a future release. Use `--auto-gc` " |
| 1583 | "instead.", |
| 1584 | _REPO_AUTO_GC, |
| 1585 | _REPO_AUTO_GC, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1586 | ) |
| 1587 | opt.auto_gc = True |
| 1588 | |
| 1589 | def _ValidateOptionsWithManifest(self, opt, mp): |
| 1590 | """Like ValidateOptions, but after we've updated the manifest. |
| 1591 | |
| 1592 | Needed to handle sync-xxx option defaults in the manifest. |
| 1593 | |
| 1594 | Args: |
| 1595 | opt: The options to process. |
| 1596 | mp: The manifest project to pull defaults from. |
| 1597 | """ |
| 1598 | if not opt.jobs: |
| 1599 | # If the user hasn't made a choice, use the manifest value. |
| 1600 | opt.jobs = mp.manifest.default.sync_j |
| 1601 | if opt.jobs: |
| 1602 | # If --jobs has a non-default value, propagate it as the default for |
| 1603 | # --jobs-xxx flags too. |
| 1604 | if not opt.jobs_network: |
| 1605 | opt.jobs_network = opt.jobs |
| 1606 | if not opt.jobs_checkout: |
| 1607 | opt.jobs_checkout = opt.jobs |
| 1608 | else: |
| 1609 | # Neither user nor manifest have made a choice, so setup defaults. |
| 1610 | if not opt.jobs_network: |
| 1611 | opt.jobs_network = 1 |
| 1612 | if not opt.jobs_checkout: |
| 1613 | opt.jobs_checkout = DEFAULT_LOCAL_JOBS |
| 1614 | opt.jobs = os.cpu_count() |
| 1615 | |
| 1616 | # Try to stay under user rlimit settings. |
| 1617 | # |
| 1618 | # Since each worker requires at 3 file descriptors to run `git fetch`, |
| 1619 | # use that to scale down the number of jobs. Unfortunately there isn't |
| 1620 | # an easy way to determine this reliably as systems change, but it was |
| 1621 | # last measured by hand in 2011. |
| 1622 | soft_limit, _ = _rlimit_nofile() |
| 1623 | jobs_soft_limit = max(1, (soft_limit - 5) // 3) |
| 1624 | opt.jobs = min(opt.jobs, jobs_soft_limit) |
| 1625 | opt.jobs_network = min(opt.jobs_network, jobs_soft_limit) |
| 1626 | opt.jobs_checkout = min(opt.jobs_checkout, jobs_soft_limit) |
| 1627 | |
| 1628 | def Execute(self, opt, args): |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1629 | errors = [] |
| 1630 | try: |
| 1631 | self._ExecuteHelper(opt, args, errors) |
| 1632 | except RepoExitError: |
| 1633 | raise |
| 1634 | except (KeyboardInterrupt, Exception) as e: |
| 1635 | raise RepoUnhandledExceptionError(e, aggregate_errors=errors) |
| 1636 | |
| 1637 | def _ExecuteHelper(self, opt, args, errors): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1638 | manifest = self.outer_manifest |
| 1639 | if not opt.outer_manifest: |
| 1640 | manifest = self.manifest |
| 1641 | |
| 1642 | if opt.manifest_name: |
| 1643 | manifest.Override(opt.manifest_name) |
| 1644 | |
| 1645 | manifest_name = opt.manifest_name |
| 1646 | smart_sync_manifest_path = os.path.join( |
| 1647 | manifest.manifestProject.worktree, "smart_sync_override.xml" |
| 1648 | ) |
| 1649 | |
| 1650 | if opt.clone_bundle is None: |
| 1651 | opt.clone_bundle = manifest.CloneBundle |
| 1652 | |
| 1653 | if opt.smart_sync or opt.smart_tag: |
| 1654 | manifest_name = self._SmartSyncSetup( |
| 1655 | opt, smart_sync_manifest_path, manifest |
| 1656 | ) |
| 1657 | else: |
| 1658 | if os.path.isfile(smart_sync_manifest_path): |
| 1659 | try: |
| 1660 | platform_utils.remove(smart_sync_manifest_path) |
| 1661 | except OSError as e: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1662 | logger.error( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1663 | "error: failed to remove existing smart sync override " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1664 | "manifest: %s", |
| 1665 | e, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1666 | ) |
| 1667 | |
| 1668 | err_event = multiprocessing.Event() |
| 1669 | |
| 1670 | rp = manifest.repoProject |
| 1671 | rp.PreSync() |
| 1672 | cb = rp.CurrentBranch |
| 1673 | if cb: |
| 1674 | base = rp.GetBranch(cb).merge |
| 1675 | if not base or not base.startswith("refs/heads/"): |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1676 | logger.warning( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1677 | "warning: repo is not tracking a remote branch, so it will " |
| 1678 | "not receive updates; run `repo init --repo-rev=stable` to " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1679 | "fix." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1680 | ) |
| 1681 | |
| 1682 | for m in self.ManifestList(opt): |
| 1683 | if not m.manifestProject.standalone_manifest_url: |
| 1684 | m.manifestProject.PreSync() |
| 1685 | |
| 1686 | if opt.repo_upgraded: |
| 1687 | _PostRepoUpgrade(manifest, quiet=opt.quiet) |
| 1688 | |
| 1689 | mp = manifest.manifestProject |
Jason Chang | 1783332 | 2023-05-23 13:06:55 -0700 | [diff] [blame] | 1690 | |
| 1691 | if _REPO_ALLOW_SHALLOW is not None: |
| 1692 | if _REPO_ALLOW_SHALLOW == "1": |
| 1693 | mp.ConfigureCloneFilterForDepth(None) |
| 1694 | elif ( |
| 1695 | _REPO_ALLOW_SHALLOW == "0" and mp.clone_filter_for_depth is None |
| 1696 | ): |
| 1697 | mp.ConfigureCloneFilterForDepth("blob:none") |
| 1698 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1699 | if opt.mp_update: |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 1700 | self._UpdateAllManifestProjects(opt, mp, manifest_name, errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1701 | else: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1702 | logger.info("Skipping update of local manifest project.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1703 | |
| 1704 | # Now that the manifests are up-to-date, setup options whose defaults |
| 1705 | # might be in the manifest. |
| 1706 | self._ValidateOptionsWithManifest(opt, mp) |
| 1707 | |
| 1708 | superproject_logging_data = {} |
| 1709 | self._UpdateProjectsRevisionId( |
| 1710 | opt, args, superproject_logging_data, manifest |
| 1711 | ) |
| 1712 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1713 | all_projects = self.GetProjects( |
| 1714 | args, |
| 1715 | missing_ok=True, |
| 1716 | submodules_ok=opt.fetch_submodules, |
| 1717 | manifest=manifest, |
| 1718 | all_manifests=not opt.this_manifest_only, |
| 1719 | ) |
| 1720 | |
| 1721 | err_network_sync = False |
| 1722 | err_update_projects = False |
| 1723 | err_update_linkfiles = False |
| 1724 | |
Jason Chang | 1d3b4fb | 2023-06-20 16:55:27 -0700 | [diff] [blame] | 1725 | # Log the repo projects by existing and new. |
| 1726 | existing = [x for x in all_projects if x.Exists] |
| 1727 | mp.config.SetString("repo.existingprojectcount", str(len(existing))) |
| 1728 | mp.config.SetString( |
| 1729 | "repo.newprojectcount", str(len(all_projects) - len(existing)) |
| 1730 | ) |
| 1731 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1732 | self._fetch_times = _FetchTimes(manifest) |
Gavin Mak | 16109a6 | 2023-08-22 01:24:46 +0000 | [diff] [blame] | 1733 | self._local_sync_state = LocalSyncState(manifest) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1734 | if not opt.local_only: |
| 1735 | with multiprocessing.Manager() as manager: |
| 1736 | with ssh.ProxyManager(manager) as ssh_proxy: |
| 1737 | # Initialize the socket dir once in the parent. |
| 1738 | ssh_proxy.sock() |
| 1739 | result = self._FetchMain( |
Jason Chang | daf2ad3 | 2023-08-31 17:06:36 -0700 | [diff] [blame^] | 1740 | opt, |
| 1741 | args, |
| 1742 | all_projects, |
| 1743 | err_event, |
| 1744 | ssh_proxy, |
| 1745 | manifest, |
| 1746 | errors, |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1747 | ) |
| 1748 | all_projects = result.all_projects |
| 1749 | |
| 1750 | if opt.network_only: |
| 1751 | return |
| 1752 | |
| 1753 | # If we saw an error, exit with code 1 so that other scripts can |
| 1754 | # check. |
| 1755 | if err_event.is_set(): |
| 1756 | err_network_sync = True |
| 1757 | if opt.fail_fast: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1758 | logger.error( |
| 1759 | "error: Exited sync due to fetch errors.\n" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1760 | "Local checkouts *not* updated. Resolve network issues " |
| 1761 | "& retry.\n" |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1762 | "`repo sync -l` will update some local checkouts." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1763 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1764 | raise SyncFailFastError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1765 | |
| 1766 | for m in self.ManifestList(opt): |
| 1767 | if m.IsMirror or m.IsArchive: |
| 1768 | # Bail out now, we have no working tree. |
| 1769 | continue |
| 1770 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1771 | try: |
| 1772 | self.UpdateProjectList(opt, m) |
| 1773 | except Exception as e: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1774 | err_event.set() |
| 1775 | err_update_projects = True |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1776 | errors.append(e) |
| 1777 | if isinstance(e, DeleteWorktreeError): |
| 1778 | errors.extend(e.aggregate_errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1779 | if opt.fail_fast: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1780 | logger.error("error: Local checkouts *not* updated.") |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1781 | raise SyncFailFastError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1782 | |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1783 | err_update_linkfiles = False |
| 1784 | try: |
| 1785 | self.UpdateCopyLinkfileList(m) |
| 1786 | except Exception as e: |
| 1787 | err_update_linkfiles = True |
| 1788 | errors.append(e) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1789 | err_event.set() |
| 1790 | if opt.fail_fast: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1791 | logger.error( |
| 1792 | "error: Local update copyfile or linkfile failed." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1793 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1794 | raise SyncFailFastError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1795 | |
| 1796 | err_results = [] |
| 1797 | # NB: We don't exit here because this is the last step. |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1798 | err_checkout = not self._Checkout( |
| 1799 | all_projects, opt, err_results, errors |
| 1800 | ) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1801 | if err_checkout: |
| 1802 | err_event.set() |
| 1803 | |
| 1804 | printed_notices = set() |
| 1805 | # If there's a notice that's supposed to print at the end of the sync, |
| 1806 | # print it now... But avoid printing duplicate messages, and preserve |
| 1807 | # order. |
| 1808 | for m in sorted(self.ManifestList(opt), key=lambda x: x.path_prefix): |
| 1809 | if m.notice and m.notice not in printed_notices: |
| 1810 | print(m.notice) |
| 1811 | printed_notices.add(m.notice) |
| 1812 | |
| 1813 | # If we saw an error, exit with code 1 so that other scripts can check. |
| 1814 | if err_event.is_set(): |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1815 | |
| 1816 | def print_and_log(err_msg): |
| 1817 | self.git_event_log.ErrorEvent(err_msg) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1818 | logger.error("%s", err_msg) |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1819 | |
| 1820 | print_and_log("error: Unable to fully sync the tree") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1821 | if err_network_sync: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1822 | print_and_log("error: Downloading network changes failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1823 | if err_update_projects: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1824 | print_and_log("error: Updating local project lists failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1825 | if err_update_linkfiles: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1826 | print_and_log("error: Updating copyfiles or linkfiles failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1827 | if err_checkout: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1828 | print_and_log("error: Checking out local projects failed.") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1829 | if err_results: |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1830 | # Don't log repositories, as it may contain sensitive info. |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1831 | logger.error("Failing repos:\n%s", "\n".join(err_results)) |
Josip Sokcevic | 131fc96 | 2023-05-12 17:00:46 -0700 | [diff] [blame] | 1832 | # Not useful to log. |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1833 | logger.error( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1834 | 'Try re-running with "-j1 --fail-fast" to exit at the first ' |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1835 | "error." |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1836 | ) |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1837 | raise SyncError(aggregate_errors=errors) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1838 | |
| 1839 | # Log the previous sync analysis state from the config. |
| 1840 | self.git_event_log.LogDataConfigEvents( |
| 1841 | mp.config.GetSyncAnalysisStateData(), "previous_sync_state" |
| 1842 | ) |
| 1843 | |
| 1844 | # Update and log with the new sync analysis state. |
| 1845 | mp.config.UpdateSyncAnalysisState(opt, superproject_logging_data) |
| 1846 | self.git_event_log.LogDataConfigEvents( |
| 1847 | mp.config.GetSyncAnalysisStateData(), "current_sync_state" |
| 1848 | ) |
| 1849 | |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 1850 | self._local_sync_state.PruneRemovedProjects() |
| 1851 | if self._local_sync_state.IsPartiallySynced(): |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1852 | logger.warning( |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 1853 | "warning: Partial syncs are not supported. For the best " |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1854 | "experience, sync the entire tree." |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 1855 | ) |
| 1856 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1857 | if not opt.quiet: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1858 | logger.info("repo sync has finished successfully.") |
Mike Frysinger | e19d9e1 | 2020-02-12 11:23:32 -0500 | [diff] [blame] | 1859 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 1860 | |
Shawn O. Pearce | 80d2ceb | 2012-10-26 12:23:05 -0700 | [diff] [blame] | 1861 | def _PostRepoUpgrade(manifest, quiet=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1862 | # Link the docs for the internal .repo/ layout for people. |
| 1863 | link = os.path.join(manifest.repodir, "internal-fs-layout.md") |
| 1864 | if not platform_utils.islink(link): |
| 1865 | target = os.path.join("repo", "docs", "internal-fs-layout.md") |
| 1866 | try: |
| 1867 | platform_utils.symlink(target, link) |
| 1868 | except Exception: |
| 1869 | pass |
Mike Frysinger | fdeb20f | 2021-11-14 03:53:04 -0500 | [diff] [blame] | 1870 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1871 | wrapper = Wrapper() |
| 1872 | if wrapper.NeedSetupGnuPG(): |
| 1873 | wrapper.SetupGnuPG(quiet) |
| 1874 | for project in manifest.projects: |
| 1875 | if project.Exists: |
| 1876 | project.PostRepoUpgrade() |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 1877 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 1878 | |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 1879 | def _PostRepoFetch(rp, repo_verify=True, verbose=False): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1880 | if rp.HasChanges: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1881 | logger.info("info: A new version of repo is available") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1882 | wrapper = Wrapper() |
| 1883 | try: |
| 1884 | rev = rp.bare_git.describe(rp.GetRevisionId()) |
| 1885 | except GitError: |
| 1886 | rev = None |
| 1887 | _, new_rev = wrapper.check_repo_rev( |
| 1888 | rp.gitdir, rev, repo_verify=repo_verify |
| 1889 | ) |
| 1890 | # See if we're held back due to missing signed tag. |
| 1891 | current_revid = rp.bare_git.rev_parse("HEAD") |
| 1892 | new_revid = rp.bare_git.rev_parse("--verify", new_rev) |
| 1893 | if current_revid != new_revid: |
| 1894 | # We want to switch to the new rev, but also not trash any |
| 1895 | # uncommitted changes. This helps with local testing/hacking. |
| 1896 | # If a local change has been made, we will throw that away. |
| 1897 | # We also have to make sure this will switch to an older commit if |
| 1898 | # that's the latest tag in order to support release rollback. |
| 1899 | try: |
| 1900 | rp.work_git.reset("--keep", new_rev) |
| 1901 | except GitError as e: |
Jason Chang | 32b5956 | 2023-07-14 16:45:35 -0700 | [diff] [blame] | 1902 | raise RepoUnhandledExceptionError(e) |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1903 | logger.info("info: Restarting repo with latest version") |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1904 | raise RepoChangedException(["--repo-upgraded"]) |
| 1905 | else: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1906 | logger.warning("warning: Skipped upgrade to unverified version") |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 1907 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1908 | if verbose: |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 1909 | logger.info( |
| 1910 | "repo version %s is current", rp.work_git.describe(HEAD) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1911 | ) |
Shawn O. Pearce | e756c41 | 2009-04-13 11:51:15 -0700 | [diff] [blame] | 1912 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 1913 | |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 1914 | class _FetchTimes(object): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1915 | _ALPHA = 0.5 |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 1916 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1917 | def __init__(self, manifest): |
| 1918 | self._path = os.path.join(manifest.repodir, ".repo_fetchtimes.json") |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1919 | self._saved = None |
| 1920 | self._seen = {} |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 1921 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1922 | def Get(self, project): |
| 1923 | self._Load() |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1924 | return self._saved.get(project.name, _ONE_DAY_S) |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 1925 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1926 | def Set(self, project, t): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1927 | name = project.name |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1928 | |
| 1929 | # For shared projects, save the longest time. |
| 1930 | self._seen[name] = max(self._seen.get(name, 0), t) |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 1931 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1932 | def _Load(self): |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1933 | if self._saved is None: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1934 | try: |
| 1935 | with open(self._path) as f: |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1936 | self._saved = json.load(f) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1937 | except (IOError, ValueError): |
| 1938 | platform_utils.remove(self._path, missing_ok=True) |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1939 | self._saved = {} |
Dave Borowitz | 67700e9 | 2012-10-23 15:00:54 -0700 | [diff] [blame] | 1940 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1941 | def Save(self): |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1942 | if self._saved is None: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1943 | return |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 1944 | |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1945 | for name, t in self._seen.items(): |
| 1946 | # Keep a moving average across the previous/current sync runs. |
| 1947 | old = self._saved.get(name, t) |
| 1948 | self._seen[name] = (self._ALPHA * t) + ((1 - self._ALPHA) * old) |
Dave Borowitz | d947858 | 2012-10-23 16:35:39 -0700 | [diff] [blame] | 1949 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1950 | try: |
| 1951 | with open(self._path, "w") as f: |
Gavin Mak | 041f977 | 2023-05-10 20:41:12 +0000 | [diff] [blame] | 1952 | json.dump(self._seen, f, indent=2) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 1953 | except (IOError, TypeError): |
| 1954 | platform_utils.remove(self._path, missing_ok=True) |
| 1955 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 1956 | |
Gavin Mak | 16109a6 | 2023-08-22 01:24:46 +0000 | [diff] [blame] | 1957 | class LocalSyncState(object): |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 1958 | _LAST_FETCH = "last_fetch" |
| 1959 | _LAST_CHECKOUT = "last_checkout" |
| 1960 | |
| 1961 | def __init__(self, manifest): |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 1962 | self._manifest = manifest |
| 1963 | self._path = os.path.join( |
| 1964 | self._manifest.repodir, ".repo_localsyncstate.json" |
| 1965 | ) |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 1966 | self._time = time.time() |
| 1967 | self._state = None |
| 1968 | self._Load() |
| 1969 | |
| 1970 | def SetFetchTime(self, project): |
| 1971 | self._Set(project, self._LAST_FETCH) |
| 1972 | |
| 1973 | def SetCheckoutTime(self, project): |
| 1974 | self._Set(project, self._LAST_CHECKOUT) |
| 1975 | |
| 1976 | def GetFetchTime(self, project): |
| 1977 | return self._Get(project, self._LAST_FETCH) |
| 1978 | |
| 1979 | def GetCheckoutTime(self, project): |
| 1980 | return self._Get(project, self._LAST_CHECKOUT) |
| 1981 | |
| 1982 | def _Get(self, project, key): |
| 1983 | self._Load() |
| 1984 | p = project.relpath |
| 1985 | if p not in self._state: |
| 1986 | return |
| 1987 | return self._state[p].get(key) |
| 1988 | |
| 1989 | def _Set(self, project, key): |
| 1990 | p = project.relpath |
| 1991 | if p not in self._state: |
| 1992 | self._state[p] = {} |
| 1993 | self._state[p][key] = self._time |
| 1994 | |
| 1995 | def _Load(self): |
| 1996 | if self._state is None: |
| 1997 | try: |
| 1998 | with open(self._path) as f: |
| 1999 | self._state = json.load(f) |
| 2000 | except (IOError, ValueError): |
| 2001 | platform_utils.remove(self._path, missing_ok=True) |
| 2002 | self._state = {} |
| 2003 | |
| 2004 | def Save(self): |
| 2005 | if not self._state: |
| 2006 | return |
| 2007 | try: |
| 2008 | with open(self._path, "w") as f: |
| 2009 | json.dump(self._state, f, indent=2) |
| 2010 | except (IOError, TypeError): |
| 2011 | platform_utils.remove(self._path, missing_ok=True) |
| 2012 | |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 2013 | def PruneRemovedProjects(self): |
| 2014 | """Remove entries don't exist on disk and save.""" |
| 2015 | if not self._state: |
| 2016 | return |
| 2017 | delete = set() |
| 2018 | for path in self._state: |
| 2019 | gitdir = os.path.join(self._manifest.topdir, path, ".git") |
| 2020 | if not os.path.exists(gitdir): |
| 2021 | delete.add(path) |
| 2022 | if not delete: |
| 2023 | return |
| 2024 | for path in delete: |
| 2025 | del self._state[path] |
| 2026 | self.Save() |
| 2027 | |
| 2028 | def IsPartiallySynced(self): |
| 2029 | """Return whether a partial sync state is detected.""" |
| 2030 | self._Load() |
| 2031 | prev_checkout_t = None |
Gavin Mak | 321b793 | 2023-08-22 03:10:01 +0000 | [diff] [blame] | 2032 | for path, data in self._state.items(): |
| 2033 | if path == self._manifest.repoProject.relpath: |
| 2034 | # The repo project isn't included in most syncs so we should |
| 2035 | # ignore it here. |
| 2036 | continue |
Gavin Mak | f0aeb22 | 2023-08-08 04:43:36 +0000 | [diff] [blame] | 2037 | checkout_t = data.get(self._LAST_CHECKOUT) |
| 2038 | if not checkout_t: |
| 2039 | return True |
| 2040 | prev_checkout_t = prev_checkout_t or checkout_t |
| 2041 | if prev_checkout_t != checkout_t: |
| 2042 | return True |
| 2043 | return False |
| 2044 | |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame] | 2045 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2046 | # This is a replacement for xmlrpc.client.Transport using urllib2 |
| 2047 | # and supporting persistent-http[s]. It cannot change hosts from |
| 2048 | # request to request like the normal transport, the real url |
| 2049 | # is passed during initialization. |
| 2050 | class PersistentTransport(xmlrpc.client.Transport): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2051 | def __init__(self, orig_host): |
| 2052 | self.orig_host = orig_host |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2053 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2054 | def request(self, host, handler, request_body, verbose=False): |
| 2055 | with GetUrlCookieFile(self.orig_host, not verbose) as ( |
| 2056 | cookiefile, |
| 2057 | proxy, |
| 2058 | ): |
| 2059 | # Python doesn't understand cookies with the #HttpOnly_ prefix |
| 2060 | # Since we're only using them for HTTP, copy the file temporarily, |
| 2061 | # stripping those prefixes away. |
| 2062 | if cookiefile: |
| 2063 | tmpcookiefile = tempfile.NamedTemporaryFile(mode="w") |
| 2064 | tmpcookiefile.write("# HTTP Cookie File") |
| 2065 | try: |
| 2066 | with open(cookiefile) as f: |
| 2067 | for line in f: |
| 2068 | if line.startswith("#HttpOnly_"): |
| 2069 | line = line[len("#HttpOnly_") :] |
| 2070 | tmpcookiefile.write(line) |
| 2071 | tmpcookiefile.flush() |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2072 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2073 | cookiejar = cookielib.MozillaCookieJar(tmpcookiefile.name) |
| 2074 | try: |
| 2075 | cookiejar.load() |
| 2076 | except cookielib.LoadError: |
| 2077 | cookiejar = cookielib.CookieJar() |
| 2078 | finally: |
| 2079 | tmpcookiefile.close() |
| 2080 | else: |
| 2081 | cookiejar = cookielib.CookieJar() |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2082 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2083 | proxyhandler = urllib.request.ProxyHandler |
| 2084 | if proxy: |
| 2085 | proxyhandler = urllib.request.ProxyHandler( |
| 2086 | {"http": proxy, "https": proxy} |
| 2087 | ) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2088 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2089 | opener = urllib.request.build_opener( |
| 2090 | urllib.request.HTTPCookieProcessor(cookiejar), proxyhandler |
| 2091 | ) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2092 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2093 | url = urllib.parse.urljoin(self.orig_host, handler) |
| 2094 | parse_results = urllib.parse.urlparse(url) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2095 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2096 | scheme = parse_results.scheme |
| 2097 | if scheme == "persistent-http": |
| 2098 | scheme = "http" |
| 2099 | if scheme == "persistent-https": |
| 2100 | # If we're proxying through persistent-https, use http. The |
| 2101 | # proxy itself will do the https. |
| 2102 | if proxy: |
| 2103 | scheme = "http" |
| 2104 | else: |
| 2105 | scheme = "https" |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2106 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2107 | # Parse out any authentication information using the base class. |
| 2108 | host, extra_headers, _ = self.get_host_info(parse_results.netloc) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2109 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2110 | url = urllib.parse.urlunparse( |
| 2111 | ( |
| 2112 | scheme, |
| 2113 | host, |
| 2114 | parse_results.path, |
| 2115 | parse_results.params, |
| 2116 | parse_results.query, |
| 2117 | parse_results.fragment, |
| 2118 | ) |
| 2119 | ) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2120 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2121 | request = urllib.request.Request(url, request_body) |
| 2122 | if extra_headers is not None: |
| 2123 | for name, header in extra_headers: |
| 2124 | request.add_header(name, header) |
| 2125 | request.add_header("Content-Type", "text/xml") |
| 2126 | try: |
| 2127 | response = opener.open(request) |
| 2128 | except urllib.error.HTTPError as e: |
| 2129 | if e.code == 501: |
| 2130 | # We may have been redirected through a login process |
| 2131 | # but our POST turned into a GET. Retry. |
| 2132 | response = opener.open(request) |
| 2133 | else: |
| 2134 | raise |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2135 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2136 | p, u = xmlrpc.client.getparser() |
| 2137 | # Response should be fairly small, so read it all at once. |
| 2138 | # This way we can show it to the user in case of error (e.g. HTML). |
| 2139 | data = response.read() |
| 2140 | try: |
| 2141 | p.feed(data) |
| 2142 | except xml.parsers.expat.ExpatError as e: |
| 2143 | raise IOError( |
| 2144 | f"Parsing the manifest failed: {e}\n" |
| 2145 | f"Please report this to your manifest server admin.\n" |
| 2146 | f'Here is the full response:\n{data.decode("utf-8")}' |
| 2147 | ) |
| 2148 | p.close() |
| 2149 | return u.close() |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 2150 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 2151 | def close(self): |
| 2152 | pass |