Beispiel #1
0
def testtwo():
    p = AnimatedProgressBar(end=100, width=80)

    while True:
        p + 5
        p.show_progress()
        time.sleep(0.1)
        if p.progress == 100:
            break
Beispiel #2
0
def hashing(disk_path, meta_path, chunk_size=4096, window_size=512):
    # TODO: need more efficient implementation, e.g. bisect
    # generate hash of base disk
    # disk_path : raw disk path
    # chunk_size : hash chunk size
    # window_size : slicing window size

    prog_bar = AnimatedProgressBar(end=100, width=80, stdout=sys.stdout)
    total_iteration = os.path.getsize(disk_path) / window_size
    iter_count = 0
    prog_interval = 100

    disk_file = open(disk_path, "rb")
    out_file = open(meta_path, "w+b")
    data = disk_file.read(chunk_size)
    if (not data) or len(data) < chunk_size:
        raise DiskError("invalid raw disk size")

    entire_hashing = sha256()
    entire_hashing.update(data)

    s_offset = 0
    data_len = len(data)
    hash_dic = dict()
    while True:
        if (iter_count) % prog_interval == 0:
            prog_bar.process(100.0 * prog_interval / total_iteration)
            prog_bar.show_progress()
        iter_count += 1

        hashed_data = sha256(data).digest()
        if hash_dic.get(hashed_data) == None:
            hash_dic[hashed_data] = (hashed_data, s_offset, data_len)

        added_data = disk_file.read(window_size)
        if (not added_data) or len(added_data) != window_size:
            break
        s_offset += window_size
        data = data[window_size:] + added_data
        entire_hashing.update(added_data)

    for hashed_data, s_offset, data_len in list(hash_dic.values()):
        out_file.write(
            struct.pack("!QI%ds" % len(hashed_data), s_offset, data_len,
                        hashed_data))
    disk_file.close()
    out_file.close()

    return entire_hashing.hexdigest()
Beispiel #3
0
def testone():
    progress = AnimatedProgressBar(end=FILESIZE, width=50)

    for i in range(0, FILESIZE, CHUNK):
        progress + CHUNK
        progress.show_progress()
