diff options
Diffstat (limited to 'scripts/lib/mic/utils/misc.py')
| -rw-r--r-- | scripts/lib/mic/utils/misc.py | 180 |
1 files changed, 1 insertions, 179 deletions
diff --git a/scripts/lib/mic/utils/misc.py b/scripts/lib/mic/utils/misc.py index 8c1f0160b1..010b16ca49 100644 --- a/scripts/lib/mic/utils/misc.py +++ b/scripts/lib/mic/utils/misc.py | |||
| @@ -18,29 +18,9 @@ | |||
| 18 | import os | 18 | import os |
| 19 | import sys | 19 | import sys |
| 20 | import time | 20 | import time |
| 21 | import tempfile | ||
| 22 | import re | ||
| 23 | import shutil | ||
| 24 | import glob | ||
| 25 | import hashlib | ||
| 26 | import subprocess | ||
| 27 | import platform | ||
| 28 | import traceback | ||
| 29 | |||
| 30 | |||
| 31 | try: | ||
| 32 | import sqlite3 as sqlite | ||
| 33 | except ImportError: | ||
| 34 | import sqlite | ||
| 35 | |||
| 36 | try: | ||
| 37 | from xml.etree import cElementTree | ||
| 38 | except ImportError: | ||
| 39 | import cElementTree | ||
| 40 | xmlparse = cElementTree.parse | ||
| 41 | 21 | ||
| 42 | from mic import msger | 22 | from mic import msger |
| 43 | from mic.utils.errors import CreatorError, SquashfsError | 23 | from mic.utils.errors import CreatorError |
| 44 | from mic.utils.fs_related import find_binary_path, makedirs | 24 | from mic.utils.fs_related import find_binary_path, makedirs |
| 45 | from mic.utils import runner | 25 | from mic.utils import runner |
| 46 | 26 | ||
| @@ -82,115 +62,6 @@ def build_name(kscfg, release=None, prefix = None, suffix = None): | |||
| 82 | ret = prefix + name + suffix | 62 | ret = prefix + name + suffix |
| 83 | return ret | 63 | return ret |
| 84 | 64 | ||
| 85 | def get_distro(): | ||
| 86 | """Detect linux distribution, support "meego" | ||
| 87 | """ | ||
| 88 | |||
| 89 | support_dists = ('SuSE', | ||
| 90 | 'debian', | ||
| 91 | 'fedora', | ||
| 92 | 'redhat', | ||
| 93 | 'centos', | ||
| 94 | 'meego', | ||
| 95 | 'moblin', | ||
| 96 | 'tizen') | ||
| 97 | try: | ||
| 98 | (dist, ver, id) = platform.linux_distribution( \ | ||
| 99 | supported_dists = support_dists) | ||
| 100 | except: | ||
| 101 | (dist, ver, id) = platform.dist( \ | ||
| 102 | supported_dists = support_dists) | ||
| 103 | |||
| 104 | return (dist, ver, id) | ||
| 105 | |||
| 106 | def get_distro_str(): | ||
| 107 | """Get composited string for current linux distribution | ||
| 108 | """ | ||
| 109 | (dist, ver, id) = get_distro() | ||
| 110 | |||
| 111 | if not dist: | ||
| 112 | return 'Unknown Linux Distro' | ||
| 113 | else: | ||
| 114 | distro_str = ' '.join(map(str.strip, (dist, ver, id))) | ||
| 115 | return distro_str.strip() | ||
| 116 | |||
| 117 | _LOOP_RULE_PTH = None | ||
| 118 | |||
| 119 | def human_size(size): | ||
| 120 | """Return human readable string for Bytes size | ||
| 121 | """ | ||
| 122 | |||
| 123 | if size <= 0: | ||
| 124 | return "0M" | ||
| 125 | import math | ||
| 126 | measure = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'] | ||
| 127 | expo = int(math.log(size, 1024)) | ||
| 128 | mant = float(size/math.pow(1024, expo)) | ||
| 129 | return "{0:.1f}{1:s}".format(mant, measure[expo]) | ||
| 130 | |||
| 131 | def get_block_size(file_obj): | ||
| 132 | """ Returns block size for file object 'file_obj'. Errors are indicated by | ||
| 133 | the 'IOError' exception. """ | ||
| 134 | |||
| 135 | from fcntl import ioctl | ||
| 136 | import struct | ||
| 137 | |||
| 138 | # Get the block size of the host file-system for the image file by calling | ||
| 139 | # the FIGETBSZ ioctl (number 2). | ||
| 140 | binary_data = ioctl(file_obj, 2, struct.pack('I', 0)) | ||
| 141 | return struct.unpack('I', binary_data)[0] | ||
| 142 | |||
| 143 | def check_space_pre_cp(src, dst): | ||
| 144 | """Check whether disk space is enough before 'cp' like | ||
| 145 | operations, else exception will be raised. | ||
| 146 | """ | ||
| 147 | |||
| 148 | srcsize = get_file_size(src) * 1024 * 1024 | ||
| 149 | freesize = get_filesystem_avail(dst) | ||
| 150 | if srcsize > freesize: | ||
| 151 | raise CreatorError("space on %s(%s) is not enough for about %s files" | ||
| 152 | % (dst, human_size(freesize), human_size(srcsize))) | ||
| 153 | |||
| 154 | def calc_hashes(file_path, hash_names, start = 0, end = None): | ||
| 155 | """ Calculate hashes for a file. The 'file_path' argument is the file | ||
| 156 | to calculate hash functions for, 'start' and 'end' are the starting and | ||
| 157 | ending file offset to calculate the has functions for. The 'hash_names' | ||
| 158 | argument is a list of hash names to calculate. Returns the the list | ||
| 159 | of calculated hash values in the hexadecimal form in the same order | ||
| 160 | as 'hash_names'. | ||
| 161 | """ | ||
| 162 | if end == None: | ||
| 163 | end = os.path.getsize(file_path) | ||
| 164 | |||
| 165 | chunk_size = 65536 | ||
| 166 | to_read = end - start | ||
| 167 | read = 0 | ||
| 168 | |||
| 169 | hashes = [] | ||
| 170 | for hash_name in hash_names: | ||
| 171 | hashes.append(hashlib.new(hash_name)) | ||
| 172 | |||
| 173 | with open(file_path, "rb") as f: | ||
| 174 | f.seek(start) | ||
| 175 | |||
| 176 | while read < to_read: | ||
| 177 | if read + chunk_size > to_read: | ||
| 178 | chunk_size = to_read - read | ||
| 179 | chunk = f.read(chunk_size) | ||
| 180 | for hash_obj in hashes: | ||
| 181 | hash_obj.update(chunk) | ||
| 182 | read += chunk_size | ||
| 183 | |||
| 184 | result = [] | ||
| 185 | for hash_obj in hashes: | ||
| 186 | result.append(hash_obj.hexdigest()) | ||
| 187 | |||
| 188 | return result | ||
| 189 | |||
| 190 | def get_md5sum(fpath): | ||
| 191 | return calc_hashes(fpath, ('md5', ))[0] | ||
| 192 | |||
| 193 | |||
| 194 | def normalize_ksfile(ksconf, release, arch): | 65 | def normalize_ksfile(ksconf, release, arch): |
| 195 | ''' | 66 | ''' |
| 196 | Return the name of a normalized ks file in which macro variables | 67 | Return the name of a normalized ks file in which macro variables |
| @@ -232,52 +103,3 @@ def normalize_ksfile(ksconf, release, arch): | |||
| 232 | atexit.register(remove_temp_ks) | 103 | atexit.register(remove_temp_ks) |
| 233 | 104 | ||
| 234 | return ksconf | 105 | return ksconf |
| 235 | |||
| 236 | |||
| 237 | def selinux_check(arch, fstypes): | ||
| 238 | try: | ||
| 239 | getenforce = find_binary_path('getenforce') | ||
| 240 | except CreatorError: | ||
| 241 | return | ||
| 242 | |||
| 243 | selinux_status = runner.outs([getenforce]) | ||
| 244 | if arch and arch.startswith("arm") and selinux_status == "Enforcing": | ||
| 245 | raise CreatorError("Can't create arm image if selinux is enabled, " | ||
| 246 | "please run 'setenforce 0' to disable selinux") | ||
| 247 | |||
| 248 | use_btrfs = filter(lambda typ: typ == 'btrfs', fstypes) | ||
| 249 | if use_btrfs and selinux_status == "Enforcing": | ||
| 250 | raise CreatorError("Can't create btrfs image if selinux is enabled," | ||
| 251 | " please run 'setenforce 0' to disable selinux") | ||
| 252 | |||
| 253 | def get_file_size(filename): | ||
| 254 | """ Return size in MB unit """ | ||
| 255 | cmd = ['du', "-s", "-b", "-B", "1M", filename] | ||
| 256 | rc, duOutput = runner.runtool(cmd) | ||
| 257 | if rc != 0: | ||
| 258 | raise CreatorError("Failed to run: %s" % ' '.join(cmd)) | ||
| 259 | size1 = int(duOutput.split()[0]) | ||
| 260 | |||
| 261 | cmd = ['du', "-s", "-B", "1M", filename] | ||
| 262 | rc, duOutput = runner.runtool(cmd) | ||
| 263 | if rc != 0: | ||
| 264 | raise CreatorError("Failed to run: %s" % ' '.join(cmd)) | ||
| 265 | |||
| 266 | size2 = int(duOutput.split()[0]) | ||
| 267 | return max(size1, size2) | ||
| 268 | |||
| 269 | |||
| 270 | def get_filesystem_avail(fs): | ||
| 271 | vfstat = os.statvfs(fs) | ||
| 272 | return vfstat.f_bavail * vfstat.f_bsize | ||
| 273 | |||
| 274 | def mkdtemp(dir = "/var/tmp", prefix = "wic-tmp-"): | ||
| 275 | """ FIXME: use the dir in wic.conf instead """ | ||
| 276 | |||
| 277 | makedirs(dir) | ||
| 278 | return tempfile.mkdtemp(dir = dir, prefix = prefix) | ||
| 279 | |||
| 280 | def strip_end(text, suffix): | ||
| 281 | if not text.endswith(suffix): | ||
| 282 | return text | ||
| 283 | return text[:-len(suffix)] | ||
