Beispiel #1
0
def DelActive(root,edict,rpt=0):
    "delete files from active client"
    # select client
    cd = clients.SelectClient(root,0,edict,1)
    if cd == None:
        if rpt:
            print 'No client selected'
        return
    #2. Choose files to delete (some or all of each protocol)
    cpath= edict['DATAPATH']+'\\'+cd['client.clientid']
    lst=simpletree.RawSumFilelist(cpath,None,1)
    flst=simpletree.SelectedFiles(root,cpath,lst)
    if flst == None:
        if rpt:
            print 'No selected files'
        return
    allyes=0
    for item in flst:
        if item[0]:
            continue    # just handle files themselves
        if item[1]:
            delme=0
            if not allyes:
                ans=_ask_about(root,item[2],item[3])
                if ans == 'all':
                    allyes=1
                    delme=1
                elif ans == 'none':
                    break
                elif ans == 'yes':
                    delme=1
            if delme or allyes:
                # here we ACTUALLY delete the file
                #print 'delete ',item[3]
                os.remove(item[3])      
def _get_files(root, edict, rpt=0):
    #1. Select a client from the current (active) client list (I plan on displaying the most recent file activity date)
    while 1:
        cd = clients.SelectClient(root, 0, edict, 1)
        if cd == None:
            if rpt:
                print 'No client selected'
            return ('No client selected', None, None)
        #2. Choose files to archive (some or all of each protocol)
        cpath = edict['DATAPATH'] + '\\' + cd['client.clientid']
        flst = glob.glob(cpath + '\\*.FULL')
        if len(flst) == 0:
            continue

        lst = []
        for fi in flst:
            n = datetime.datetime.fromtimestamp(os.stat(fi)[8])
            name = '%s Full recording (%s)' % (n.ctime(), os.path.basename(fi))
            lst.append([0, 0, name, fi, 0, 0])
        flst = simpletree.SelectedFiles(root, cpath, lst)
        if flst == None:
            if rpt:
                print 'No selected files'
            continue
        return (0, flst, cd)
Beispiel #3
0
def _get_files(root, edict, rpt=0, exclude='RAW'):
    #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 ('No client selected', None, None)
    #2. Choose files to archive (some or all of each protocol)
    cpath = edict['DATAPATH'] + '\\' + cd['client.clientid']
    lst = simpletree.RawSumFilelist(cpath, exclusion=exclude)
    flst = simpletree.SelectedFiles(root, cpath, lst)
    if flst == None:
        if rpt:
            print 'No selected files'
        return ('No selected files', None, None)
    return (0, flst, cd)
Beispiel #4
0
def UnConsult(root,edict,apath,rpt=0):
    zf=zipfile.ZipFile(apath,'r')
    cname=None
    for f in zf.namelist():
        fields=os.path.split(f)
        if fields[-1] == 'client.':
            cname=f
            break
    if not cname:
        zf.close()
        return 'Incorrect file format'
    rd=zf.read(cname)

    cd=IniAid.LoadConfigString(rd)

    lst=simpletree.ArchiveRawSumFilelist(apath)
    flst=simpletree.SelectedFiles(root,apath+' :'+cd['client.clientid'],lst)
    if flst == None:
        return 'No Selected Files'
    return _most_unarchive(root,edict,zf,cd,flst,rpt,title="Importing consultation")
Beispiel #5
0
def UnArchive(root,edict,rpt=0):
    
   #1. Select a client from the ARCHIVE client list
    cd = clients.SelectArchiveClient(root,0,edict)
    if cd == None:
        if rpt:
            print 'No client selected'
        return
   #2. Choose files to unarchive (some or all of each protocol)
    apath= edict['ARCHIVES']+'\\'+cd['client.clientid']+'.zip'
    lst=simpletree.ArchiveRawSumFilelist(apath)
    flst=simpletree.SelectedFiles(root,apath,lst)
    try:
        gc.collect()
    except:
        pass
    if flst == None:
        if rpt:
            print 'No selected files'
        return
    zf=zipfile.ZipFile(apath,'r')
    return _most_unarchive(root,edict,zf,cd,flst,rpt,title="Unarchiving "+cd['client.clientid'])
