Ejemplo n.º 1
0
def ArchiveInactive(root,edict,rpt=0):
    " archive clients unchanged in last year"
    #get year today
    gatedate=time.time()
    gt1=list(time.localtime(gatedate))
    # back up one year
    gt1[0] -= 1
    gstamp=time.mktime(gt1)   # where I mark from
    busy=BusyBox.BusyBox(root,"Archiving Inactive")
    #make list of clients
    clist=_getinact(root,edict,gstamp,busy,rpt)
    if len(clist) == 0:
        busy.destroy()
        return
    busy.newstatus('')
    text='Found %d inactive clients\nDo you really want to archive them?' %len(clist)
    ans = tkMessageBox.askyesno('Archiving Inactive',text)
    if ans == 0:
        busy.destroy()
        return
    #allow to UNselect some
    ##lst=[]
    ##for cid in clist:
    ##    pass
    #archive those clients
    top=edict['DATAPATH']
    toplen=len(top)+1
    for cid,plist in clist:
        tmppath=edict['ARCHIVES']+'\\'+cid+'.BAK.ZIP'
        dpath=edict['ARCHIVES']+'\\'+cid+'.ZIP'
        nodel=0
        zf=zipfile.ZipFile(tmppath,'w',zipfile.ZIP_DEFLATED)
        for fn in plist:
            aname=fn[toplen:]
            if aname.find('.') < 0:
                aname=aname+'.'   # force append of dot
            zf.write(fn,aname,zipfile.ZIP_DEFLATED)
            busy.newstatus('Archiving '+aname)
        zf.close()
    
        if 1:
            try:
                os.remove(dpath)
            except:
                pass
            os.rename(tmppath,dpath)
            shutil.rmtree(edict['DATAPATH']+'\\'+cid,1)
        try:
            gc.collect()
        except:
            pass
    busy.destroy()
    clients.NewClientIndexNeeded(root,edict,0)  # rebuild on next access
def SelectFile(root, kind, edict, pathn, pcode, title, freeze=1):
    top = BusyBox.BusyBox(root, 'Busy examining %s files...' % kind)
    # find list of all directories in client region
    c, fl = rawlist.rawlist(pathn, pcode)
    top.destroy()
    if c == None or len(c) == 0:
        return (None, 0, 0, 0)
    while 1:
        # make selection box of these with cancel, ok options
        sd = SelDlg(root, title, c, freeze)

        sd.Activate()
        if sd.val == 'OK' and len(sd.ans):
            # now get parts
            fn = None
            for inx, v in enumerate(c):
                if v == sd.ans[0]:
                    fn = fl[inx]
                    break
            return (1, fn, 1, 1)
        else:
            break
    return (None, 0, 0, 0)
