This file is indexed.

/usr/share/pyshared/mic/utils/bootstrap.py is in mic2 0.24.12-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#
# bootstrap.py : functions for building and using bootstrap
#
# Copyright 2010, Intel Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import os
import subprocess
import re
import glob
import xml.dom.minidom
import tarfile

try:
    import sqlite3 as sqlite
except ImportError:
    import sqlite
import _sqlitecache

try:
    from xml.etree import cElementTree
except ImportError:
    import cElementTree
xmlparse = cElementTree.parse

from mic.imgcreate.errors import *
from mic.imgcreate.fs import *
from mic.utils.misc import *

class BootstrapError(Exception):
    """An exception base class for all bootstrap errors."""
    def __init__(self, msg):
        Exception.__init__(self, msg)

def get_repo_metadata(repo, cachedir, reponame, proxies = {}, arch = None):
    if not repo:
        raise BootstrapError("Repository can't be empty string.")
    
    gunzip = find_binary_path('gunzip')
    bunzip2 = find_binary_path('bunzip2')
    makedirs(cachedir + "/" + reponame)
    url = str(repo + "/repodata/repomd.xml")
    filename = str("%s/%s/repomd.xml" % (cachedir, reponame))
    repomd = myurlgrab(url, filename, proxies)
    f = open(repomd, "r")
    content = f.read()
    f.close()
    m = re.match(".*href=\"(.*?primary.sqlite.*?)\"", content, re.M | re.S)
    if not m:
        m = re.match(".*href=\"(.*?primary.xml.*?)\"", content, re.M | re.S)
        if not m:
            print "Failed to get metadata from repo %s" % reponame
            sys.exit(-1)
        primaryxml = str(cachedir + "/%s/%s" % (reponame, os.path.basename(m.group(1))))
        primarydb = cachedir + "/%s/%s" % (reponame, ".tmp.primary.sqlite")
        primaryxmlurl = str(repo + "/" + m.group(1))
        primaryxml = myurlgrab(primaryxmlurl, primaryxml, proxies)
        if primaryxml.rfind(".gz") != -1:
            subprocess.call([gunzip, "-f", primaryxml])
            primaryxml = primaryxml[0:len(primaryxml)-3]
        print "Generating primary.sqlite from primary.xml for repo %s with arch %s..." % (reponame,arch)
        gen_sqlitedb_from_xml(primaryxml, primarydb, arch)
    else:
        primarydb = str(cachedir + "/%s/%s" % (reponame, ".tmp.primary.sqlite"))
        primarydburl = str(repo + "/" + m.group(1))
        if primarydburl.rfind(".gz") != -1:
           primarydb = primarydb + ".gz"
        elif primarydburl.rfind(".bz2") != -1:
           primarydb = primarydb + ".bz2"
        primarydb = myurlgrab(primarydburl, primarydb, proxies)
        if primarydb.rfind(".gz") != -1:
            subprocess.call([gunzip, "-f", primarydb])
            primarydb = primarydb[0:len(primarydb)-3]
        elif primarydb.rfind(".bz2") != -1:
            subprocess.call([bunzip2, "-f", primarydb])
            primarydb = primarydb[0:len(primarydb)-4]
    return primarydb

