3 # Copyright (c) 2010, 2011 Intel Inc.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 import sqlite3 as sqlite
33 import sqlite3 as sqlite
38 from xml.etree import cElementTree
41 xmlparse = cElementTree.parse
44 from fs_related import *
45 from grabber import myurlgrab
46 from proxy import get_proxy_for
51 RPM_RE = re.compile("(.*)\.(.*) (.*)-(.*)")
52 RPM_FMT = "%(name)s.%(arch)s %(ver_rel)s"
53 SRPM_RE = re.compile("(.*)-(\d+.*)-(\d+\.\d+).src.rpm")
55 def build_name(kscfg, release=None, prefix = None, suffix = None):
56 """Construct and return an image name string.
58 This is a utility function to help create sensible name and fslabel
59 strings. The name is constructed using the sans-prefix-and-extension
60 kickstart filename and the supplied prefix and suffix.
62 kscfg -- a path to a kickstart file
63 release -- a replacement to suffix for image release
64 prefix -- a prefix to prepend to the name; defaults to None, which causes
66 suffix -- a suffix to append to the name; defaults to None, which causes
67 a YYYYMMDDHHMM suffix to be used
69 Note, if maxlen is less then the len(suffix), you get to keep both pieces.
72 name = os.path.basename(kscfg)
80 suffix = time.strftime("%Y%m%d%H%M")
81 if release is not None:
84 if name.startswith(prefix):
85 name = name[len(prefix):]
87 ret = prefix + name + "-" + suffix
91 """Detect linux distribution, support "meego"
94 support_dists = ('SuSE',
103 (dist, ver, id) = platform.linux_distribution( \
104 supported_dists = support_dists)
106 (dist, ver, id) = platform.dist( \
107 supported_dists = support_dists)
109 return (dist, ver, id)
111 def get_distro_str():
112 """Get composited string for current linux distribution
114 (dist, ver, id) = get_distro()
117 return 'Unknown Linux Distro'
119 distro_str = ' '.join(map(str.strip, (dist, ver, id)))
120 return distro_str.strip()
122 _LOOP_RULE_PTH = None
123 def hide_loopdev_presentation():
124 udev_rules = "80-prevent-loop-present.rules"
126 '/usr/lib/udev/rules.d/',
127 '/lib/udev/rules.d/',
131 for rdir in udev_rules_dir:
132 if os.path.exists(rdir):
133 _LOOP_RULE_PTH = os.path.join(rdir, udev_rules)
135 if not _LOOP_RULE_PTH:
139 with open(_LOOP_RULE_PTH, 'w') as wf:
140 wf.write('KERNEL=="loop*", ENV{UDISKS_PRESENTATION_HIDE}="1"')
142 runner.quiet('udevadm trigger')
146 def unhide_loopdev_presentation():
147 if not _LOOP_RULE_PTH:
151 os.unlink(_LOOP_RULE_PTH)
152 runner.quiet('udevadm trigger')
156 def extract_rpm(rpmfile, targetdir):
157 rpm2cpio = find_binary_path("rpm2cpio")
158 cpio = find_binary_path("cpio")
163 msger.verbose("Extract rpm file with cpio: %s" % rpmfile)
164 p1 = subprocess.Popen([rpm2cpio, rpmfile], stdout=subprocess.PIPE)
165 p2 = subprocess.Popen([cpio, "-idv"], stdin=p1.stdout,
166 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
167 (sout, serr) = p2.communicate()
168 msger.verbose(sout or serr)
172 def compressing(fpath, method):
177 if method not in comp_map:
178 raise CreatorError("Unsupport compress format: %s, valid values: %s"
179 % (method, ','.join(comp_map.keys())))
180 cmd = find_binary_path(comp_map[method])
181 rc = runner.show([cmd, "-f", fpath])
183 raise CreatorError("Failed to %s file: %s" % (comp_map[method], fpath))
185 def taring(dstfile, target):
187 basen, ext = os.path.splitext(dstfile)
188 comp = {".tar": None,
189 ".gz": "gz", # for .tar.gz
190 ".bz2": "bz2", # for .tar.bz2
194 # specify tarball file path
197 elif basen.endswith(".tar"):
200 tarpath = basen + ".tar"
201 wf = tarfile.open(tarpath, 'w')
203 if os.path.isdir(target):
204 for item in os.listdir(target):
205 wf.add(os.path.join(target, item), item)
207 wf.add(target, os.path.basename(target))
211 compressing(tarpath, comp)
212 # when dstfile ext is ".tgz" and ".tbz", should rename
213 if not basen.endswith(".tar"):
214 shutil.move("%s.%s" % (tarpath, comp), dstfile)
216 def ziping(dstfile, target):
218 wf = zipfile.ZipFile(dstfile, 'w', compression=zipfile.ZIP_DEFLATED)
219 if os.path.isdir(target):
220 for item in os.listdir(target):
221 fpath = os.path.join(target, item)
222 if not os.path.isfile(fpath):
224 wf.write(fpath, item, zipfile.ZIP_DEFLATED)
226 wf.write(target, os.path.basename(target), zipfile.ZIP_DEFLATED)
238 def packing(dstfile, target):
239 (base, ext) = os.path.splitext(dstfile)
240 if ext in (".gz", ".bz2") and base.endswith(".tar"):
242 if ext not in pack_formats:
243 raise CreatorError("Unsupport pack format: %s, valid values: %s"
244 % (ext, ','.join(pack_formats.keys())))
245 func = pack_formats[ext]
246 # func should be callable
247 func(dstfile, target)
249 def human_size(size):
250 """Return human readable string for Bytes size
256 measure = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
257 expo = int(math.log(size, 1024))
258 mant = float(size/math.pow(1024, expo))
259 return "{0:.1f}{1:s}".format(mant, measure[expo])
261 def check_space_pre_cp(src, dst):
262 """Check whether disk space is enough before 'cp' like
263 operations, else exception will be raised.
266 srcsize = get_file_size(src) * 1024 * 1024
267 freesize = get_filesystem_avail(dst)
268 if srcsize > freesize:
269 raise CreatorError("space on %s(%s) is not enough for about %s files"
270 % (dst, human_size(freesize), human_size(srcsize)))
272 def calc_hashes(file_path, hash_names, start = 0, end = None):
273 """ Calculate hashes for a file. The 'file_path' argument is the file
274 to calculate hash functions for, 'start' and 'end' are the starting and
275 ending file offset to calculate the has functions for. The 'hash_names'
276 argument is a list of hash names to calculate. Returns the the list
277 of calculated hash values in the hexadecimal form in the same order
281 end = os.path.getsize(file_path)
284 to_read = end - start;
288 for hash_name in hash_names:
289 hashes.append(hashlib.new(hash_name))
291 with open(file_path, "rb") as f:
294 while read < to_read:
295 if read + chunk_size > to_read:
296 chunk_size = to_read - read
297 chunk = f.read(chunk_size)
298 for hash_obj in hashes:
299 hash_obj.update(chunk)
303 for hash_obj in hashes:
304 result.append(hash_obj.hexdigest())
308 def get_md5sum(fpath):
309 return calc_hashes(fpath, ('md5', ))[0]
311 def normalize_ksfile(ksconf, release, arch):
318 if not os.path.exists(ksconf):
323 if not arch or re.match(r'i.86', arch):
326 with open(ksconf) as f:
329 if "@ARCH@" in ksc or "@BUILD_ID@" in ksc:
330 msger.info("Substitute macro variable @BUILD_ID@/@ARCH@ in ks: %s" % ksconf)
331 ksc = ksc.replace("@ARCH@", arch)
332 ksc = ksc.replace("@BUILD_ID@", release)
333 fd, ksconf = tempfile.mkstemp(prefix=os.path.basename(ksconf), dir="/tmp/")
337 msger.debug('new ks path %s' % ksconf)
340 atexit.register(_clrtempks)
344 def _check_mic_chroot(rootdir):
346 return rootdir.rstrip('/') + path
348 release_files = map(_path, [ "/etc/moblin-release",
349 "/etc/meego-release",
350 "/etc/tizen-release"])
352 if not any(map(os.path.exists, release_files)):
353 msger.warning("Dir %s is not a MeeGo/Tizen chroot env" % rootdir)
355 if not glob.glob(rootdir + "/boot/vmlinuz-*"):
356 msger.warning("Failed to find kernel module under %s" % rootdir)
360 def selinux_check(arch, fstypes):
362 getenforce = find_binary_path('getenforce')
366 selinux_status = runner.outs([getenforce])
367 if arch and arch.startswith("arm") and selinux_status == "Enforcing":
368 raise CreatorError("Can't create arm image if selinux is enabled, "
369 "please run 'setenforce 0' to disable selinux")
371 use_btrfs = filter(lambda typ: typ == 'btrfs', fstypes)
372 if use_btrfs and selinux_status == "Enforcing":
373 raise CreatorError("Can't create btrfs image if selinux is enabled,"
374 " please run 'setenforce 0' to disable selinux")
376 def get_image_type(path):
377 def _get_extension_name(path):
378 match = re.search("(?<=\.)\w+$", path)
380 return match.group(0)
384 if os.path.isdir(path):
385 _check_mic_chroot(path)
397 extension = _get_extension_name(path)
398 if extension in maptab:
399 return maptab[extension]
401 fd = open(path, "rb")
402 file_header = fd.read(1024)
404 vdi_flag = "<<< Sun VirtualBox Disk Image >>>"
405 if file_header[0:len(vdi_flag)] == vdi_flag:
408 output = runner.outs(['file', path])
409 isoptn = re.compile(r".*ISO 9660 CD-ROM filesystem.*(bootable).*")
410 usbimgptn = re.compile(r".*x86 boot sector.*active.*")
411 rawptn = re.compile(r".*x86 boot sector.*")
412 vmdkptn = re.compile(r".*VMware. disk image.*")
413 ext3fsimgptn = re.compile(r".*Linux.*ext3 filesystem data.*")
414 ext4fsimgptn = re.compile(r".*Linux.*ext4 filesystem data.*")
415 btrfsimgptn = re.compile(r".*BTRFS.*")
416 if isoptn.match(output):
418 elif usbimgptn.match(output):
419 return maptab["usbimg"]
420 elif rawptn.match(output):
422 elif vmdkptn.match(output):
423 return maptab["vmdk"]
424 elif ext3fsimgptn.match(output):
426 elif ext4fsimgptn.match(output):
428 elif btrfsimgptn.match(output):
431 raise CreatorError("Cannot detect the type of image: %s" % path)
433 def get_file_size(file):
434 """ Return size in MB unit """
435 rc, duOutput = runner.runtool(['du', "-s", "-b", "-B", "1M", file])
437 raise CreatorError("Failed to run %s" % du)
439 size1 = int(duOutput.split()[0])
440 rc, duOutput = runner.runtool(['du', "-s", "-B", "1M", file])
442 raise CreatorError("Failed to run %s" % du)
444 size2 = int(duOutput.split()[0])
450 def get_filesystem_avail(fs):
451 vfstat = os.statvfs(fs)
452 return vfstat.f_bavail * vfstat.f_bsize
454 def convert_image(srcimg, srcfmt, dstimg, dstfmt):
457 raise CreatorError("Invalid destination image format: %s" % dstfmt)
458 msger.debug("converting %s image to %s" % (srcimg, dstimg))
460 path = find_binary_path("qemu-img")
461 argv = [path, "convert", "-f", "vmdk", srcimg, "-O", dstfmt, dstimg]
462 elif srcfmt == "vdi":
463 path = find_binary_path("VBoxManage")
464 argv = [path, "internalcommands", "converttoraw", srcimg, dstimg]
466 raise CreatorError("Invalid soure image format: %s" % srcfmt)
468 rc = runner.show(argv)
470 msger.debug("convert successful")
472 raise CreatorError("Unable to convert disk to %s" % dstfmt)
474 def uncompress_squashfs(squashfsimg, outdir):
475 """Uncompress file system from squshfs image"""
476 unsquashfs = find_binary_path("unsquashfs")
477 args = [ unsquashfs, "-d", outdir, squashfsimg ]
478 rc = runner.show(args)
480 raise SquashfsError("Failed to uncompress %s." % squashfsimg)
482 def mkdtemp(dir = "/var/tmp", prefix = "mic-tmp-"):
483 """ FIXME: use the dir in mic.conf instead """
486 return tempfile.mkdtemp(dir = dir, prefix = prefix)
488 def get_repostrs_from_ks(ks):
489 def _get_temp_reponame(baseurl):
490 md5obj = hashlib.md5(baseurl)
491 tmpreponame = "%s" % md5obj.hexdigest()
496 for repodata in ks.handler.repo.repoList:
501 'includepkgs', # val is list
502 'excludepkgs', # val is list
514 if hasattr(repodata, attr) and getattr(repodata, attr):
515 repo[attr] = getattr(repodata, attr)
517 if 'name' not in repo:
518 repo['name'] = _get_temp_reponame(repodata.baseurl)
520 kickstart_repos.append(repo)
522 return kickstart_repos
524 def _get_uncompressed_data_from_url(url, filename, proxies):
525 filename = myurlgrab(url, filename, proxies)
527 if filename.endswith(".gz"):
529 runner.quiet(['gunzip', "-f", filename])
530 elif filename.endswith(".bz2"):
532 runner.quiet(['bunzip2', "-f", filename])
534 filename = filename.replace(suffix, "")
537 def _get_metadata_from_repo(baseurl, proxies, cachedir, reponame, filename,
538 sumtype=None, checksum=None):
539 url = os.path.join(baseurl, filename)
540 filename_tmp = str("%s/%s/%s" % (cachedir, reponame, os.path.basename(filename)))
541 if os.path.splitext(filename_tmp)[1] in (".gz", ".bz2"):
542 filename = os.path.splitext(filename_tmp)[0]
544 filename = filename_tmp
545 if sumtype and checksum and os.path.exists(filename):
547 sumcmd = find_binary_path("%ssum" % sumtype)
551 file_checksum = runner.outs([sumcmd, filename]).split()[0]
553 if file_checksum and file_checksum == checksum:
556 return _get_uncompressed_data_from_url(url,filename_tmp,proxies)
558 def get_metadata_from_repos(repos, cachedir):
559 my_repo_metadata = []
561 reponame = repo['name']
562 baseurl = repo['baseurl']
566 proxy = repo['proxy']
568 proxy = get_proxy_for(baseurl)
572 proxies = {str(baseurl.split(":")[0]):str(proxy)}
574 makedirs(os.path.join(cachedir, reponame))
575 url = os.path.join(baseurl, "repodata/repomd.xml")
576 filename = os.path.join(cachedir, reponame, 'repomd.xml')
577 repomd = myurlgrab(url, filename, proxies)
579 root = xmlparse(repomd)
581 raise CreatorError("repomd.xml syntax error.")
583 ns = root.getroot().tag
584 ns = ns[0:ns.rindex("}")+1]
590 for elm in root.getiterator("%sdata" % ns):
591 if elm.attrib["type"] == "patterns":
592 filepaths['patterns'] = elm.find("%slocation" % ns).attrib['href']
593 checksums['patterns'] = elm.find("%sopen-checksum" % ns).text
594 sumtypes['patterns'] = elm.find("%sopen-checksum" % ns).attrib['type']
597 for elm in root.getiterator("%sdata" % ns):
598 if elm.attrib["type"] in ("group_gz", "group"):
599 filepaths['comps'] = elm.find("%slocation" % ns).attrib['href']
600 checksums['comps'] = elm.find("%sopen-checksum" % ns).text
601 sumtypes['comps'] = elm.find("%sopen-checksum" % ns).attrib['type']
605 for elm in root.getiterator("%sdata" % ns):
606 if elm.attrib["type"] in ("primary_db", "primary"):
607 primary_type = elm.attrib["type"]
608 filepaths['primary'] = elm.find("%slocation" % ns).attrib['href']
609 checksums['primary'] = elm.find("%sopen-checksum" % ns).text
610 sumtypes['primary'] = elm.find("%sopen-checksum" % ns).attrib['type']
616 for item in ("primary", "patterns", "comps"):
617 if item not in filepaths:
618 filepaths[item] = None
620 if not filepaths[item]:
622 filepaths[item] = _get_metadata_from_repo(baseurl,
632 repokey = _get_metadata_from_repo(baseurl,
636 "repodata/repomd.xml.key")
639 msger.debug("\ncan't get %s/%s" % (baseurl, "repodata/repomd.xml.key"))
641 my_repo_metadata.append({"name":reponame,
644 "primary":filepaths['primary'],
647 "patterns":filepaths['patterns'],
648 "comps":filepaths['comps'],
651 return my_repo_metadata
653 def get_rpmver_in_repo(repometadata):
654 for repo in repometadata:
655 if repo["primary"].endswith(".xml"):
656 root = xmlparse(repo["primary"])
657 ns = root.getroot().tag
658 ns = ns[0:ns.rindex("}")+1]
661 for elm in root.getiterator("%spackage" % ns):
662 if elm.find("%sname" % ns).text == 'rpm':
663 for node in elm.getchildren():
664 if node.tag == "%sversion" % ns:
665 versionlist.append(node.attrib['ver'])
671 key = lambda ver: map(int, ver.split('.')))).next()
673 elif repo["primary"].endswith(".sqlite"):
674 con = sqlite.connect(repo["primary"])
675 for row in con.execute("select version from packages where "
676 "name=\"rpm\" ORDER by version DESC"):
682 def get_arch(repometadata):
683 def uniqarch(archlist=[]):
685 for i in range(len(archlist)):
686 if archlist[i] not in rpmmisc.archPolicies.keys():
690 while j < len(uniq_arch):
691 if archlist[i] in rpmmisc.archPolicies[uniq_arch[j]].split(':'):
694 if uniq_arch[j] in rpmmisc.archPolicies[archlist[i]].split(':'):
696 uniq_arch[j] = archlist[i]
699 uniq_arch.remove(uniq_arch[j])
703 uniq_arch.append(archlist[i])
710 for repo in repometadata:
712 if repo["primary"].endswith(".xml"):
713 root = xmlparse(repo["primary"])
714 ns = root.getroot().tag
715 ns = ns[0:ns.rindex("}")+1]
716 for elm in root.getiterator("%spackage" % ns):
717 if elm.find("%sarch" % ns).text not in ("noarch", "src"):
718 arch = elm.find("%sarch" % ns).text
719 if arch not in archlist:
720 archlist.append(arch)
721 elif repo["primary"].endswith(".sqlite"):
722 con = sqlite.connect(repo["primary"])
723 for row in con.execute("select arch from packages where arch not in (\"src\", \"noarch\")"):
724 if row[0] not in archlist:
725 archlist.append(row[0])
729 uniq_arch = uniqarch(archlist)
730 if not ret_uniq_arch and len(uniq_arch) == 1:
731 ret_uniq_arch = uniq_arch
732 ret_arch_list += uniq_arch
734 ret_arch_list = uniqarch(ret_arch_list)
735 return ret_uniq_arch, ret_arch_list
737 def get_package(pkg, repometadata, arch = None):
742 elif arch not in rpmmisc.archPolicies:
745 arches = rpmmisc.archPolicies[arch].split(':')
746 arches.append('noarch')
748 for repo in repometadata:
749 if repo["primary"].endswith(".xml"):
750 root = xmlparse(repo["primary"])
751 ns = root.getroot().tag
752 ns = ns[0:ns.rindex("}")+1]
753 for elm in root.getiterator("%spackage" % ns):
754 if elm.find("%sname" % ns).text == pkg:
755 if elm.find("%sarch" % ns).text in arches:
756 version = elm.find("%sversion" % ns)
757 tmpver = "%s-%s" % (version.attrib['ver'], version.attrib['rel'])
760 location = elm.find("%slocation" % ns)
761 pkgpath = "%s" % location.attrib['href']
764 if repo["primary"].endswith(".sqlite"):
765 con = sqlite.connect(repo["primary"])
767 sql = 'select version, release, location_href from packages ' \
768 'where name = "%s" and arch IN ("%s")' % \
769 (pkg, '","'.join(arches))
770 for row in con.execute(sql):
771 tmpver = "%s-%s" % (row[0], row[1])
774 pkgpath = "%s" % row[2]
778 sql = 'select version, release, location_href from packages ' \
779 'where name = "%s"' % pkg
780 for row in con.execute(sql):
781 tmpver = "%s-%s" % (row[0], row[1])
784 pkgpath = "%s" % row[2]
789 makedirs("%s/packages/%s" % (target_repo["cachedir"], target_repo["name"]))
790 url = os.path.join(target_repo["baseurl"], pkgpath)
791 filename = str("%s/packages/%s/%s" % (target_repo["cachedir"], target_repo["name"], os.path.basename(pkgpath)))
792 if os.path.exists(filename):
793 ret = rpmmisc.checkRpmIntegrity('rpm', filename)
797 msger.warning("package %s is damaged: %s" %
798 (os.path.basename(filename), filename))
801 pkg = myurlgrab(str(url), filename, target_repo["proxies"])
806 def get_source_name(pkg, repometadata):
808 def get_bin_name(pkg):
809 m = RPM_RE.match(pkg)
814 def get_src_name(srpm):
815 m = SRPM_RE.match(srpm)
823 pkg_name = get_bin_name(pkg)
827 for repo in repometadata:
828 if repo["primary"].endswith(".xml"):
829 root = xmlparse(repo["primary"])
830 ns = root.getroot().tag
831 ns = ns[0:ns.rindex("}")+1]
832 for elm in root.getiterator("%spackage" % ns):
833 if elm.find("%sname" % ns).text == pkg_name:
834 if elm.find("%sarch" % ns).text != "src":
835 version = elm.find("%sversion" % ns)
836 tmpver = "%s-%s" % (version.attrib['ver'], version.attrib['rel'])
839 fmt = elm.find("%sformat" % ns)
841 fns = fmt.getchildren()[0].tag
842 fns = fns[0:fns.rindex("}")+1]
843 pkgpath = fmt.find("%ssourcerpm" % fns).text
847 if repo["primary"].endswith(".sqlite"):
848 con = sqlite.connect(repo["primary"])
849 for row in con.execute("select version, release, rpm_sourcerpm from packages where name = \"%s\" and arch != \"src\"" % pkg_name):
850 tmpver = "%s-%s" % (row[0], row[1])
852 pkgpath = "%s" % row[2]
857 return get_src_name(pkgpath)
861 def get_pkglist_in_patterns(group, patterns):
865 root = xmlparse(patterns)
867 raise SyntaxError("%s syntax error." % patterns)
869 for elm in list(root.getroot()):
871 ns = ns[0:ns.rindex("}")+1]
872 name = elm.find("%sname" % ns)
873 summary = elm.find("%ssummary" % ns)
874 if name.text == group or summary.text == group:
882 for requires in list(elm):
883 if requires.tag.endswith("requires"):
890 for pkg in list(requires):
891 pkgname = pkg.attrib["name"]
892 if pkgname not in pkglist:
893 pkglist.append(pkgname)
897 def get_pkglist_in_comps(group, comps):
901 root = xmlparse(comps)
903 raise SyntaxError("%s syntax error." % comps)
905 for elm in root.getiterator("group"):
907 name = elm.find("name")
908 if id.text == group or name.text == group:
909 packagelist = elm.find("packagelist")
916 for require in elm.getiterator("packagereq"):
917 if require.tag.endswith("packagereq"):
918 pkgname = require.text
919 if pkgname not in pkglist:
920 pkglist.append(pkgname)
924 def is_statically_linked(binary):
925 return ", statically linked, " in runner.outs(['file', binary])
927 def setup_qemu_emulator(rootdir, arch):
928 # mount binfmt_misc if it doesn't exist
929 if not os.path.exists("/proc/sys/fs/binfmt_misc"):
930 modprobecmd = find_binary_path("modprobe")
931 runner.show([modprobecmd, "binfmt_misc"])
932 if not os.path.exists("/proc/sys/fs/binfmt_misc/register"):
933 mountcmd = find_binary_path("mount")
934 runner.show([mountcmd, "-t", "binfmt_misc", "none", "/proc/sys/fs/binfmt_misc"])
936 # qemu_emulator is a special case, we can't use find_binary_path
937 # qemu emulator should be a statically-linked executable file
938 qemu_emulator = "/usr/bin/qemu-arm"
939 if not os.path.exists(qemu_emulator) or not is_statically_linked(qemu_emulator):
940 qemu_emulator = "/usr/bin/qemu-arm-static"
941 if not os.path.exists(qemu_emulator):
942 raise CreatorError("Please install a statically-linked qemu-arm")
944 # qemu emulator version check
945 armv7_list = [arch for arch in rpmmisc.archPolicies.keys() if arch.startswith('armv7')]
946 if arch in armv7_list: # need qemu (>=0.13.0)
947 qemuout = runner.outs([qemu_emulator, "-h"])
948 m = re.search("version\s*([.\d]+)", qemuout)
950 qemu_version = m.group(1)
951 if qemu_version < "0.13":
952 raise CreatorError("Requires %s version >=0.13 for %s" % (qemu_emulator, arch))
954 msger.warning("Can't get version info of %s, please make sure it's higher than 0.13.0" % qemu_emulator)
956 if not os.path.exists(rootdir + "/usr/bin"):
957 makedirs(rootdir + "/usr/bin")
958 shutil.copy(qemu_emulator, rootdir + qemu_emulator)
960 # disable selinux, selinux will block qemu emulator to run
961 if os.path.exists("/usr/sbin/setenforce"):
962 msger.info('Try to disable selinux')
963 runner.show(["/usr/sbin/setenforce", "0"])
965 node = "/proc/sys/fs/binfmt_misc/arm"
966 if is_statically_linked(qemu_emulator) and os.path.exists(node):
969 # unregister it if it has been registered and is a dynamically-linked executable
970 if not is_statically_linked(qemu_emulator) and os.path.exists(node):
971 qemu_unregister_string = "-1\n"
972 fd = open("/proc/sys/fs/binfmt_misc/arm", "w")
973 fd.write(qemu_unregister_string)
976 # register qemu emulator for interpreting other arch executable file
977 if not os.path.exists(node):
978 qemu_arm_string = ":arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfa\\xff\\xff\\xff:%s:\n" % qemu_emulator
979 fd = open("/proc/sys/fs/binfmt_misc/register", "w")
980 fd.write(qemu_arm_string)
985 def SrcpkgsDownload(pkgs, repometadata, instroot, cachedir):
986 def get_source_repometadata(repometadata):
988 for repo in repometadata:
989 if repo["name"].endswith("-source"):
990 src_repometadata.append(repo)
992 return src_repometadata
995 def get_src_name(srpm):
996 m = SRPM_RE.match(srpm)
1001 src_repometadata = get_source_repometadata(repometadata)
1003 if not src_repometadata:
1004 msger.warning("No source repo found")
1010 for repo in src_repometadata:
1011 cachepath = "%s/%s/packages/*.src.rpm" %(cachedir, repo["name"])
1012 lpkgs_path += glob.glob(cachepath)
1014 for lpkg in lpkgs_path:
1015 lpkg_name = get_src_name(os.path.basename(lpkg))
1016 lpkgs_dict[lpkg_name] = lpkg
1017 localpkgs = lpkgs_dict.keys()
1020 destdir = instroot+'/usr/src/SRPMS'
1021 if not os.path.exists(destdir):
1022 os.makedirs(destdir)
1026 srcpkg_name = get_source_name(_pkg, repometadata)
1029 srcpkgset.add(srcpkg_name)
1031 for pkg in list(srcpkgset):
1032 if pkg in localpkgs:
1034 shutil.copy(lpkgs_dict[pkg], destdir)
1035 src_pkgs.append(os.path.basename(lpkgs_dict[pkg]))
1037 src_pkg = get_package(pkg, src_repometadata, 'src')
1039 shutil.copy(src_pkg, destdir)
1040 src_pkgs.append(src_pkg)
1041 msger.info("%d source packages gotten from cache" % cached_count)
1045 def strip_end(text, suffix):
1046 if not text.endswith(suffix):
1048 return text[:-len(suffix)]