filewriter.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python
  2. """
  3. Helper code for file writing with optional compression.
  4. @contact: Debian FTPMaster <ftpmaster@debian.org>
  5. @copyright: 2011 Torsten Werner <twerner@debian.org>
  6. @license: GNU General Public License version 2 or later
  7. """
  8. ################################################################################
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. ################################################################################
  21. from daklib.daksubprocess import check_call
  22. import errno
  23. import os
  24. import os.path
  25. class CompressionMethod(object):
  26. def __init__(self, keyword, extension, command):
  27. self.keyword = keyword
  28. self.extension = extension
  29. self.command = command
  30. _compression_methods = (
  31. CompressionMethod('bzip2', '.bz2', ['bzip2', '-9']),
  32. CompressionMethod('gzip', '.gz', ['gzip', '-9cn', '--rsyncable', '--no-name']),
  33. CompressionMethod('xz', '.xz', ['xz', '-c']),
  34. # 'none' must be the last compression method as BaseFileWriter
  35. # handling it will remove the input file for other compressions
  36. CompressionMethod('none', '', None),
  37. )
  38. class BaseFileWriter(object):
  39. '''
  40. Base class for compressed and uncompressed file writing.
  41. '''
  42. def __init__(self, template, **keywords):
  43. '''
  44. The template argument is a string template like
  45. "dists/%(suite)s/%(component)s/Contents-%(architecture)s.gz" that
  46. should be relative to the archive's root directory. The keywords
  47. include strings for suite, component, architecture and booleans
  48. uncompressed, gzip, bzip2.
  49. '''
  50. self.compression = keywords.get('compression', ['none'])
  51. self.path = template % keywords
  52. def open(self):
  53. '''
  54. Returns a file object for writing.
  55. '''
  56. # create missing directories
  57. try:
  58. os.makedirs(os.path.dirname(self.path))
  59. except:
  60. pass
  61. self.file = open(self.path + '.new', 'w')
  62. return self.file
  63. # internal helper function
  64. def rename(self, filename):
  65. tempfilename = filename + '.new'
  66. os.chmod(tempfilename, 0o644)
  67. os.rename(tempfilename, filename)
  68. # internal helper function to compress output
  69. def compress(self, cmd, suffix, path):
  70. in_filename = "{0}.new".format(path)
  71. out_filename = "{0}{1}.new".format(path, suffix)
  72. if cmd is not None:
  73. with open(in_filename, 'r') as in_fh, open(out_filename, 'w') as out_fh:
  74. check_call(cmd, stdin=in_fh, stdout=out_fh, close_fds=True)
  75. self.rename("{0}{1}".format(path, suffix))
  76. def close(self):
  77. '''
  78. Closes the file object and does the compression and rename work.
  79. '''
  80. self.file.close()
  81. for method in _compression_methods:
  82. if method.keyword in self.compression:
  83. self.compress(method.command, method.extension, self.path)
  84. else:
  85. # Try removing the file that would be generated.
  86. # It's not an error if it does not exist.
  87. try:
  88. os.unlink("{0}{1}".format(self.path, method.extension))
  89. except OSError as e:
  90. if e.errno != errno.ENOENT:
  91. raise
  92. else:
  93. os.unlink(self.path + '.new')
  94. class BinaryContentsFileWriter(BaseFileWriter):
  95. def __init__(self, **keywords):
  96. '''
  97. The value of the keywords suite, component, and architecture are
  98. strings. The value of component may be omitted if not applicable.
  99. Output files are gzip compressed only.
  100. '''
  101. flags = {
  102. 'compression': ['gzip'],
  103. }
  104. flags.update(keywords)
  105. if flags['debtype'] == 'deb':
  106. template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-%(architecture)s"
  107. else: # udeb
  108. template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-udeb-%(architecture)s"
  109. BaseFileWriter.__init__(self, template, **flags)
  110. class SourceContentsFileWriter(BaseFileWriter):
  111. def __init__(self, **keywords):
  112. '''
  113. The value of the keywords suite and component are strings.
  114. Output files are gzip compressed only.
  115. '''
  116. flags = {
  117. 'compression': ['gzip'],
  118. }
  119. flags.update(keywords)
  120. template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-source"
  121. BaseFileWriter.__init__(self, template, **flags)
  122. class PackagesFileWriter(BaseFileWriter):
  123. def __init__(self, **keywords):
  124. '''
  125. The value of the keywords suite, component, debtype and architecture
  126. are strings. Output files are gzip compressed only.
  127. '''
  128. flags = {
  129. 'compression': ['gzip', 'xz'],
  130. }
  131. flags.update(keywords)
  132. if flags['debtype'] == 'deb':
  133. template = "%(archive)s/dists/%(suite)s/%(component)s/binary-%(architecture)s/Packages"
  134. else: # udeb
  135. template = "%(archive)s/dists/%(suite)s/%(component)s/debian-installer/binary-%(architecture)s/Packages"
  136. BaseFileWriter.__init__(self, template, **flags)
  137. class SourcesFileWriter(BaseFileWriter):
  138. def __init__(self, **keywords):
  139. '''
  140. The value of the keywords suite and component are strings. Output
  141. files are gzip compressed only.
  142. '''
  143. flags = {
  144. 'compression': ['gzip', 'xz'],
  145. }
  146. flags.update(keywords)
  147. template = "%(archive)s/dists/%(suite)s/%(component)s/source/Sources"
  148. BaseFileWriter.__init__(self, template, **flags)
  149. class TranslationFileWriter(BaseFileWriter):
  150. def __init__(self, **keywords):
  151. '''
  152. The value of the keywords suite, component and language are strings.
  153. Output files are bzip2 compressed only.
  154. '''
  155. flags = {
  156. 'compression': ['bzip2'],
  157. 'language': 'en',
  158. }
  159. flags.update(keywords)
  160. template = "%(archive)s/dists/%(suite)s/%(component)s/i18n/Translation-%(language)s"
  161. super(TranslationFileWriter, self).__init__(template, **flags)