pebble_package.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2024 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import print_function
  15. import argparse
  16. import errno
  17. import os
  18. from shutil import rmtree
  19. import zipfile
  20. class MissingFileException(Exception):
  21. pass
  22. class DuplicatePackageFileException(Exception):
  23. pass
  24. def _calculate_file_size(path):
  25. return os.stat(path).st_size
  26. def _calculate_crc(path):
  27. pass
  28. class PebblePackage(object):
  29. def __init__(self, package_filename):
  30. self.package_filename = package_filename
  31. self.package_files = {}
  32. def add_file(self, name, file_path):
  33. if not os.path.exists(file_path):
  34. raise MissingFileException("The file '{}' does not exist".format(file_path))
  35. if name in self.package_files and self.package_files.get(name) != file_path:
  36. raise DuplicatePackageFileException("The file '{}' cannot be added to the package "
  37. "because `{}` has already been assigned to `{}`".
  38. format(file_path,
  39. self.package_files.get(name),
  40. name))
  41. else:
  42. self.package_files[name] = file_path
  43. def pack(self, package_path=None):
  44. with zipfile.ZipFile(os.path.join(package_path, self.package_filename), 'w') as zip_file:
  45. for filename, file_path in self.package_files.iteritems():
  46. zip_file.write(file_path, filename)
  47. zip_file.comment = type(self).__name__
  48. def unpack(self, package_path=''):
  49. try:
  50. rmtree(package_path)
  51. except OSError as e:
  52. if e.errno != errno.ENOENT:
  53. raise e
  54. with zipfile.ZipFile(self.package_filename, 'r') as zip_file:
  55. zip_file.extractall(package_path)
  56. class RockyPackage(PebblePackage):
  57. def __init__(self, package_filename):
  58. super(RockyPackage, self).__init__(package_filename)
  59. def add_files(self, rockyjs, binaries, resources, pkjs, platforms):
  60. for platform in platforms:
  61. self.add_file(os.path.join(platform, rockyjs[platform]), rockyjs[platform])
  62. self.add_file(os.path.join(platform, binaries[platform]), binaries[platform])
  63. self.add_file(os.path.join(platform, resources[platform]), resources[platform])
  64. self.add_file(pkjs, pkjs)
  65. def write_manifest(self):
  66. pass
  67. class LibraryPackage(PebblePackage):
  68. def __init__(self, package_filename="dist.zip"):
  69. super(LibraryPackage, self).__init__(package_filename)
  70. def add_files(self, includes, binaries, resources, js):
  71. for include, include_path in includes.iteritems():
  72. self.add_file(os.path.join('include', include), include_path)
  73. for binary, binary_path in binaries.iteritems():
  74. self.add_file(os.path.join('binaries', binary), binary_path)
  75. for resource, resource_path in resources.iteritems():
  76. self.add_file(os.path.join('resources', resource), resource_path)
  77. for js_file, js_file_path in js.iteritems():
  78. self.add_file(os.path.join('js', js_file), js_file_path)
  79. def unpack(self, package_path='dist'):
  80. super(LibraryPackage, self).unpack(package_path)
  81. if __name__ == '__main__':
  82. parser = argparse.ArgumentParser(description="Manage Pebble packages")
  83. parser.add_argument('command', type=str, help="Command to use")
  84. parser.add_argument('filename', type=str, help="Path to your Pebble package")
  85. args = parser.parse_args()
  86. with zipfile.ZipFile(args.filename, 'r') as package:
  87. cls = globals()[package.comment](args.filename)
  88. getattr(cls, args.command)()