aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/lib/nots/package_manager/base/lockfile.py
blob: 5ff0cbf4495be39cc0676d7eae4b554a0b19d79b (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os

from abc import ABCMeta, abstractmethod
from six import add_metaclass


class LockfilePackageMetaInvalidError(RuntimeError):
    pass


def is_tarball_url_valid(tarball_url):
    if not tarball_url.startswith("https://") and not tarball_url.startswith("http://"):
        return True

    return tarball_url.startswith("https://npm.yandex-team.ru/") or tarball_url.startswith("http://npm.yandex-team.ru/")


class LockfilePackageMeta(object):
    """
    Basic struct representing package meta from lockfile.
    """

    __slots__ = ("key", "tarball_url", "sky_id", "integrity", "integrity_algorithm", "tarball_path")

    @staticmethod
    def from_str(s):
        return LockfilePackageMeta(*s.strip().split(" "))

    def __init__(self, key, tarball_url, sky_id, integrity, integrity_algorithm):
        if not is_tarball_url_valid(tarball_url):
            raise LockfilePackageMetaInvalidError(
                "tarball can only point to npm.yandex-team.ru, got {}".format(tarball_url)
            )

        # http://npm.yandex-team.ru/@scope%2fname/-/name-0.0.1.tgz
        parts = tarball_url.split("/")

        self.key = key
        self.tarball_url = tarball_url
        self.sky_id = sky_id
        self.integrity = integrity
        self.integrity_algorithm = integrity_algorithm
        self.tarball_path = "/".join(parts[-3:]).replace("%2f", "/")  # @scope%2fname/-/name-0.0.1.tgz

    def to_str(self):
        return " ".join([self.tarball_url, self.sky_id, self.integrity, self.integrity_algorithm])

    def to_uri(self):
        tarball_url: str = self.tarball_url
        if not tarball_url.startswith("https://") and not tarball_url.startswith("http://"):
            tarball_url = "https://npm.yandex-team.ru/" + tarball_url
        pkg_uri = f"{tarball_url}#integrity={self.integrity_algorithm}-{self.integrity}"
        return pkg_uri


@add_metaclass(ABCMeta)
class BaseLockfile(object):
    @classmethod
    def load(cls, path):
        """
        :param path: lockfile path
        :type path: str
        :rtype: BaseLockfile
        """
        pj = cls(path)
        pj.read()

        return pj

    def __init__(self, path):
        if not os.path.isabs(path):
            raise TypeError("Absolute path required, given: {}".format(path))

        self.path = path
        self.data = None

    @abstractmethod
    def read(self):
        pass

    @abstractmethod
    def write(self, path=None):
        pass

    @abstractmethod
    def get_packages_meta(self):
        pass

    @abstractmethod
    def update_tarball_resolutions(self, fn):
        pass

    @abstractmethod
    def validate_has_addons_flags(self):
        pass

    @abstractmethod
    def get_requires_build_packages(self):
        pass