Ejemplo n.º 3
0
def SelectArchiveClient(root, protoflag, edict=None, nonew=1):
    "Select a client"
    global localedict
    global _sd, _ans, _val

    if edict:
        localedict = edict
    try:
        hmode = localedict['SETTINGS']['HipSelect']
    except:
        hmode = 'All'

    pathn = localedict['ARCHIVES'] + '\\*.zip'
    if bbox:
        top = BusyBox.BusyBox(root, 'Busy examining client files...')
    else:
        top = review.BusyBox(root, 'Busy examining client files...')

    # find list of all directories in client region
    plist = glob.glob(pathn)
    cl = []
    plist.sort()
    for d in plist:
        try:
            zf = zipfile.ZipFile(d, 'r', zipfile.ZIP_DEFLATED)
        except:
            #print 'no zipfile',d
            continue
        dir, bn = os.path.split(d)
        for cname in ('client.', 'CLIENT.', 'client', 'CLIENT'):
            rd = None
            try:
                rd = zf.read(bn[:-4] + '/' + cname)
            except:
                pass
            if rd:
                break
        zf.close()
        del zf
        if not rd:
            continue
        cdict = IniAid.LoadConfigString(rd)
        if cdict:
            cl.append('%-32s  %s' %
                      (cdict['client.clientid'], HIPAA(cdict, hmode)))
    cl.sort(_igsort)

    top.destroy()

    # make selection box of these with cancel, new client, ok options
    _sd = None
    _ans = ''
    _val = ''

    def _execute(result):
        global _sd, _ans, _val
        _val = result
        _ans = _sd.getcurselection()
        _sd.deactivate()

    def _sayok():
        global _sd, _ans, _val
        _val = 'OK'
        _ans = _sd.getcurselection()
        _sd.deactivate()

    if 1:  #nonew:
        blist = ['OK', 'Cancel']
    else:
        blist = ['OK', 'Cancel', 'Create new client']
    _sd=Pmw.SelectionDialog(root,title='Select client',command=_execute,\
            buttons=blist,\
            scrolledlist_items=cl)
    _sd.component('scrolledlist').configure(dblclickcommand=_sayok)
    _sd.component('scrolledlist').component('listbox').configure(
        width=80, font=edict['FIXFONT'])
    gpos = '=%dx%d+0+%d' % (root.maxw, root.maxh - 90, 25)
    hull = _sd.component('hull')
    hull.wm_geometry(gpos)
    hull.update()
    hull.transient(root)

    def _freeze_window(tp, geom, event, *args):
        if tp.wm_geometry() != geom:
            tp.wm_geometry(geom)

    hull.bind('<Configure>', Command(_freeze_window, hull, hull.wm_geometry()))
    _sd.activate()
    if _val == 'OK' and len(_ans):
        vl = string.split(_ans[0])
        client = {}
        client['client.clientid'] = vl[0].strip()
        return client
    return None
Ejemplo n.º 4
0
def makeCDlist(root, edict, pathn=None, live=1):
    "Create selection list of existing clients, perhaps from *-terminated pathname"
    global localedict

    if root:
        if bbox:
            top = BusyBox.BusyBox(root, 'Busy examining client files...')
        else:
            top = review.BusyBox(root, 'Busy examining client files...')

    # find list of all directories in client region
    if pathn == None:
        pathn = edict['DATAPATH'] + '\\*'  # that single * is IMPORTANT
    plist = glob.glob(pathn)
    cl = []
    for d in plist:
        if not os.path.isdir(d):
            continue
        # now see about study subjects
        ans = win32api.GetFileAttributes(d)
        #print live,ans,d
        if live:
            if ans & 6:  # either FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM
                continue
        else:
            if not (ans & 6):
                continue  # want ONLY those marked
        cl.append(string.lower(os.path.basename(d)))
    cl.sort()
    cdict = {}
    for p in cl:
        # get name fullname protocols
        client = GetClientFile(pathn[:-1] + p)
        if not client:
            continue
        if string.lower(client['client.clientid']) != p:
            #raise DataError,'ClientID mismatch'
            ##print 'mismatched client'
            if live:
                continue
        id = client['client.clientid']
        full = client['client.fullname']
        try:
            guid = client['client.xguid']
        except:
            guid = None
        v = pathn[:-1] + p
        if guid == None:
            # fix things up
            _updateGUIDentry(v + '\\Client.', edict)  #rewrites the client file
            client = GetClientFile(v)
            guid = client.get('client.xguid', 'Unknown')
        plist = []
        for look in vals:
            gl = glob.glob(v + '\\' + look + '\\Stages.*')
            if len(gl):
                plist.append(look)
        cdict[id] = (id, full, guid, plist)
    if root:
        top.destroy()
    try:
        gc.collect(
        )  # garbage collect all those lost fd in ConfigParser and IniAid
    except:
        pass
    return cdict