Beispiel #4
0
    def dispatch_command(self, command):
        """
        Apply user command
        
        @param command : user command
        """
        if len(command) == 1:
            if command[0] == "help":
                self.print_help()
            elif command[0] == "version":
                print self.VERSION_NUMBER
            elif command[0] == "quit":
                self.stop()
                sys.exit(0)
        elif len(command) < 2:
            print "Incorrect command. Run \"help\" to get more information"
        # Print SWAP traffic
        elif command[0] == "traffic":
            if command[1].lower() == "on":
                self.verbose = True
                self.server.verbose = True
                self.server.modem.verbose = True
                self.server.modem._serport._verbose = True
            elif command[1].lower() == "off":
                self.verbose = False
                self.server.verbose = False
                self.server.modem.verbose = False
                self.server.modem._serport._verbose = False
            else:
                print command[1] + " is not a correct value"
        # Set HEX file
        elif command[0] == "hexfile":
            # HEX file object
            self.hexfile = HexFile(command[1])
        # Print list of nodes
        elif command[0] == "list":
            if command[1] == "nodes":
                if len(self.network.motes) == 0:
                    print "No nodes detected"
                else:
                    for mote in self.network.motes:
                        print "Addr: " + hex(
                            mote.address) + " - " + mote.definition.product
        # Clear list list of nodes
        elif command[0] == "clear":
            if command[1] == "nodes":
                self.server.network.clear()
                self.server.network.save()
        # Change device device property
        elif command[0] == "node":
            if len(command) < 3:
                print "Insufficient arguments"
                print "Correct format is: node <address> <options...>"
            else:
                addr = self.str_to_int(command[1])
                if addr is None:
                    print "Incorrect address format"
                else:
                    # Get mote object
                    mote = self.server.network.get_mote(address=addr)

                    if mote is None:
                        print "Node not found in data base"
                        print "Clear your list of nodes and restart them again"
                    else:
                        # Program node
                        if command[2] == "program":
                            if (self.hexfile == None):
                                print "Please set a hexfile before running \"program\""
                            else:
                                # Create progress bar
                                self.progress = AnimatedProgressBar(
                                    end=self.hexfile.nbof_data_lines, width=50)
                                # Save address of node being programmed
                                self.prog_address = addr
                                # Transmit product code
                                self.transmit_product_code()

                                # Put node in upgrade mode
                                val = SwapValue(SwapState.UPGRADE, 1)
                                if mote.cmdRegisterWack(
                                        SwapRegId.ID_SYSTEM_STATE, val):
                                    print "Node now in programming mode"
                                elif self.hexfile_line == 0:
                                    print "Unable to put node in progamming mode"
                                    self.prog_address = None
                        # Restart node (if not sleeping)
                        elif command[2] == "restart":
                            if mote.restart():
                                print "Node restarting"
                            else:
                                print "Got no response from node. It's probably sleeping"
                        # Show details of device
                        elif command[2] == "details":
                            print "SWAP address : " + hex(mote.address)
                            print "Developer : " + mote.definition.manufacturer
                            print "Product name : " + mote.definition.product
                            if mote.config_registers is not None:
                                print "Config registers :"
                                for reg in mote.config_registers:
                                    print "Register ID : " + hex(reg.id)
                                    print "Register name : " + reg.name
                                    print "Register value : 0x" + reg.value.toAsciiHex(
                                    )
                                    for param in reg.parameters:
                                        print "  Parameter name : " + param.name
                                        print "  Parameter value : 0x" + param.value.toAsciiHex(
                                        )
                            if mote.regular_registers is not None:
                                print "Regular registers :"
                                for reg in mote.regular_registers:
                                    print "Register ID : " + hex(reg.id)
                                    print "Register name : " + reg.name
                                    print "Register value : 0x" + reg.value.toAsciiHex(
                                    )
                                    for endp in reg.parameters:
                                        print "  Endpoint name : " + endp.name
                                        print "  Endpoint value : 0x" + endp.value.toAsciiHex(
                                        )
                        # Change device address
                        elif command[2] == "address":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_DEVICE_ADDR), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_addr = self.str_to_int(command[3])
                                if new_addr is not None:
                                    self.print_confirmation(
                                        mote.setAddress(new_addr), command[2],
                                        command[3])
                        # Change Tx interval
                        elif command[2] == "txinterval":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_TX_INTERVAL), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_interval = self.str_to_int(command[3])
                                if new_interval is not None:
                                    self.print_confirmation(
                                        mote.setTxInterval(new_interval),
                                        command[2], command[3])
                        # Change network id
                        elif command[2] == "netid":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_NETWORK_ID), "network ID")
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_netid = self.str_to_int(command[3])
                                if new_netid is not None:
                                    self.print_confirmation(
                                        mote.setNetworkId(new_netid),
                                        command[2], command[3])
                        # Change frequency channel
                        elif command[2] == "channel":
                            if len(command) == 3:
                                self.print_response(
                                    mote.qryRegisterWack(
                                        SwapRegId.ID_FREQ_CHANNEL), command[2])
                            elif len(command) > 4:
                                self.print_format_error(command[2])
                            else:
                                new_channel = self.str_to_int(command[3])
                                if new_channel is not None:
                                    self.print_confirmation(
                                        mote.setFreqChannel(new_channel),
                                        command[2], command[3])
                        # Read/write register
                        elif command[2] == "reg":
                            if len(command) > 3:
                                reg_id = self.str_to_int(command[3])
                                if reg_id is not None:
                                    if len(command) == 4:
                                        self.print_response(
                                            mote.qryRegisterWack(reg_id),
                                            "register value")
                                    elif len(command) == 5:
                                        reg = mote.getRegister(reg_id)
                                        val = SwapValue(
                                            command[4], reg.value.getLength())
                                        self.print_confirmation(
                                            mote.cmdRegisterWack(reg_id, val),
                                            "register value", command[4])
                                    else:
                                        print "Too many arguments"
                                        print "Correct format is: node <address> reg <reg_id> [<reg_val>]"
                            else:
                                print "Insufficient arguments"
                                print "Correct format is: node <address> reg <reg_id> [<reg_val>]"
                        else:
                            print "Command not supported"
        else:
            print "Command not supported"
