Example #1
0
    def load(self, pab, devname):
        logger.info("load devname - %s", devname)
        try:
            url = self.parseDev(devname)
            tmpname = self.http_get(url, pab)

            if (not ti_files.isTiFile(tmpname)
                ) and devname.lower().endswith(basicSuffixes):
                prog_file = BasicFile.load(tmpname)
            else:
                prog_file = ProgramImageFile.load(tmpname)

            filesize = prog_file.getImageSize()
            bytes = prog_file.getImage()
            maxsize = recordNumber(pab)
            if filesize > maxsize:
                logger.debug(
                    "TI buffer too small, only loading %d of %d bytes",
                    maxsize, filesize)
                bytes = bytes[:maxsize]

            self.tipi_io.send([SUCCESS])
            logger.info("LOAD image size %d", filesize)
            self.tipi_io.send(bytes)

        except Exception:
            # I don't think this will work. we need to check for as
            #   many errors as possible up front.
            self.tipi_io.send([EFILERR])
            logger.exception("failed to load file - %s", devname)
Example #2
0
    def handleDelete(self, pab, devname):
        logger.info("Opcode 7 Delete - %s", devname)
        logPab(pab)

        unix_name = tinames.devnameToLocal(devname)
        if unix_name is None:
            self.sendErrorCode(EDVNAME)
            return

        logger.debug("deleting file %s", unix_name)
        try:
            del self.openFiles[unix_name]
        except Exception as e:
            logger.debug(
                "removing open file on delete: file was not open! Good")
        try:
            # check protect flag
            if ti_files.isTiFile(unix_name):
                fh = open(unix_name, "rb")
                header = bytearray(fh.read())[:128]
                if ti_files.isProtected(header):
                    self.sendErrorCode(EWPROT)
                    return
            os.unlink(unix_name)
            self.sendSuccess()
        except Exception as e:
            logger.exception("failed to delete a file")
            self.sendErrorCode(EDEVERR)
Example #3
0
    def load(self, pab, devname):
        logger.info("load devname - %s", devname)
        try:
            tmpname = '/tmp/CF'
            url = self.parseDev(devname)
            cmd = "wget -O {} {}".format(tmpname, url)
            logger.info("cmd: %s", cmd)
            code = os.system(cmd)
            if code != 0:
                raise Exception("error downloading resource")
            if (not ti_files.isTiFile(tmpname)
                ) and devname.lower().endswith(basicSuffixes):
                prog_file = BasicFile.load(tmpname)
            else:
                prog_file = ProgramImageFile.load(tmpname)

            filesize = prog_file.getImageSize()
            bytes = prog_file.getImage()
            maxsize = recordNumber(pab)
            if filesize > maxsize:
                logger.debug(
                    "TI buffer too small, only loading %d of %d bytes",
                    maxsize, filesize)
                bytes = bytes[:maxsize]

            self.tipi_io.send([SUCCESS])
            logger.info("LOAD image size %d", filesize)
            self.tipi_io.send(bytes)

        except Exception:
            # I don't think this will work. we need to check for as
            #   many errors as possible up front.
            self.tipi_io.send([EFILERR])
            logger.exception("failed to load file - %s", devname)
