filewriter.py 6.6 KB

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