Skip to content

Instantly share code, notes, and snippets.

@hurie
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save hurie/8455c880b645bdb940e8 to your computer and use it in GitHub Desktop.

Select an option

Save hurie/8455c880b645bdb940e8 to your computer and use it in GitHub Desktop.

Revisions

  1. hurie revised this gist Aug 1, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions update-cache.py
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@ def generate_index(cache, index):

    files = []

    for file in path.rglob('*'):
    if file.is_dir() or file.name in ['desktop.ini', 'index.html'] or file.parent == cache:
    for file in path.glob('*/*'):
    if file.parent == cache:
    continue

    print(file)
    @@ -114,7 +114,6 @@ def main():
    if index.parent != cache.parent:
    error(cfg_file)

    print(sys.argv)
    if move_cache(cache) or len(sys.argv) > 1:
    generate_index(cache, index)

  2. hurie created this gist Aug 1, 2014.
    123 changes: 123 additions & 0 deletions update-cache.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    #!/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.rglob('*'):
    if file.is_dir() or file.name in ['desktop.ini', 'index.html'] or 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)

    print(sys.argv)
    if move_cache(cache) or len(sys.argv) > 1:
    generate_index(cache, index)


    if __name__ == '__main__':
    main()