Example #4
0
    def handleDirectOutput(self):
        logger.info("direct output")
        bytes = self.tipi_io.receive()
        unit = bytes[0]
        blocks = bytes[1]
        filename = str(self.tipi_io.receive(), 'latin1').strip()
        bytes = self.tipi_io.receive()
        startblock = bytes[1] + (bytes[0] << 8)
        finfo = bytes[2:]

        logger.info("unit: %d, blocks: %d, filename: %s, startblock %d", unit,
                    blocks, filename, startblock)

        localfilename = self.getLocalName(unit, filename)
        if localfilename is None:
            logger.info("passing request to next device")
            self.tipi_io.send([EDVNAME])
            return True
        if os.path.exists(localfilename) and os.path.isdir(localfilename):
            logger.info("folder with same name exists")
            self.tipi_io.send([EDEVERR])
            return True

        # check protect flag
        if os.path.exists(localfilename) and ti_files.isTiFile(localfilename):
            fh = open(localfilename, "rb")
            header = bytearray(fh.read())[:128]
            # if ti_files.isProtected(header):
            #     self.tipi_io.send([EWPROT])
            #     return True

        startbyte = 128 + (startblock * 256)
        endbyte = startbyte + (blocks * 256)

        if os.path.exists(localfilename) and blocks != 0:
            fbytes = self.getFileBytes(localfilename)
        else:
            raw = bytearray(endbyte - 128)
            header = ti_files.createHeader(0, filename, raw)
            logger.debug("header len %d, raw len %d", len(header), len(raw))
            fbytes = header + raw
            logger.debug("created file bytes: %d", len(fbytes))

        if blocks == 0:
            fbytes[10:16] = finfo[0:6]
            self.saveFile(localfilename, fbytes)

        logger.info("Accepting request")
        self.tipi_io.send([SUCCESS])

        if blocks == 0:
            return True

        blockdata = self.tipi_io.receive()
        fbytes[startbyte:endbyte] = blockdata
        self.saveFile(localfilename, fbytes)

        self.tipi_io.send([SUCCESS])
        return True
Example #5
0
 def fetch(self, url, pab):
     tmpname = self.http_get(url, pab)
     if ti_files.isTiFile(tmpname):
         if recordType(pab) == FIXED:
             return FixedRecordFile.load(tmpname, pab)
         else:
             return VariableRecordFile.load(tmpname, pab)
     else:
         return NativeFile.load(tmpname, pab, url)
Example #6
0
    def handleLoad(self, pab, devname):
        logger.info("Opcode 5 LOAD - %s", devname)
        logPab(pab)
        maxsize = recordNumber(pab)
        unix_name = tinames.devnameToLocal(devname)

        if unix_name is None or not os.path.exists(unix_name):
            logger.info("Passing to other controllers")
            self.sendErrorCode(EDVNAME)
            return
        try:
            if (not ti_files.isTiFile(unix_name)
                ) and unix_name.lower().endswith(basicSuffixes):
                prog_file = BasicFile.load(unix_name)
            elif 'DV' == ti_files.get_file_type(
                    unix_name) and unix_name.lower().endswith(basicSuffixes):
                # if it has a basic suffix, lets try to coerce it
                # from DISPLAY VARIABLE maybe
                prog_file = BasicFile.load(unix_name)
            else:
                prog_file = ProgramImageFile.load(unix_name)

            filesize = prog_file.getImageSize()
            bytes = prog_file.getImage()
            if filesize > maxsize:
                logger.debug(
                    "TI buffer too small, only loading %d of %d bytes",
                    maxsize,
                    filesize,
                )
                bytes = bytes[:maxsize]

            dirname = os.path.dirname(unix_name)
            if tipi_config.get("AUTO") == "on":
                tipidirname = tinames.local2tipi(dirname)
                logger.debug("tmp mapping DSK1 to %s", tipidirname)
                tipi_config.settmp("DSK1_DIR", tipidirname)
            else:
                logger.debug("AUTO mapping not enabled")

            tipifile = os.path.join(dirname, "TIPI")
            if os.path.isfile(tipifile):
                config_records = load_internal(tipifile)
                tipi_config.applyrecords(config_records)

            self.sendSuccess()
            logger.info("LOAD image size %d", filesize)
            self.tipi_io.send(bytes)

        except Exception as e:
            # I don't think this will work. we need to check for as
            #   many errors as possible up front.
            self.sendErrorCode(EFILERR)
            logger.exception("failed to load file - %s", devname)
