fixbbbts.py :  » Web-Frameworks » Zope » Zope-2.6.0 » utilities » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » utilities » fixbbbts.py
import sys

__doc__="""Fix BoboPOS time stamps

If a system has a problem with it's clock setting, it may cause
database records to be written with time stamps in the future.  This
causes problems when the clock is fixed or when the data are moved to
a system that doesn't have a broken clock.

The database has a requirement that records should be chronologically
ordered and that times not be in the future.

This copies a database, restamping the times as it goes.

%s [options] file1 file2

Copy file1 to file2 restamping the records.

  options:

    -o offset

       Records that are later offset seconds in the past
       are moved back to offset seconds in the past plus
       some small offset chosen so that times are not the same and
       are chronological.

""" % sys.argv[0]

InvalidFormat='Format Error'
Corrupted='Data Corruption'


def main():
    import getopt, string, struct, time
    file__version__=3.0
    packed_version='SDBMV'+struct.pack(">f",file__version__)

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'o:')
        file1, file2 = args
        offset=86400
        for o, v in opts:
            if o=='-o':
                offset=string.atoi(v)
    except:
        print __doc__
        print "%s: %s" % sys.exc_info()[:2]

    start=time.time()-offset
    next=start+0.001
    input=open(file1,'rb')
    read=input.read
    output=open(file2,'wb')
    write=output.write
    pack=struct.pack
    unpack=struct.unpack

    h=read(len(packed_version))
    if h != packed_version:
        raise InvalidFormat, 'This is not a BoboPOS file'
    write(h)

    pos=len(h)

    while 1:
        h=read(24)
        if not h: break
        if len(h) < 24: raise Corrupted, pos
        oid, prev, t, tlen, plen = unpack(">iidii", h)
        if start is None or t > start:
            t=next
            next=next+0.001
            start=None
        if plen > tlen or tlen < 28: raise Corrupted, pos

        write(pack(">iidii", oid, prev, t, tlen, plen))
        l=tlen-28
        s=8196
        while l > 0:
            if s > l: s=l
            d=read(s)
            if not d: raise Corrupted, pos
            write(d)
            l=l-len(d)
        d=read(4)
        if d != h[16:20]: raise Corrupted, pos
        write(d)
        pos=pos+tlen

if __name__=='__main__': main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.