deb.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) 2021, Open Source Robotics Foundation
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. # * Neither the name of the Willow Garage, Inc. nor the names of its
  13. # contributors may be used to endorse or promote products derived from
  14. # this software without specific prior written permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. # POSSIBILITY OF SUCH DAMAGE.
  27. import os
  28. from . import open_gz_url
  29. from . import PackageEntry
  30. from . import RepositoryCacheCollection
  31. def enumerate_blocks(url):
  32. """
  33. Enumerate blocks of mapped data from a URL to a text file.
  34. :param url: the URL of the text file.
  35. :returns: an enumeration of mappings.
  36. """
  37. block = {}
  38. key = None
  39. with open_gz_url(url) as f:
  40. while True:
  41. line = f.readline().decode('utf-8')
  42. if not len(line):
  43. break
  44. elif line[0] in ['\r', '\n']:
  45. yield block
  46. block = {}
  47. key = None
  48. continue
  49. elif line[0] in [' ', '\t']:
  50. # This is a list element
  51. if not key:
  52. raise ValueError('list element at block beginning')
  53. if not isinstance(block[key], list):
  54. block[key] = [block[key]] if block[key] else []
  55. block[key].append(line.strip())
  56. continue
  57. key, val = line.split(':', 1)
  58. key = key.strip()
  59. val = val.strip()
  60. if not key:
  61. raise ValueError('empty key')
  62. block[key] = val
  63. if block:
  64. yield block
  65. def enumerate_deb_packages(base_url, comp, os_code_name, os_arch):
  66. """
  67. Enumerate debian packages in a repository.
  68. :param base_url: the debian repository base URL.
  69. :param comp: the component of the repository to enumerate.
  70. :param os_code_name: the OS version associated with the repository.
  71. :param os_arch: the system architecture associated with the repository.
  72. :returns: an enumeration of package entries.
  73. """
  74. pkgs_url = os.path.join(base_url, 'dists', os_code_name,
  75. comp, 'binary-' + os_arch, 'Packages.gz')
  76. print('Reading debian package metadata from ' + pkgs_url)
  77. for block in enumerate_blocks(pkgs_url):
  78. pkg_url = os.path.join(base_url, block['Filename'])
  79. yield PackageEntry(block['Package'], block['Version'], pkg_url,
  80. block.get('Source', block['Package']))
  81. def deb_base_url(base_url, comp):
  82. """
  83. Create an enumerable cache for a debian repository.
  84. :param base_url: the URL of the debian repository.
  85. :returns: an enumerable repository cache instance.
  86. """
  87. return RepositoryCacheCollection(
  88. lambda os_name, os_code_name, os_arch:
  89. enumerate_deb_packages(base_url, comp, os_code_name, os_arch))