Beispiel #1
0
def recursive_generator(conn, credentials, path, prefix='', pretty=True):
    'A generator function for the `ls` function when used recursively.'
    maxsize = 10**7 # 10 million files should be sufficient

    try:
        parent = fs.read_directory(conn, credentials, maxsize, path, None)[0]
        # An exception is raised if that is not a directory!

        pretty_path = parent['path']
        if prefix != '' and pretty:
            pretty_path = prefix + name_only(pretty_path)

        yield bold(pretty_path)

        if parent['child_count'] > 0:
            prefix += '     '
            for child in parent['files']:
                child_path = child['path']
                if child['type'] == 'FS_FILE_TYPE_DIRECTORY':
                    for i in recursive_generator(conn, credentials,
                                                 child_path, prefix, pretty):
                        yield i
                else:
                    if pretty:
                        child_path = prefix + name_only(child_path)
                    yield child_path

    except request.RequestError:
        yield red('Unable to access {0}'.format(bold(path)))
Beispiel #2
0
def du(conn, credentials, path, verbose=False):
    'Prints out capacity usage for the directory.'
    response = fs.read_dir_aggregates(conn, credentials, path.remote())[0]
    size = metricize(int(response['total_capacity']), 'B')

    mypath = path.remote()
    if path.complete:
        mypath = path.local()

    if verbose:
        print('{0: >10s}    {1}'.format(bold(size), mypath))

        filecount = metricize(int(response['total_files']))
        dircount = metricize(int(response['total_directories']))
        print('    {0} files, {1} directories'.format(filecount, dircount))
    else:
        print('{0: >10s}    {1}'.format(bold(size), mypath))
Beispiel #3
0
def statline(statname, statvalue, abbrev=''):
    'Returns a single formatted statistics line, suitable for statcol()'
    # It would be easier to just ''.format the dots, but ANSI
    # codes change the effective length of the string, which
    # makes everything weird.
    dots = '.' * (13 - len(statname))
    statname = bold(statname) + dots
    statvalue = '{0:.>9s}'.format(metricize(statvalue, abbrev))

    return statname + statvalue
Beispiel #4
0
def listing_generator(conn, credentials, path, pretty=True):
    'A generator function for the `ls` function'
    maxsize = 10**7 # 10 million files should be sufficient
    parent = fs.read_directory(conn, credentials, maxsize, path, None)[0]
    # An exception is raised if that is not a directory!

    yield bold(parent['path'])
    if parent['child_count'] > 0:
        for child in parent['files']:
            child_path = child['path']
            if pretty:
                child_path = '     ' + name_only(parent['path'], child_path)
            yield child_path
Beispiel #5
0
def who_am_i(conn, credentials, verbose=False):
    '''Prints out the current logged in username'''
    response = request.rest_request(conn, credentials, "GET", "/v1/session/who-am-i")[0]

    if verbose:
        print("{0} (QID {1}, NFS {2}, AD {3}, QGID {4})".format(
            bold(response['name']),
            response['id'],
            response['uid'],
            response['sid'],
            response['primary_group']
            ))
    else:
        print(response['name'])
Beispiel #6
0
def ls(conn, credentials, path, recurse=False):
    'Lists everything in a directory'
    listing = []
    if recurse:
        listing = recursive_generator(conn, credentials, path.remote())
    else:
        listing = listing_generator(conn, credentials, path.remote(), True)

    if path.complete:
        first = True
        for i in listing:
            if first:
                print(bold(path.local()))
                first = False
            else:
                print(i)
    else:
        for i in listing:
            print(i)