示例#1
0
    def test_notty(self):
        """test_notty
           Given a valid progress object
           When the time is set to a valid integer and stream is set to a file pointer
           Then it outputs progress bar stream to file
           When I read the file by line
           And get the current percentage
           Then the current percentage is greater than the previous percentage
           When the current pertagtage exceeds 100
           Then it stops reading and exits the file
        """
        with tempfile.NamedTemporaryFile(mode='w+',
                                         **temp_cfg) as stream:
            with progress(time=10, stream=stream) as prg:
                for _ in range(100):
                    prg.tick()
                    time.sleep(0.1)

            stream.seek(0)
            line = stream.readline().strip('\n')
            p_percent = 0
            while line:
                c_percent = get_percent(line)
                if c_percent is not None:
                    self.assertGreater(c_percent, p_percent)
                    p_percent = c_percent
                    line = stream.readline()
                else:
                    break
示例#2
0
 def test_noop(self):
     """test_noop
        Given a valid progress object
        When time is set to a valid integer and null is set to True
        Then progress reporting tool becomes a 'noop' method
     """
     with progress(time=0, null=True) as prg:
         for _ in range(10):
             prg.tick()
             time.sleep(0.1)
示例#3
0
 def test_time0(self):
     """test_time0
        Given a valid progress object
        When time is set to zero
        Then the progress bar is streamed to stdout
        And the test succeeds
     """
     with progress(time=0) as prg:
         for _ in range(10):
             prg.tick()
             time.sleep(0.1)
示例#4
0
 def test_bytes_set_none(self):
     """test_bytes_set_none
        Given a valid progress object
        When I set bytes to none
        Then it exits with RunTimeError exception
     """
     with progress(bytes=None) as prg:
         for _ in range(10):
             with self.assertRaises(RuntimeError):
                 prg.update(self.TEST_SIZE)
                 time.sleep(0.1)
示例#5
0
 def test_time_set_none(self):
     """test_time_set_none
        Given a valid progress object
        When I set time to none
        Then it exits with RunTimeError exception
     """
     with progress(time=None) as prg:
         for _ in range(10):
             with self.assertRaises(RuntimeError):
                 prg.tick()
                 time.sleep(0.1)
示例#6
0
 def test_stream_readonly(self):
     """test_stream_readonly
        Given a valid progress object
        When time is set to a valid integer
        And stream is set with read-only object
        Then it prints Error and redirects stream to stdout
     """
     stream = io.IOBase()
     with progress(time=10, stream=stream) as prg:
         for _ in range(100):
             prg.tick()
             time.sleep(0.1)
示例#7
0
 def test_label(self):
     """test_label
        Given a valid progress object with a label
        When time is set to zero
        Then the progress bar is streamed to stdout
        And the label is shown
        And the test succeeds
     """
     with progress(time=0, label='Writing') as prg:
         for _ in range(10):
             prg.tick()
             time.sleep(0.1)
示例#8
0
文件: mtd.py 项目: rchriste1/opae-sdk
    def _copy_chunked(self, src, dest, size, chunk_size, prg_writer=None):
        offset = 0

        cfg = {'bytes': size}
        if callable(prg_writer):
            cfg['log'] = prg_writer
        elif isinstance(prg_writer, _ftype):
            cfg['stream'] = prg_writer
        else:
            cfg['null'] = True

        prg = progress(**cfg)

        with prg:
            while offset < size:
                n_bytes = min(chunk_size, size - offset)
                chunk = src.read(n_bytes)
                dest.write(chunk)
                n_read = len(chunk)
                if n_read < n_bytes:
                    self.log.warning('read %d less bytes than requested',
                                     n_bytes - n_read)
                offset += n_read
                prg.update(offset)