def refilter(root, fn, edict=None, recompute=0):

    if root:
        bob = BusyBox.BusyBox(root, 'Busy processing raw data')
    ans = rawlist.rawinfo(fn)
    if ans[0] == None:
        if root:
            bob.destroy()  # oops
        return None  # couldn't open
    # must also insert the site and eyes open/close changes
    cbxx = rawlist.rawcb(fn)
    cbl = cbxx[0]
    true_length = ans[0]
    if len(cbl):
        ans = _handle_other_events(ans, cbl)
    # now figure out the periods
    plist = _decode_evlist(ans)

    fobj = sumfile.get_sum_file(fn[:-4] + '.sum')
    evd = sumcalc.make_all_events(fobj)
    get_freqset(edict)

    dofilts.Load_Filter_File(edict)
    dofilts.Load_Raw_File(fn)
    d = {}
    per = 1
    for itm in plist:
        # get the starting sample, and length
        end = itm[1]
        if end == 0:
            end = true_length
        begin = itm[0]
        if recompute:
            start = begin
        else:
            start = end - 30 * 256
            if start < begin:
                start = begin
            if start < 0:
                start = 0
        length = (end - start) / 256.0
        for chan in range(len(itm[2])):
            # compute them
            clamp = _getclamp(evd, chan, start, end)
            move = dofilts.Get_Average_Value(chan,
                                             freqlims,
                                             start,
                                             length,
                                             trueaverage=1,
                                             clamp=clamp,
                                             useartifact=avalue)
            if len(move) == 0:
                continue
            ##print itm,chan,move
            ##print start,length,true
            v = Value()
            v.theta = move[0][0]
            v.alpha = move[0][1]
            v.beta = move[0][2]
            v.hibeta = move[0][3]
            v.values = move[0][:]  # for newer method
            v.period = per
            v.site = itm[2]
            v.eyes = itm[3]
            v.rewards = itm[-1]
            v.freqlims = freqlims
            # following for debug only
            v.tx = move[2]
            v.secs = length
            v.sums = move[3]
            v.count = move[4]
            d['%2d%02d' % (per, chan)] = v
        if len(itm[2]) > 1:
            d['%2dxx' % (per, )] = len(itm[2])
        per += 1

    if root:
        bob.destroy()
    dofilts.Unload_Filter_File()
    return d
Ejemplo n.º 6
0
def main(root=None, edict=None):
    if not root:
        root = Tk()
    if not edict:
        edict = ReadEnvSettings(root)
    busy = BusyBox.BusyBox(root,
                           'Determining amount of backup space needed...')
    c, p, a = get_size_all(edict)
    need = (c + p + a) / 1000000000.0
    busy = None
    tkMessageBox.showinfo(
        'Backup EEGer data',
        'After selecting a destination folder\nsub-folders that are date-stamped will be created:\n  Other-datestamp\n  Setup-datestamp\n  Archive-datestamp\n  Clients-datestamp'
    )

    while 1:
        query = 'I need %.1fGB for new backup \nand NOT in EEGer folder' % need
        dest = getwhere(root, query)
        if dest == '':
            return 4
        herex = os.getcwd()
        here2 = os.path.split(herex)

        maybe = os.path.normpath(dest.upper())
        here = os.path.normpath(here2[0].upper())
        ans = maybe.find(here)
        ##print ans,'here:',here,' maybe:',maybe,here2
        if ans != -1:
            tkMessageBox.showerror(
                'Backup EEGer data',
                'Destination folder CANNOT be within EEGer path')
            continue
        if os.path.exists(dest):
            if not os.path.isdir(dest):
                continue
        else:
            try:
                os.makedirs(dest)
            except:
                continue
        try:
            sv = os.stat(dest)
        except:
            #print dest
            continue
        tup = win32api.GetDiskFreeSpaceEx(dest)
        if tup[0] > (c + p + a):
            break
    ans = tkMessageBox.askyesno(
        'dest is ' + dest,
        'Backup cannot be halted once started \n Really want to continue?')
    if not ans:
        return 9
    busy = BusyBox.BusyBox(root, 'Backing up to ' + dest)
    # now to save version
    vline = open('C:/EEGer/BIN/Econfig', 'r').readline().strip()
    if vline == '':
        return 10

#now copy other stuff there
    busy.newstatus('Misc files')
    if othercopy(dest, edict, vline):
        busy = None
        tkMessageBox.showerror('Backup EEGer data',
                               'Failure copying misc settings/keys,etc.')
        return 3

# ok, now archive clients there
    busy.newstatus('Client files')
    if clientarchive(dest, edict, busy):
        busy = None
        tkMessageBox.showerror('Backup EEGer data', 'Failure client backups')
        return 1