Example #7
0
 def fetch(self, url, pab):
     tmpname = '/tmp/CF'
     cmd = "wget -O {} {}".format(tmpname, url)
     logger.info("cmd: %s", cmd)
     code = os.system(cmd)
     if code != 0:
         raise Exception("error downloading resource")
     if ti_files.isTiFile(tmpname):
         if recordType(pab) == FIXED:
             return FixedRecordFile.load(tmpname, pab)
         else:
             return VariableRecordFile.load(tmpname, pab)
     else:
         return NativeFile.load(tmpname, pab, url)
Example #8
0
    def __createFileRecord(self, f):
        logger.debug("createFileRecord %s", f)
        fh = None
        try:
            fp = os.path.join(self.localpath, f)
            if os.path.isdir(fp):
                return self.__encodeDirRecord(f, 6, 2, 0)

            if ti_files.isTiFile(fp):
                fh = open(fp, 'rb')

                header = bytearray(fh.read()[:128])

                ft = ti_files.catFileType(header)
                sectors = ti_files.getSectors(header) + 1
                recordlen = ti_files.recordLength(header)
                return self.__encodeDirRecord(f, ft, sectors, recordlen)

            # else it is a native file
            if fp.lower().endswith(NativeFile.dv80suffixes):
                # dis/var
                ft = 2
                recCount = self.__line_count(fp)
                recSize = 80
            else:
                # dis/fix
                ft = 1
                stats = os.stat(fp)
                recCount = stats.st_size / 128 + 1
                recSize = 128
            return self.__encodeDirRecord(f, ft, recCount, recSize)

        except Exception as e:
            traceback.print_exc()
            raise

        finally:
            if fh is not None:
                fh.close()
Example #9
0
    def handleSave(self, pab, devname):
        logger.info("Opcode 6 Save - %s", devname)
        logPab(pab)
        unix_name = tinames.devnameToLocal(devname)
        logger.debug("unix_name for program is %s", unix_name)

        if unix_name == "" or unix_name is None:
            self.sendErrorCode(EDVNAME)
            return

        logger.debug("saving program to %s", unix_name)
        if self.parentExists(unix_name):
            # check protect flag
            if ti_files.isTiFile(unix_name):
                fh = open(unix_name, "rb")
                header = bytearray(fh.read())[:128]
                if ti_files.isProtected(header):
                    self.sendErrorCode(EWPROT)
                    return
            self.sendSuccess()
            fdata = self.tipi_io.receive()
            logger.debug("received program image")
            try:
                if unix_name.lower().endswith(basicSuffixes):
                    prog_file = BasicFile.create(fdata)
                else:
                    prog_file = ProgramImageFile.create(
                        devname, unix_name, fdata)
                logger.debug("created file object")
                prog_file.save(unix_name)

                self.sendSuccess()
            except Exception as e:
                logger.exception("failed to save PROGRAM")
                self.sendErrorCode(EDEVERR)
            return
        self.sendErrorCode(EDEVERR)
Example #10
0
    def handleStatus(self, pab, devname):
        logger.info("Opcode 9 Status - %s", devname)
        logPab(pab)
        statbyte = 0

        unix_name = tinames.devnameToLocal(devname)
        if unix_name is None:
            self.sendErrorCode(EDVNAME)
            return

        if not os.path.exists(unix_name):
            statbyte |= STNOFILE
        else:
            if not os.path.isdir(unix_name):
                open_file = None
                try:
                    open_file = self.openFiles[unix_name]
                except:
                    pass
                if open_file is not None:
                    statbyte = open_file.getStatusByte()
                else:
                    if ti_files.isTiFile(unix_name):
                        fh = open(unix_name, "rb")
                        header = bytearray(fh.read())[:128]
                        if ti_files.isVariable(header):
                            statbyte |= STVARIABLE
                        if ti_files.isProgram(header):
                            statbyte |= STPROGRAM
                        if ti_files.isInternal(header):
                            statbyte |= STINTERNAL
                    else:
                        statbyte = NativeFile.status(unix_name)

        self.tipi_io.send([SUCCESS])
        self.tipi_io.send([statbyte])
