Exemplo n.º 1
0
def main(args):
    if args.command == 'init':
        print(f'Initialize DFS root at {args.dir}')
        if args.force:
            if os.path.isdir(args.dir):
                shutil.rmtree(args.dir)
            elif os.path.isfile(args.dir):
                os.remove(args.dir)
            os.mkdir(args.dir)
        else:
            raise NotImplementedError()
        with open('/tmp/.dfsroot', 'w') as file:
            root = os.path.abspath(args.dir)
            file.write(root)
    else:
        root = open('/tmp/.dfsroot', 'r').readline()

        if args.command == 'ls':
            abs_path = full_path(args.dir, root)
            rel_path = abs_path[len(root) + 1:]
            local_files = list_local_files(rel_path, abs_path)
            server_files = list_files(rel_path)

            for p in local_files:
                if p not in server_files:
                    local_files[p]['status'] = 'new'
                elif local_files[p]['hash'] != server_files[p]['hash']:
                    local_files[p]['status'] = 'updated'
                    local_files[p]['modified'] = server_files[p]['modified']
                else:
                    local_files[p]['status'] = 'unchanged'
                    local_files[p]['modified'] = server_files[p]['modified']
            for p in server_files:
                if p not in local_files:
                    local_files[p] = server_files[p]
                    local_files[p]['status'] = 'deleted'

            tasks = [
                ('new', 'green'),
                ('updated', 'yellow'),
                ('unchanged', 'grey'),
                ('deleted', 'red'),
            ]
            for status, color in tasks:
                first = True
                for p in local_files:
                    file = local_files[p]
                    if file['status'] == status:
                        if first:
                            first = False
                            print(f'{status.capitalize()} files:')

                        if 'modified' in file:
                            s = f'\t{p} - {file["hash"]} - {file["file_size"]}MB - {file["modified"]}'
                        else:
                            s = f'\t{p} - {file["hash"]} - {file["file_size"]}MB'

                        cprint(s, color)
        elif args.command == 'push':
            for path in args.path:
                abs_path = full_path(path, root)
                rel_path = abs_path[len(root) + 1:]
                print(f'Uploading {path} ...')

                server_files = list_files(rel_path)
                local_files = list_local_files(rel_path, abs_path)

                for p in local_files:
                    if p in server_files and server_files[p]['hash'] != local_files[p]['hash']:
                        upload_file(p, full_path(p, root, root))
                    elif p not in server_files:
                        upload_file(p, full_path(p, root, root))
                    else:
                        print(f'File {p} is already up-to-date')
                for p in server_files:
                    if p not in local_files:
                        delete_file(p)
        elif args.command == 'pull':
            for path in args.path:
                abs_path = full_path(path, root)
                rel_path = abs_path[len(root) + 1:]
                server_files = list_files(rel_path)
                local_files = list_local_files(rel_path, abs_path)
                for p in server_files:
                    fp = full_path(p, root, root)
                    if p in local_files and local_files[p]['hash'] != server_files[p]['hash'] or p not in local_files:
                        os.makedirs(os.path.dirname(fp), exist_ok=True)
                        download_file(p, fp)
                    else:
                        print(f'File {p} is already up-to-date')
        else:
            raise NotImplementedError(f'Unknown command {args.command}')
Exemplo n.º 2
0
 def download_file(self, fileid, target='workbin', auth=True):
     return api.download_file(fileid, target, auth)
Exemplo n.º 3
0
 def workbin_download_file(self, id, auth=True):
     return api.download_file(id, 'workbin', auth)
Exemplo n.º 4
0
 def community_download_file(id, auth=True):
     return api.download_file(id, 'community', auth)