Example #1
0
def _getFileInfo(name):
    dv80suffixes = (".txt", ".a99", ".b99", ".bas", ".xb", ".tb")
    basicSuffixes = (".b99", ".bas", ".xb", ".tb")
        
    header = None
    
    with open(name,"rb") as fdata:
        header = bytearray(fdata.read())[:128]

    valid = ti_files.isValid(header)

    isprotected = 0
    icon = "native"
    type = "DIS/FIX 128"
    tiname = tinames.asTiShortName(name)
    size = os.stat(name).st_size

    if valid:
        type = ti_files.flagsToString(header) 
        if type != 'PROGRAM':
            type = type + " " + str(ti_files.recordLength(header))
        isprotected = ti_files.isProtected(header)
        icon = 'tifile'
    elif name.lower().endswith(dv80suffixes):
        type = "DIS/VAR 80"
        if name.lower().endswith(basicSuffixes):
            icon = 'basic'

    if type == 'PROGRAM' and ti_files.isTiBasicPrg(name):
        icon = 'basic'
    if type == 'INT/VAR 254' and ti_files.isTiBasicPrg(name):
        icon = 'basic'

    return (name, icon, type, tiname, size, isprotected)
Example #2
0
 def __init__(self, bytes, pab):
     self.dirty = False
     self.mode = mode(pab)
     self.header = bytes[:128]
     self.recordLength = ti_files.recordLength(self.header)
     self.records = self.__loadRecords(bytes[128:])
     if self.mode == APPEND:
         self.currentRecord = len(self.records)
     else:
         self.currentRecord = 0
Example #3
0
 def __init__(self, bytes, pab):
     self.dirty = False
     self.header = bytes[:128]
     self.mode = mode(pab)
     self.filetype = fileType(pab)
     self.recordLength = ti_files.recordLength(self.header)
     if self.mode == OUTPUT and self.filetype == SEQUENTIAL:
         self.records = []
         self.dirty = True
     else:
         self.records = self.__loadRecords(bytes[128:])
     if self.mode == APPEND:
         self.currentRecord = len(self.records)
     else:
         self.currentRecord = 0
     logger.info("records loaded: %d, currentRecord: %d, recordLength: %d",
                 len(self.records), self.currentRecord, self.recordLength)
Example #4
0
def basicContents(filename):
    logger.debug("fetching BASIC PROGRAM as ascii in %s", filename)
    # We are assuming the test for FIAD isTiFile has already passed.

    prg_tmp_file = '/tmp/' + str(uuid.uuid4()) + '.tmp'
    bas_tmp_file = '/tmp/' + str(uuid.uuid4()) + '.tmp'

    try:
        # strip the FIAD header off to get the raw file xbas99 needs.
        with open(filename, "rb") as tifile:
            with open(prg_tmp_file, "wb") as program:
                bytes = bytearray(tifile.read())
                if ti_files.isProgram(bytes):
                    program.write(bytes[128:])
                elif ti_files.isVariable(bytes) and ti_files.isInternal(
                        bytes) and ti_files.recordLength(bytes) == 254:
                    i = 128
                    limit = len(bytes)
                    while (i < limit):
                        rlen = bytes[i]
                        next = i + rlen + 1
                        program.write(bytes[i:next])
                        i = next
                        if bytes[i] == 0xff:
                            # skip to next 256 byte boundary
                            i = (256 * (((i - 128) / 256) + 1)) + 128
                else:
                    return False

        call([
            '/home/tipi/xdt99/xbas99.py', '-d', prg_tmp_file, '-o',
            bas_tmp_file
        ])

        if ti_files.isTiBasicAscii(bas_tmp_file):
            with open(bas_tmp_file, 'rb') as content_file:
                return content_file.read().decode("latin_1")

    finally:
        if os.path.exists(prg_tmp_file):
            os.unlink(prg_tmp_file)
        if os.path.exists(bas_tmp_file):
            os.unlink(bas_tmp_file)

    return False
Example #5
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()