filewriter.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.config import Config
  22. from daklib.daksubprocess import check_call
  23. import os, os.path
  24. class BaseFileWriter(object):
  25. '''
  26. Base class for compressed and uncompressed file writing.
  27. '''
  28. def __init__(self, template, **keywords):
  29. '''
  30. The template argument is a string template like
  31. "dists/%(suite)s/%(component)s/Contents-%(architecture)s.gz" that
  32. should be relative to the archive's root directory. The keywords
  33. include strings for suite, component, architecture and booleans
  34. uncompressed, gzip, bzip2.
  35. '''
  36. compression = keywords.get('compression', ['none'])
  37. self.uncompressed = 'none' in compression
  38. self.gzip = 'gzip' in compression
  39. self.bzip2 = 'bzip2' in compression
  40. self.xz = 'xz' in compression
  41. self.path = template % keywords
  42. def open(self):
  43. '''
  44. Returns a file object for writing.
  45. '''
  46. # create missing directories
  47. try:
  48. os.makedirs(os.path.dirname(self.path))
  49. except:
  50. pass
  51. self.file = open(self.path + '.new', 'w')
  52. return self.file
  53. # internal helper function
  54. def rename(self, filename):
  55. tempfilename = filename + '.new'
  56. os.chmod(tempfilename, 0o644)
  57. os.rename(tempfilename, filename)
  58. # internal helper function to compress output
  59. def compress(self, cmd, suffix, path):
  60. in_filename = "{0}.new".format(path)
  61. out_filename = "{0}.{1}.new".format(path, suffix)
  62. with open(in_filename, 'r') as in_fh, open(out_filename, 'w') as out_fh:
  63. check_call(cmd, stdin=in_fh, stdout=out_fh)
  64. self.rename("{0}.{1}".format(path, suffix))
  65. def close(self):
  66. '''
  67. Closes the file object and does the compression and rename work.
  68. '''
  69. self.file.close()
  70. if self.gzip:
  71. self.compress(['gzip', '-9cn', '--rsyncable'], 'gz', self.path)
  72. if self.bzip2:
  73. self.compress(['bzip2', '-9'], 'bz2', self.path)
  74. if self.xz:
  75. self.compress(['xz', '-c'], 'xz', self.path)
  76. if self.uncompressed:
  77. self.rename(self.path)
  78. else:
  79. os.unlink(self.path + '.new')
  80. class BinaryContentsFileWriter(BaseFileWriter):
  81. def __init__(self, **keywords):
  82. '''
  83. The value of the keywords suite, component, and architecture are
  84. strings. The value of component may be omitted if not applicable.
  85. Output files are gzip compressed only.
  86. '''
  87. flags = {
  88. 'compression': ['gzip'],
  89. }
  90. flags.update(keywords)
  91. if flags['debtype'] == 'deb':
  92. template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-%(architecture)s"
  93. else: # udeb
  94. template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-udeb-%(architecture)s"
  95. BaseFileWriter.__init__(self, template, **flags)
  96. class SourceContentsFileWriter(BaseFileWriter):
  97. def __init__(self, **keywords):
  98. '''
  99. The value of the keywords suite and component are strings.
  100. Output files are gzip compressed only.
  101. '''
  102. flags = {
  103. 'compression': ['gzip'],
  104. }
  105. flags.update(keywords)
  106. template = "%(archive)s/dists/%(suite)s/%(component)s/Contents-source"
  107. BaseFileWriter.__init__(self, template, **flags)
  108. class PackagesFileWriter(BaseFileWriter):
  109. def __init__(self, **keywords):
  110. '''
  111. The value of the keywords suite, component, debtype and architecture
  112. are strings. Output files are gzip compressed only.
  113. '''
  114. flags = {
  115. 'compression': ['gzip', 'xz'],
  116. }
  117. flags.update(keywords)
  118. if flags['debtype'] == 'deb':
  119. template = "%(archive)s/dists/%(suite)s/%(component)s/binary-%(architecture)s/Packages"
  120. else: # udeb
  121. template = "%(archive)s/dists/%(suite)s/%(component)s/debian-installer/binary-%(architecture)s/Packages"
  122. BaseFileWriter.__init__(self, template, **flags)
  123. class SourcesFileWriter(BaseFileWriter):
  124. def __init__(self, **keywords):
  125. '''
  126. The value of the keywords suite and component are strings. Output
  127. files are gzip compressed only.
  128. '''
  129. flags = {
  130. 'compression': ['gzip', 'xz'],
  131. }
  132. flags.update(keywords)
  133. template = "%(archive)s/dists/%(suite)s/%(component)s/source/Sources"
  134. BaseFileWriter.__init__(self, template, **flags)
  135. class TranslationFileWriter(BaseFileWriter):
  136. def __init__(self, **keywords):
  137. '''
  138. The value of the keywords suite, component and language are strings.
  139. Output files are bzip2 compressed only.
  140. '''
  141. flags = {
  142. 'compression': ['bzip2'],
  143. 'language': 'en',
  144. }
  145. flags.update(keywords)
  146. template = "%(archive)s/dists/%(suite)s/%(component)s/i18n/Translation-%(language)s"
  147. super(TranslationFileWriter, self).__init__(template, **flags)