Ticket #26: buildbot-gzip.patch

File buildbot-gzip.patch, 2.6 kB (added by slinkp, 8 months ago)

first attempt at gzip support patch

  • buildbot/status/builder.py

    old new  
    66from twisted.internet import reactor, defer 
    77from twisted.protocols import basic 
    88 
     9import gzip 
    910import os, shutil, sys, re, urllib, itertools 
     11 
    1012from cPickle import load, dump 
    1113from cStringIO import StringIO 
    1214 
     
    4042HEADER = interfaces.LOG_CHANNEL_HEADER 
    4143ChunkTypes = ["stdout", "stderr", "header"] 
    4244 
     45def gzopen(path, *args): 
     46    """Open a file, gzipped if possible.""" 
     47    if args: 
     48        mode = args[0] 
     49    else: 
     50        mode = 'r' 
     51    f = gzip.open(path, *args) 
     52    if mode.startswith('w'): 
     53        # we're wiping out any existing file, so, unconditionally gzip it. 
     54        return f 
     55    try: 
     56        # See if the existing file is really gzipped. 
     57        f.seek(1) 
     58        f.seek(0) 
     59        return f 
     60    except IOError: 
     61        del f 
     62    # We have an existing, non-gzipped file. 
     63    return open(path, *args) 
     64 
     65 
    4366class LogFileScanner(basic.NetstringReceiver): 
    4467    def __init__(self, chunk_cb, channels=[]): 
    4568        self.chunk_cb = chunk_cb 
     
    5073        if not self.channels or (channel in self.channels): 
    5174            self.chunk_cb((channel, line[1:])) 
    5275 
     76     
    5377class LogFileProducer: 
    5478    """What's the plan? 
    5579 
     
    227251            # is out of date, and we're overlapping with earlier builds now. 
    228252            # Warn about it, but then overwrite the old pickle file 
    229253            log.msg("Warning: Overwriting old serialized Build at %s" % fn) 
    230         self.openfile = open(fn, "w+") 
     254        self.openfile = gzopen(fn, "w+") 
    231255        self.runEntries = [] 
    232256        self.watchers = [] 
    233257        self.finishedWatchers = [] 
     
    260284            # don't close it! 
    261285            return self.openfile 
    262286        # otherwise they get their own read-only handle 
    263         return open(self.getFilename(), "r") 
     287        return gzopen(self.getFilename(), "r") 
    264288 
    265289    def getText(self): 
    266290        # this produces one ginormous string 
     
    441465        pickled LogFile (inside the pickled Build) won't be modified.""" 
    442466        self.filename = logfilename 
    443467        if not os.path.exists(self.getFilename()): 
    444             self.openfile = open(self.getFilename(), "w") 
     468            self.openfile = gzopen(self.getFilename(), "w") 
    445469            self.finished = False 
    446470            for channel,text in self.entries: 
    447471                self.addEntry(channel, text)