#!/usr/bin/env python # pylint: disable=too-many-statements, line-too-long, W0703 import os import sys import subprocess import time from datetime import datetime startupinfo = None def filecheck(fname): if fname.startswith(('./.git', './.cache')): return None args = ['git', 'log', '-1', '--pretty="%ci"', fname] try: b = subprocess.check_output(args, startupinfo=startupinfo) data = b.decode()[:-1] x = datetime.strptime(data, '\"%Y-%m-%d %H:%M:%S %z\"') f = x.strftime('%d-%b-%Y %H:%M') except : return None return f def _get_date_size(filename): """return file date and size""" stat_result = os.stat(filename) date = filecheck(filename) print(date, filename) 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, open('meta.html', 'rb') as meta: meta = meta.read().decode('utf-8') autoindex_html = file.read().decode('utf-8') for root, dirs, files in os.walk(u'.', topdown=True, followlinks=True): html = u'Index of /%s\n' % root[1:].strip('\\/') html += meta if os.path.basename(root).startswith('@'): continue for name in sorted(dirs): if name.startswith(('.', '@', 'hide')) or name in ('img'): 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', 'meta.html', 'styles.css'): continue html += u'{0} {1} {2}\n'.format(name, *_get_date_size(os.path.join(root, name))) html += autoindex_html with open(os.path.join(root, 'index.html'), 'wb') as file: file.write(html.encode('utf-8')) def push(): """main function""" index() os.system('&&'.join([ 'git add -A .', 'git commit -S -m "[skip ci] build new index" -s -a', 'git push https master', ])) if __name__ == '__main__': globals()[sys.argv[1]]()