Ejemplo n.º 1
0
 def write_config(self):
     """Write current config to the config file
     """
     config_file = FileIO(self.container + REPO_FILE, "wb")
     config_file.seek(0)
     config_file.truncate(0)
     config_file.write(json.dumps(self.data).encode())
Ejemplo n.º 2
0
def build_blob(bin1: FileIO, bin2: FileIO, out: FileIO) -> None:
    """Combines two firmware binary blobs ``bin1`` and ``bin2`` into a single
    firmware blob and saves them in ``out``.
    """
    bin1_size = os.fstat(bin1.fileno()).st_size
    bin2_size = os.fstat(bin2.fileno()).st_size
    bin2_offset = BIN2_BASE_OFFSET - BIN1_BASE_OFFSET
    size = bin2_offset + bin2_size
    max_size = FLASH_SIZE - BIN1_BASE_OFFSET

    if bin1_size >= bin2_offset:
        raise ValueError(f"{bin1.name} is too big!")
    if size >= max_size:
        raise ValueError(f"{bin2.name} is too big!")

    # Create a new combined firmware blob
    blob = memoryview(bytearray(size))
    bin1.readinto(blob)
    bin2.readinto(blob[bin2_offset:])

    # Read Reset_Handler pointers in vector tables.
    bin1_reset_handler = blob[4:8].tobytes()
    bin2_reset_handler = blob[bin2_offset + 4 : bin2_offset + 8].tobytes()

    # Swap Reset_Handler pointers. This will cause the second
    # firmware to boot first.
    blob[4:8] = bin2_reset_handler
    blob[bin2_offset + 4 : bin2_offset + 8] = bin1_reset_handler

    # The final checksum is for the entire new blob
    # This overrides the checksum of the second firmware.
    blob[-4:] = crc32_checksum(BytesIO(blob), max_size).to_bytes(4, "little")

    out.write(blob)
Ejemplo n.º 3
0
 def get_status(self, ep0: io.FileIO) -> None:
     buf = AuthStatusReport(type=ReportType.get_auth_status)
     buf.seq = self._seq
     buf.status = self._status
     buf.crc32 = zlib.crc32(
         bytes(buf)[:ctypes.sizeof(AuthStatusReport) -
                    ctypes.sizeof(c_uint32)])
     ep0.write(buf)  #type: ignore[arg-type]
Ejemplo n.º 4
0
 def flipByteAt(inputfile, position):
     """Flips the bits for the byte at the specified position in the input file."""
     f = FileIO(inputfile, "r+")
     f.seek(position)
     byte = ord(f.read(1))
     f.seek(-1, 1)   # go back 1 byte from current position
     f.write(struct.pack("B", byte^0xFF))    # read in the byte and XOR it
     f.close()
Ejemplo n.º 5
0
 def flipByteAt(inputfile, position):
     """Flips the bits for the byte at the specified position in the input file."""
     f = FileIO(inputfile, "r+")
     f.seek(position)
     byte = ord(f.read(1))
     f.seek(-1, 1)  # go back 1 byte from current position
     f.write(struct.pack("B", byte ^ 0xFF))  # read in the byte and XOR it
     f.close()
Ejemplo n.º 6
0
    def process(self, output=None, vlc=False):

        assert not (output is None and not vlc),\
            "Either output file or viewer must be valid"

        conn = None
        n_bytes = 0

        if output is not None:
            f = FileIO(output, "w")

        if vlc:
            # open pipe to vlc to view incoming video data
            cmdline = [
                'vlc',
                '--demux',
                'h264',
                '--network-caching=',
                '512',  # in ms
                '--h264-fps',
                '30',
                '-'
            ]
            player = subprocess.Popen(cmdline, stdin=subprocess.PIPE)

        try:
            conn, addr = self.server.accept()
            if self.verbose:
                print('Connection address:', addr)

            while 1:

                data = conn.recv(1048576)
                if not data:
                    break

                n_bytes += len(data)

                # write data to file and video stream
                if output is not None:
                    f.write(data)

                if vlc:
                    player.stdin.write(data)

                # indicate that we are ready for more data (move up?)
                conn.send("")

        finally:
            if conn is not None:
                conn.close()
            if output is not None:
                f.close()
            if vlc:
                player.terminate()

        if self.verbose:
            print(" total # bytes received:", n_bytes)
