Example #1
0
   def walk(self):
       '''Perform the acts upon each dir in the dir walk (get mtimes, MD5s, etc).'''
 
       pl = self.cfg.conf['LOCALDIR']
 
       for path, dirs, files in os.walk(pl):
           prs = path.replace(pl+'/','')
 
           # Ignore dif if excluded:
           if not core.find_exc(prs, self.cfg.conf['EXCLUDES']):
               for file in files:
                   self.walked += 1
     
                   fn = os.path.join(path, file)
     
                   # Ignore excluded files and symlinks (in ORs, check first
                   # the cheapest and most probable condition, to speed up):
                   if not os.path.islink(fn) and not core.find_exc(fn, self.cfg.conf['EXCLUDES']):
                       if path == pl: # current path is root path
                           fname = file
                       else: # it's a subdir of root
                           fname = '%s/%s' % (prs, file)
                       if not fname in self.files:
                           self.files[fname] = Fileitem(repos=self)
                       self.files[fname].name  = fname
                       self.files_local[fname] = True
                       
                       mt = int(os.path.getmtime(fn))
                       time_differ = False
                       
                       # Get file mtime:
                       try:
                           rmt = self.files[fname].mtime_read
                           #rmt = long(float(rmt))
                           rmt = float(rmt)
                           
                           if rmt - mt != 0:
                               time_differ = True
                       except:
                           time_differ = True
                       if self.options.force_hash or time_differ:
                           # Calc hash and save data:
                           try:
                               old_hash = self.files[fname].hash_read
                           except:
                               old_hash = -1 # if file is not in "read_files", give it a hash no-one can have
                           new_hash = self.files[fname].get_hash()
                           self.hashed += 1
                           
                           #if old_hash != new_hash: # (avoid mtime-ing unchanged files)
                           if True: # mtime all files, even unchanged ones
                               if self.options.verbosity > 0:
                                   print('[MD5] {0}'.format(core.fitit(fname)))
                               self.files[fname].hash_local = new_hash
                               self.files[fname].get_size()
                               self.files[fname].mtime_local = mt
       
                           else:
                               self.files[fname].hash_local = old_hash
                               self.files[fname].size_local = self.files[fname].size_read
                               self.files[fname].mtime_local = self.files[fname].mtime_read
                       else:
                           # Skip, because it's the same file (relying on mtime here):
                           self.files[fname].hash_local  = self.files[fname].hash_read
                           self.files[fname].size_local  = self.files[fname].size_read
                           self.files[fname].mtime_local = self.files[fname].mtime_read
       
                           if self.options.verbosity > 2: # VERY verbose!
                               print('[SKIP]: {0}'.format(core.fitit(fname)))
Example #2
0
    def compare(self):
        '''Compare local and remote repositories.'''

        # Check in single loop:
        for k,v in self.files.items():
            if v.hash_local: # then file exists locally
                if v.hash_remote: # then file exists remotelly too
                    if v.hash_local != v.hash_remote:
                        # Then files differ, use mtime to decice which
                        # one to keep (newest):
                        lmt = v.mtime_local
                        rmt = v.mtime_remote

                        if lmt < rmt:
                            self.diff.newremote.append(k)
                            self.diff.newremote_hash[v.hash_remote] = k
                            if self.options.verbosity > 0:
                                fmt = '\ndiff: local [{0}] -- remote [\033[32m{1}\033[0m] {2}'
                                print(fmt.format(core.e2d(lmt), core.e2d(rmt), k))

                        elif lmt > rmt or (self.options.up and self.options.force_hash):
                            self.diff.newlocal.append(k)
                            self.diff.newlocal_hash[v.hash_local] = k
                            if self.options.verbosity > 0:
                                fmt = '\ndiff: local [\033[32m{0}\033[0m] -- remote [{1}] {2}'
                                print(fmt.format(core.e2d(lmt), core.e2d(rmt), k))

                        else:
                            fmt = '\033[33m[WARN]\033[0m "{0}" differs, but has same mtime.'
                            print(fmt.format(k))
                            if self.options.update_equals:
                                if self.options.up:
                                    self.diff.newlocal.append(k)
                                    self.diff.newlocal_hash[v.hash_local] = k
                                else:
                                    self.diff.newremote.append(k)
                                    self.diff.newremote_hash[v.hash_remote] = k

                else:  # then file exists only locally
                    self.diff.local.append(k)
                    self.diff.local_hash[v.hash_local] = k

            elif v.hash_remote: # then file exists only remotely
                if not core.find_exc(k, self.cfg.conf['EXCLUDES']): # ignore files matching some rule
                    self.diff.remote.append(k)
                    self.diff.remote_hash[v.hash_remote] = k

        # Print summaries if enough verbosity:
        if self.options.verbosity > 1:
            print("\nLocal:")
            for k,v in self.diff.local_hash.items():
                print(k,v)
            print("Remote:")
            for k,v in self.diff.remote_hash.items():
                print(k,v)

            print("New local:")
            for k,v in self.diff.newlocal_hash.items():
                print(k,v)

            print("New remote:")
            for k,v in self.diff.newremote_hash.items():
                print(k,v)