Beispiel #5
0
    def _get_mem_hash(self, fin, end_offset, hash_list, **kwargs):
        # kwargs
        #  diff: compare hash_list with self object
        #  free_pfn_dict: free memory physical frame number as a dictionary {'#':1, ... }
        diff = kwargs.get("diff", None)
        apply_free_memory = kwargs.get("apply_free_memory", True)
        free_pfn_dict = kwargs.get("free_pfn_dict", None)
        LOG.info("Get hash list of memory page")
        prog_bar = AnimatedProgressBar(end=100, width=80, stdout=sys.stdout)

        total_size = end_offset
        ram_offset = 0
        freed_page_counter = 0
        base_hashlist_length = len(self.hash_list)
        while total_size != ram_offset:
            data = fin.read(Memory.RAM_PAGE_SIZE)
            if not diff:
                hash_list.append((ram_offset, len(data), sha256(data).digest()))
            else:
                # compare input with hash or corresponding base memory, save only when it is different
                hash_list_index = ram_offset/Memory.RAM_PAGE_SIZE
                if hash_list_index < base_hashlist_length:
                    self_hash_value = self.hash_list[hash_list_index][2]
                else:
                    self_hash_value = None

                if self_hash_value != sha256(data).digest():
                    is_free_memory = False
                    if (free_pfn_dict != None) and \
                            (free_pfn_dict.get(long(ram_offset/Memory.RAM_PAGE_SIZE), None) == 1):
                        is_free_memory = True

                    if is_free_memory and apply_free_memory:
                        # Do not compare. It is free memory
                        freed_page_counter += 1
                    else:
                        #get xdelta comparing self.raw
                        source_data = self.get_raw_data(ram_offset, len(data))
                        #save xdelta as DeltaItem only when it gives smaller
                        try:
                            if source_data == None:
                                raise IOError("launch memory snapshot is bigger than base vm")
                            patch = tool.diff_data(source_data, data, 2*len(source_data))
                            if len(patch) < len(data):
                                delta_item = DeltaItem(DeltaItem.DELTA_MEMORY,
                                        ram_offset, len(data),
                                        hash_value=sha256(data).digest(),
                                        ref_id=DeltaItem.REF_XDELTA,
                                        data_len=len(patch),
                                        data=patch)
                            else:
                                raise IOError("xdelta3 patch is bigger than origianl")
                        except IOError as e:
                            #LOG.info("xdelta failed, so save it as raw (%s)" % str(e))
                            delta_item = DeltaItem(DeltaItem.DELTA_MEMORY,
                                    ram_offset, len(data),
                                    hash_value=sha256(data).digest(),
                                    ref_id=DeltaItem.REF_RAW,
                                    data_len=len(data),
                                    data=data)
                        hash_list.append(delta_item)

                # memory over-usage protection
                if len(hash_list) > Memory.RAM_PAGE_SIZE*1000000: # 400MB for hashlist
                    raise MemoryError("possibly comparing with wrong base VM")
            ram_offset += len(data)
            # print progress bar for every 100 page
            if (ram_offset % (Memory.RAM_PAGE_SIZE*100)) == 0:
                prog_bar.set_percent(100.0*ram_offset/total_size)
                prog_bar.show_progress()
        prog_bar.finish()
        return freed_page_counter