Beispiel #6
0
def MoveFiles(root,edict,rpt=0):
    "move files from one client/protocol to another"
    cd = clients.SelectClient(root,1,edict,1,'Source client')
    if cd == None:
        if rpt:
            print 'No client selected'
        return
    cpath= edict['DATAPATH']+'\\'+cd['client.clientid']
    lst=simpletree.RawSumFilelist(cpath,cd['pcode'])
    flst=simpletree.SelectedFiles(root,cpath,lst)
    try:
        gc.collect()
    except:
        pass
    if flst == None:
        if rpt:
            print 'No selected files'
        return
    cd2=clients.SelectClient(root,0,edict,1,'Destination client')
    if cd2 == None:
        return
    dest_proto=clients.SelectProtocol(root,'Select class for destination')
    if dest_proto == None:
        return
    if cd['client.clientid'] == cd2['client.clientid'] and cd['pcode'] == dest_proto:
        _dialoghelper('Invalid operation','Sorry, you cannot move to the source class',root=root)
        return
    # things to consider
    #   if I have to create the destination protocol directory
    #       must also copy across lastset.ini and stages.ini
    #   copy file with new 'name' after checking for no conflict 
    #   update header data for protocol and client names
    #   make new file have 'old' date
    spath=edict['DATAPATH']+ '\\'+cd['client.clientid']+'\\'+ cd['pcode']
    dpath=edict['DATAPATH']+'\\'+cd2['client.clientid']+'\\'+dest_proto

    allyes=0
    for item in flst:
        if item[0]:
            continue    # just handle files themselves
        if item[1]:
            delme=0
            if not allyes:
                ans=_ask_about(root,item[2],item[3],dpath)
                if ans == 'all':
                    allyes=1
                    delme=1
                elif ans == 'none':
                    break
                elif ans == 'yes':
                    delme=1
            if delme or allyes:
                # here we ACTUALLY move the file
                if not os.path.exists(dpath):
                    os.mkdir(dpath)
                    stgsx=['lastset.*','stages.*','lastlayout.ini']
                    stgs=[]
                    for x  in stgsx:
                        stgs.extend(glob.glob(spath+'\\'+x))
                    for fnx in stgs:
                        fn=os.path.basename(fnx)
                        filename=spath+'\\'+fn
                        newname=dpath+'\\'+fn
                        if os.path.exists(filename):
                            # must change internal names matching the OLD name to the NEW name
                            if _renameinside(filename,newname,cd,cd2):
                                # if failed or no change needed, use bulk copier
                                shutil.copy(filename,dpath)
                                shutil.copystat(filename,dpath+'\\'+fn)
                destfilename=dpath+'\\'+cd2['client.clientid']+os.path.basename(item[3])[-10:]
                while 1:
                    if not os.path.exists(destfilename):
                        break
                    num=int(destfilename[-6:-4])
                    num = num+1
                    if num >= 100:
                        _dialoghelper('File move failure','Too many files for one day',root=root)
                        destfilename=None
                        break
                    destfilename='%s%02d%s' %(destfilename[:-6],num,destfilename[-4:])
                if destfilename == None:
                    continue    # no place for this guy
                srcfilename=spath+'\\'+os.path.basename(item[3])
                shutil.copy(srcfilename,destfilename)
                # must know if RAW or SUM since headers differ!!!!
                if destfilename.upper()[-4:] == '.RAW':
                    # update raw header
                    rawlist.writeheader(destfilename,cd2['client.clientid'],cd2['client.fullname'],dest_proto)
                else:
                    # update sum header
                    sumfile.writeheader(destfilename,cd2['client.clientid'],cd2['client.fullname'],dest_proto)
                shutil.copystat(srcfilename,destfilename) # move date
                os.remove(srcfilename)
    clients.NewClientIndexNeeded(root,edict,0)
    try:
        gc.collect()
    except:
        pass
