File: go.py

package info (click to toggle)
python-nvchecker 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: python: 4,801; makefile: 25
file content (40 lines) | stat: -rw-r--r-- 1,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# MIT licensed
# Copyright (c) 2024 bgme <i@bgme.me>.

from lxml import html

from nvchecker.api import (
  RichResult, Entry, AsyncCache, KeyManager,
  session, GetVersionError,
)

GO_PKG_URL = 'https://pkg.go.dev/{pkg}?tab=versions'
GO_PKG_VERSION_URL = 'https://pkg.go.dev/{pkg}@{version}'


async def get_version(
    name: str, conf: Entry, *,
    cache: AsyncCache, keymanager: KeyManager,
    **kwargs,
) -> RichResult:
  key = tuple(sorted(conf.items()))
  return await cache.get(key, get_version_impl)


async def get_version_impl(info) -> RichResult:
  conf = dict(info)
  pkg_name = conf.get('go')

  url = GO_PKG_URL.format(pkg=pkg_name)
  res = await session.get(url)
  doc = html.fromstring(res.body.decode())

  elements = doc.xpath("//div[@class='Version-tag']/a/text()")
  try:
    version = elements[0]
    return RichResult(
      version = version,
      url = GO_PKG_VERSION_URL.format(pkg=pkg_name, version=version),
    )
  except IndexError:
    raise GetVersionError("parse error", pkg_name=pkg_name)