Exemplo n.º 1
0
 def encrypt(self, file_list, control):
     if file_list:
         print('\n')
         
     for name in file_list:
         v = self.files[name]
         
         # Log it:
         if not name in self.files_remote:
             self.files_remote[name] = True
         # If --size-control, GPG nothing:
         if not control:
             # GPG it:
             fgpg  = '{0}.gpg'.format(v.hash_local)
             lfile = '{0}/data/{1}'.format(self.tmpdir, fgpg)
             
             # Only GPG if not GPGed yet:
             if not os.path.isfile(lfile):
                 if self.options.verbosity < 2:
                     string = '\033[32m[GPG]\033[0m {0}'.format(core.fitit(name))
                     print(string)
                 cmnd = '{0.gpgcom} -o {1} '.format(self, lfile)
                 for recipient in self.cfg.prefs['RECIPIENTS']:
                     cmnd += ' -r {0} '.format(recipient)
                 cmnd += ' -e "{0}" '.format(v.fullname())
                 #fmt = '{0} -r {1} -o "{2}" -e "{3}"'
                 #cmnd = fmt.format(self.gpgcom, self.cfg.prefs['RECIPIENT'], lfile, v.fullname())
                 self.doit(cmnd,2)
Exemplo n.º 2
0
    def say_nuke_remote(self):
        '''Print corresponding message for each file we are deleting
        from remote, with nuke_remote().'''

        if self.diff.remote:
            print('\n')
            for name in self.diff.remote:
                print('\033[31m[DEL]\033[0m {0}'.format(core.fitit(name)))
Exemplo n.º 3
0
    def enumerate(self,summary=True):
        if self.options.up:
          if not self.options.safe:
              self.say_nuke_remote()

          if self.options.size_control:
              if self.diff.local:
                  print('\n')

                  for name in self.diff.local:
                    print('\033[32m[FKUP]\033[0m {0}'.format(core.fitit(name)))
              if self.diff.newlocal:
                  print('')
              
                  for name in self.diff.newlocal:
                    print('\033[33m[FKSY]\033[0m {0}'.format(core.fitit(name)))
          else:
              if self.diff.local:
                  print('\n')

                  for name in self.diff.local:
                    print('\033[32m[UP]\033[0m {0}'.format(core.fitit(name)))
              if self.diff.newlocal:
                  print('')
              
                  for name in self.diff.newlocal:
                      print('\033[33m[SYNC]\033[0m {0}'.format(core.fitit(name)))
        else:
            if not self.options.safe:
                self.say_nuke_local()
            if self.diff.remote:
                print('\n')
                for name in self.diff.remote:
                    print('\033[32m[DOWN]\033[0m {0}'.format(core.fitit(name)))
            if self.diff.newremote:
                print('\n')
                for name in self.diff.newremote:
                    print('\033[33m[SYNC]\033[0m {0}'.format(core.fitit(name)))
        if summary:
            self.summary()
Exemplo n.º 4
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)))
Exemplo n.º 5
0
 def say_nuke_local(self):
     if self.diff.local:
         print('\n')
         for name in self.diff.local:
             print('\033[31m[DEL]\033[0m {0}'.format(core.fitit(name)))
Exemplo n.º 6
0
    def download(self):
        '''Execute the downloading of remote files not in local, or
        superceding the ones in local.'''

        # List of file(-hashe)s to download:
        lista = []
        for h in self.diff.remote_hash:
            lista.append(h)
        for h in self.diff.newremote_hash:
            lista.append(h)

        # Check which ones present remotely:
        dir = self.cfg.conf['REPODIR']
        server = self.cfg.prefs['REMOTE']
        newlist = core.get_present_files(server, dir, lista)

        # Proceed only if some or all are present:
        if newlist:
            # Build list of files to dl from repo (defined by md5 hash):
            tmpfile = '{0}/filelist.txt'.format(self.tmpdir)
            with open(tmpfile,'w') as f:
                for h in newlist:
                    f.write(h+'.gpg\n')
            # Download all of them from repo to tmpdir:
            fmt = '{0.rsync} -vh --progress {0.cfg.prefs[REMOTE]}/{0.cfg.conf[REPODIR]}/data/ --files-from={1}'
            fmt += ' {0.tmpdir}/data/'
            cmnd = fmt.format(self,tmpfile)
            try:
                self.doit(cmnd,2)
                os.unlink(tmpfile)
            except:
                return False

        # List of file names of files we just downloaded (or tried to):
        file_list = []

        for fn in self.diff.remote:
            file_list.append(fn)

        for fn in self.diff.newremote:
            file_list.append(fn)

        # Un-GPG from tmpdir dir to final destination in local:
        if file_list:
            print('\n')

        for fn in file_list:
            file = self.files[fn]
            fgpg = '{0}.gpg'.format(file.hash_remote)

            # Source GPG file:
            fn = '{0}/data/{1}'.format(self.tmpdir, fgpg)

            if os.path.exists(fn):
                # Create local dir to accomodate file, if necessary:
                dir_to = file.fullname().split('/')[:-1]
                dir_to = '/'.join(dir_to)
                if not os.path.isdir(dir_to):
                    os.makedirs(dir_to)
                # First un-GPG it to tmp dir:
                cmnd = '{0} -o "{1}/tmp" -d "{2}"'.format(self.gpgcom, self.tmpdir, fn)
                self.doit(cmnd,2)

                # Then check if not corrupted:
                ref = file.hash_remote
                act = core.hashof('{0}/tmp'.format(self.tmpdir))
                
                if ref == act: # then it is OK. Proceed:
                    # Warn of what is being done:
                    print('\033[32m[DOWN]\033[0m {0}'.format(core.fitit(file.name)))

                    # Move tmp file into actual destination:
                    cmnd = 'mv -f "{0}/tmp" "{1}"'.format(self.tmpdir,file.fullname())
                    self.doit(cmnd)

                    # Log changes:
                    file.hash_local  = file.hash_remote
                    file.size_local  = file.size_remote
                    file.mtime_local = file.mtime_remote

                    # Touch file accordingly:
                    os.utime(file.fullname(),(-1,file.mtime_remote))
                    
                else:
                    msg  = '\033[31m[NOOK]\033[0m {0}\n'.format(file.name)
                    msg += '\033[33m[IGNO]\033[0m {0}'.format(file.name)
                    print(msg)

            else:
                # Then file was not physically in repo:
                print('\033[31m[MISS]\033[0m %s' % (file.name))
                del self.files_remote[file.name]

        # If all went OK, return True:
        return True