#!/usr/bin/env python # pylint: disable=too-many-statements, line-too-long, W0703 import os import sys import time def _get_date_size(filename): """return file date and size""" stat_result = os.stat(filename) date = time.strftime('%d-%b-%Y %H:%M', time.localtime(stat_result.st_mtime)) size = stat_result.st_size for unit in ['', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB']: if abs(size) < 1024.0: break size /= 1024.0 return date, '%d%s' % (int(size), unit) def index(): """generate index.html for current folder""" with open(os.path.splitext(os.path.abspath(__file__))[0]+'.html', 'rb') as file: autoindex_html = file.read().decode('utf-8') for root, dirs, files in os.walk(u'.', topdown=True, followlinks=True): html = u'
\n' % root[1:].strip('\\/') if os.path.basename(root).startswith('@'): continue for name in sorted(dirs): if name.startswith(('.', '@')): continue html += u'{0}/ {1} {2}\n'.format(name, *_get_date_size(os.path.join(root, name))) for name in sorted(files): if name.startswith('.') or name in ('index.html', 'autoindex.py', 'autoindex.html'): continue html += u'{0} {1} {2}\n'.format(name, *_get_date_size(os.path.join(root, name))) html += '