コード例 #1
0
ファイル: phpush.py プロジェクト: gursimar/kvpy-missCall
    def synchronize( self, with_reload = False ):

        # change current directory to Python home
        cwd = os.getcwd()
        if os.path.exists( 'c:/system/apps/python/python.app' ):
            os.chdir( 'c:/system/apps/python' )
        elif os.path.exists( 'e:/system/apps/python/python.app' ):
            os.chdir( 'e:/system/apps/python' )

        # ask for file names, checksums
        try:
            pc_offering = eval(self.server.recv_data())
        except 'Timeout':
            print >> self.server.orig_stdout, 'BT connection timed out.'
            print >> self.server.orig_stdout, 'Are you sure sync demon is running on PC?'
            return
        pc_demand   = eval(self.server.recv_data())

        # check whether some of the files should be retrieved
        for ph_file, pc_file, checksum in pc_offering:
            if checksum != phcomm.file_checksum( ph_file ):
                # checksums differ, get the file
                self.server.send( 'getfile', pc_file )
                # create / overwrite the file
                dirpath = os.path.split( ph_file )[0]
                if not os.path.exists( dirpath ):
                    os.makedirs( dirpath ) 
                self.server.recv_file( ph_file )
                print >> self.server.orig_stdout, 'received', ph_file

                if with_reload:
                    # reload a module if it appears in sys.modules
                    modpath, ext = os.path.splitext( ph_file )
                    modname = os.path.split(modpath)[1].lower()
                    if modname in sys.modules.keys():
                        reload( sys.modules[modname] )
                        print >> self.server.orig_stdout, 'reloaded module: ', modname

        # check which files the pc wants, whether some should be sent
        for targetdir, phone_patterns in pc_demand:
            # force patterns to be a sequence
            if not isinstance(phone_patterns, tuple) and not isinstance(phone_patterns, list):
                phone_patterns = [ phone_patterns ]
            for patt in phone_patterns:
                for fname in glob.glob( patt ):
                    try:
                        # read in the data
                        f = open( fname, 'rb' )
                        data = f.read()
                        f.close()
                        # calculate checksum
                        crc = phcomm.data_checksum( data )
                        # offer the file (with checksum and target filename)
                        print >> self.server.orig_stdout, 'offering', fname
                        self.server.send( 'offerfile %d' % crc,
                                          os.path.join(targetdir, os.path.split(fname)[1]) )
                        # does the pc want the file?
                        if int(self.server.readline()):
                            # yes, the pc wants this file
                            self.server.send_data( data )
                            print >> self.server.orig_stdout, '         SENT.'
                        else:
                            # no, skip this file
                            print >> self.server.orig_stdout, '         NOT sent.'
                        del data # help in memory cleanup
                    except:
                        pass
        print >> self.server.orig_stdout, 'sync done'
        print 'sync done.'

        # restore cwd
        os.chdir( cwd )
コード例 #2
0
    def synchronize( self, reload=False ):
        # read in the config file
        # it should create variables COM_PORT and SYNC_FILES
        #print 'config file', self.owner.sync_config_file
        #execfile( self.owner.sync_config_file, {}, d )
        execfile( 'sync.config', globals(), globals() )
        # chdir to the directory of the config_file (if not already there)
        # so relative paths work
        cwd = os.getcwd()
        try:
            os.chdir( os.path.split( config_file )[0] )
        except:
            pass

        #print 'before sync'
        if reload:
            self.bt.send( 'syncl' )
        else:
            self.bt.send( 'sync' )
        #print 'after sync'

        # create lists of local and corresponding remote files and checksums
        remotefiles, localfiles, checksums = [], [], []
        for dest, srcs in SYNC_FROM_PC:
            # for each destination directory there can be several
            # source directories
            dpath = os.path.normpath( dest )
            if not isinstance(srcs, tuple) and not isinstance(srcs, list):
                # force sources to be a list
                srcs = [ srcs ]
            for src in srcs:
                # for each source location
                for f in glob.glob( src ):
                    # find matching files
                    file = os.path.split(f)[1]
                    # create remote name, local name, checksum
                    remotefiles.append( os.path.join( dpath, file ) )
                    localfiles.append( os.path.normpath( f ) )
                    checksums.append( phcomm.file_checksum( f ) )

        #print 'send upload files', repr(zip(remotefiles, localfiles, checksums))
        # send to phone a list of remote, local, checksum triplets
        self.bt.send_data( repr(zip(remotefiles, localfiles, checksums)) )

        #print 'send SYNC_TO_PC', repr( SYNC_TO_PC )
        # then send the info about the files that you want to download to pc
        self.bt.send_data( repr( SYNC_TO_PC ) )
        #print 'after download files'

        # now the phone loops over the files that it wants to get
        line = self.bt.readline()
        while line.split()[0] == 'getfile':
            filename = self.bt.recv_data()
            print filename
            data = open( filename, 'rb' ).read()
            print 'read data'
            self.bt.send_data( data )
            print 'sent it'
            print >> self.stdout, 'sent file: ' + filename
            line = self.bt.readline()

        # now the phone loops over the files that it wants to send
        while line.split()[0] == 'offerfile':
            crc      = int( line.split()[1] )
            filename = self.bt.recv_data()
            pc_crc   = phcomm.file_checksum( filename )
            if crc == pc_crc:
                # don't need this file
                self.bt.send( '0' )
            else:
                # yes, want this file
                self.bt.send( '1' )
                # create directory if needed
                dirpath = os.path.split( filename )[0]
                if not os.path.exists( dirpath ):
                    os.makedirs( dirpath )
                # read and write the file
                print >> self.stdout, 'getting file: ' + filename
                print 'getting file: ' + filename
                wx.YieldIfNeeded()
                #open( filename, 'wb' ).write( self.bt.recv_data() )
                self.bt.recv_file( filename )
                print >> self.stdout, 'received file: ' + filename
                print 'received file: ' + filename
                wx.YieldIfNeeded()
            line = self.bt.readline()

        # done, the last message is a printout that we indeed are done
        assert  line.split()[0] == 'output'
        output = self.bt.recv_data().rstrip()
        print >> self.stdout, output

        print 'done'

        # restore cwd
        os.chdir( cwd )