Source

builders / build.py

Full commit
#!/usr/bin/env python

from __future__ import with_statement

import os
import os.path
import platform
import shutil
import subprocess
import sys
import urllib

class machine(object):
    if platform.architecture()[0] == '64bit':
        bits = 64
    else:
        bits = 32

    os_name = platform.system().lower()


def clean(path):
    return os.path.abspath(os.path.expandvars(os.path.expanduser(path)))


def error(msg):
    print msg
    sys.exit(1)


def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)


def rmtree(path):
    if os.path.exists(path):
        shutil.rmtree(path)


join = os.path.join


ROOT = os.path.expanduser('~/software')
SRC_DIR = join(ROOT, 'src')
BUILD_DIR = join(ROOT, 'build')
INSTALL_DIR = join(ROOT, 'install')


def log(str):
    print str


def call(argv, cwd, fail_ok=False):
    log('+ %s (cwd=%s)' % (' '.join(argv), cwd))
    try:
        subprocess.check_call(argv, cwd=cwd,
                              stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError:
        if not fail_ok:
            raise


def get_parallelism_flag():
    count = 0
    f = open('/proc/cpuinfo')
    try:
        for line in f:
            if line.startswith('processor'):
                count += 1
        return '-j%d' % (count + 1)
    finally:
        f.close()
parallelism_flag = get_parallelism_flag()


def apt_get(packages, remove=False):
    if remove:
        call(['sudo', 'apt-get', 'remove', '--purge'] + packages, cwd='/')
    else:
        call(['sudo', 'apt-get', 'install', '-y'] + packages, cwd='/')


def download(url, filename):
    mkdir(SRC_DIR)
    out = join(SRC_DIR, filename)

    if not os.path.exists(out):
        log('Downloading ' + url)
        urllib.urlretrieve(url, out)
    else:
        log('Not redownloading ' + url)


def decompress(filename):
    path = join(SRC_DIR, filename)
    mkdir(BUILD_DIR)

    GZ_EXT = '.tar.gz'
    BZ_EXT = '.tar.bz2'

    log('Decompressing ' + filename)

    if filename.endswith(GZ_EXT):
        call(['tar', 'zxvf', path], cwd=BUILD_DIR)
        extension = GZ_EXT
    elif filename.endswith(BZ_EXT):
        call(['tar', 'jxvf', path], cwd=BUILD_DIR)
        extension = BZ_EXT
    else:
        error('Unknown archive format for ' + filename)

    dir = join(BUILD_DIR, filename[:-len(extension)])
    if not os.path.exists(dir):
        error('Could not find decompressed directory for ' + filename)

    return dir


def init(url, filename=None):
    rmtree(BUILD_DIR)
    if not filename:
        filename = os.path.basename(url)
    download(url, filename)
    return decompress(filename)


def configure(dir, config_args=[]):
    call([join(dir, 'configure'),
          '--prefix=' + INSTALL_DIR] +
         config_args, cwd=dir)


def make(dir):
    call(['make', parallelism_flag], cwd=dir)


def install(dir):
    call(['make', 'install'], cwd=dir)


def configure_make_install(url, filename=None, config_args=[]):
    dir = init(url, filename)
    configure(dir, config_args)
    make(dir)
    install(dir)