コード例 #1
0
def html_index(remotepath, localdir, recursive=False, topdir=False, verbose=False):
    """Generate a HTML index of the remote path in the local directory.

    Returns the sum of the file sizes in the directory.
    """

    if not os.path.isdir(localdir):
        raise IOError("No such directory.")

    if verbose:
        print_("Creating index for %s..."%(remotepath,))

    with temp_dir() as tempdir:
        index_name = os.path.join(tempdir, "index.html")
        size = 0
        maxsize = 1
        with open(index_name, 'wt') as f:
            f.write("<!DOCTYPE html><html><head><title>%s</title></head><body><h3>%s</h3><table>\n"%(remotepath,remotepath))
            f.write("<tr><th>size</th><th>modified</th><th>name</th></tr>\n")
            if topdir:
                # link to dir one level up
                f.write("<tr><td style=\"text-align:right;\">-</td><td>-</td><td><a href=\"../index.html\">../</a></td></tr>\n")
            for entry in dm.ls(remotepath):
                path = posixpath.join(remotepath, entry.name)
                if dm.is_dir(path):
                    if recursive:
                        newdir = os.path.join(localdir, entry.name)
                        try:
                            os.mkdir(newdir)
                        except OSError:
                            # directory probably exists
                            pass
                        sub_size = html_index(path, newdir, recursive=True, topdir=True, verbose=verbose)
                        f.write("<tr><td style=\"text-align:right;%s\">%s</td><td>%s</td><td><a href=\"%s/index.html\">%s/</a></td></tr>\n"%(_bgstyle(sub_size), _format_number(sub_size), entry.modified, entry.name, entry.name))
                        size += sub_size
                        maxsize = max(maxsize, sub_size)
                    else:
                        f.write("<tr><td style=\"text-align:right;\">%s</td><td>%s</td><td>%s/</td></tr>\n"%(_format_number(entry.size), entry.modified, entry.name))
                else:
                    # Not a dir
                    f.write("<tr><td style=\"text-align:right;%s\">%s</td><td>%s</td><td>%s</td></tr>\n"%(_bgstyle(entry.size), _format_number(entry.size), entry.modified, entry.name))
                    if entry.size > 0:
                        size += entry.size
                        maxsize = max(maxsize, entry.size)
            f.write("</table><p>Total size: %s</p><style>:root{--maxsize: %d} td,th{padding-left:3pt; padding-right:3pt;}</style></body></html>\n"%(_format_number(size),maxsize))

        # Move file over
        sh.mv(index_name, localdir, _tty_out=False)

    return size
コード例 #2
0
def remote_iter_recursively(remotepath, regex=None):
    """Iter over remote paths recursively.

    If `regex` is given, only consider files/folders that match the reular expression.
    """

    if isinstance(regex, str):
        regex = re.compile(regex)

    if t2kdm.is_dir(remotepath):
        for entry in t2kdm.ls(remotepath):
            if regex is None or regex.search(entry.name):
                new_path = posixpath.join(remotepath, entry.name)
                for path in remote_iter_recursively(new_path, regex):
                    yield path
    else:
        yield remotepath
コード例 #3
0
ファイル: interactive.py プロジェクト: mlawe/t2kdm
def ls(*args, **kwargs):
    """Print the contents of a directory on screen."""

    long = kwargs.pop('long', False)
    entries = t2kdm.ls(*args, **kwargs)
    if long:
        # Detailed listing
        for e in entries:
            print_(
                "{mode:<11} {links:4d} {uid:5} {gid:5} {size:13d} {modified:>12} {name}"
                .format(name=e.name,
                        mode=e.mode,
                        links=e.links,
                        uid=e.uid,
                        gid=e.gid,
                        size=e.size,
                        modified=e.modified))
    else:
        # Just the names
        for e in entries:
            print_(e.name)
    return 0
コード例 #4
0
ファイル: commands.py プロジェクト: t2k-software/t2kdm
    def _condition_argument(name, value, localdir=None, remotedir=None):
        """Apply some processing to the arguments when needed."""

        # Make local paths absolute
        if localdir is not None and 'localpath' in name and not path.isabs(
                value):
            value = path.normpath(path.join(localdir, value))

        # Make remote paths absolute
        if remotedir is not None and 'remotepath' in name and not posixpath.isabs(
                value):
            value = posixpath.normpath(posixpath.join(remotedir, value))

        # Special case: remote basename == @
        if 'remotepath' in name:
            # Get all entries in the directory and replace the @ with the (lexigraphically) last one
            dirname, basename = posixpath.split(value)
            if basename == '@':
                entries = [x.name for x in dm.ls(dirname)]
                entries.sort()
                value = posixpath.join(dirname, entries[-1])

        return value
コード例 #5
0
def ls(*args, **kwargs):
    return [x.name for x in t2kdm.ls(*args, cached=True, **kwargs)]