def gen_sqlitedb_from_xml(primaryxml, sqlitedb, target_arch = None):
    def get_text(element, default = ""):
        if element.text is None:
            return default
        else:
            return element.text

    con = sqlite.connect(sqlitedb)
    con.executescript("""
        DROP table IF EXISTS packages;
        DROP table IF EXISTS provides;
        DROP table IF EXISTS requires;
    """)
    sql = "CREATE TABLE IF NOT EXISTS packages (  pkgKey INTEGER PRIMARY KEY,  pkgId TEXT,  name TEXT,  arch TEXT,  version TEXT,  epoch TEXT,  release TEXT,  summary TEXT,  description TEXT,  url TEXT,  time_file INTEGER,  time_build INTEGER,  rpm_license TEXT,  rpm_vendor TEXT,  rpm_group TEXT,  rpm_buildhost TEXT,  rpm_sourcerpm TEXT,  rpm_header_start INTEGER,  rpm_header_end INTEGER,  rpm_packager TEXT,  size_package INTEGER,  size_installed INTEGER,  size_archive INTEGER,  location_href TEXT,  location_base TEXT,  checksum_type TEXT);"
    con.execute(sql)
    
    sql = "CREATE TABLE IF NOT EXISTS provides (  name TEXT,  flags TEXT,  epoch TEXT,  version TEXT,  release TEXT,  pkgKey INTEGER );"
    con.execute(sql)
    
    sql = "CREATE TABLE IF NOT EXISTS requires (  name TEXT,  flags TEXT,  epoch TEXT,  version TEXT,  release TEXT,  pkgKey INTEGER , pre BOOLEAN DEFAULT FALSE);"
    con.execute(sql)

    elementroot = xmlparse(primaryxml).getroot()
    pkgKey = 0
    
    pkgelements = elementroot.getchildren()
    for element in pkgelements:
        if (len(list(element)) >= 12):
            pkgKey += 1
            myprovides = []
            myrequires = []
            for subelement in element.getchildren():
                if subelement.tag.endswith("name"):
                    pkgname = get_text(subelement)
                elif subelement.tag.endswith("arch"):
                    arch = get_text(subelement)
                elif subelement.tag.endswith("version"):
                    epoch = subelement.get("epoch", "")
                    version = subelement.get("ver", "")
                    release = subelement.get("rel", "")
                elif subelement.tag.endswith("checksum"):
                    pkgId = get_text(subelement)
                    checksum_type = subelement.get("type", "")
                elif subelement.tag.endswith("summary"):
                    summary = get_text(subelement)
                elif subelement.tag.endswith("description"):
                    description = get_text(subelement, summary)
                elif subelement.tag.endswith("packager"):
                    rpm_packager = get_text(subelement)
                elif subelement.tag.endswith("url"):
                    url = get_text(subelement)
                elif subelement.tag.endswith("time"):
                    time_file = subelement.get("file", "")
                    time_build = subelement.get("build", "")
                elif subelement.tag.endswith("size"):
                    size_package = subelement.get("package", "")
                    size_installed = subelement.get("installed", "")
                    size_archive = subelement.get("archive", "")
                elif subelement.tag.endswith("location"):
                    location_href = subelement.get("href", "")
                    location_base = subelement.get("base", "")
                elif subelement.tag.endswith("format"):
                    for subsubelement in subelement.getchildren():
                        if subsubelement.tag.endswith("license"):
                            rpm_license = get_text(subsubelement)
                        elif subsubelement.tag.endswith("vendor"):
                            rpm_vendor = get_text(subsubelement)
                        elif subsubelement.tag.endswith("group"):
                            rpm_group = get_text(subsubelement)
                        elif subsubelement.tag.endswith("buildhost"):
                            rpm_buildhost = get_text(subsubelement)
                        elif subsubelement.tag.endswith("sourcerpm"):
                            rpm_sourcerpm = get_text(subsubelement)
                        elif subsubelement.tag.endswith("header-range"):
                            rpm_header_start = subsubelement.get("start", "")
                            rpm_header_end = subsubelement.get("end", "")
                        elif subsubelement.tag.endswith("provides"):
                            for tmpelement in subsubelement.getchildren():
                                if tmpelement.tag.endswith("entry"):
                                    myprovides.append((tmpelement.get("name", ""), tmpelement.get("flag", ""), tmpelement.get("epoch", ""), tmpelement.get("ver", ""), tmpelement.get("rel", ""), pkgKey))
                        elif subsubelement.tag.endswith("requires"):
                            for tmpelement in subsubelement.getchildren():
                                if tmpelement.tag.endswith("entry"):
                                    myrequires.append((tmpelement.get("name", ""), tmpelement.get("flag", ""), tmpelement.get("epoch", ""), tmpelement.get("ver", ""), tmpelement.get("rel", ""), pkgKey, tmpelement.get("pre", "")))
            """ Skip arm packages on ia32 """
            if not target_arch and arch.startswith("arm"):
                continue
            """ Skip source packages """
            if arch == "src":
                continue
            """ Skip if architecture is not compatible """
            """ FIXME: We need to have proper architecture mapping here. 
                e.g. armv7nhl, armv7hl, noarch
                     armv7l, armv6l, armv5tejl, armv5tel, noarch
                     i686, i586, i486, i386, None (!!!)
            """
            if arch != "noarch":
                """ ^ noarch is always compatible so continue with those always """
                intel_archs = ( None, "i386", "i486", "i586", "i686")
                """ intel arch if both arch and target_arch are in intel_archs list """
                if target_arch not in intel_archs or arch not in intel_archs:
                    """ Here we have the arm architectures. """
                    if arch != target_arch:
                        continue
            sql = "INSERT INTO packages VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
            con.execute(sql, (pkgKey, pkgId,  pkgname, arch, version, epoch, release, summary, description, url, time_file, time_build, rpm_license, rpm_vendor, rpm_group, rpm_buildhost, rpm_sourcerpm, rpm_header_start, rpm_header_end, rpm_packager, size_package, size_installed, size_archive, location_href, location_base, checksum_type))
            sql = "INSERT INTO provides values (?,?,?,?,?,?)";
            con.executemany(sql, myprovides)
            sql = "INSERT INTO requires values (?,?,?,?,?,?,?)";
            con.executemany(sql, myrequires)
    con.commit()
    con.close()