Ejemplo n.º 7
0
class HidrawDS4Device(DS4Device):
    def __init__(self, name, addr, type, hidraw_device, event_device):
        try:
            self.report_fd = os.open(hidraw_device, os.O_RDWR | os.O_NONBLOCK)
            self.fd = FileIO(self.report_fd, "rb+", closefd=False)
            self.input_device = InputDevice(event_device)
            self.input_device.grab()
        except (OSError, IOError) as err:
            raise DeviceError(err)

        self.buf = bytearray(self.report_size)

        super(HidrawDS4Device, self).__init__(name, addr, type)

    def read_report(self):
        try:
            ret = self.fd.readinto(self.buf)
        except IOError:
            return

        # Disconnection
        if ret == 0:
            return

        # Invalid report size or id, just ignore it
        if ret < self.report_size or self.buf[0] != self.valid_report_id:
            return False

        if self.type == "bluetooth":
            # Cut off bluetooth data
            buf = zero_copy_slice(self.buf, 2)
        else:
            buf = self.buf

        return self.parse_report(buf)

    def read_feature_report(self, report_id, size):
        op = HIDIOCGFEATURE(size + 1)
        buf = bytearray(size + 1)
        buf[0] = report_id

        return fcntl.ioctl(self.fd, op, bytes(buf))

    def write_report(self, report_id, data):
        if self.type == "bluetooth":
            # TODO: Add a check for a kernel that supports writing
            # output reports when such a kernel has been released.
            return

        hid = bytearray((report_id,))
        self.fd.write(hid + data)

    def close(self):
        try:
            self.fd.close()
            self.input_device.ungrab()
        except IOError:
            pass
Ejemplo n.º 8
0
 def __init__(self, data=None):
     # Raise exception if Blobs are getting subclassed
     # refer to ZODB-Bug No.127182 by Jim Fulton on 2007-07-20
     if (self.__class__ is not Blob):
         raise TypeError('Blobs do not support subclassing.')
     self.__setstate__()
     if data is not None:
         with self.open('w') as file:
             file.write(data)
Ejemplo n.º 9
0
 def __init__(self, data=None):
     # Raise exception if Blobs are getting subclassed
     # refer to ZODB-Bug No.127182 by Jim Fulton on 2007-07-20
     if (self.__class__ is not Blob):
         raise TypeError('Blobs do not support subclassing.')
     self.__setstate__()
     if data is not None:
         with self.open('w') as file:
             file.write(data)
Ejemplo n.º 10
0
class HidrawDS4Device(DS4Device):
    def __init__(self, name, addr, type, hidraw_device, event_device):
        try:
            self.report_fd = os.open(hidraw_device, os.O_RDWR | os.O_NONBLOCK)
            self.fd = FileIO(self.report_fd, "rb+", closefd=False)
            self.input_device = InputDevice(event_device)
            self.input_device.grab()
        except (OSError, IOError) as err:
            raise DeviceError(err)

        self.buf = bytearray(self.report_size)

        super(HidrawDS4Device, self).__init__(name, addr, type)

    def read_report(self):
        try:
            ret = self.fd.readinto(self.buf)
        except IOError:
            return 'ioerror'

        # Disconnection
        if ret == 0:
            return 'disconnection'

        # Invalid report size or id, just ignore it
        if ret < self.report_size or self.buf[0] != self.valid_report_id:
            return False

        if self.type == "bluetooth":
            # Cut off bluetooth data
            buf = zero_copy_slice(self.buf, 2)
        else:
            buf = self.buf

        return self.parse_report(buf)

    def read_feature_report(self, report_id, size):
        op = HIDIOCGFEATURE(size + 1)
        buf = bytearray(size + 1)
        buf[0] = report_id

        return fcntl.ioctl(self.fd, op, bytes(buf))

    def write_report(self, report_id, data):
        hid = bytearray((report_id, ))
        self.fd.write(hid + data)

    def close(self):
        try:
            # Reset LED to original hidraw pairing colour.
            self.set_led(0, 0, 1)

            self.fd.close()
            self.input_device.ungrab()
        except IOError:
            pass
Ejemplo n.º 11
0
 def __del__(self):
     if self.logger_file is not None:
         sys.stdout = sys.__stdout__
         self.logger.seek(0)
         log = FileIO(self.logger_file, "w")  # odd behaviour at
         # deconstruction
         # prohibits open(...)
         log.write(self.logger.read().encode())
         log.close()
         print(f"written logs to {self.logger_file!r}, exiting ...")
Ejemplo n.º 12
0
    def output_log(
            self,
            output: FileIO,
            message: Union[str, Dict] = None,
            msgprefix: str = '[{time}][{function}@{filename}:{lineno}][{name}]',
            timestamp: str = '%Y-%m-%d %H:%M:%S %z',
            caller: int = 1,
            **kwargs):
        """
    Outputs log entry during this experiment to the given output file.

    Args:
      output:     The output log file.
      message:    The output message as log entry. If None or
                  Dict, construct message from data property.
      msgprefix:  The string to prefix to output message.
      timestamp:  The string to format time stamps.
      caller:     The number of stack frames to associate
                  with log entry, to prepend function name
                  to log entry.
      kwargs:     The data properties to insert within
                  the given message string.
    """
        def msg(message) -> str:
            if message is None:
                return ' '.join([f'{k}={v}' for k, v in kwargs.items()])
            elif isinstance(message, Dict):
                return ' '.join(
                    [f'{k}={v}' for k, v in {
                        **message,
                        **kwargs
                    }.items()])
            elif isinstance(message, NamedTuple):
                return ' '.join([
                    f'{k}={v}' for k, v in {
                        **message._asdict(),
                        **kwargs
                    }.items()
                ])
            else:
                return message

        time, frame = strftime(timestamp), stack()[caller]._asdict()
        frame['filename'] = basename(frame['filename'])
        msgprefix = msgprefix.format(**{
            'time': time,
            'name': self.name,
            **frame
        })
        message = message.format(
            **kwargs) if isinstance(message, str) else msg(message)

        output.write(f'{msgprefix}: {message}\n')