# now copy archives there
    busy.newstatus('Archives')
    if archivecopy(dest, edict):
        busy = None
        tkMessageBox.showerror('Backup EEGer data',
                               'Failure copying archived clients')
        return 2


# report backup made if all ok
    busy = None
    return 0
Ejemplo n.º 7
0
def SyncClients(root,edict,rpt=0):
    if os.path.normpath(edict['RAWPATH']).upper() == r'C:\EEGER\CLIENTS':
        tkMessageBox.showinfo("Sync Clients",'Client path is set to local now anyway')
        return
    busy=BusyBox.BusyBox(root,'Syncing clients')
    rdict=_makefdict(edict['RAWPATH'])
    ldict=_makefdict(r'C:\EEGer\Clients')
    rkeys=rdict.keys()
    lkeys=ldict.keys()
    rkeys.sort()
    needclients=0
    needfiles=0
    for key in lkeys:
        if not key in rkeys:
            # here to copy an entire client
            # copy entire folder tree
            if doit:
                try:
                    busy.newstatus(key)
                    shutil.copytree('C:/EEGer/Clients/'+key,edict['RAWPATH']+'/'+key)
                    needclients += 1
                except:
                    print 'FAILURE copy client: ',key
            else:
                print "copytree  ",'C:/EEGer/Clients/'+key,edict['RAWPATH']
                needclients += 1
            continue
        # here if maybe just some files missing
        rlist,slist,xlist=rdict[key]
        hasrlist,hasslist,hasxlist=ldict[key]
        for cand,has in ((hasrlist,rlist),(hasslist,slist),(hasxlist,xlist)):
            for f in cand:
                if not f in has:
                    folder,proto=f.split('/')[0:2]
                    # copy local to remote  c:\EEGer\Clients\(clientkey)\filenamewithfolder [rawpath]\key\foldername
                    if not os.path.exists(edict['RAWPATH']+'/'+folder+'/'+proto):
                        if doit > 1:
                            try:
                                os.mkdir(edict['RAWPATH']+'/'+folder+'/'+proto)
                            except:
                                pass
                        else:
                            print 'mkdir ',edict['RAWPATH']+'/'+folder+'/'+proto
                    if doit > 1:
                        try:
                            busy.newstatus(f)
                            shutil.copy2('C:/EEGER/Clients/'+f,edict['RAWPATH']+'/'+folder+'/'+proto)
                            needfiles += 1
                        except:
                            print 'FAILURE file copy: ',f
                    else:
                        needfiles += 1
                        print 'copy2 ','C:/EEGER/Clients/'+f,edict['RAWPATH']+'/'+folder+'/'+proto
                
    if needclients:
        clients.NewClientIndexNeeded(root,edict,0)
    busy.destroy()
    
    if needfiles or needclients:
        tkMessageBox.showinfo('Sync Clients','Transfered %d clients and %d separate files'% (needclients,needfiles))
    else:
        tkMessageBox.showinfo('Sync Clients','Nothing needed to be done')
