Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/ooblib.py
blob: a746c941f7b64299254c9bac4dec7001b98b89b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (C) 2009 One Laptop Per Child
# Licensed under the terms of the GNU GPL v2 or later; see COPYING for details.

import os
import shutil
import urllib2
from xml.etree.ElementTree import ElementTree

libdir = os.environ['OOB__libdir']
bindir = os.environ['OOB__bindir']
builddir = os.environ['OOB__builddir']
cachedir = os.environ['OOB__cachedir']
intermediatesdir = os.environ['OOB__intermediatesdir']
outputdir = os.environ['OOB__outputdir']
statedir = os.environ['OOB__statedir']
fsmount = os.environ['OOB__fsmount']

METADATA_NS = "http://linux.duke.edu/metadata/common"

def read_config(module, option):
    vname = "CFG_%s__%s" % (module, option)
    if not vname in os.environ:
        return None
    return os.environ[vname]

def read_config_bool(module, option):
    vname = "CFG_%s__%s" % (module, option)
    if not vname in os.environ:
        return None
    return bool(int(os.environ[vname]))

def read_buildnr():
    buildnr_path = os.path.join(intermediatesdir, 'buildnr')
    if not os.path.isfile(buildnr_path):
        return "0"
    return open(buildnr_path, "r").readline().strip()

def image_name():
    name_tmpl = read_config('global', 'image_name')
    return name_tmpl % int(read_buildnr())

def arch_matches(myarch, arch):
    # figure out if a package under 'arch' is suitable for 'myarch'
    # myarch is either 'i386' or 'arm'
    # but 'arch' can be i386, i586, i686, armv5tel, armv7hl, and so on

    # noarch is always suitable
    if arch == 'noarch':
        return True

    if myarch == 'arm':
        return arch.startswith('arm')
    elif myarch == 'i386':
        return arch in ['i386', 'i486', 'i586', 'i686']
    else:
        return False

def add_packages_from_xml(fd, pkglist, myarch=None):
    et = ElementTree(file=fd)
    root = et.getroot()
    for i in root.getchildren():
        if not i.tag.endswith("}package"):
            continue
        arch = i.find("{%s}arch" % METADATA_NS)
        name = i.find("{%s}name" % METADATA_NS)

        # Only add packages that are suitable for myarch.
        if myarch and arch is not None:
            if not arch_matches(myarch, arch.text):
                continue

        if name is not None:
            pkglist.add(name.text)

def get_repomd(baseurl):

    # default
    md = {
        'primary'      : 'repodata/primary.xml.gz',
        'primary_db'   : 'repodata/primary.sqlite.bz2',
        'group'        : 'repodata/comps.xml',
        'group_gz'     : 'repodata/repodata/comps.xml.gz',
        'filelists'    : 'repodata/filelists.xml.gz',
        'filelists_db' : 'repodata/filelists.sqlite.bz2',
        'other'        : 'repodata/other.xml.gz',
        'other_db'     : 'repodata/other.sqlite.bz2'
        }

    url = "%s/repodata/repomd.xml" % baseurl
    try:
        fd = urllib2.urlopen(url)
        et = ElementTree(file=fd)
        root = et.getroot()
        # iterate over data tags
        for data in root.findall('{http://linux.duke.edu/metadata/repo}data'):
            type = data.attrib['type']
            location = data.find('{http://linux.duke.edu/metadata/repo}location')
            md[type] = location.attrib['href']
    except:
        pass
    return md

def ln_or_cp(src, dest):
    src_dev = os.stat(src).st_dev
    dest_dev = os.stat(dest).st_dev

    if src_dev == dest_dev:
        if os.path.isdir(dest):
            dest = os.path.join(dest, os.path.basename(src))
        os.link(src, dest)
    else:
        shutil.copy(src, dest)

def install_sugar_bundle(path):
    bundlesdir = os.path.join(intermediatesdir, "shared", "sugar-bundles")
    if not os.path.exists(bundlesdir):
        os.makedirs(bundlesdir)
    ln_or_cp(path, bundlesdir)