Last active
August 29, 2015 14:04
-
-
Save hurie/8455c880b645bdb940e8 to your computer and use it in GitHub Desktop.
Organize PIP download cache
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
#!/python3.4 | |
""" | |
Created on Aug 01, 2014 | |
@author: Azhar | |
""" | |
from configparser import ConfigParser | |
import hashlib | |
import os | |
from pathlib import Path | |
import sys | |
import urllib.parse | |
def generate_index(cache, index): | |
path = cache.parent | |
files = [] | |
for file in path.glob('*/*'): | |
if file.parent == cache: | |
continue | |
print(file) | |
md5 = hashlib.md5(open(str(file), 'rb').read()).hexdigest() | |
files.append('<a href="{0}#md5={3}">{1}/{2}</a><br>'.format(file.as_uri(), file.parent.name, file.name, md5)) | |
with open(str(index), 'w') as f: | |
f.write(''' | |
<html><head></head><body> | |
{} | |
</body></html> | |
'''.format('\n'.join(files))) | |
def cleanup_cache(filename): | |
parent = filename.parent | |
if filename.exits(): | |
filename.unlink() | |
try: | |
parent.rmdir() | |
except: | |
pass | |
def move_cache(cache): | |
source_dir, base_target = cache, cache.parent | |
result = False | |
for source in source_dir.iterdir(): | |
if source.suffix == '.content-type': | |
continue | |
filename = str(source.name) | |
fileurl = urllib.parse.unquote(filename) | |
sourcename = Path(fileurl) | |
target = base_target / sourcename.parent.name / sourcename.name | |
if not target.parent.is_dir(): | |
print('creating directory "{}"'.format(target.parent.relative_to(base_target))) | |
target.parent.mkdir(parents=True) | |
if target.exists(): | |
if target.stat().st_ino == source.stat().st_ino: | |
print('skipping "{}"'.format(target.relative_to(base_target))) | |
continue | |
else: | |
print('removing "{}"'.format(target.relative_to(base_target))) | |
target.unlink() | |
alt_target = base_target / sourcename.parent.name.replace('_', '-') / sourcename.name | |
if alt_target != target and alt_target.exists(): | |
print('removing "{}"'.format(alt_target.relative_to(base_target))) | |
alt_target.unlink() | |
if not list(alt_target.parent.iterdir()): | |
print('removing "{}"'.format(alt_target.parent.relative_to(base_target))) | |
alt_target.parent.rmdir() | |
print('hardlinking "{}" to "{}"'.format(source.relative_to(base_target), target.relative_to(base_target))) | |
os.link(str(source), str(target)) | |
result = True | |
return result | |
def error(cfg): | |
print(''' | |
Invalid value for download-cache and find-links in {} | |
This options should be like: | |
[global] | |
download-cache = ~/.cache/.all | |
find-links = ~/.cache/index.html | |
'''.format(cfg)) | |
sys.exit(1) | |
def main(): | |
if sys.platform == 'win32': | |
cfg_file = Path(os.path.expanduser('~/pip/pip.ini')) | |
else: | |
cfg_file = Path(os.path.expanduser('~/.pip/pip.conf')) | |
cfg = ConfigParser() | |
cfg.read(str(cfg_file)) | |
if not cfg.has_option('global', 'download-cache') or not cfg.has_option('global', 'find-links'): | |
error(cfg_file) | |
index = Path(cfg['global']['find-links']) | |
cache = Path(cfg['global']['download-cache']) | |
if index.parent != cache.parent: | |
error(cfg_file) | |
if move_cache(cache) or len(sys.argv) > 1: | |
generate_index(cache, index) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment