Пример #1
0
def parse_memory(memory):
    """
    Parses a string representing memory and returns
    an integer # of bytes.

    :param memory:
    :return:
    """
    memory = str(memory)
    if 'None' in memory:
        return 2147483648  # toil's default
    try:
        import re
        raw_mem_split = re.split('([a-zA-Z]+)', memory)
        mem_split = []

        for r in raw_mem_split:
            if r:
                mem_split.append(r.replace(' ', ''))

        if len(mem_split) == 1:
            return int(memory)

        if len(mem_split) == 2:
            num = mem_split[0]
            unit = mem_split[1]
            return int(float(num) * bytes_in_unit(unit))
        else:
            raise RuntimeError(f'Memory parsing failed: {memory}')
    except:
        return 2147483648  # toil's default
Пример #2
0
def size(f: Optional[Union[str, WDLFile, List[Union[str, WDLFile]]]] = None,
         unit: Optional[str] = 'B',
         fileStore: Optional[AbstractFileStore] = None) -> float:
    """
    Given a `File` and a `String` (optional), returns the size of the file in Bytes
    or in the unit specified by the second argument.

    Supported units are KiloByte ("K", "KB"), MegaByte ("M", "MB"), GigaByte
    ("G", "GB"), TeraByte ("T", "TB") (powers of 1000) as well as their binary version
    (https://en.wikipedia.org/wiki/Binary_prefix) "Ki" ("KiB"), "Mi" ("MiB"),
    "Gi" ("GiB"), "Ti" ("TiB") (powers of 1024). Default unit is Bytes ("B").

    WDL syntax: Float size(File, [String])
    Varieties:  Float size(File?, [String])
                Float size(Array[File], [String])
                Float size(Array[File?], [String])
    """

    if f is None:
        return 0

    # it is possible that size() is called directly (e.g.: size('file')) and so it is not treated as a file.
    if isinstance(f, str):
        f = WDLFile(file_path=f)
    elif isinstance(f, list):
        f = [WDLFile(file_path=sf) if isinstance(sf, str) else sf for sf in f]

    assert isinstance(f, (
        WDLFile,
        list)), f'size() excepts a "File" or "File?" argument!  Not: {type(f)}'

    # validate the input. fileStore is only required if the input is not processed.
    f = process_infile(f, fileStore)

    divisor = bytes_in_unit(unit)

    if isinstance(f, list):
        total_size = sum(file.file_path.size for file in f)
        return total_size / divisor

    fileID = f.file_path
    return fileID.size / divisor