def get_my_all_deps_in_order(pkg, con):
    class Node:
        def __init__(self, data=None):
            self.data = data
            self.parent = None
            self.childs = []
            self.root = self
            self.level = 0
        def setroot(self, node):
            self.root = node
        def getroot(self):
            return self.root
        def setnode(self, data):
            self.data = data
        def addChild(self, node):
            self.childs.append(node)
            node.setroot(self.root)
            node.level = self.level + 1
        def removeChild(self, node):
            self.childs.remove(node)
        def findNode(self, data):
            if self.data == data:
                return self
            elif len(self.childs) == 0:
                return None
            else:
               for child in self.childs:
                   tmpnode = child.findNode(data)
                   if tmpnode:
                       return tmpnode
               return None
        def lastordertranverse(self):
            tmplist = []
            if len(self.childs) == 0:
                tmplist.append({"name":self.data, "isleaf":True})
            else:
                for child in self.childs:
                    tmplist = tmplist + child.lastordertranverse()
                tmplist.append({"name":self.data, "isleaf":False})
            return tmplist
        def firstordertranverse(self):
            tmplist = []
            if len(self.childs) != 0:
                tmplist.append({"name":self.data, "isleaf":False})
                for child in self.childs:
                    tmplist = tmplist + child.firstordertranverse()
            else:
                tmplist.append({"name":self.data, "isleaf":True})
            return tmplist

        def getlevels(self, levels = {}):
            levels[self.data] = self.level
            if len(self.childs) != 0:
                for child in self.childs:
                    child.getlevels(levels)

    def getmydepends(pkgname, con = con):
        myrequires = []
        mydepends = []
        for row in con.execute("select requires.name from packages inner join requires where packages.name = \"" + pkgname + "\" and packages.pkgKey = requires.pkgKey and packages.arch <> \"src\" group by requires.name"):
            myrequires.append(row[0])
        for i in range(len(myrequires)):
            for row in con.execute("select packages.name from provides, packages where provides.name = \"" + myrequires[i] + "\" and packages.pkgKey = provides.pkgKey group by packages.name"):
                if row[0] not in mydepends:
                    mydepends.append(row[0])

        if pkgname in mydepends:
            mydepends.remove(pkgname)

        return mydepends

    def recursivegetmydependstree(pkgname, mydepstree):
        if (mydepstree.getroot().findNode(pkgname)):
            return
        mydepstree.addChild(Node(pkgname))
        mydeps = getmydepends(pkgname)
        for pkga in mydeps:
            recursivegetmydependstree(pkga, mydepstree.findNode(pkgname))
        return

    def getalldependstree(pkgname, mydepstree):
        mydeps = getmydepends(pkgname)
        for pkga in mydeps:
            recursivegetmydependstree(pkga, mydepstree.findNode(pkgname))
        return

    dependstree = Node(pkg)
    getalldependstree(pkg, dependstree)
    childpkgs = dependstree.lastordertranverse()
    if pkg in childpkgs:
        childpkgs.remove(pkg)

    return childpkgs

