Created
October 16, 2014 16:19
-
-
Save jkjuopperi/5cffb6ebfc37a9e3a352 to your computer and use it in GitHub Desktop.
nginx proxy cache listing and purging tool in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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...]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment