Changeset 387

Show
Ignore:
Timestamp:
2007-06-06 17:46:36 (1 year ago)
Author:
MaierMan
Message:

restructuring and some more options

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tools/make.py

    r320 r387  
    11#!/bin/env python 
     2""" 
     3Small python make tool for XPIs using SVN. 
     4(C) 2007 Nils Maier 
     5Licensed under MPL1.1/GPL2.0/LGPL2.1 
     6""" 
     7 
    28import os, re, sys 
    39import dircache 
     
    1824from time import strftime 
    1925 
    20  
    21 svn_path = 'http://code.downthemall.net/repos/%s/' 
     26BLOCK_START = "***** BEGIN LICENSE BLOCK *****" 
     27BLOCK_END = "***** END LICENSE BLOCK *****" 
     28 
     29block_replacement = "You may find the license in the LICENSE file" 
     30svn_path = 'http://code.downthemall.net/repos/' 
    2231locales_url = 'http://www.babelzilla.org/components/com_wts/com_download.php?extension=1455&type=all' 
    2332locales_headers = {} 
     33locales_file = 'locales.tar.gz' 
    2434update_url = 'http://code.downthemall.net/nightly/update.rdf' 
    2535 
    2636xpi_file = 'downthemall%s.xpi' 
    2737 
    28 def prepare(): 
    29     if path.isdir('export'): 
    30         rmtree('export') 
    31  
    32 def checkout(module): 
    33     global svn_path 
    34     client = svn.Client() 
    35     client.export(svn_path % module, 'export', True) 
    36  
    37 def get_locales(cookie): 
    38     global locales_url, locales_header 
    39  
    40     if path.isfile('locales.tar.gz'): 
    41         os.remove('locales.tar.gz') 
    42  
    43     locales_headers['Cookie'] = cookie 
    44  
    45  
    46     url = urlparse(locales_url) 
    47     http = HTTPConnection(url.hostname, url.port) 
    48     http.request('GET', "%s?%s" % (url.path, url.query), None, locales_headers) 
    49     r = http.getresponse() 
    50     f = open('locales.tar.gz', 'wb') 
    51     f.write(r.read()) 
    52     f.close() 
    53     http.close() 
    54  
    55 def integrate_locales(): 
    56     tar = tarfile.open('locales.tar.gz') 
    57     tar.extractall('export/chrome/locale/') 
    58  
    59     man = open("export/chrome.manifest", 'ab') 
    60     man.write("# integrated locales:\n") 
     38class XPI: 
     39    rev = None 
    6140     
    62     locales = dircache.listdir('export/chrome/locale/') 
    63     for x in locales: 
    64         if not re.match(r'\w+-\w+$', x): 
    65             continue 
    66         if x == 'en-US': 
    67             continue 
    68         man.write('locale\tdta\t%s\tchrome/locale/%s/\n' % (x, x)) 
    69     man.close() 
    70  
    71 def nightlify(module): 
    72     global update_url, svn_path 
    73  
    74     client = svn.Client() 
    75     rev = client.info2(svn_path % module, recurse=False)[0][1].rev.number 
    76  
    77     'open' 
    78     rdf = xml_open('export/install.rdf') 
    79  
    80     'update the version' 
    81     node = rdf.getElementsByTagName('version')[0].childNodes[0] 
    82     node.data = "%s.%s.%s" % (node.data, strftime("%Y%m%d"), rev) 
    83  
    84     node = rdf.getElementsByTagName('name')[0].childNodes[0] 
    85     node.data = node.data + ' *nightly*' 
    86  
    87     'insert the updateURL node' 
    88     node = rdf.getElementsByTagName('aboutURL')[0] 
    89     u = rdf.createElement('updateURL') 
    90     u.appendChild(rdf.createTextNode(update_url)) 
    91     node.parentNode.insertBefore(u, node) 
    92  
    93     'prettify' 
    94     node.parentNode.insertBefore(rdf.createTextNode('\n\t\t'), node) 
    95  
    96     'save' 
    97     f = open('export/install.rdf', 'w') 
    98     rdf.writexml(f) 
    99  
    100     'cleanup' 
    101     rdf.unlink() 
    102     f.close() 
    103          
    104  
    105 def xpi_filelist(p): 
    106     l = dircache.listdir(p)[:] 
    107     for x in l: 
    108         f = p + "/" + x 
    109         if path.isfile(f): 
    110             yield unicode(f) 
    111         elif path.isdir(f): 
    112             for i in xpi_filelist(f): 
    113                 yield i 
    114  
    115 def xpi(version): 
    116     global xpi_file 
    117  
    118     if version: 
    119         rdf = xml_open('export/install.rdf') 
    120         version = '-' + rdf.getElementsByTagName('version')[0].childNodes[0].data 
     41    def __init__(self, opts, repos, exportTo='export'): 
     42        self.client = svn.Client() 
     43        self.opts = opts 
     44        self.opts.branch = repos + opts.branch + "/" 
     45        self.opts.exportTo = exportTo 
     46         
     47    def create(self, xpi_file): 
     48        global locales_url, locales_headers, update_url 
     49        yield "preparing" 
     50        self.prepare() 
     51        yield "checking out" 
     52        self.checkout() 
     53        if self.opts.locget: 
     54            yield "getting locales" 
     55            self.getlocales(locales_url, locales_headers, locales_file, self.opts.locget) 
     56        if self.opts.locint or self.opts.locget: 
     57            yield "integrating locales" 
     58            self.integratelocales(locales_file) 
     59        if self.opts.nightly: 
     60            yield "nightlifying" 
     61            self.nightlify(update_url) 
     62        if self.opts.nocomments: 
     63            yield "stripping comments" 
     64            for x in self.stripcomments(): 
     65                yield x 
     66        yield "creating the xpi" 
     67        self.createXPI(xpi_file) 
     68        yield "cleaning up" 
     69        self.cleanup() 
     70 
     71    def prepare(self): 
     72        if path.isdir(self.opts.exportTo): 
     73            rmtree(self.opts.exportTo) 
     74    def checkout(self): 
     75        self.client.export(self.opts.branch, self.opts.exportTo, True) 
     76 
     77    def getlocales(self, url, headers, filename, cookie): 
     78        if path.isfile(filename): 
     79            os.remove(filename) 
     80        if not headers: 
     81            headers = dict() 
     82        if cookie: 
     83            headers['Cookie'] = cookie 
     84 
     85        url = urlparse(url) 
     86        http = HTTPConnection(url.hostname, url.port) 
     87        http.request('GET', "%s?%s" % (url.path, url.query), None, headers) 
     88        r = http.getresponse() 
     89        f = open(filename, 'wb') 
     90        f.write(r.read()) 
     91        f.close() 
     92        http.close() 
     93 
     94    def integratelocales(self, filename): 
     95        tar = tarfile.open(filename) 
     96        localedir = '%s/chrome/locale/' % self.opts.exportTo 
     97        tar.extractall(localedir) 
     98 
     99        man = open("export/chrome.manifest", 'ab') 
     100        man.write("# integrated locales:\n") 
     101         
     102        for x in dircache.listdir(localedir): 
     103            if not re.match(r'\w+-\w+$', x): 
     104                continue 
     105            if x == 'en-US': 
     106                continue 
     107            man.write('locale\tdta\t%s\tchrome/locale/%s/\n' % (x, x)) 
     108        man.close() 
     109 
     110    def getrevision(self): 
     111        if not self.rev: 
     112            self.rev = self.client.info2(self.opts.branch, recurse=False)[0][1].rev.number 
     113        return self.rev 
     114         
     115 
     116    def nightlify(self, update_url): 
     117        'open' 
     118        rdf = xml_open('%s/install.rdf' % self.opts.exportTo) 
     119 
     120        'update the version' 
     121        node = rdf.getElementsByTagName('version')[0].childNodes[0] 
     122        node.data = "%s.%s.%s" % (node.data, strftime("%Y%m%d"), self.getrevision()) 
     123 
     124        node = rdf.getElementsByTagName('name')[0].childNodes[0] 
     125        node.data = node.data + ' *nightly*' 
     126 
     127        'insert the updateURL node' 
     128        node = rdf.getElementsByTagName('aboutURL')[0] 
     129        u = rdf.createElement('updateURL') 
     130        u.appendChild(rdf.createTextNode(update_url)) 
     131        node.parentNode.insertBefore(u, node) 
     132 
     133        'prettify' 
     134        node.parentNode.insertBefore(rdf.createTextNode('\n\t\t'), node) 
     135 
     136        'save' 
     137        f = open('export/install.rdf', 'w') 
     138        rdf.writexml(f) 
     139 
     140        'cleanup' 
    121141        rdf.unlink() 
    122     else: 
    123         version = '' 
    124  
    125     files = xpi_filelist('export') 
    126      
    127     xpi_file = ZipFile(xpi_file % version, 'w', ZIP_DEFLATED) 
    128     for x in files: 
    129         xpi_file.write(x, x[len('export') + 1:].encode('cp437')) 
    130     xpi_file.close()     
    131  
    132 def cleanup(): 
    133     if path.isdir('export'): 
    134         rmtree('export') 
     142        f.close()        
     143 
     144    def getfilelist(self, p): 
     145        l = dircache.listdir(p)[:] 
     146        for x in l: 
     147            f = p + "/" + x 
     148            if path.isfile(f): 
     149                yield unicode(f) 
     150            elif path.isdir(f): 
     151                for i in self.getfilelist(f): 
     152                    yield i 
     153 
     154    def stripcomments(self): 
     155        global BLOCK_START, BLOCK_END, block_replacement 
     156        replacement = r'%s.+%s' % (re.escape(BLOCK_START), re.escape(BLOCK_END)) 
     157        replacement = re.compile(replacement, re.S | re.M) 
     158        mask = re.compile(r'\.(xul|xml|dtd|js)$', re.I) 
     159        for f in self.getfilelist(self.opts.exportTo): 
     160            if not mask.search(f): 
     161                continue 
     162            p = open(f, 'rb') 
     163            c = p.read() 
     164            p.close() 
     165            c = replacement.sub(block_replacement, c) 
     166            p = open(f, 'wb') 
     167            p.write(c) 
     168            p.close() 
     169            yield f + " stripped" 
     170         
     171    def createXPI(self, xpi_file): 
     172        additional = '' 
     173 
     174        if self.opts.version: 
     175            rdf = xml_open('%s/install.rdf' % self.opts.exportTo) 
     176            additional += '-' + rdf.getElementsByTagName('version')[0].childNodes[0].data 
     177            rdf.unlink() 
     178        if self.opts.revision: 
     179            additional += '-r%d' % self.getrevision() 
     180        xpi_file = ZipFile(xpi_file % additional, 'w', ZIP_DEFLATED) 
     181        for x in self.getfilelist(self.opts.exportTo): 
     182            xpi_file.write(x, x[len('export') + 1:].encode('cp437')) 
     183        xpi_file.close() 
     184 
     185    def cleanup(self): 
     186        if path.isdir(self.opts.exportTo): 
     187            rmtree(self.opts.exportTo) 
    135188 
    136189def main(): 
     
    159212        ) 
    160213    parser.add_option( 
     214        '--addrevision', 
     215        dest="revision", 
     216        help="adds the SVN revision to the filename", 
     217        action="store_true", 
     218        default=False 
     219        ) 
     220    parser.add_option( 
    161221        '--nightly', 
    162222        dest='nightly', 
     
    165225        default=False 
    166226        )     
     227    parser.add_option( 
     228        '--nocomment', 
     229        dest='nocomments', 
     230        help='collapse those comment blocks', 
     231        action='store_true', 
     232        default=False 
     233        ) 
     234    parser.add_option( 
     235        '--quiet', 
     236        dest="quiet", 
     237        help="don't display info messages", 
     238        action="store_true", 
     239        default=False 
     240        ) 
    167241    opts, args = parser.parse_args() 
    168242    if len(args) > 1: 
     
    171245        args = ['trunk'] 
    172246 
    173     branch, = args 
    174  
    175     if opts.locget: 
    176         opts.locint = True 
    177  
    178     prepare() 
    179     checkout(branch) 
    180  
    181     if opts.locget: 
    182         get_locales(opts.locget) 
    183     if opts.locint: 
    184         integrate_locales() 
    185  
    186     if opts.nightly: 
    187         nightlify(branch) 
    188  
    189     xpi(opts.version) 
    190  
    191     cleanup() 
    192  
     247    opts.branch, = args 
     248 
     249    global xpi_file, svn_path 
     250 
     251    xpi = XPI(opts, svn_path) 
     252    for x in xpi.create(xpi_file): 
     253        if not opts.quiet: 
     254            print x 
    193255 
    194256if __name__ == "__main__":