Ejemplo n.º 13
0
    def flipBitAt(inputfile, position):
        """Flips the bit at the specified position in the input file."""
        if not 0 <= position < (8 * os.path.getsize(inputfile)):
            raise IndexError("Position " + str(position) + " is out of range")

        f = FileIO(inputfile, "r+")
        f.seek(position / 8)
        byte = ord(f.read(1))
        f.seek(-1, 1)  # go back 1 byte from the current position
        bitnum = position % 8
        f.write(struct.pack("B", byte ^ (1 << (7 - bitnum))))
        f.close()
Ejemplo n.º 14
0
 def flipBitAt(inputfile, position):
     """Flips the bit at the specified position in the input file."""
     if not 0<=position<(8*os.path.getsize(inputfile)):
         raise IndexError("Position "+str(position)+" is out of range")
     
     f = FileIO(inputfile, "r+")
     f.seek(position/8)
     byte = ord(f.read(1))
     f.seek(-1, 1)   # go back 1 byte from the current position
     bitnum = position%8
     f.write(struct.pack("B", byte^(1<<(7-bitnum))))
     f.close()
Ejemplo n.º 15
0
def _write_to_file(output_file: FileIO, file: str) -> None:
    """
    Add data from file to output.
    :param output_file: binary file to append data to.
    :type output_file: FileIO
    :param file: file to read from
    :type file: str
    """
    b = getsize(file)
    output_file.write(struct.pack('I', b))
    with open(file, 'rb') as input_file:
        output_file.write(input_file.read())
Ejemplo n.º 16
0
    def import_users(self):
        """
        Store imported users in a new .htpasswd style file.
        """
        log.info('  {} Users...'.format(len(self.jiraData['users'])))

        line = '{}:{}\n'
        output = FileIO(self.authentication, 'w')
        output.write(line.format(self.username, create_hash(self.password)))
        
        for user in self.jiraData['users']:
            output.write(line.format(user['name'], user['password']))

        output.close()
Ejemplo n.º 17
0
    def importUsers(self):
        """
        Store imported users in a new .htpasswd style file.
        """
        log.info('  %d Users...' % (len(self.jiraData['users'])))

        line = '%s:%s\n'
        output = FileIO(self.authentication, 'w')
        output.write(line % (self.username, create_hash(self.password)))

        for user in self.jiraData['users']:
            output.write(line % (user['name'], user['password']))

        output.close()
Ejemplo n.º 18
0
def createFile(size):
 headers=['Id', 'Name', 'Balance']
 try: fp=open('sample.txt', 'w')
 except:fp=FileIO('sample.txt','w')
 fp.truncate()
 table=getTable(size)
 for row in table:
  i=0
  for item in row:
   readyItem=headers[i]+':'+item+'\n'
   i+=1
   fp.write(readyItem)
  fp.write('\n')
 fp.close()
Ejemplo n.º 19
0
 def get_response(self, ep0: io.FileIO) -> None:
     buf = AuthReport(type=ReportType.get_response)
     buf.seq = self._seq
     buf.page = self._resp_page
     if self._response.readinto(cast(bytearray,
                                     buf.data)) == 0:  # bytearray-like
         logger.warning(
             'Attempt to read outside of the auth response buffer.')
     if self._resp_page == self._resp_max_page:
         self.reset()
     buf.crc32 = zlib.crc32(
         bytes(buf)[:ctypes.sizeof(AuthReport) - ctypes.sizeof(c_uint32)])
     ep0.write(buf)  #type: ignore[arg-type]
     if self._status == 0x0:
         self._resp_page += 1
Ejemplo n.º 20
0
def write_to_file(file: io.FileIO, data: bytes, start: int = 0):
    length_to_write = len(data)
    file.seek(start)
    written = 0
    while written < length_to_write:
        written += file.write(data[written:])
    os.fsync(file.fileno())
Ejemplo n.º 21
0
def write_to_file(file_fd: io.FileIO, data: bytes, f_sync: bool = False):
    length_to_write = len(data)
    written = 0
    while written < length_to_write:
        written = file_fd.write(data[written:])
    if f_sync:
        file_flush_and_sync(file_fd)
