Simon Glass | 2c266d8 | 2025-04-29 07:22:13 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
| 3 | # Copyright 2025 Simon Glass <sjg@chromium.org> |
| 4 | # |
| 5 | """Provides a basic API for the patchwork server |
| 6 | """ |
| 7 | |
| 8 | import asyncio |
Simon Glass | 232eefd | 2025-04-29 07:22:14 -0600 | [diff] [blame] | 9 | import re |
Simon Glass | 2c266d8 | 2025-04-29 07:22:13 -0600 | [diff] [blame] | 10 | |
| 11 | import aiohttp |
Simon Glass | 3729b8b | 2025-04-29 07:22:24 -0600 | [diff] [blame^] | 12 | from u_boot_pylib import terminal |
Simon Glass | 2c266d8 | 2025-04-29 07:22:13 -0600 | [diff] [blame] | 13 | |
| 14 | # Number of retries |
| 15 | RETRIES = 3 |
| 16 | |
| 17 | # Max concurrent request |
| 18 | MAX_CONCURRENT = 50 |
| 19 | |
Simon Glass | 232eefd | 2025-04-29 07:22:14 -0600 | [diff] [blame] | 20 | # Patches which are part of a multi-patch series are shown with a prefix like |
| 21 | # [prefix, version, sequence], for example '[RFC, v2, 3/5]'. All but the last |
| 22 | # part is optional. This decodes the string into groups. For single patches |
| 23 | # the [] part is not present: |
| 24 | # Groups: (ignore, ignore, ignore, prefix, version, sequence, subject) |
| 25 | RE_PATCH = re.compile(r'(\[(((.*),)?(.*),)?(.*)\]\s)?(.*)$') |
| 26 | |
| 27 | # This decodes the sequence string into a patch number and patch count |
| 28 | RE_SEQ = re.compile(r'(\d+)/(\d+)') |
| 29 | |
| 30 | |
| 31 | class Patch(dict): |
| 32 | """Models a patch in patchwork |
| 33 | |
| 34 | This class records information obtained from patchwork |
| 35 | |
| 36 | Some of this information comes from the 'Patch' column: |
| 37 | |
| 38 | [RFC,v2,1/3] dm: Driver and uclass changes for tiny-dm |
| 39 | |
| 40 | This shows the prefix, version, seq, count and subject. |
| 41 | |
| 42 | The other properties come from other columns in the display. |
| 43 | |
| 44 | Properties: |
| 45 | pid (str): ID of the patch (typically an integer) |
| 46 | seq (int): Sequence number within series (1=first) parsed from sequence |
| 47 | string |
| 48 | count (int): Number of patches in series, parsed from sequence string |
| 49 | raw_subject (str): Entire subject line, e.g. |
| 50 | "[1/2,v2] efi_loader: Sort header file ordering" |
| 51 | prefix (str): Prefix string or None (e.g. 'RFC') |
| 52 | version (str): Version string or None (e.g. 'v2') |
| 53 | raw_subject (str): Raw patch subject |
| 54 | subject (str): Patch subject with [..] part removed (same as commit |
| 55 | subject) |
Simon Glass | a1046c8 | 2025-04-29 07:22:22 -0600 | [diff] [blame] | 56 | data (dict or None): Patch data: |
Simon Glass | 232eefd | 2025-04-29 07:22:14 -0600 | [diff] [blame] | 57 | """ |
Simon Glass | a1046c8 | 2025-04-29 07:22:22 -0600 | [diff] [blame] | 58 | def __init__(self, pid, state=None, data=None, comments=None, |
| 59 | series_data=None): |
Simon Glass | 232eefd | 2025-04-29 07:22:14 -0600 | [diff] [blame] | 60 | super().__init__() |
| 61 | self.id = pid # Use 'id' to match what the Rest API provides |
| 62 | self.seq = None |
| 63 | self.count = None |
| 64 | self.prefix = None |
| 65 | self.version = None |
| 66 | self.raw_subject = None |
| 67 | self.subject = None |
Simon Glass | a1046c8 | 2025-04-29 07:22:22 -0600 | [diff] [blame] | 68 | self.state = state |
| 69 | self.data = data |
| 70 | self.comments = comments |
| 71 | self.series_data = series_data |
| 72 | self.name = None |
Simon Glass | 232eefd | 2025-04-29 07:22:14 -0600 | [diff] [blame] | 73 | |
| 74 | # These make us more like a dictionary |
| 75 | def __setattr__(self, name, value): |
| 76 | self[name] = value |
| 77 | |
| 78 | def __getattr__(self, name): |
| 79 | return self[name] |
| 80 | |
| 81 | def __hash__(self): |
| 82 | return hash(frozenset(self.items())) |
| 83 | |
| 84 | def __str__(self): |
| 85 | return self.raw_subject |
| 86 | |
| 87 | def parse_subject(self, raw_subject): |
| 88 | """Parse the subject of a patch into its component parts |
| 89 | |
| 90 | See RE_PATCH for details. The parsed info is placed into seq, count, |
| 91 | prefix, version, subject |
| 92 | |
| 93 | Args: |
| 94 | raw_subject (str): Subject string to parse |
| 95 | |
| 96 | Raises: |
| 97 | ValueError: the subject cannot be parsed |
| 98 | """ |
| 99 | self.raw_subject = raw_subject.strip() |
| 100 | mat = RE_PATCH.search(raw_subject.strip()) |
| 101 | if not mat: |
| 102 | raise ValueError(f"Cannot parse subject '{raw_subject}'") |
| 103 | self.prefix, self.version, seq_info, self.subject = mat.groups()[3:] |
| 104 | mat_seq = RE_SEQ.match(seq_info) if seq_info else False |
| 105 | if mat_seq is None: |
| 106 | self.version = seq_info |
| 107 | seq_info = None |
| 108 | if self.version and not self.version.startswith('v'): |
| 109 | self.prefix = self.version |
| 110 | self.version = None |
| 111 | if seq_info: |
| 112 | if mat_seq: |
| 113 | self.seq = int(mat_seq.group(1)) |
| 114 | self.count = int(mat_seq.group(2)) |
| 115 | else: |
| 116 | self.seq = 1 |
| 117 | self.count = 1 |
| 118 | |
| 119 | |
| 120 | class Review: |
| 121 | """Represents a single review email collected in Patchwork |
| 122 | |
| 123 | Patches can attract multiple reviews. Each consists of an author/date and |
| 124 | a variable number of 'snippets', which are groups of quoted and unquoted |
| 125 | text. |
| 126 | """ |
| 127 | def __init__(self, meta, snippets): |
| 128 | """Create new Review object |
| 129 | |
| 130 | Args: |
| 131 | meta (str): Text containing review author and date |
| 132 | snippets (list): List of snippets in th review, each a list of text |
| 133 | lines |
| 134 | """ |
| 135 | self.meta = ' : '.join([line for line in meta.splitlines() if line]) |
| 136 | self.snippets = snippets |
| 137 | |
| 138 | |
Simon Glass | 2c266d8 | 2025-04-29 07:22:13 -0600 | [diff] [blame] | 139 | class Patchwork: |
| 140 | """Class to handle communication with patchwork |
| 141 | """ |
| 142 | def __init__(self, url, show_progress=True): |
| 143 | """Set up a new patchwork handler |
| 144 | |
| 145 | Args: |
| 146 | url (str): URL of patchwork server, e.g. |
| 147 | 'https://patchwork.ozlabs.org' |
| 148 | """ |
| 149 | self.url = url |
Simon Glass | 25b91c1 | 2025-04-29 07:22:19 -0600 | [diff] [blame] | 150 | self.fake_request = None |
Simon Glass | 2c266d8 | 2025-04-29 07:22:13 -0600 | [diff] [blame] | 151 | self.proj_id = None |
| 152 | self.link_name = None |
| 153 | self._show_progress = show_progress |
| 154 | self.semaphore = asyncio.Semaphore(MAX_CONCURRENT) |
| 155 | self.request_count = 0 |
| 156 | |
| 157 | async def _request(self, client, subpath): |
| 158 | """Call the patchwork API and return the result as JSON |
| 159 | |
| 160 | Args: |
| 161 | client (aiohttp.ClientSession): Session to use |
| 162 | subpath (str): URL subpath to use |
| 163 | |
| 164 | Returns: |
| 165 | dict: Json result |
| 166 | |
| 167 | Raises: |
| 168 | ValueError: the URL could not be read |
| 169 | """ |
| 170 | # print('subpath', subpath) |
| 171 | self.request_count += 1 |
Simon Glass | 25b91c1 | 2025-04-29 07:22:19 -0600 | [diff] [blame] | 172 | if self.fake_request: |
| 173 | return self.fake_request(subpath) |
Simon Glass | 2c266d8 | 2025-04-29 07:22:13 -0600 | [diff] [blame] | 174 | |
| 175 | full_url = f'{self.url}/api/1.2/{subpath}' |
| 176 | async with self.semaphore: |
| 177 | # print('full_url', full_url) |
| 178 | for i in range(RETRIES + 1): |
| 179 | try: |
| 180 | async with client.get(full_url) as response: |
| 181 | if response.status != 200: |
| 182 | raise ValueError( |
| 183 | f"Could not read URL '{full_url}'") |
| 184 | result = await response.json() |
| 185 | # print('- done', full_url) |
| 186 | return result |
| 187 | break |
| 188 | except aiohttp.client_exceptions.ServerDisconnectedError: |
| 189 | if i == RETRIES: |
| 190 | raise |
Simon Glass | 1568b69 | 2025-04-29 07:22:15 -0600 | [diff] [blame] | 191 | |
Simon Glass | 25b91c1 | 2025-04-29 07:22:19 -0600 | [diff] [blame] | 192 | @staticmethod |
| 193 | def for_testing(func): |
| 194 | """Get an instance to use for testing |
| 195 | |
| 196 | Args: |
| 197 | func (function): Function to call to handle requests. The function |
| 198 | is passed a URL and is expected to return a dict with the |
| 199 | resulting data |
| 200 | |
| 201 | Returns: |
| 202 | Patchwork: testing instance |
| 203 | """ |
| 204 | pwork = Patchwork(None, show_progress=False) |
| 205 | pwork.fake_request = func |
| 206 | return pwork |
| 207 | |
Simon Glass | 1568b69 | 2025-04-29 07:22:15 -0600 | [diff] [blame] | 208 | async def get_series(self, client, link): |
| 209 | """Read information about a series |
| 210 | |
| 211 | Args: |
| 212 | client (aiohttp.ClientSession): Session to use |
| 213 | link (str): Patchwork series ID |
| 214 | |
| 215 | Returns: dict containing patchwork's series information |
| 216 | id (int): series ID unique across patchwork instance, e.g. 3 |
| 217 | url (str): Full URL, e.g. |
| 218 | 'https://patchwork.ozlabs.org/api/1.2/series/3/' |
| 219 | web_url (str): Full URL, e.g. |
| 220 | 'https://patchwork.ozlabs.org/project/uboot/list/?series=3 |
| 221 | project (dict): project information (id, url, name, link_name, |
| 222 | list_id, list_email, etc. |
| 223 | name (str): Series name, e.g. '[U-Boot] moveconfig: fix error' |
| 224 | date (str): Date, e.g. '2017-08-27T08:00:51' |
| 225 | submitter (dict): id, url, name, email, e.g.: |
| 226 | "id": 6125, |
| 227 | "url": "https://patchwork.ozlabs.org/api/1.2/people/6125/", |
| 228 | "name": "Chris Packham", |
| 229 | "email": "judge.packham@gmail.com" |
| 230 | version (int): Version number |
| 231 | total (int): Total number of patches based on subject |
| 232 | received_total (int): Total patches received by patchwork |
| 233 | received_all (bool): True if all patches were received |
| 234 | mbox (str): URL of mailbox, e.g. |
| 235 | 'https://patchwork.ozlabs.org/series/3/mbox/' |
| 236 | cover_letter (dict) or None, e.g.: |
| 237 | "id": 806215, |
| 238 | "url": "https://patchwork.ozlabs.org/api/1.2/covers/806215/", |
| 239 | "web_url": "https://patchwork.ozlabs.org/project/uboot/cover/ |
| 240 | 20170827094411.8583-1-judge.packham@gmail.com/", |
| 241 | "msgid": "<20170827094411.8583-1-judge.packham@gmail.com>", |
| 242 | "list_archive_url": null, |
| 243 | "date": "2017-08-27T09:44:07", |
| 244 | "name": "[U-Boot,v2,0/4] usb: net: Migrate USB Ethernet", |
| 245 | "mbox": "https://patchwork.ozlabs.org/project/uboot/cover/ |
| 246 | 20170827094411.8583-1-judge.packham@gmail.com/mbox/" |
| 247 | patches (list of dict), each e.g.: |
| 248 | "id": 806202, |
| 249 | "url": "https://patchwork.ozlabs.org/api/1.2/patches/806202/", |
| 250 | "web_url": "https://patchwork.ozlabs.org/project/uboot/patch/ |
| 251 | 20170827080051.816-1-judge.packham@gmail.com/", |
| 252 | "msgid": "<20170827080051.816-1-judge.packham@gmail.com>", |
| 253 | "list_archive_url": null, |
| 254 | "date": "2017-08-27T08:00:51", |
| 255 | "name": "[U-Boot] moveconfig: fix error message do_autoconf()", |
| 256 | "mbox": "https://patchwork.ozlabs.org/project/uboot/patch/ |
| 257 | 20170827080051.816-1-judge.packham@gmail.com/mbox/" |
| 258 | """ |
| 259 | return await self._request(client, f'series/{link}/') |
| 260 | |
| 261 | async def get_patch(self, client, patch_id): |
| 262 | """Read information about a patch |
| 263 | |
| 264 | Args: |
| 265 | client (aiohttp.ClientSession): Session to use |
| 266 | patch_id (str): Patchwork patch ID |
| 267 | |
| 268 | Returns: dict containing patchwork's patch information |
| 269 | "id": 185, |
| 270 | "url": "https://patchwork.ozlabs.org/api/1.2/patches/185/", |
| 271 | "web_url": "https://patchwork.ozlabs.org/project/cbe-oss-dev/patch/ |
| 272 | 200809050416.27831.adetsch@br.ibm.com/", |
| 273 | project (dict): project information (id, url, name, link_name, |
| 274 | list_id, list_email, etc. |
| 275 | "msgid": "<200809050416.27831.adetsch@br.ibm.com>", |
| 276 | "list_archive_url": null, |
| 277 | "date": "2008-09-05T07:16:27", |
| 278 | "name": "powerpc/spufs: Fix possible scheduling of a context", |
| 279 | "commit_ref": "b2e601d14deb2083e2a537b47869ab3895d23a28", |
| 280 | "pull_url": null, |
| 281 | "state": "accepted", |
| 282 | "archived": false, |
| 283 | "hash": "bc1c0b80d7cff66c0d1e5f3f8f4d10eb36176f0d", |
| 284 | "submitter": { |
| 285 | "id": 93, |
| 286 | "url": "https://patchwork.ozlabs.org/api/1.2/people/93/", |
| 287 | "name": "Andre Detsch", |
| 288 | "email": "adetsch@br.ibm.com" |
| 289 | }, |
| 290 | "delegate": { |
| 291 | "id": 1, |
| 292 | "url": "https://patchwork.ozlabs.org/api/1.2/users/1/", |
| 293 | "username": "jk", |
| 294 | "first_name": "Jeremy", |
| 295 | "last_name": "Kerr", |
| 296 | "email": "jk@ozlabs.org" |
| 297 | }, |
| 298 | "mbox": "https://patchwork.ozlabs.org/project/cbe-oss-dev/patch/ |
| 299 | 200809050416.27831.adetsch@br.ibm.com/mbox/", |
| 300 | "series": [], |
| 301 | "comments": "https://patchwork.ozlabs.org/api/patches/185/ |
| 302 | comments/", |
| 303 | "check": "pending", |
| 304 | "checks": "https://patchwork.ozlabs.org/api/patches/185/checks/", |
| 305 | "tags": {}, |
| 306 | "related": [], |
| 307 | "headers": {...} |
| 308 | "content": "We currently have a race when scheduling a context |
| 309 | after we have found a runnable context in spusched_tick, the |
| 310 | context may have been scheduled by spu_activate(). |
| 311 | |
| 312 | This may result in a panic if we try to unschedule a context |
| 313 | been freed in the meantime. |
| 314 | |
| 315 | This change exits spu_schedule() if the context has already |
| 316 | scheduled, so we don't end up scheduling it twice. |
| 317 | |
| 318 | Signed-off-by: Andre Detsch <adetsch@br.ibm.com>", |
| 319 | "diff": '''Index: spufs/arch/powerpc/platforms/cell/spufs/sched.c |
| 320 | ======================================================= |
| 321 | --- spufs.orig/arch/powerpc/platforms/cell/spufs/sched.c |
| 322 | +++ spufs/arch/powerpc/platforms/cell/spufs/sched.c |
| 323 | @@ -727,7 +727,8 @@ static void spu_schedule(struct spu *spu |
| 324 | \t/* not a candidate for interruptible because it's called |
| 325 | \t from the scheduler thread or from spu_deactivate */ |
| 326 | \tmutex_lock(&ctx->state_mutex); |
| 327 | -\t__spu_schedule(spu, ctx); |
| 328 | +\tif (ctx->state == SPU_STATE_SAVED) |
| 329 | +\t\t__spu_schedule(spu, ctx); |
| 330 | \tspu_release(ctx); |
| 331 | } |
| 332 | ''' |
| 333 | "prefixes": ["3/3", ...] |
| 334 | """ |
| 335 | return await self._request(client, f'patches/{patch_id}/') |
| 336 | |
| 337 | async def _get_patch_comments(self, client, patch_id): |
| 338 | """Read comments about a patch |
| 339 | |
| 340 | Args: |
| 341 | client (aiohttp.ClientSession): Session to use |
| 342 | patch_id (str): Patchwork patch ID |
| 343 | |
| 344 | Returns: list of dict: list of comments: |
| 345 | id (int): series ID unique across patchwork instance, e.g. 3331924 |
| 346 | web_url (str): Full URL, e.g. |
| 347 | 'https://patchwork.ozlabs.org/comment/3331924/' |
| 348 | msgid (str): Message ID, e.g. |
| 349 | '<d2526c98-8198-4b8b-ab10-20bda0151da1@gmx.de>' |
| 350 | list_archive_url: (unknown?) |
| 351 | date (str): Date, e.g. '2024-06-20T13:38:03' |
| 352 | subject (str): email subject, e.g. 'Re: [PATCH 3/5] buildman: |
| 353 | Support building within a Python venv' |
| 354 | date (str): Date, e.g. '2017-08-27T08:00:51' |
| 355 | submitter (dict): id, url, name, email, e.g.: |
| 356 | "id": 61270, |
| 357 | "url": "https://patchwork.ozlabs.org/api/people/61270/", |
| 358 | "name": "Heinrich Schuchardt", |
| 359 | "email": "xypron.glpk@gmx.de" |
| 360 | content (str): Content of email, e.g. 'On 20.06.24 15:19, |
| 361 | Simon Glass wrote: |
| 362 | >...' |
| 363 | headers: dict: email headers, see get_cover() for an example |
| 364 | """ |
| 365 | return await self._request(client, f'patches/{patch_id}/comments/') |
| 366 | |
| 367 | async def get_patch_comments(self, patch_id): |
| 368 | async with aiohttp.ClientSession() as client: |
| 369 | return await self._get_patch_comments(client, patch_id) |
| 370 | |
| 371 | async def _get_patch_status(self, client, patch_id): |
| 372 | """Get the patch status |
| 373 | |
| 374 | Args: |
| 375 | client (aiohttp.ClientSession): Session to use |
| 376 | patch_id (int): Patch ID to look up in patchwork |
| 377 | |
| 378 | Return: |
| 379 | PATCH: Patch information |
| 380 | |
| 381 | Requests: |
| 382 | 1 for patch, 1 for patch comments |
| 383 | """ |
| 384 | data = await self.get_patch(client, patch_id) |
| 385 | state = data['state'] |
| 386 | comment_data = await self._get_patch_comments(client, patch_id) |
| 387 | |
| 388 | return Patch(patch_id, state, data, comment_data) |
| 389 | |
| 390 | async def series_get_state(self, client, link, read_comments): |
| 391 | """Sync the series information against patchwork, to find patch status |
| 392 | |
| 393 | Args: |
| 394 | client (aiohttp.ClientSession): Session to use |
| 395 | link (str): Patchwork series ID |
| 396 | read_comments (bool): True to read the comments on the patches |
| 397 | |
| 398 | Return: tuple: |
| 399 | list of Patch objects |
| 400 | """ |
| 401 | data = await self.get_series(client, link) |
| 402 | patch_list = list(data['patches']) |
| 403 | |
| 404 | count = len(patch_list) |
| 405 | patches = [] |
| 406 | if read_comments: |
| 407 | # Returns a list of Patch objects |
| 408 | tasks = [self._get_patch_status(client, patch_list[i]['id']) |
| 409 | for i in range(count)] |
| 410 | |
| 411 | patch_status = await asyncio.gather(*tasks) |
| 412 | for patch_data, status in zip(patch_list, patch_status): |
| 413 | status.series_data = patch_data |
| 414 | patches.append(status) |
| 415 | else: |
| 416 | for i in range(count): |
| 417 | info = patch_list[i] |
| 418 | pat = Patch(info['id'], series_data=info) |
| 419 | pat.raw_subject = info['name'] |
| 420 | patches.append(pat) |
| 421 | if self._show_progress: |
| 422 | terminal.print_clear() |
| 423 | |
| 424 | return patches |