Beispiel #7
0
def DelArchived(root,edict,rpt=0):
    "delete files from archived client"
   #1. Select a client from the ARCHIVE client list
    cd = clients.SelectArchiveClient(root,0,edict)
    if cd == None:
        if rpt:
            print 'No client selected'
        return
   #2. Choose files to delete (some or all of each protocol)
    apath= edict['ARCHIVES']+'\\'+cd['client.clientid']+'.zip'
    lst=simpletree.ArchiveRawSumFilelist(apath)
    flst=simpletree.SelectedFiles(root,apath,lst)
    if flst == None:
        if rpt:
            print 'No selected files'
        return
    #3. Create archive client (compressed file) if it doesn't exist
    tmppath=edict['ARCHIVES']+'\\'+str(os.getpid())+'.ZIP'
    arcpath=cd['client.clientid']+'\\'
    zfold=zipfile.ZipFile(apath,'r',zipfile.ZIP_DEFLATED)
    oldlist=zfold.namelist()
    copylist=[]
    upperlist=[]
    for v in oldlist:
        # keep list of files already there
        u=v.upper()
        copylist.append(v)
        upperlist.append(os.path.normpath(u))
    allyes=0
    for item in flst:
        if item[0]:
            continue    # just handle files themselves
        if item[1]:
            delme=0
            if not allyes:
                ans=_ask_about(root,item[2],item[3])
                if ans == 'all':
                    allyes=1
                    delme=1
                elif ans == 'none':
                    break
                elif ans == 'yes':
                    delme=1
            if delme or allyes:
                #print 'delete ',item[3]
                #remove from copylist
                try:
                    k=upperlist.index(os.path.normpath(arcpath+item[3]).upper())
                    del copylist[k]
                    del upperlist[k]
                    #print 'deleted ',item[3]
                except:
                    pass


    zf=zipfile.ZipFile(tmppath,'w',zipfile.ZIP_DEFLATED)
    if len(copylist):
        for v in copylist:
            #print 'copying',v
            of=zfold.read(v)
            ofinfo=zfold.getinfo(v)
            zf.writestr(ofinfo,of)
        zfold.close()
    zf.close()
    os.remove(apath)
    os.rename(tmppath,apath)
Beispiel #8
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()
def smdump(root, interface, fmtlimit=FMTCHECK):
    global edict

    edict = interface['EDICT']
    # get list of clients
    cdict = clients.makeCDlist(root, edict)
    ##print cdict
    cl = []
    cdkeys = cdict.keys()
    cdkeys.sort()
    for cdk in cdkeys:
        # each item is code,full,guid,list_of_protos
        clst = cdict[cdk]
        if EXPLODE:
            for pclass in clst[3]:
                cl.append([
                    clst[0], 0,
                    '%-3s %-32s %s' % (pclass, clst[0], clst[1]), clst[2],
                    [
                        pclass,
                    ], 0
                ])
        else:
            cl.append([
                clst[0], 0,
                '%-32s %s' % (clst[0], clst[1]), clst[2], clst[3], 0
            ])
    # and select the desired ones
    #print cl[0]
    chosen = simpletree.SelectedFiles(root,
                                      'Session Manager Dumper',
                                      cl,
                                      font=('Courier New', '10'))
    ##print chosen
    if chosen == None:
        #tellem so
        return
    #print chosen[0]
    box = BusyBox(root, 'Busy dumping files')
    # for each selected client
    for cdta in chosen:
        if cdta[1] == 0:
            continue

        #    create a zip file
        ##print 'cdta=',cdta
        if EXPLODE:
            dpath = edict['PROGRAMPATH'] + '/BIN/settings/' + cdta[
                0] + '_' + cdta[4][0] + '.zip'
        else:
            dpath = edict['PROGRAMPATH'] + '/BIN/settings/' + cdta[0] + '.zip'
        try:
            zf = zipfile.ZipFile(dpath, 'w', zipfile.ZIP_DEFLATED)
        except:
            tkMessageBox.showerror('Study manager Dump',
                                   'Unable to create zipfile :' + dpath)
            continue

        #    locate live data for sham clients
        if os.path.exists(edict['DATAPATH'] + '/' + cdta[3]):
            dolist = (cdta[0], cdta[3])
            kinda = 'sham'
        else:
            kinda = 'real'
            dolist = (cdta[0], )
        for name in dolist:
            for pclass in cdta[4]:  # for each proto class
                #print name,pclass
                flist = glob.glob(edict['DATAPATH'] + '/' + name + '/' +
                                  pclass + '/' + name + '*')
                flist.sort(reverse=1)
                for inx, f in enumerate(flist):
                    if LIMIT:
                        if inx > LIMIT:
                            break
                    #    for each file
                    #         dump csv
                    if os.path.splitext(f)[1].upper() == '.RAW':
                        box.newstatus('%s (%d of %d)' %
                                      (f, inx + 1, len(flist)))
                        sdata, datetime = smdumpfile(f, fmtlimit)
                        if sdata == None:
                            continue
                        filename = os.path.splitext(os.path.basename(
                            f))[0] + '_' + pclass + '_' + kinda + '.csv'
                        sinst = zipfile.ZipInfo(filename, datetime)
                        zf.writestr(sinst, sdata)
            kinda = 'live'
        zf.close()
        del zf
        #    close zip file
    del box