Example #1
0
    def spool_source_data_nt(self, outfn, spConf, payload):
        (shard_name, from_pld, to_pld) = payload

        outf = open(outfn, "w")
        #print self.cols_from
        if self.args.nls_timestamp_format:
            os.environ['NLS_TIMESTAMP_FORMAT'] = self.args.nls_timestamp_format
        else:
            os.environ['NLS_TIMESTAMP_FORMAT'] = ''
        if self.args.nls_timestamp_tz_format:
            os.environ[
                'NLS_TIMESTAMP_TZ_FORMAT'] = self.args.nls_timestamp_tz_format
        else:
            os.environ['NLS_TIMESTAMP_TZ_FORMAT'] = ''
        #print outfn
        #pprint(spConf)
        #e(0)
        p = Popen(spConf, stdout=outf)  # '-S',  stdin=p1.stdout,

        output, err = p.communicate()
        #print output, err

        if err:
            self.log.err(err)
            #print output
        status = p.wait()
        outf.close()
        #e(0)
        count = -1

        if 1:
            #print 'spooled %d B' % stat
            import io
            #file = open(outfn,  mode='r+')
            file = io.open(outfn, 'r+', encoding='utf-8')

            file.seek(0, os.SEEK_END)
            pos = file.tell() - 3
            char = file.read(1)
            eol = char != "\n"
            nums = [str(i) for i in range(0, 10)]
            #print nums
            cnt = []
            out = []
            while pos > 0 and eol:
                #pprint(file.read(1))
                pos -= 1
                file.seek(pos, os.SEEK_SET)
                char = file.read(1)
                eol = char != "\n"
                #pprint(char)
                #print 1,pos

            char = file.read(1)
            eol = char != "\n"
            while pos > 0 and eol:
                #pprint(file.read(1))
                pos -= 1
                file.seek(pos, os.SEEK_SET)
                char = file.read(1)
                eol = char != "\n"
                #pprint(char)
                #print 2,pos
            char = file.read(1)
            eol = char != "\n"
            while pos > 0 and eol:

                pos -= 1
                file.seek(pos, os.SEEK_SET)
                char = file.read(1)
                eol = char != "\n"
                #pprint(char)
                if char in nums:
                    cnt.append(char)
                else:
                    out.append(char)
                #print 3,pos, char, ord(char)
            #print 'last',pos
            #print 			 pos
            if pos > 2:
                pos -= 1
                file.seek(pos, os.SEEK_SET)
                #pprint(file.read(1))
                #print 'truncate'
                file.truncate()
                if cnt:
                    count = int(''.join(cnt[::-1]))
                else:
                    last_line = ''.join(out[::-1])
                    #print last line
                    print ':::', last_line
                    raise RowCountError(last_line)
            else:

                #self.log.error('Cannot get row count.')
                last_line = ''.join(out[::-1])
                #print last line
                tail = open(outfn).read(4000).strip()
                if 'no rows selected' in tail:
                    print tail
                    return (0, status)
                else:
                    raise RowCountError(last_line)

            file.close()

        return (count, status)
Example #2
0
    def read_last_line(self, outfn):
        count = -1
        #stat=os.stat(outfn).st_size
        #print stat
        if 1:
            #print 'spooled %d B' % stat
            import io
            #file = open(outfn,  mode='r+')
            file = io.open(outfn, 'r+', encoding='utf-8')

            file.seek(0, os.SEEK_END)
            pos = file.tell() - 3
            #print pos
            char = file.read(1)
            #pprint (char)
            eol = char != "\n"
            nums = [str(i) for i in range(0, 10)]
            #print nums
            cnt = []
            out = []
            while pos > 0 and eol:
                #pprint(file.read(1))
                pos -= 1
                file.seek(pos, os.SEEK_SET)
                char = file.read(1)
                eol = char != "\n"
                #pprint(char)
                #print 1,pos
                if char in nums:
                    cnt.append(char)
                else:
                    out.append(char)
            if 0:
                char = file.read(1)
                eol = char != "\n"
                while pos > 0 and eol:
                    #pprint(file.read(1))
                    pos -= 1
                    file.seek(pos, os.SEEK_SET)
                    char = file.read(1)
                    eol = char != "\n"
                    #pprint(char)
                    #print 2,pos
                char = file.read(1)
            if 0:
                eol = char != "\n"
                while pos > 0 and eol:

                    pos -= 1
                    file.seek(pos, os.SEEK_SET)
                    char = file.read(1)
                    eol = char != "\n"
                    #pprint(char)
                    if char in nums:
                        cnt.append(char)
                    else:
                        out.append(char)
                    #print 3,pos, char, ord(char)
                #print 'last',pos
            if pos > 2:
                pos -= 2
                file.seek(pos, os.SEEK_SET)
                #pprint(file.read(1))
                #print 'truncate'
                file.truncate()

            else:

                self.log.error('Cannot get spool row count.')
                last_line = ''.join(out[::-1])
                #print lastline
                file.close()
                raise RowCountError(last_line)

            file.close()
            #print ord('0'),  ord('9')

            #stat=os.stat(outfn).st_size

        return (cnt, out)