~fboudra/linaro-image-tools/initial-arndale-bl1-support

« back to all changes in this revision

Viewing changes to linaro_image_tools/utils.py

  • Committer: Milo Casagrande
  • Date: 2012-10-24 13:34:59 UTC
  • mfrom: (584.1.2 bug1059579)
  • Revision ID: milo@ubuntu.com-20121024133459-ku3i2kr1tgmz5hr9
Re-enabled use of common logging infrastructure, fixed error with global variable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
from linaro_image_tools import cmd_runner
30
30
 
 
31
DEFAULT_LOGGER_NAME = 'linaro_image_tools'
 
32
 
31
33
 
32
34
# try_import was copied from python-testtools 0.9.12 and was originally
33
35
# licensed under a MIT-style license but relicensed under the GPL in Linaro
76
78
 
77
79
 
78
80
def path_in_tarfile_exists(path, tar_file):
79
 
    tarinfo = tarfile.open(tar_file, 'r:gz')
 
81
    exists = True
80
82
    try:
 
83
        tarinfo = tarfile.open(tar_file, 'r:gz')
81
84
        tarinfo.getmember(path)
82
 
        return True
 
85
        tarinfo.close()
83
86
    except KeyError:
84
 
        return False
85
 
    tarinfo.close()
 
87
        exists = False
 
88
    finally:
 
89
        return exists
86
90
 
87
91
 
88
92
def verify_file_integrity(sig_file_list):
145
149
 
146
150
    # Check the outputs from verify_file_integrity
147
151
    # Abort if anything fails.
 
152
    logger = logging.getLogger(__name__)
148
153
    if len(sig_file_list):
149
154
        if not gpg_sig_pass:
150
 
            logging.error("GPG signature verification failed.")
 
155
            logger.error("GPG signature verification failed.")
151
156
            return False, []
152
157
 
153
158
        if not os.path.basename(binary) in verified_files:
154
 
            logging.error("OS Binary verification failed")
 
159
            logger.error("OS Binary verification failed")
155
160
            return False, []
156
161
 
157
162
        for hwpack in hwpacks:
158
163
            if not os.path.basename(hwpack) in verified_files:
159
 
                logging.error("Hwpack {0} verification failed".format(hwpack))
 
164
                logger.error("Hwpack {0} verification failed".format(hwpack))
160
165
                return False, []
161
166
 
162
167
        for verified_file in verified_files:
163
 
            logging.info('Hash verification of file {0} OK.'.format(
 
168
            logger.info('Hash verification of file {0} OK.'.format(
164
169
                                                                verified_file))
165
 
 
166
170
    return True, verified_files
167
171
 
168
172
 
348
352
        raise MissingRequiredOption("--dev option is required")
349
353
    if args.binary is None:
350
354
        raise MissingRequiredOption("--binary option is required")
 
355
 
 
356
 
 
357
def get_logger(name=DEFAULT_LOGGER_NAME, debug=False):
 
358
    """
 
359
    Retrieves a named logger. Default name is set in the variable
 
360
    DEFAULT_LOG_NAME. Debug is set to False by default.
 
361
 
 
362
    :param name: The name of the logger.
 
363
    :param debug: If debug level should be turned on
 
364
    :return: A logger instance.
 
365
    """
 
366
    logger = logging.getLogger(name)
 
367
    ch = logging.StreamHandler()
 
368
 
 
369
    if debug:
 
370
        ch.setLevel(logging.DEBUG)
 
371
        formatter = logging.Formatter(
 
372
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
 
373
        ch.setFormatter(formatter)
 
374
        logger.setLevel(logging.DEBUG)
 
375
    else:
 
376
        ch.setLevel(logging.INFO)
 
377
        formatter = logging.Formatter("%(message)s")
 
378
        ch.setFormatter(formatter)
 
379
        logger.setLevel(logging.INFO)
 
380
 
 
381
    logger.addHandler(ch)
 
382
    return logger