Example #11
0
 def __include(self, fp):
     logger.debug("__include %s", fp)
     return os.path.isdir(fp) or ti_files.isTiFile(fp) or os.path.isfile(fp)
Example #12
0
def files(path):
    tipi_subdirs = []
    tipi_files = []

#
# Add a link to go back up a directory:
#
    if len(path):
        rel_path = path
    
        p = rel_path.split('/')
        
        p = p[:-1]
        
        new_path = '/'.join(p)
    
        if len(new_path):
            new_path = '/' + new_path
    
    
        tipi_subdirs.append( { 'icon' : '<div class="tooltip"><a href="/files%s"><img src="/images/dots.png" width=20 border=0 alt="Go to parent directory"></a> &nbsp; <span class="tooltiptext">Return to parent directory</span></div>' % new_path,
                             }
                           )
   
    full_path = tipi_disk_base
    
    if len(path):
        full_path += '/' + path
    
    for item in sorted(os.listdir(full_path)):
        item_path = os.path.join(full_path, item)
        
        if os.path.isdir(item_path):
        
            item_display_path = item
            
            if len(path):
                item = path + '/' + item

            item = '/' + item
        
            tipi_subdirs.append( { 'name'      : item_display_path,
                                   #'type'      : 'subdir',
                                   'icon'   : '<a href="/files%s"><img src="/images/folder_icon.png" width=22 border=0></a>' %item,
                                 }
                               )

        else:
            # Determine file type:
            #
            icon = ''
            type = ''
            edit_link = ''
            
            if ti_files.isTIBasicPrg(item_path):
                icon = '<img src="/images/BASIC.png" width=36>'
                type = 'TI'
                edit_link = '<a href="/edit_basic_file?file=' + path + '/' + item + '&rp=/files/' + path + '">Edit</a>'            
            
            elif ti_files.isTiFile(item_path):
                icon = '<img src="/images/ti_logo_icon.jpg" width=22>'
                type = 'TI'
                
            else:
                icon = '<img src="/images/clearpixel.gif" width=30>'
                type = ' '
            
            
            if item_path.endswith('.b99'):
                edit_link = '<a href="/edit_b99_file?file=' + path + '/' + item + '&rp=/files/' + path + '">Edit</a>'
        
            tipi_files.append( { 'name'      : item,
                                 'size'      : os.stat(item_path).st_size,
                                 # 'date'      : time.ctime(os.path.getmtime(item_path)),
                                 'date'      : time.strftime("%b %d %Y %H:%M:%S", time.gmtime(os.path.getmtime(item_path))),
                                 'icon'      : icon,
                                 'edit_link' : edit_link,
                                 
                                 'type'      : type,
                               #  'full_path' : os.path.join(root, file_name),
                               }
                             )

    tipi_dir_listing = tipi_subdirs
    tipi_dir_listing.extend(tipi_files)

    return render_template('files.html', tipi_dir_listing=tipi_dir_listing, total_files = len(tipi_files), display_path = '/' + path, rp = '/' + path)