Ejemplo n.º 22
0
    def __init__(self, initer):
        if isinstance(initer, str):
            #test if initer is path to a file ...
            if os.path.exists(initer):
                iobj = FileIO(initer, 'rb')
            else:
                # it should be a binary buffer ..
                iobj = StringIO()
                iobj.write(initer)
                iobj.seek(0, os.SEEK_SET)
        else:
            #it should be an io object (StringIO, FiliIO)
            #(interface will be great in python)
            iobj = initer
        ElfStream.__init__(self, iobj)
        self.iobj = iobj

        # HEADER
        self.header = Header(self)

        # LOAD PROGRAM and SECTION HEADER TABLES
        self.prog_headers = self.load_entries(self.header.ph_offset,
                                              self.header.ph_count,
                                              ProgramHeader)
        self.sect_headers = self.load_entries(self.header.sh_offset,
                                              self.header.sh_count,
                                              SectionHeader)

        # LOAD SECTION HEADERS STRING TABLE
        strtab = self.sect_headers[self.header.shstrndx]
        self.shstrtab = StringTable(self.io, strtab.offset, strtab.size)

        # Create a section dictionary
        self.sect_dict = {}
        for sec in self.sect_headers:
            self.sect_dict[sec.name] = sec

        # LOAD STRING TABLE
        if '.strtab' in self.sect_dict:
            strtab = self.sect_dict['.strtab']
            self.strtab = StringTable(self.io, strtab.offset, strtab.size)

        # LOAD SYMBOL TABLE
        if '.symtab' in self.sect_dict:
            symtab = self.sect_dict['.symtab']
            count = symtab.size / Symbol.LENGTH
            self.symbols = self.load_entries(symtab.offset, count, Symbol)
Ejemplo n.º 23
0
def write_to_file(file_fd: io.FileIO, dir_fileno: Optional[int],
                  data: bytes, fsync: bool=True):
    length_to_write = len(data)
    written = 0
    while written < length_to_write:
        written = file_fd.write(data[written:])
    if fsync:
        fsync_file_and_dir(file_fd.fileno(), dir_fileno)
