1#!/usr/bin/env python 2# 3# Copyright 2018 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16"""Common code used by acloud create methods/classes.""" 17 18from __future__ import print_function 19 20import glob 21import logging 22import os 23import tempfile 24import time 25import zipfile 26 27from acloud import errors 28from acloud.internal import constants 29from acloud.internal.lib import utils 30 31logger = logging.getLogger(__name__) 32 33_ACLOUD_IMAGE_ZIP_POSTFIX = "-local-img-%s.zip" 34 35 36def ParseHWPropertyArgs(dict_str, item_separator=",", key_value_separator=":"): 37 """Helper function to initialize a dict object from string. 38 39 e.g. 40 cpu:2,dpi:240,resolution:1280x800 41 -> {"cpu":"2", "dpi":"240", "resolution":"1280x800"} 42 43 Args: 44 dict_str: A String to be converted to dict object. 45 item_separator: String character to separate items. 46 key_value_separator: String character to separate key and value. 47 48 Returns: 49 Dict created from key:val pairs in dict_str. 50 51 Raises: 52 error.MalformedDictStringError: If dict_str is malformed. 53 """ 54 hw_dict = {} 55 if not dict_str: 56 return hw_dict 57 58 for item in dict_str.split(item_separator): 59 if key_value_separator not in item: 60 raise errors.MalformedDictStringError( 61 "Expecting ':' in '%s' to make a key-val pair" % item) 62 key, value = item.split(key_value_separator) 63 if not value or not key: 64 raise errors.MalformedDictStringError( 65 "Missing key or value in %s, expecting form of 'a:b'" % item) 66 hw_dict[key.strip()] = value.strip() 67 68 return hw_dict 69 70 71@utils.TimeExecute(function_description="Compressing images") 72def ZipCFImageFiles(basedir): 73 """Zip images from basedir. 74 75 TODO(b/129376163):Use lzop for fast sparse image upload when host image 76 support it. 77 78 Args: 79 basedir: String of local images path. 80 81 Return: 82 Strings of zipped image path. 83 """ 84 tmp_folder = os.path.join(tempfile.gettempdir(), 85 constants.TEMP_ARTIFACTS_FOLDER) 86 if not os.path.exists(tmp_folder): 87 os.makedirs(tmp_folder) 88 archive_name = "%s-local-%d.zip" % (os.environ.get(constants.ENV_BUILD_TARGET), 89 int(time.time())) 90 archive_file = os.path.join(tmp_folder, archive_name) 91 if os.path.exists(archive_file): 92 raise errors.ZipImageError("This file shouldn't exist, please delete: %s" 93 % archive_file) 94 95 zip_file = zipfile.ZipFile(archive_file, 'w', zipfile.ZIP_DEFLATED, 96 allowZip64=True) 97 required_files = ([os.path.join(basedir, "android-info.txt")] + 98 glob.glob(os.path.join(basedir, "*.img"))) 99 logger.debug("archiving images: %s", required_files) 100 101 for f in required_files: 102 # Pass arcname arg to remove the directory structure. 103 zip_file.write(f, arcname=os.path.basename(f)) 104 105 zip_file.close() 106 logger.debug("zip images done:%s", archive_file) 107 return archive_file 108