Skip to content

Instantly share code, notes, and snippets.

@congdonglinux
Forked from jkjuopperi/proxycache.py
Created December 12, 2016 16:02
Show Gist options
  • Save congdonglinux/abe8fe8268d5ec8e038baa0f8344ac36 to your computer and use it in GitHub Desktop.
Save congdonglinux/abe8fe8268d5ec8e038baa0f8344ac36 to your computer and use it in GitHub Desktop.

Revisions

  1. @jkjuopperi jkjuopperi created this gist Oct 16, 2014.
    41 changes: 41 additions & 0 deletions proxycache.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/usr/bin/env python
    from __future__ import print_function
    import os
    import sys
    from collections import namedtuple
    CacheFile = namedtuple('CacheFile', ['path', 'key'])
    def read_cache_file(path):
    with open(path, 'r') as f:
    f.seek(0x28) # skip header
    key_magic = f.read(6)
    if key_magic != '\nKEY: ':
    raise ValueError('Not a cache file: ' + str(path))
    key = f.readline()[:-1]
    return CacheFile(path, key)

    def read_cache_dirs(dirs):
    cacheFiles = []
    for cachedir in dirs:
    for root, dirs, files in os.walk(cachedir):
    for f in files:
    try:
    cacheFiles.append(read_cache_file(os.path.join(root, f)))
    except ValueError as e:
    print('Problem:', str(e), file=sys.stderr)
    return cacheFiles

    if __name__ == '__main__':
    command = None
    if len(sys.argv) >= 3:
    command = sys.argv[1]
    if command == 'list':
    cacheFiles = read_cache_dirs(sys.argv[2:])
    for cf in cacheFiles:
    print(cf.path, cf.key)
    elif command == 'purge':
    cacheFiles = read_cache_dirs(sys.argv[2:])
    for f in cacheFiles:
    os.remove(f.path)
    print('Purged', len(cacheFiles), "files")
    else:
    print(sys.argv[0], "<list|purge>", "<cache_dir>", "[cache_dir...]")