Ejemplo n.º 24
0
def bench_file_write55():
    f = FileIO(tmpf.name, "r+")
    zblk = b"\x55" * blksize
    for i in xrange(filesize // blksize):
        pos = 0
        while pos < blksize:
            n = f.write(memoryview(zblk)[pos:])
            assert n != 0
            pos += n
    f.close()
Ejemplo n.º 25
0
    def __init__(self, initer ):
        if isinstance(initer, str):
            #test if initer is path to a file ...
            if os.path.exists(initer) :
                iobj = FileIO(initer,'rb')
            else : 
                # it should be a binary buffer ..
                iobj = StringIO()
                iobj.write(initer)
                iobj.seek(0,os.SEEK_SET)
        else :
            #it should be an io object (StringIO, FiliIO) 
            #(interface will be great in python)
            iobj = initer
        ElfStream.__init__(self,iobj)
        self.iobj = iobj
        
        # HEADER
        self.header = Header(self)
        
        # LOAD PROGRAM and SECTION HEADER TABLES
        self.prog_headers = self.load_entries(self.header.ph_offset, self.header.ph_count, ProgramHeader)
        self.sect_headers = self.load_entries(self.header.sh_offset, self.header.sh_count, SectionHeader)
        
        # LOAD SECTION HEADERS STRING TABLE
        strtab = self.sect_headers[self.header.shstrndx]
        self.shstrtab = StringTable(self.io, strtab.offset, strtab.size)
        
        # Create a section dictionary
        self.sect_dict = {}
        for sec in self.sect_headers:
            self.sect_dict[sec.name] = sec
        
        # LOAD STRING TABLE
        if '.strtab' in self.sect_dict:
            strtab = self.sect_dict['.strtab']
            self.strtab = StringTable(self.io, strtab.offset, strtab.size)

        # LOAD SYMBOL TABLE
        if '.symtab' in self.sect_dict:
            symtab = self.sect_dict['.symtab']
            count = symtab.size / Symbol.LENGTH
            self.symbols = self.load_entries(symtab.offset, count, Symbol)
Ejemplo n.º 26
0
def relst(lst_filename: str, module: Module, rlst_file: FileIO) -> None:
    # for k,v in module.segments.items():
    #     print(f"key = {k}, value = {v}")
    with open(lst_filename) as f:
        line_num = 0
        cur_segment: Segment = None
        for line in f:
            line_num += 1
            lst_part = line[0:24]
            code_part = line[24:]

            # Check for a segment change
            segment_match = seg_matcher.match(code_part)
            if not segment_match:  # Wasn't .segment, check one of the special segment names
                segment_match = spc_matcher.match(code_part)
            if segment_match:
                matched_segment_name = segment_match.group(1).upper()
                if matched_segment_name in module.segments:
                    cur_segment = module.segments[matched_segment_name]
                else:
                    print(
                        f"INFO: Could not find segment matching '{matched_segment_name}' in module {module.name}, assuming zero offset",
                        file=sys.stderr)
                    cur_segment = Segment(matched_segment_name, 0, 0)
            if cur_segment:
                # Only update lst-file-like lines
                lst_match = lst_matcher.match(lst_part)
                if lst_match:
                    # Found a normal lst file line, replace its relative offset with the absolute offset.
                    # TODO: This labels comment blocks of functions with the address of the first instruction of the
                    #       function. It looks kind of ugly. Perhaps if the offset of the current line hasn't changed
                    #       from the previous line, we should just scrub the offset altogether to improve readability.
                    location = int(lst_match.group(1), 16) + cur_segment.offset
                    rlst_file.write(f"{location:06X} a {line[9:].rstrip()}\n")
                else:
                    rlst_file.write(line)
            else:
                # Writing these lines will often put a large chunk of include file spew at the top
                # of the file. Not sure if this is something we'd want in the rlst file or not.
                # It's already in the regular .lst file so for now we'll scrub these lines.
                #rlst_file.write(line)
                pass
Ejemplo n.º 27
0
    def save(self, file: FileIO, close=False, chunk_size=2**10):
        """Write the image from Imgur to a file object."""

        r = requests.get(self._source, stream=True)

        # attempt to retrieve image size
        try:
            size = int(r.headers['content-length'])
            pro = True
        except KeyError:
            size = 0
            pro = False
        done = 0

        # download image and return progress
        for chunk in r.iter_content(chunk_size):
            file.write(chunk)
            done += len(chunk)
            yield done / size if pro else 1

        # close file at end of write automatically
        if close:
            file.close()
Ejemplo n.º 28
0
    def save(self, file: FileIO, close=False, chunk_size=2**10):
        """Write the image from Imgur to a file object."""

        r = requests.get(self._source, stream=True)

        # attempt to retrieve image size
        try:
            size = int(r.headers['content-length'])
            pro = True
        except KeyError:
            size = 0
            pro = False
        done = 0

        # download image and return progress
        for chunk in r.iter_content(chunk_size):
            file.write(chunk)
            done += len(chunk)
            yield done / size if pro else 1

        # close file at end of write automatically
        if close:
            file.close()
Ejemplo n.º 29
0
 def dump(self, file: FileIO):
     self.ptr = file.tell()
     file.write(bytes(self))
Ejemplo n.º 30
0
 def SaveFileContent(self, request_iterator, context):
     result = fms.SaveFileContentReply()
     try:
         path = ''
         dest_size = 0
         curr_size = 0
         first = True
         file_tmp = None
         count = 0
         for chunk in request_iterator:
             if chunk.file.package:
                 pkg_path = get_pkg_path(chunk.file.package)
                 if pkg_path:
                     path = os.path.join(
                         pkg_path, chunk.file.path.lstrip(os.path.sep))
             else:
                 path = chunk.file.path
             result = fms.SaveFileContentReply()
             if first:
                 if os.path.exists(path):
                     # checks for mtime
                     if chunk.overwrite or chunk.file.mtime == os.path.getmtime(
                             path):
                         file_tmp = FileIO("%s.tmp" % path, 'w')
                         dest_size = chunk.file.size
                     else:
                         result.status.code = CHANGED_FILE
                         result.status.error_code = file_item.EFILE_CHANGED
                         result.status.error_msg = utf8(
                             "file was changed in meantime")
                         result.status.error_file = utf8(path)
                 elif chunk.overwrite or chunk.file.mtime == 0:
                     # mtime == 0 stands for create a new file
                     try:
                         os.makedirs(os.path.dirname(path))
                     except OSError:
                         pass
                     file_tmp = FileIO("%s.tmp" % path, 'w')
                     dest_size = chunk.file.size
                 else:
                     result.status.code = REMOVED_FILE
                     result.status.error_code = file_item.EFILE_REMOVED
                     result.status.error_msg = utf8(
                         "file was removed in meantime")
                     result.status.error_file = utf8(path)
                 first = False
             if result.status.code == 0:
                 written = 0
                 if chunk.file.data:
                     written = file_tmp.write(chunk.file.data)
                     if written is None:
                         written = len(chunk.file.data)
                 if written != len(chunk.file.data):
                     result.status.code = ERROR
                     result.status.error_msg = utf8(
                         "error while write to tmp file")
                     result.status.error_file = utf8(path)
                     yield result
                 curr_size += written
                 result.ack.path = path
                 result.ack.size = curr_size
             if dest_size == curr_size:
                 if file_tmp is not None:
                     file_tmp.close()
                 if file_tmp is not None:
                     os.rename("%s.tmp" % path, path)
                     result.ack.mtime = os.path.getmtime(path)
                     result.status.code = OK
             count += 1
             yield result
             if result.status.code != OK:
                 break
         if count == 0:
             result.status.code = ERROR
             result.status.error_msg = utf8("No iterating objects found")
             result.status.error_file = utf8(path)
             yield result
     except IOError as ioe:
         result.status.code = IO_ERROR
         if ioe.errno:
             result.status.error_code = ioe.errno
         result.status.error_msg = utf8(ioe.strerror)
         result.status.error_file = utf8(ioe.filename)
         yield result
Ejemplo n.º 31
0
 def write_error_log(message: str, stream: io.FileIO = sys.stdout):
     return stream.write(
         Logger.__get_message_with_prefix("[ERROR]", message))
Ejemplo n.º 32
0
 def write_warning_log(message: str, stream: io.FileIO = sys.stdout):
     return stream.write(
         Logger.__get_message_with_prefix("[WARNING]", message))
Ejemplo n.º 33
0
 def write_debug_log(message: str, stream: io.FileIO = sys.stdout):
     return stream.write(
         Logger.__get_message_with_prefix('[DEBUG]', message
                                          ) if __debug__ else '')
Ejemplo n.º 34
0
 def write_log(message: str, stream: io.FileIO = sys.stdout):
     stream.write(Logger.__get_message_with_prefix("", message))
Ejemplo n.º 35
0
def writeCostumeFile(idx, path, name):
    file = FileIO(path, 'w')
    file.write(top)
    file.write(getXMLTag(idx, name))
    file.write(bottom)
    file.close()
Ejemplo n.º 36
0
def my_close(file:io.FileIO, byar:bytearray):
	file.seek(-1*len(byar), io.SEEK_CUR)
	file.write(byar)
	file.close()
Ejemplo n.º 37
0
 def write(self, s):
     s = re_fetch.sub(self._append_async0, s)
     file.write(self, s.encode())
Ejemplo n.º 38
0
 def get_feature_configuration(self, ep0: io.FileIO) -> None:
     ep0.write(self._features)  #type: ignore[arg-type]
Ejemplo n.º 39
0
class Joystick(Part):
    """Joystick to drive the car around manually without keyboard."""
    def __init__(self, config):
        """Joystick to drive the car around manually without keyboard."""
        super(Joystick, self).__init__(config, "joystick", [])

        # State/Controls
        self.speed = 0
        self.steer = 0
        self.speed_offset = 0
        self.steer_offset = 0
        self.is_calibrated = True
        self.is_autonomous = False
        self.state = DS4State()
        self.last_state = DS4State()
        self.__fd = None
        self.__input_device = None
        self.__report_fd = None
        self.__report_id = 0x11
        self.__keep_running = True
        self.__connect()

    def __del__(self):
        self.publish("action", isManual=True, speed=0, steer=0)
        self.publish("controller",
                     isAutonomous=False,
                     speedOffset=0,
                     steerOffset=0,
                     exit=True)
        super(Joystick, self).__del__()
        try:
            self.send(red=1, rumble_high=1)
            time.sleep(0.5)
            self.send(blue=0.1, green=0.1, red=0.5)
        except:
            pass
        if self.__fd is not None:
            self.__fd.close()
        if self.__input_device is not None:
            self.__input_device.ungrab()

    def __find_device(self):
        context = Context()
        for hidraw_device in context.list_devices(subsystem="hidraw"):
            hid_device = hidraw_device.parent
            if hid_device.subsystem != "hid" or hid_device.get(
                    "HID_NAME") != "Wireless Controller":
                continue
            for child in hid_device.parent.children:
                event_device = child.get("DEVNAME", "")
                if event_device.startswith("/dev/input/event"):
                    break
            else:
                continue

            device_addr = hid_device.get("HID_UNIQ", "").upper()
            return device_addr, hidraw_device.device_node, event_device
        return None, None, None

    def __connect(self):
        device_addr, hidraw_device, event_device = self.__find_device()
        if device_addr is None:
            return False
        self.__report_fd = os.open(hidraw_device, os.O_RDWR | os.O_NONBLOCK)
        self.__fd = FileIO(self.__report_fd, "rb+", closefd=False)
        self.__input_device = InputDevice(event_device)
        self.__input_device.grab()
        buf = bytearray(38)
        buf[0] = 0x02
        try:
            return bool(fcntl.ioctl(self.__fd, 3223734279, bytes(buf)))
        except:
            pass
        if self.recv():
            self.update_controller()

    def __in_deadzone(self, value):
        """ Deadzone checker for analog sticks """
        return 128 - self._config["deadzone"] < value <= 128 + self._config[
            "deadzone"]

    def __normalize_stick(self, value, deadzone):
        """
        Normalize stick value from [0, 255] to [0, 1]
        Ignore a 128-centered deadzone
        """
        value -= 128
        value = value - deadzone if value > 0 else value + deadzone
        value /= 127 - deadzone
        return value

    def recv(self, limit=1000, duration=0.001, report_size=78):
        """
        Attempt to get a message from the device. 
        Args:
            limit (int): number of device polls to do
            duration (int): how long to wait between polls
        Returns:
            Whether we have successfully updated the status of the program
        """
        for i in range(limit):
            time.sleep(duration)
            recv_buffer = bytearray(report_size)
            try:
                ret = self.__fd.readinto(recv_buffer)
            except IOError:
                # print("joystick: IO Error")
                continue
            except AttributeError:
                # print("joystick: Attribute Error")
                continue
            if ret is None:
                # print("joystick: ret is none")
                continue
            if ret < report_size:
                # print("joystick: ret too small (%i) expected (%i)" % (ret, report_size))
                continue
            if recv_buffer[0] != self.__report_id:
                # print("joystick: Wrong report id (%i) expected (%i):"
                #      % (recv_buffer[0], self.__report_id))
                continue
            self._timestamp = derp.util.get_timestamp()
            self.last_state = self.state
            self.state = DS4State(recv_buffer)
            self.process_state()
            return True
        return False

    def update_controller(self):
        """Send the state of the system to the controller"""
        green = 1.0 if self.is_autonomous else 0
        red = 1.0 if self.is_calibrated else 0
        blue = 1.0
        light_on = 1.0
        light_off = 0.0
        self.send(red=red,
                  green=green,
                  blue=blue,
                  light_on=light_on,
                  light_off=light_off)
        return True

    def send(self,
             rumble_high=0,
             rumble_low=0,
             red=0,
             green=0,
             blue=0,
             light_on=0,
             light_off=0):
        """Actuate the controller by setting its rumble or light color/blink"""
        packet = bytearray(79)
        packet[:5] = [0xA2, 0x11, 0x80, 0x00, 0xFF]
        packet[7] = int(rumble_high * 255 + 0.5)
        packet[8] = int(rumble_low * 255 + 0.5)
        packet[9] = int(red * 255 + 0.5)
        packet[10] = int(green * 255 + 0.5)
        packet[11] = int(blue * 255 + 0.5)
        packet[12] = int(light_on * 255 + 0.5)
        packet[13] = int(light_off * 255 + 0.5)
        crc = crc32(packet[:-4])
        packet[-4] = crc & 0x000000FF
        packet[-3] = (crc & 0x0000FF00) >> 8
        packet[-2] = (crc & 0x00FF0000) >> 16
        packet[-1] = (crc & 0xFF000000) >> 24
        hid = bytearray((self.__report_id, ))
        if self.__fd is not None:
            self.__fd.write(hid + packet[2:])
            return True
        return False

    def process_state(self):
        """
        For the given  input, figure out how we should affect the state
        and put that into out.
        """
        self.controller_changed = False
        self.action_changed = False
        self.__keep_running = not self.state.button_trackpad
        if not self.__in_deadzone(self.state.left_analog_x):
            steer = self.__normalize_stick(
                self.state.left_analog_x,
                self._config["deadzone"]) * self._config['steer_normalizer']
            if steer != self.steer:
                self.steer = steer
                self.action_changed = True
        elif not self.__in_deadzone(self.last_state.left_analog_x):
            self.steer = 0
            self.action_changed = True
        if self.state.left_trigger:
            speed = -self.state.left_trigger / 255 * self._config[
                'speed_normalizer']
            if speed != self.speed:
                self.speed = speed
                self.action_changed = True
        elif self.last_state.left_trigger:
            self.speed = 0
            self.action_changed = True
        if self.state.right_trigger:
            speed = self.state.right_trigger / 255 * self._config[
                'speed_normalizer']
            if speed != self.speed:
                self.speed = speed
                self.action_changed = True
        elif self.last_state.right_trigger:
            self.speed = 0
            self.action_changed = True
        if self.state.left and not self.last_state.left:
            self.steer_offset -= 5 / 255
            self.controller_changed = True
        if self.state.right and not self.last_state.right:
            self.steer_offset += 5 / 255
            self.controller_changed = True
        if self.state.up and not self.last_state.up:
            self.speed_offset += 5 / 255
            self.controller_changed = True
        if self.state.down and not self.last_state.down:
            self.speed_offset -= 5 / 255
            self.controller_changed = True
        if self.state.button_square and not self.last_state.button_square:
            pass
        if self.state.button_cross and not self.last_state.button_cross:
            self.speed = 0
            self.steer = 0
            self.speed_offset = 0
            self.is_autonomous = False
            self.action_changed = True
            self.controller_changed = True
        if self.state.button_triangle and not self.last_state.button_triangle:
            self.is_autonomous = True
            self.controller_changed = True
        if self.state.button_circle and not self.last_state.button_circle:
            self.controller_changed = True

    def run(self):
        """Query one set of inputs from the joystick and send it out."""
        start_time = derp.util.get_timestamp()
        if not self.recv():
            print("joystick: timed out", start_time)
            self.__connect()
            return True
        if self.controller_changed:
            self.update_controller()
            self.publish(
                "controller",
                isAutonomous=self.is_autonomous,
                speedOffset=self.speed_offset,
                steerOffset=self.steer_offset,
            )
        if self.action_changed:
            self.publish("action",
                         isManual=True,
                         speed=self.speed,
                         steer=self.steer)
        return self.__keep_running
Ejemplo n.º 40
0
class CloudStorageIO(StorageIOSeekable):
    def __init__(self, uri):
        """
        """
        StorageIOSeekable.__init__(self, uri)

        # Path of the temp local file
        self.temp_path = None

        # Stores the temp local FileIO object
        self.__file_io = None

        # Cache the size information
        # TODO: use cached property
        self.__size = None

    @property
    def size(self):
        if not self.__size:
            if self.__file_io:
                return os.fstat(self.__file_io.fileno).st_size
            self.__size = self.get_size()
        return self.__size

    def seek(self, pos, whence=0):
        if self.__file_io:
            self._offset = self.__file_io.seek(pos, whence)
            return self._offset
        return self._seek(pos, whence)

    def tell(self):
        if self.__file_io:
            self._offset = self.__file_io.tell()
        return self._offset

    def local(self):
        """Creates a local copy of the file.
        """
        if not self.__file_io:
            file_obj = self.create_temp_file()
            # Download file if appending or updating
            if self.exists() and ('a' in self.mode or '+' in self.mode):
                self.download(file_obj)
            # Close the temp file and open it with FileIO
            file_obj.close()
            mode = "".join([c for c in self.mode if c in "rw+ax"])
            self.__file_io = FileIO(file_obj.name, mode)
            self.temp_path = file_obj.name
        return self

    def read(self, size=None):
        """Reads the file from the Google Cloud bucket to memory

        Returns: Bytes containing the contents of the file.
        """
        start = self.tell()
        if self.__file_io:
            self.__file_io.seek(start)
            b = self.__file_io.read(size)
        else:
            if not self.exists():
                raise FileNotFoundError("File %s does not exists." % self.uri)
            file_size = self.size
            # TODO: size unknown?
            if not file_size:
                return b""
            if start >= file_size:
                return b""
            end = file_size - 1
            if size:
                end = start + size - 1
            if end > file_size - 1:
                end = file_size - 1
            # logger.debug("Reading from %s to %s" % (start, end))
            b = self.read_bytes(start, end)
        self._offset += len(b)
        return b

    def write(self, b):
        """Writes data into the file.

        Args:
            b: Bytes data

        Returns: The number of bytes written into the file.

        """
        if self.closed:
            raise ValueError("write to closed file %s" % self.uri)
        # Create a temp local file
        self.local()
        # Write data from buffer to file
        self.__file_io.seek(self.tell())
        size = self.__file_io.write(b)
        self._offset += size
        return size

    def __rm_temp(self):
        if self.temp_path and os.path.exists(self.temp_path):
            os.unlink(self.temp_path)
        logger.debug("Deleted temp file %s of %s" % (self.temp_path, self.uri))
        self.temp_path = None
        return

    def open(self, mode='r', *args, **kwargs):
        """Opens the file for writing
        """
        if not self._closed:
            self.close()
        super().open(mode)
        self._closed = False
        # Reset offset position when open
        self.seek(0)
        if 'a' in self.mode:
            # Move to the end of the file if open in appending mode.
            self.seek(0, 2)
        elif 'w' in self.mode:
            # Create empty local file
            self.local()
        return self

    def close(self):
        """Flush and close the file.
        This method has no effect if the file is already closed.
        """

        if self._closed:
            return

        if self.__file_io:
            if not self.__file_io.closed:
                self.__file_io.close()
            self.__file_io = None

        if self.temp_path:
            logger.debug("Uploading file to %s" % self.uri)
            with open(self.temp_path, 'rb') as f:
                self.upload(f)
            # Remove __temp_file if it exists.
            self.__rm_temp()
            # Set _closed attribute
            self._closed = True

    @property
    def updated_time(self):
        raise NotImplementedError()

    def exists(self):
        raise NotImplementedError()

    def get_size(self):
        raise NotImplementedError()

    def delete(self):
        raise NotImplementedError()

    def upload(self, from_file_obj):
        raise NotImplementedError()

    def download(self, to_file_obj):
        """Downloads the data to a file object
        Caution: This method does not call flush()
        """
        raise NotImplementedError()

    def read_bytes(self, start, end):
        """Reads bytes from position start to position end, inclusive
        """
        raise NotImplementedError()
Ejemplo n.º 41
0
 def get_page_size(self, ep0: io.FileIO) -> None:
     buf = AuthPageSizeReport(type=ReportType.get_auth_page_size)
     buf.size_challenge = self.req_size
     buf.size_response = self.resp_size
     ep0.write(buf)  #type: ignore[arg-type]