def find_key_in_file(key, file):
    found = False
    if not os.path.exists(file):
        return found
    fd = open(file, "r")
    content = fd.read()
    for line in content.split("\n"):
        if line.find(key) != -1:
            found = True
            break
    fd.close()
    return found

def get_distribution():
    import platform
    try:
        # Python 2.6
        dist = platform.linux_distribution()[0]
    except AttributeError:
        dist = platform.dist()[0]

    return dist

def rpm_has_force_debian_option():
    rpm = find_binary_path("rpm")
    dev_null = os.open("/dev/null", os.O_WRONLY)
    argv = [rpm, "--force-debian"]
    ret = subprocess.call(argv, stdout = dev_null, stderr = dev_null)
    os.close(dev_null)
    if ret != 0:
        return False
    return True

def install_rpm(pkgpath, pkgname, rootpath, force = False):
    rpm = find_binary_path("rpm")

    rootpath = os.path.abspath(os.path.expanduser(rootpath))
    print "Installing %s..." % os.path.basename(pkgpath)
    dev_null = os.open("/dev/null", os.O_WRONLY)
    try:
        argv = [rpm, "-i", "--ignorearch", "--ignoreos", "--nodigest", "--nosignature", "--root=" + rootpath, pkgpath]
        if force:
            argv.extend(["--force", "--nodeps"])
        dist = get_distribution()
        if dist == "Ubuntu" or dist == "debian":
            if rpm_has_force_debian_option():
                argv.append("--force-debian")
        subprocess.call(argv, stdout = dev_null, stderr = dev_null)
    finally:
        os.close(dev_null)

def get_pkg_url(pkg, con, arch):
    """ use i_86 by default as it is the most common case. """
    arch_match = "i_86"

    if arch and arch.startswith("arm"):
        # FIXME: We need to have mapping with arch compatibilies here.
        # This is just temporary hack to allow some arm functionality.
        if arch in ("armv7hl, armv7nhl, armv7thl, armv7tnhl"):
            arch_match = "armv7%hl"
        else:
            arch_match = "armv*"

    query = "SELECT location_href,location_base FROM packages WHERE name = \"%s\" AND (arch LIKE \'noarch\' OR arch LIKE \'%s\')" % (pkg,arch_match)
    rows = con.execute(query)
    for row in rows:
        return row[0]
    return None

