lockfile.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. from abc import ABCMeta, abstractmethod
  3. from six import add_metaclass
  4. class LockfilePackageMeta(object):
  5. """
  6. Basic struct representing package meta from lockfile.
  7. """
  8. __slots__ = ("key", "tarball_url", "sky_id", "integrity", "integrity_algorithm", "tarball_path")
  9. @staticmethod
  10. def from_str(s):
  11. return LockfilePackageMeta(*s.strip().split(" "))
  12. def __init__(self, key, tarball_url, sky_id, integrity, integrity_algorithm):
  13. # http://npm.yandex-team.ru/@scope%2fname/-/name-0.0.1.tgz
  14. parts = tarball_url.split("/")
  15. self.key = key
  16. self.tarball_url = tarball_url
  17. self.sky_id = sky_id
  18. self.integrity = integrity
  19. self.integrity_algorithm = integrity_algorithm
  20. self.tarball_path = "/".join(parts[-3:]) # @scope%2fname/-/name-0.0.1.tgz
  21. def to_str(self):
  22. return " ".join([self.tarball_url, self.sky_id, self.integrity, self.integrity_algorithm])
  23. def to_uri(self):
  24. pkg_uri = f"{self.tarball_url}#integrity={self.integrity_algorithm}-{self.integrity}"
  25. return pkg_uri
  26. class LockfilePackageMetaInvalidError(RuntimeError):
  27. pass
  28. @add_metaclass(ABCMeta)
  29. class BaseLockfile(object):
  30. @classmethod
  31. def load(cls, path):
  32. """
  33. :param path: lockfile path
  34. :type path: str
  35. :rtype: BaseLockfile
  36. """
  37. pj = cls(path)
  38. pj.read()
  39. return pj
  40. def __init__(self, path):
  41. if not os.path.isabs(path):
  42. raise TypeError("Absolute path required, given: {}".format(path))
  43. self.path = path
  44. self.data = None
  45. @abstractmethod
  46. def read(self):
  47. pass
  48. @abstractmethod
  49. def write(self, path=None):
  50. pass
  51. @abstractmethod
  52. def get_packages_meta(self):
  53. pass
  54. @abstractmethod
  55. def update_tarball_resolutions(self, fn):
  56. pass
  57. @abstractmethod
  58. def validate_has_addons_flags(self):
  59. pass
  60. @abstractmethod
  61. def get_requires_build_packages(self):
  62. pass