示例#9
0
def update_fw(args, pac):
    """Writes firmware to secure device.

    args - the object resulting from command-line parsing.

    returns a 2-tuple of the process exit status and a message.
    """
    # bytes/sec when staging is flash
    flash_copy_bps = 43000.0
    # bytes/sec when staging area is dram
    dram_copy_bps = 92000.0
    dram_copy_offset = 42.0

    infile = args.file

    orig_pos = infile.tell()
    infile.seek(0, os.SEEK_END)
    payload_size = infile.tell() - orig_pos
    infile.close()

    sec_dev = pac.secure_dev
    error = sec_dev.find_one(os.path.join('update', 'error'))
    filename = sec_dev.find_one(os.path.join('update', 'filename'))
    size = sec_dev.find_one(os.path.join('update', 'remaining_size'))
    status = sec_dev.find_one(os.path.join('update', 'status'))

    fw_path = os.path.join(os.sep, 'usr', 'lib', 'firmware')
    if not os.path.isdir(fw_path):
        LOG.error("Can't find %s", fw_path)
        return 1, 'Secure update failed'

    intel_fw_path = os.path.join(fw_path, 'intel')
    if not os.path.isdir(intel_fw_path):
        try:
            os.mkdir(intel_fw_path, 0o755)
        except OSError:
            LOG.error("Can't create %s", intel_fw_path)
            return 1, 'Secure update failed'

    tfile = tempfile.NamedTemporaryFile(dir=intel_fw_path, delete=False)
    tfile.close()

    shutil.copy(infile.name, tfile.name)
    global TMPFILE
    TMPFILE = tfile.name

    LOG.info('waiting for idle')
    retries = 0
    timeout = 1.0
    max_retries = 60 * 5
    while status.value != 'idle':
        time.sleep(timeout)
        retries += 1
        if retries >= max_retries:
            os.remove(tfile.name)
            return errno.ETIMEDOUT, 'Secure update timed out'

    LOG.info('updating from file %s with size %d', infile.name, payload_size)

    filename.value = os.path.join('intel', os.path.basename(tfile.name))

    retries = 0
    timeout = 1.0
    max_retries = 60 * 5
    # read_file is now deprecated. Leaving it in for backwards compat.
    while status.value in ['read_file', 'reading', 'preparing']:
        time.sleep(timeout)
        retries += 1
        if retries >= max_retries:
            os.remove(tfile.name)
            return errno.ETIMEDOUT, 'Secure update timed out'

    if status.value == 'idle':
        e = error.value
        if e:
            os.remove(tfile.name)
            return 1, e

    progress_cfg = {}
    level = min([handler.level for handler in LOG.handlers])
    if level < logging.INFO:
        progress_cfg['log'] = LOG.debug
    else:
        progress_cfg['stream'] = sys.stdout

    retries = 0
    timeout = 1.0
    max_retries = 60 * 60 * 2
    with progress(bytes=payload_size, **progress_cfg) as prg:
        while int(size.value) > 0:
            time.sleep(timeout)
            retries += 1
            if retries >= max_retries:
                os.remove(tfile.name)
                return errno.ETIMEDOUT, 'Secure update timed out'
            prg.update(payload_size - int(size.value))

    if status.value == 'idle':
        e = error.value
        if e:
            os.remove(tfile.name)
            return 1, e

    if pac.fme.have_node('tcm'):
        estimated_time = payload_size / dram_copy_bps + dram_copy_offset
    else:
        estimated_time = payload_size / flash_copy_bps
    # over-estimate by 1.5 to account for flash performance degradation
    estimated_time *= 1.5

    LOG.info('applying update to %s', pac.pci_node.pci_address)

    retries = 0
    timeout = 1.0
    max_retries = 60 * 60 * 3
    with progress(time=estimated_time, **progress_cfg) as prg:
        while status.value == 'writing' or status.value == 'programming':
            time.sleep(timeout)
            retries += 1
            if retries >= max_retries:
                os.remove(tfile.name)
                return errno.ETIMEDOUT, 'Secure update timed out'
            prg.tick()

    if status.value == 'idle':
        e = error.value
        if e:
            os.remove(tfile.name)
            return 1, e

    LOG.info('update of %s complete', pac.pci_node.pci_address)

    os.remove(tfile.name)

    return 0, 'Secure update OK'