def build_bootstrap(repoList, mainrepo, cachedir, bootstrapdir, bootstrap_arch = None, bootstrap_target_arch = None ):
    print "Creating bootsrap environment to '%s' with bootstrap arch '%s' and bootstrap target arch '%s'." % (bootstrapdir, bootstrap_arch, bootstrap_target_arch)
    
    # Use main repo from command line option
    proxies = {}
    reponame = None
    
    for repo in repoList:
        if repo.name == mainrepo:
            reponame = repo.name
            repourl = repo.baseurl
            if repo.proxy:
                proxies = {"http":repo.proxy,"https":repo.proxy, "ftp":repo.proxy, "ftps":repo.proxy}
            break

    # guess main repo if not provided
    if not reponame:
        for repo in repoList:
            if repo.proxy:
                proxies = {"http":repo.proxy,"https":repo.proxy, "ftp":repo.proxy, "ftps":repo.proxy}
            get_repo_metadata(repo.baseurl, cachedir, repo.name, proxies, bootstrap_arch)
            if is_mainrepo(cachedir, repo.name):
                reponame = repo.name
                repourl = repo.baseurl
                break
        if not reponame:
            raise BootstrapError("Please specify main repo name using --mainrepo option.")
    else:
       get_repo_metadata(repourl, cachedir, reponame, proxies, bootstrap_arch)
    
    """ HACK: This hack is here to enable backward compatibility until the 
        .ks files have been updated to support the @ARCH@ option for the URLs."""
    """ We can't use repourl.find(bootstrap_target_arch) != -1 here because
        for example armv7nhl architecture uses armv7hl repository URL. 
        This is similar to iX86 uses ia32 repository URL."""
    if bootstrap_target_arch and bootstrap_target_arch.startswith("arm"):
        pattern = re.compile('/arm.*?/')
        repourl = pattern.sub("/ia32/", repourl)
        get_repo_metadata(repourl, cachedir, reponame, proxies, bootstrap_arch)
    
    sqlitedb = "%s/%s/.tmp.primary.sqlite" % (cachedir, reponame)
    g = URLGrabber()
    con = sqlite.connect(sqlitedb)
    installed_pkgs = []

    cachedir = os.path.abspath(os.path.expanduser(cachedir))
    bootstrapdir = os.path.abspath(os.path.expanduser(bootstrapdir))
    pkgspath = "%s/%s/packages" % (cachedir, reponame)
    makedirs(pkgspath)
    makedirs(bootstrapdir)
    makedirs(bootstrapdir + "/var/lib/rpm")

    if bootstrap_target_arch and bootstrap_target_arch.startswith("arm"):
        setup_qemu_emulator(bootstrapdir, "arm")

    def download_and_install(pkg, force = False):
        if pkg in installed_pkgs:
            return

        local_href = get_pkg_url(pkg, con, bootstrap_arch)
        if not local_href:
            print "WARNING: Could not find package '%s' suitable arch for arch '%s' for bootstrap installation." % (pkg,bootstrap_arch)
            return
        pkgurl = str(repourl + "/" + get_pkg_url(pkg, con, bootstrap_arch))
        pkgfn = str(pkgspath + "/" + os.path.basename(pkgurl))
        if not os.path.exists(pkgfn):
            if pkgurl.startswith("file://"):
                 pkgfn = pkgurl.replace("file://", "")
            else:
                 print "Downloading %s..." % os.path.basename(pkgurl)
                 try:
                     pkgfn = g.urlgrab(url = pkgurl, filename = pkgfn, proxies = proxies)
                 except URLGrabError, e:
                     raise BootstrapError("URLGrabber error: %s: %s" % (e, pkgurl))
                 except:
                     raise BootstrapError("URLGrabber error: %s" % pkgurl)
        install_rpm(pkgfn, pkg, bootstrapdir, force)
        installed_pkgs.append(pkg)

    # First install a mini base
    pkglist = [ "setup", "filesystem", "basesystem", "tzdata",
                "libgcc", "ncurses-base", "ncurses", "nss-softokn-freebl",
                "glibc-common", "glibc", "ncurses-libs", "bash", "zlib",
                "info", "cpio", "coreutils" ]
    
    for pkg in pkglist:
        download_and_install(pkg, True)

    print "* Mini base installed. Continuing with the rest of the packages. *"

    pkglist = ["glibc", "pam", "passwd", "meego-release",
               "fastinit", "nss", "genisoimage", "bzip2",
               "gzip", "cpio", "perl", "syslinux-extlinux", "btrfs-progs",
               "make", "file", "psmisc", "satsolver-tools", "libzypp",
               "python-zypp", "mic2", "isomd5sum", "wget",
               # mtd-utils is installed to support e.g. mkfs.jffs2
               "mtd-utils", "mtd-utils-ubi"]
    
    for pkg in pkglist:
        for node in get_my_all_deps_in_order(pkg, con):
            #download_and_install(node["name"], node["isleaf"])
            """ We must force it to install because our dependencies tree isn't complete """
            download_and_install(node["name"], True)
        download_and_install(pkg, False)

    """ Remove rpmdb because they are non-sense and will result in some errors message """
    for rpmdbfile in glob.glob(bootstrapdir + "/var/lib/rpm/__db.*"):
        os.unlink(rpmdbfile)

    print "Installed total of %d packages to bootstrap located in %s." % (len(installed_pkgs),bootstrapdir)

    """ Create a emtpy file /tmp/SampleMedia.tar to avoid long download time """
    if not os.path.exists(bootstrapdir + "/tmp"):
        makedirs(bootstrapdir + "/tmp")
    fd = open(bootstrapdir + "/tmp/SampleMedia.tar", "w")
    fd.close()
    con.close()

def isbootstrap(rootdir):
    if (os.path.exists(rootdir + "/etc/meego-release") \
       or os.path.exists(rootdir + "/etc/moblin-release")) \
       and os.path.exists(rootdir + "/bin/bash"):
        return True

    return False

