+ header = None # a class object holding the header section of contents file
+
+ def __init__(self, filename):
+ """
+ @ptype filename: string
+ @param filename: the name of the file to write to
+ """
+ self.queue = Queue.Queue()
+ self.current_file = None
+ self.first_package = True
+ self.output = self.open_file(filename)
+ self.thread = threading.Thread(target=self.write_thread,
+ name='Contents writer')
+ self.thread.start()
+
+ def open_file(self, filename):
+ """
+ opens a gzip stream to the contents file
+ """
+ filepath = Config()["Contents::Root"] + filename
+ filedir = os.path.dirname(filepath)
+ if not os.path.isdir(filedir):
+ os.makedirs(filedir)
+ return gzip.open(filepath, "w")
+
+ def write(self, filename, section, package):
+ """
+ enqueue content to be written to the file on a separate thread
+ """
+ self.queue.put((filename,section,package))
+
+ def write_thread(self):
+ """
+ the target of a Thread which will do the actual writing
+ """
+ while True:
+ next = self.queue.get()
+ if isinstance(next, EndOfContents):
+ self.output.write('\n')
+ self.output.close()
+ break
+
+ (filename,section,package)=next
+ if next != self.current_file:
+ # this is the first file, so write the header first
+ if not self.current_file:
+ self.output.write(self._getHeader())
+
+ self.output.write('\n%s\t' % filename)
+ self.first_package = True
+
+ self.current_file=filename
+
+ if not self.first_package:
+ self.output.write(',')
+ else:
+ self.first_package=False
+ self.output.write('%s/%s' % (section,package))