File update.py of Package java-1_7_0-openjdk.openSUSE_12.3_Update

#!/usr/bin/python
#
# generates the script updates tarballs and update working copy
#
# Copyright (C) 2013-2014, Michal Vyskocil<mvyskocil@suse.com>
#
# Released under terms of MIT, see
# http://opensource.org/licenses/MIT
#
# Changelog:
#   2014-01-15: require icedtea-dir, instead of Makefile.am and hotspot.map paths
#   2013-05-13: initial version

from __future__ import print_function

import re
import collections
import sys
import os.path

if len(sys.argv) < 3:
    print("Usage: update.py spec icedtea-dir")
    sys.exit(1)

SPEC=sys.argv[1]
ITDIR=sys.argv[2]

if not os.path.isdir(ITDIR):
    print("Usage: update.py spec icedtea-dir")
    sys.exit(1)

MAKEFILE=os.path.join(ITDIR, "Makefile.am")
HOTSPOT_MAP=os.path.join(ITDIR, "hotspot.map")

SOURCE_RE = re.compile(r'^Source([5-9]|1[0-3]):')

MAKEFILE_DEF_PATTERN = (r"(?P<def>[A-Z_]+)")
MAKEFILE_DEF_RE = re.compile(r"\$\({}\)".format(MAKEFILE_DEF_PATTERN))
MAKEFILE_DEFLINE_RE = re.compile(r"^{} = (?P<value>.*)".format(MAKEFILE_DEF_PATTERN))

defs = None

class MakefileDefs(object):

    def __init__(self, d):
        self._d = d

    @classmethod
    def fromio(cls, io):
        ret = dict()

        for line in io:
            m = MAKEFILE_DEFLINE_RE.match(line)
            if m:
                ret[m.group('def')] = m.group('value')

        return cls(ret)

    def __getitem__(self, key):
        v = self._d[key]

        m = MAKEFILE_DEF_RE.search(v)
        while m:
            #unless makefile, we ends with an error on undefined variable
            v = v.replace(m.group(), defs[m.groups()[0]], 1)
            self._d[key] = v
            m = MAKEFILE_DEF_RE.search(v)

        return v

    def get(self, key, default=None):
        try:
            return self.__getitem__(key)
        except KeyError:
            return default

    def __setitem__(self, key, value):
        return self._d.__setitem__(key, value)

    def update_from_map(self, io):
        for line in hm:
            key, url, changeset, sha = line.split(' ', 3)
            if key == 'default':
                self._d['HOTSPOT_CHANGESET'] = changeset
                self._d['HOTSPOT_URL'] = url
            elif key == 'aarch64':
                self._d['AARCH64_CHANGESET'] = changeset
                self._d['AARCH64_URL'] = url

        return self

with open(MAKEFILE, "rt") as make:

    defs = MakefileDefs.fromio(make)

if HOTSPOT_MAP is not None:
    with open(HOTSPOT_MAP, 'rt') as hm:
        defs.update_from_map(hm)


MAP = {
    'Source5:'  :   'OPENJDK',
    'Source6:'  :   'CORBA',
    'Source7:'  :   'JAXP',
    'Source8:'  :   'JAXWS',
    'Source9:'  :   'JDK',
    'Source10:' :   'LANGTOOLS',
    'Source11:' :   'HOTSPOT',
    'Source12:' :   'AARCH64',
    'Source13:' :   'JAMVM'
    }

def generate_update_script_dict(spec_io, defs, MAP):

    us = collections.defaultdict(list)

    for line in spec:

        m = SOURCE_RE.match(line)
        if not m:
            continue
        
        # things from spec file
        source, url = line.split(' ', 1)
        url = url.strip()
        f = url.rsplit('/', 1)[1]

        # find matching Makefile.am defs
        dpref = MAP[source]
        if dpref not in ("JAMVM", ):
            changeset = defs.get("{}_CHANGESET".format(dpref))
            if not changeset:
                print("WARNING: {}_CHANGESET not available".format(dpref), file=sys.stderr)
                continue
            m_url = "{}/archive/{}.tar.gz".format(
                     defs["{}_URL".format(dpref)],
                     changeset)
        else:
            changeset = defs["{}_VERSION".format(dpref)]
            m_url = defs["{}_URL".format(dpref)]

        if m_url != url:
            us['wget'].append(m_url)
            us['osc'].append(f)
            us['sed'].append((source, m_url))

    return us

def print_update_script(out, us):

    if not us:
        pass

    out.write("""#!/bin/bash
#
#  an openjdk.spec update script generated by update.py
#
#  this downloads all files, removes old ones, adds new ones and change spec
#

# be verbose
set -x

if [[ -n "$(osc status | grep '^?')" ]]; then
    echo "ERROR: osc status shows come uncommited changes, please fix-em-first"
    exit 1
fi



""")
    for url in us['wget']:
        out.write("[[ ! -f \"{file}\" ]] && wget -q '{url}'\n".format(url=url, file=url.split("/")[-1:][0]))

    for f in us['osc']:
        out.write("osc rm -f '{}'\n".format(f))

    for source, url in us['sed']:
        out.write("sed -i 's@^{s}.*@{s}       {u}@' {spec}\n".format(
            s=source,
            space=(17-len(source))*' ',
            u=url,
            spec=SPEC))

    out.write("osc addremove\n")

with open(SPEC, "rt") as spec:
    us = generate_update_script_dict(spec, defs, MAP)
    print_update_script(sys.stdout, us)
openSUSE Build Service is sponsored by