File: gpuarray-cache

package info (click to toggle)
libgpuarray 0.7.6-13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,176 kB
  • sloc: ansic: 19,235; python: 4,591; makefile: 208; javascript: 71; sh: 15
file content (55 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python

import os
import sys

def clean(max_size, path):
    content = []
    for root, dirs, files in os.walk(path):
        for file in files:
            fpath = os.path.join(root, file)
            st = os.stat(fpath)
            content.append((st.st_atime, st.st_size, fpath))

    content.sort()
    cur_size = 0
    for _, size, path in content:
        cur_size += size
        if cur_size > max_size:
            os.remove(path)


SUFFIXES = {'B': 1, 'K': 1 << 10, 'M': 1 << 20, 'G': 1 << 30, 'T': 1 << 40,
            'P': 1 << 50, 'E': 1 << 60, 'Z': 1 << 70, 'Y': 1 << 80}


def get_size(s):
    i = 0
    s = s.strip()
    if s[-1].upper() in SUFFIXES:
        num = s[:-1]
        suf = s[-1].upper()
    else:
        num = s
        suf = ""
    num = float(num)
    if suf != "":
        mult = SUFFIXES[suf]
    else:
        mult = 1
    return int(num * mult)


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='libgpuarray cache maintenance utility')
    parser.add_argument('-s', '--max_size', help='Set the maximum size for pruning (in bytes with suffixes: K, M, G, ...)')
    args = parser.parse_args()
    path = os.environ.get('GPUARRAY_CACHE_PATH', None)
    if path is None:
        print("You need to set GPUARRAY_CACHE_PATH so that this programs knows which path to clean.")
        sys.exit(1)

    clean(get_size(args.max_size), path)