def has_package_in_repo(con, pkg):
    for row in con.execute("select * from packages where name = \"%s\"" % pkg):
        return True
    return False

def is_mainrepo(cachedir, reponame):
    sqlitedb = "%s/%s/.tmp.primary.sqlite" % (cachedir, reponame)
    con = sqlite.connect(sqlitedb)
    retval = False
    if has_package_in_repo(con, "libzypp") \
       and has_package_in_repo(con, "mic2") \
       and has_package_in_repo(con, "rpm"):
        retval = True

    con.close()
    return retval

def check_depends(imgfmt):
    if imgfmt == "vmdk":
        find_binary_path("qemu-img")
    if imgfmt == "vdi":
        find_binary_path("VBoxManage")

def save_imginfo(outimage, infofile):
    print "Saving image info..."
    fd = open(infofile, "w")
    for file in outimage:
        fd.write(file + "\n")
    fd.close()

def get_imgfile(infofile):
    print "Getting image info..."
    fd = open(infofile, "r")
    content = fd.read()
    fd.close()
    os.unlink(infofile)
    for file in content.split("\n"):
        if file.endswith(".raw"):
            return file
    return None

def write_image_vmx(imgfile):
    vmdkcfg_file = imgfile[0:-4] + "vmx"
    vmdkcfg = open(vmdkcfg_file, "w")
    vmx = """#!/usr/bin/vmware
.encoding = "UTF-8"
displayName = "MeeGo 1.0"
guestOS = "linux"

memsize = "512"
"""
    vmx += "ide0:0.fileName = \"" + "%s\"" % (os.path.basename(imgfile))
    vmx += """

# DEFAULT SETTINGS UNDER THIS LINE
config.version = "8"
virtualHW.version = "4"

MemAllowAutoScaleDown = "FALSE"
MemTrimRate = "-1"

uuid.location = "56 4d 8a 28 88 e5 86 1f-7e ed 8f 25 45 7d f8 e4"
uuid.bios = "56 4d 8a 28 88 e5 86 1f-7e ed 8f 25 45 7d f8 e4"

uuid.action = "create"

ethernet0.present = "TRUE"
ethernet0.connectionType = "nat"
ethernet0.addressType = "generated"
ethernet0.generatedAddress = "00:0c:29:7d:f8:e4"
ethernet0.generatedAddressOffset = "0"

usb.present = "TRUE"
ehci.present = "TRUE"
sound.present = "TRUE"
sound.autodetect = "TRUE"

scsi0.present = "FALSE"
floppy0.present = "FALSE"
ide0:0.present = "TRUE"
ide0:0.deviceType = "disk"
ide0:1.present = "FALSE"

virtualHW.productCompatibility = "hosted"
tools.upgrade.policy = "manual"

tools.syncTime = "FALSE"

ide0:0.redo = ""
"""
    vmdkcfg.write(vmx)
    vmdkcfg.close()
    return vmdkcfg_file

def convert_to(imgfile, imgfmt):
    outimage = []
    dst = imgfile[0:-3] + imgfmt
    outimage.append(dst)
    print "converting %s image to %s" % (imgfile, dst)
    if imgfmt == "vmdk":
        qemu_img = find_binary_path("qemu-img")
        rc = subprocess.call([qemu_img, "convert",
                                      "-f", "raw", imgfile,
                                      "-O", imgfmt, dst])
    elif imgfmt == "vdi":
        vboxmanage = find_binary_path("VBoxManage")
        rc = subprocess.call([vboxmanage, "convertfromraw",
                                      imgfile, dst,
                                      "--format", "VDI"])
                
    if rc != 0:
        raise BootstrapError("Unable to convert to %s" % imgfmt)
    else:
        if imgfmt == "vmdk":
            vmxfile = write_image_vmx(dst)
            outimage.append(vmxfile)
        print "convert successfully"
        try:
            os.unlink(imgfile[0:-8] + ".xml")
            os.unlink(imgfile)
        except OSError:
            pass
    return outimage