Example #13
0
    def handleOpen(self, pab, devname):
        logger.info("Opcode 0 Open - %s", devname)
        logPab(pab)
        localPath = tinames.devnameToLocal(devname)
        if localPath is None:
            logger.info("Passing to other controllers")
            self.sendErrorCode(EDVNAME)
            return

        logger.debug("  local file: " + localPath)
        if mode(pab) == INPUT and not os.path.exists(localPath):
            logger.info("Passing to other controllers")
            self.sendErrorCode(EDVNAME)
            return

        if os.path.isdir(localPath):
            try:
                cat_file = CatalogFile.load(localPath, pab, devname)
                self.sendSuccess()
                self.tipi_io.send([cat_file.getRecordLength()])
                self.openFiles[localPath] = cat_file
                return
            except Exception as e:
                self.sendErrorCode(EOPATTR)
                logger.exception("failed to open dir - %s", devname)
                return

        if os.path.exists(localPath):
            try:
                if ti_files.isTiFile(localPath):
                    if recordType(pab) == FIXED:
                        open_file = FixedRecordFile.load(localPath, pab)
                    else:
                        open_file = VariableRecordFile.load(localPath, pab)
                else:
                    open_file = NativeFile.load(localPath, pab)

                if open_file is None:
                    self.sendErrorCode(EOPATTR)
                    return

                fillInRecordLen = open_file.getRecordLength()

                self.sendSuccess()
                self.tipi_io.send([fillInRecordLen])
                self.openFiles[localPath] = open_file
                return

            except Exception as e:
                self.sendErrorCode(EOPATTR)
                logger.error("failed to open file - %s", devname)
                return

        else:
            if self.parentExists(localPath):
                if recordType(pab) == VARIABLE:
                    open_file = VariableRecordFile.create(
                        devname, localPath, pab)
                else:
                    open_file = FixedRecordFile.create(devname, localPath, pab)
                self.openFiles[localPath] = open_file
                self.sendSuccess()
                self.tipi_io.send([open_file.getRecordLength()])
                return
            else:
                # EDEVERR triggers passing on the pab request to other controllers.
                self.sendErrorCode(EDVNAME)
                return

        self.sendErrorCode(EFILERR)
Example #14
0
    def handleOpen(self, pab, devname):
        logger.info("Opcode 0 Open - %s", devname)
        logPab(pab)
        unix_name = tinames.devnameToLocal(devname)
        if unix_name is None:
            logger.info("(1) Passing to other controllers")
            self.sendErrorCode(EDVNAME)
            return

        logger.debug("  local file: " + unix_name)
        if mode(pab) == INPUT and not os.path.exists(unix_name):
            logger.info("(2) Passing to other controllers")
            self.sendErrorCode(EDVNAME)
            return

        if os.path.isdir(unix_name):
            try:
                if recordLength(pab) == 0 or recordLength(pab) == 146:
                    cat_file = CatalogFileTimestamps.load(
                        unix_name, pab, devname)
                else:
                    cat_file = CatalogFile.load(unix_name, pab, devname)
                self.sendSuccess()
                self.tipi_io.send([cat_file.getRecordLength()])
                self.openFiles[unix_name] = cat_file
                return
            except Exception as e:
                self.sendErrorCode(EOPATTR)
                logger.exception("failed to open dir - %s", devname)
                return

        if os.path.exists(unix_name):
            try:
                if ti_files.isTiFile(unix_name):
                    # check protect flag
                    if mode(pab) != INPUT:
                        fh = open(unix_name, "rb")
                        header = bytearray(fh.read())[:128]
                        if ti_files.isProtected(header):
                            self.sendErrorCode(EWPROT)
                            return

                    if recordType(pab) == FIXED:
                        open_file = FixedRecordFile.load(unix_name, pab)
                    else:
                        open_file = VariableRecordFile.load(unix_name, pab)
                else:
                    open_file = NativeFile.load(unix_name, pab)

                if open_file is None:
                    self.sendErrorCode(EOPATTR)
                    return

                fillInRecordLen = open_file.getRecordLength()

                self.sendSuccess()
                self.tipi_io.send([fillInRecordLen])
                self.openFiles[unix_name] = open_file
                return

            except Exception as e:
                self.sendErrorCode(EOPATTR)
                logger.error("failed to open file - %s", devname)
                return

        else:
            if self.parentExists(unix_name):
                if recordType(pab) == VARIABLE:
                    open_file = VariableRecordFile.create(
                        devname, unix_name, pab)
                else:
                    open_file = FixedRecordFile.create(devname, unix_name, pab)
                self.openFiles[unix_name] = open_file
                self.sendSuccess()
                self.tipi_io.send([open_file.getRecordLength()])
                return
            else:
                # EDEVERR triggers passing on the pab request to other controllers.
                self.sendErrorCode(EDVNAME)
                return

        self.sendErrorCode(EFILERR)