Ejemplo n.º 1
0
class DownloaderScript(PupyModule):
    """ download a file/directory from a remote system """

    dependencies = {
        'all': [ 'transfer', 'scandir' ],
        'windows': [ 'junctions' ]
    }

    @classmethod
    def init_argparse(cls):
        cls.arg_parser = PupyArgumentParser(prog='download', description=cls.__doc__)
        cls.arg_parser.add_argument('-v', '--verbose', action='store_true', default=False,
                                         help='Be verbose during download')
        cls.arg_parser.add_argument('-a', '--archive', action='store_true', default=False,
                                         help='Store to archive (use this only for dirs)')
        cls.arg_parser.add_argument('-i', '--include', help='Regex to include files')
        cls.arg_parser.add_argument('-e', '--exclude', help='Regex to exclude files')
        cls.arg_parser.add_argument('-F', '--follow-symlinks', action='store_true', help='Follow symlinks')
        cls.arg_parser.add_argument('-I', '--ignore-size', action='store_true', help='Ignore st_size')
        cls.arg_parser.add_argument('-X', '--no-single-device', action='store_false', default=True,
                                     help='Allow to touch another devices (st_rdev)')
        cls.arg_parser.add_argument('-S', '--calculate-size', action='store_true', help='Calculate size only')

        cls.arg_parser.add_argument('remote_file', metavar='<remote_path>', completer=remote_path_completer)
        cls.arg_parser.add_argument('local_file', nargs='?', metavar='<local_path>', completer=path_completer)

    def run(self, args):
        self._downloader = DownloadFronted(
            self.client,
            args.exclude, args.include, args.follow_symlinks, args.ignore_size, args.no_single_device,
            False, self.info if args.verbose else None, self.success, self.error
        )

        if args.calculate_size:
            count, size = self._downloader.du(args.remote_file)
            if count is not None and size is not None:
                self.success('Files: {} Size: {}'.format(count, size_human_readable(size)))
        else:
            self._downloader.download(
                args.remote_file,
                args.local_file,
                args.archive
            )

            if args.verbose:
                self.info('Destination folder: {}'.format(self._downloader.dest_file))


    def interrupt(self):
        if self._downloader:
            self._downloader.interrupt()
Ejemplo n.º 2
0
class DownloaderScript(PupyModule):
    """ download a file/directory from a remote system """

    dependencies = {
        'all': ['transfer', 'scandir', 'zipfile', 'tarfile'],
        'windows': ['junctions']
    }

    @classmethod
    def init_argparse(cls):
        cls.arg_parser = PupyArgumentParser(prog='download',
                                            description=cls.__doc__)
        cls.arg_parser.add_argument('-v',
                                    '--verbose',
                                    action='store_true',
                                    default=False,
                                    help='Be verbose during download')
        cls.arg_parser.add_argument('-O',
                                    '--open',
                                    action='store_true',
                                    default=False,
                                    help='Open file with xdg-open')
        cls.arg_parser.add_argument(
            '-a',
            '--archive',
            action='store_true',
            default=False,
            help='Store to archive (use this only for dirs)')
        cls.arg_parser.add_argument('-C',
                                    '--chunk-size',
                                    type=int,
                                    default=1 * 1024,
                                    help='Max transfer chunk (Kbytes)')
        cls.arg_parser.add_argument('-i',
                                    '--include',
                                    help='Regex to include files')
        cls.arg_parser.add_argument('-e',
                                    '--exclude',
                                    help='Regex to exclude files')
        cls.arg_parser.add_argument('-F',
                                    '--follow-symlinks',
                                    action='store_true',
                                    help='Follow symlinks')
        cls.arg_parser.add_argument('-I',
                                    '--ignore-size',
                                    action='store_true',
                                    help='Ignore st_size')
        cls.arg_parser.add_argument(
            '-X',
            '--no-single-device',
            action='store_false',
            default=True,
            help='Allow to touch another devices (st_rdev)')
        cls.arg_parser.add_argument('-S',
                                    '--calculate-size',
                                    action='store_true',
                                    help='Calculate size only')

        cls.arg_parser.add_argument('remote_file',
                                    metavar='<remote_path>',
                                    completer=remote_path_completer)
        cls.arg_parser.add_argument('local_file',
                                    nargs='?',
                                    metavar='<local_path>',
                                    completer=path_completer)

    def run(self, args):
        self._downloader = DownloadFronted(self.client, args.exclude,
                                           args.include, args.follow_symlinks,
                                           args.ignore_size,
                                           args.no_single_device, False,
                                           self.info if args.verbose else None,
                                           self.success, self.error)

        if args.calculate_size:
            obj = self.client.remote('transfer')
            count, size = self._downloader.du(args.remote_file, obj)
            if count is not None and size is not None:
                self.success('Files: {} Size: {}'.format(
                    count, size_human_readable(size)))
        else:
            self._downloader.download(args.remote_file, args.local_file,
                                      args.archive, args.chunk_size * 1024)

            if args.verbose:
                self.info('Destination folder: {}'.format(
                    self._downloader.dest_file))

            if args.open and path.exists(self._downloader.dest_file):
                viewer = self.config.get('default_viewers',
                                         'xdg_open') or 'xdg-open'
                if args.verbose:
                    self.info('Open file {} with {}'.format(
                        self._downloader.dest_file, viewer))
                with open(devnull, 'w') as DEVNULL:
                    Popen([viewer, self._downloader.dest_file],
                          stdin=DEVNULL,
                          stdout=DEVNULL,
                          stderr=DEVNULL)

    def interrupt(self):
        if self._downloader:
            self._downloader.interrupt()