def package_image(imagefiles, image_format, destdir, package):
    if not package or package == "none":
        return None

    for file in imagefiles:
        if file.endswith(".vmdk"):
            name = os.path.basename(file)[0:-9]
            break
        if file.endswith(".vdi"):
            name = os.path.basename(file)[0:-8]
            break
    destdir = os.path.abspath(os.path.expanduser(destdir))
    (pkg, comp) = os.path.splitext(package)
    if comp:
        comp=comp.lstrip(".")

    if pkg == "tar":
        if comp:
            dst = "%s/%s-%s.tar.%s" % (destdir, name, image_format, comp)
        else:
            dst = "%s/%s-%s.tar" % (destdir, name, image_format)
        print "creating %s" % dst
        tar = tarfile.open(dst, "w:" + comp)
        for file in imagefiles:
            print "adding %s to %s" % (file, dst)
            tar.add(file, arcname=os.path.join("%s-%s" % (name, image_format), os.path.basename(file)))
        tar.close()
        return dst

def print_imginfo(outimage):
    print "Your new image can be found here:"
    for file in outimage:
        print os.path.abspath(file)

def clean_files(pattern, dir):
    if not os.path.exists(dir):
        return
    for f in os.listdir(dir):
        entry = os.path.join(dir, f)
        if os.path.isdir(entry):
            clean_files(pattern, entry)
        elif re.match(pattern, entry):
            os.unlink(entry)

def copy_mic2(bootstrap, bin_path = "/usr/bin", python_lib_path = "/usr/lib/python2.6/site-packages"):
        bootstrap_mic = bootstrap + "/usr/lib/python2.6/site-packages/mic"
        shutil.rmtree(bootstrap_mic, ignore_errors = True)
        copycmd = find_binary_path("cp")
        subprocess.call([copycmd, "-af", python_lib_path + "/mic", os.path.dirname(bootstrap_mic)])
        clean_files(".*\.py[co]$", bootstrap_mic)
        for file in glob.glob(bootstrap + "/usr/bin/mic-*"):
            os.unlink(file)
        for file in glob.glob(bootstrap + "/usr/bin/moblin-*"):
            os.unlink(file)
        if os.path.lexists(bootstrap + "/usr/bin/mic"):
            os.unlink(bootstrap + "/usr/bin/mic")
        for file in glob.glob(bin_path + "/mic-*"):
            shutil.copy(file, bootstrap + "/usr/bin/" + os.path.basename(file))
        for file in glob.glob(bin_path + "/moblin-*"):
            if os.path.islink(file):
                linkto = os.readlink(file)
                os.symlink(linkto, bootstrap + "/usr/bin/" + os.path.basename(file))
        shutil.copy(bin_path + "/mic", bootstrap + "/usr/bin/mic")

def check_mic(bootstrap):
    import distutils.sysconfig
    pythonlib = distutils.sysconfig.get_python_lib()
    bin_path = "/usr/bin"
    if not os.path.exists(pythonlib + "/mic/imgcreate/creator.py"):
        dist = get_distribution()
        if dist == 'Ubuntu':
            dev_null = os.open("/dev/null", os.O_WRONLY)
            proc = subprocess.Popen(["/usr/bin/pycentral", "pycentraldir", "mic2"], 
                                    stdout = subprocess.PIPE, stderr = dev_null)
            output = proc.communicate()[0]
            os.close(dev_null)
            if proc.returncode != 0:
                raise BootstrapError("Failed to run /usr/bin/pycentral")
            pycentraldir = output.split()[0]
            pythonlib = pycentraldir
            bin_path = "/usr/local/bin"
        elif dist == 'debian':
            if os.path.exists("/usr/share/python-support/mic2"):
                pythonlib = "/usr/share/python-support/mic2"
            elif os.path.exists("/usr/share/pyshared"):
                pythonlib = "/usr/share/pyshared"
    copy_mic2(bootstrap, bin_path = bin_path, python_lib_path = pythonlib)
    if not os.path.exists(bootstrap + "/etc/sysconfig"):
         makedirs(bootstrap + "/etc/sysconfig")
    if os.path.exists("/etc/sysconfig/proxy"):
        shutil.copy("/etc/sysconfig/proxy", bootstrap + "/etc/sysconfig/proxy")