Ejemplo n.º 8
0
def _most_unarchive(root,edict,zf,cd,flst,overwrite=0,rpt=0,title="Unarchiving"):
    busy=BusyBox.BusyBox(root,title)
   #3. If current client does not exist, must create it and retrieve basic files also
    lpath=edict['DATAPATH']+'\\'+cd['client.clientid']
    prefix=cd['client.clientid']+'/'
    if not os.path.exists(lpath):
        os.mkdir(lpath)
    if not os.path.exists(lpath+'/client.'):
        try:
            rd=zf.read(prefix+'client.')
            rdinfo=zf.getinfo(prefix+'client.')
        except:
            rd=zf.read(prefix+'client')
            rdinfo=zf.getinfo(prefix+'client')
            
        _make_file(edict['DATAPATH'],rdinfo,rd)
        notes=prefix+cd['client.clientid']+'.txt'
        try:
            rd=zf.read(notes)
        except:
            rd=None
        if rd:
            rdinfo=zf.getinfo(notes)
            _make_file(edict['DATAPATH'],rdinfo,rd)

    error='No files selected'
   #4. Restore selected files (If a file already exists, ask if overwrite is desired. Remember selection if OVERWRITE ALL or NEVER OVERWRITE is selected.)
    subpath=''
    for item in flst:
        if item[0]:
            continue    # only do files themselves
        if item[1]:
            if error == 'No files selected':
                error=None
            # want to extract THIS item
            nm=prefix+item[3]
            namepieces=nm.split('/')
            directory=edict['DATAPATH']+'\\'+cd['client.clientid']+'\\'+namepieces[1]
            ##print 'dir=',directory,item
            if not os.path.exists(directory):
                os.mkdir(directory)
            if namepieces[1] != subpath:
                subpath=namepieces[1]
                other=zf.namelist()
                part=cd['client.clientid']+'\\'+namepieces[1]
                part=os.path.normpath(part)
                for x in other:
                    if os.path.normpath(x).find(part) < 0:
                        continue
                    ext= os.path.splitext(x)[1]
                    if ext.lower() == '.raw' or ext.lower() == '.sum':
                        continue
                    fn=directory+'/'+os.path.basename(x)
                    exists=os.path.exists(directory+'/'+fn)
                    if overwrite or not exists:
                        if exists:
                            pass # always overwrite for now continue    # just ignore for now
                        gotcha=0
                        fn=x
                        rd=zf.read(fn)
                        busy.newstatus(fn)
                        rdinfo=zf.getinfo(fn)
                        _make_file(edict['DATAPATH'],rdinfo,rd)
            exists=os.path.exists(edict['DATAPATH']+nm)
            if overwrite or not exists:
                # ask if overwrite desired
                if exists:
                    continue  # later - ask
                # not overwrite existing
                rdinfo=zf.getinfo(nm)
                rd=zf.read(nm)
                busy.newstatus(nm)
                _make_file(edict['DATAPATH'],rdinfo,rd)
    clients.NewClientIndexNeeded(root,edict,0)  # rebuild on next access
    try:
        gc.collect()
    except:
        pass
    zf.close()
    busy.destroy()
    return error
Ejemplo n.º 9
0
def Archive(root,edict,rpt=1):
    " handle archive process"
    #1. Select a client from the current (active) client list (I plan on displaying the most recent file activity date)
    cd = clients.SelectClient(root,0,edict,1)
    if cd == None:
        if rpt:
            print 'No client selected'
        return
    #2. Choose files to archive (some or all of each protocol)
    cpath= edict['DATAPATH']+'\\'+cd['client.clientid']
    lst=simpletree.RawSumFilelist(cpath)
    flst=simpletree.SelectedFiles(root,cpath,lst)
    try:
        gc.collect()
    except:
        pass
    if flst == None:
        if rpt:
            print 'No selected files'
        return
    #3. Create archive client (compressed file) if it doesn't exist
    busy=BusyBox.BusyBox(root,'Archiving '+cd['client.clientid'])
    dpath=edict['ARCHIVES']+'\\'+cd['client.clientid']+'.ZIP'
    tmppath=edict['ARCHIVES']+'\\'+str(os.getpid())+'.ZIP'
    arcpath=cd['client.clientid']+'\\'
    if os.path.exists(dpath):
        zfold=zipfile.ZipFile(dpath,'r',zipfile.ZIP_DEFLATED)
        oldflag=1
        oldlist=zfold.namelist()
        copylist=[]
        upperlist=[]
        for v in oldlist:
            # keep list of sum/raw files already there
            u=v.upper()
            if u.find('.RAW') > 0 or u.find('.SUM') > 0:
                copylist.append(v)
                upperlist.append(os.path.normpath(edict['DATAPATH'].upper()+'\\'+u))
    else:
        oldflag=0
    zf=zipfile.ZipFile(tmppath,'w',zipfile.ZIP_DEFLATED)
    #4. Move selected files there
    #5. Make sure that basic client files exist in the archive
        #   add client file
    s1=cpath+'\\client.'
    if os.path.exists(s1):
        zf.write(s1,arcpath+'client.',zipfile.ZIP_DEFLATED)
    else:
        if rpt:
            print 'could not find ',s1
    s1=cpath+'\\'+cd['client.clientid']+'.txt'
    if os.path.exists(s1):
        zf.write(s1,arcpath+cd['client.clientid']+'.txt',zipfile.ZIP_DEFLATED)
    # loop through selected files
    subpath=''
    nodel=0
    for i in range(len(flst)):
        if flst[i][1]:
            if flst[i][0]:
                continue    # all the work is done by selected files!!!
            # copy lastset.ini and stages.ini if exist
            ap=os.path.split(flst[i][3])
            apath=os.path.split(ap[0])[1]
            if apath != subpath:
                subpath=apath

                gotstages=0
                # make up list of all possible stages in src
                stgsx=['lastset.*','stages.*','lastlayout.ini']
                stgs=[]
                for x  in stgsx:
                    stgs.extend(glob.glob(ap[0]+'/'+x))
                #print ap,flst[i][3],stgs
                for fn in stgs:
                    fn=os.path.basename(fn)     # just the name part
                    tpath=arcpath+apath+'/'+fn
                    s1=cpath+'\\'+apath+'\\'+fn
                    if os.path.exists(s1):
                        if fn[:6].lower() == 'stages':
                            gotstages=1
                        zf.write(s1,tpath,zipfile.ZIP_DEFLATED)
                        busy.newstatus(tpath)
                        if oldflag: # must also remove it from list of files to copy
                            try:
                                k=upperlist.index(os.path.normpath(s1.upper()))
                                del copylist[k]
                                del upperlist[k]
                            except:
                                pass
                if gotstages == 0:
                    oops=Pmw.MessageDialog(root,title="Archive Error",message_text="No stages file found during archive.\nPlease contact Customer Support for assistance")
                    oops.activate()
                    nodel=1
                        
            bname=os.path.basename(flst[i][3])
            tpath=arcpath+apath+'\\'+bname
            zf.write(flst[i][3],tpath,zipfile.ZIP_DEFLATED)
            if oldflag:
                look=flst[i][3].upper()
                looker=os.path.normpath(look)
                try:
                    k=upperlist.index(looker)
                    del copylist[k]
                    del upperlist[k]
                except:
                    pass
    if oldflag:
        if len(copylist):
            for v in copylist:
                #print 'copying',v
                of=zfold.read(v)
                ofinfo=zfold.getinfo(v)
                zf.writestr(ofinfo,of)
                busy.newstatus(of)
        zfold.close()
    zf.close()
    if oldflag:
        os.remove(dpath)
    os.rename(tmppath,dpath)
    try:
        gc.collect()
    except:
        pass
    if nodel:
        busy.destroy()
        return      #don't delete anything if error!
    #6. If all files were selected, delete current client entries after archive
    # loop through selected files
    allsel=1
    for i in range(len(flst)):
        if flst[i][0]:
            # here for special subdir files
            if flst[i][1]:
                # delete entire protocol set
                downem=1
                spath=flst[i][3]+'\\*'
                xtra=glob.glob(spath)
                for fn in xtra:
                    try:
                        os.remove(fn)
                        busy.newstatus('removing '+fn)
                    except:
                        if rpt:
                            print 'unable to delete ',fn
                        downem=0
                        allsel=0
                if downem:
                    try:
                        os.rmdir(flst[i][3])
                        clients.NewClientIndexNeeded(None,edict,0)  # rebuild on next access
                    except:
                        allsel=0
                        if rpt:
                            print 'error removing dir',flst[i][3]
            else:
                allsel=0
        else:
            if flst[i][1]:
                if os.path.exists(flst[i][3]):
                    try:
                        os.remove(flst[i][3])
                        busy.newstatus('removing '+fn)
                    except:
                        allsel=0
                        if rpt:
                            print 'error removing ',flst[i][3]
    if allsel:
        try:
            os.remove(cpath+'\\client.')
        except:
            allsel=0
            if rpt:
                print 'error removing ',cpath+'\\client.'
        if os.path.exists(cpath+'\\'+cd['client.clientid']+'.txt'):
            try:
                os.remove(cpath+'\\'+cd['client.clientid']+'.txt')
            except:
                allsel=0
                if rpt:
                    print 'error removing notes file'
    if allsel:
        try:
            os.rmdir(cpath)
        except:
            pass
    busy.destroy()