Ejemplo n.º 1
0
    def get_hash(self):
        '''Calc hash function for Fileitem.'''

        return core.hashof(self.fullname())
Ejemplo n.º 2
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