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