more python3

This commit is contained in:
Ville Rantanen
2020-10-12 11:41:17 +03:00
parent 52db6b9bf4
commit dfd5111bea
3 changed files with 220 additions and 171 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# coding=utf-8
''' A script that creates an index for a folder.
'''
""" A script that creates an index for a folder.
"""
import os
import sys
@@ -13,45 +13,89 @@ import base64
import random
VERSION = "20200619"
IMAGE_EXTENSIONS = ['png', 'gif', 'jpg', 'jpeg', 'tif', 'tiff']
IMAGE_EXTENSIONS = ["png", "gif", "jpg", "jpeg", "tif", "tiff"]
def setup():
''' Setup the command line options '''
""" Setup the command line options """
from argparse import ArgumentParser
parser = ArgumentParser(
epilog = "Recursively generate indexes: \n# find . -type d -not -path '*/.*' -exec SimpleWebPage -f \{\} \;"
epilog="Recursively generate indexes: \n# find . -type d -not -path '*/.*' -exec SimpleWebPage -f \{\} \;"
)
parser.add_argument("-f",action="store_true",dest="overwrite",default=False,
help="Overwrite existing index file, even if it's not generated with SimpleWebPage. By default, if file is generated with SimpleWebPage it will be overwritten!")
parser.add_argument("-H",action="store_true",dest="hidden",default=False,
help="Show hidden files")
parser.add_argument("-t",type=str,dest="title",default=None,
help="Name for the title (Default: Folder name)")
parser.add_argument("-o",type=str,dest="filename",default="index.html",
help="Output filename (Default: index.html)")
parser.add_argument("-p",action="store_false",dest="parent",default=True,
help="Do no print .. link for parent folder.")
parser.add_argument("--password",type=str,dest="password",default=None,
help="Set a password to view page. The file list will be written to a randomly generated filename. Note: this is not secure, as the target link will still be unprotected if known.")
parser.add_argument("-r",action="store_true",dest="recursive",default=False,
help="Include all files recursively in the list. Do not include any folders.")
parser.add_argument("--images",action="store_true",dest="images",default=False,
help="Show images with <img> tags")
parser.add_argument("--include","-i",
action="store",
dest="includes",
default=['*'],
help="Glob match for files to be included in the table. ex. *.jpg. You can pass several includes.",
nargs = '*'
)
parser.add_argument("--version",action='version', version=VERSION)
parser.add_argument(
"path",
type=str,
action="store",
default=os.path.abspath('.'),
nargs='?',
help="Root path of the index"
"-f",
action="store_true",
dest="overwrite",
default=False,
help="Overwrite existing index file, even if it's not generated with SimpleWebPage. By default, if file is generated with SimpleWebPage it will be overwritten!",
)
parser.add_argument(
"-H",
action="store_true",
dest="hidden",
default=False,
help="Show hidden files",
)
parser.add_argument(
"-t",
type=str,
dest="title",
default=None,
help="Name for the title (Default: Folder name)",
)
parser.add_argument(
"-o",
type=str,
dest="filename",
default="index.html",
help="Output filename (Default: index.html)",
)
parser.add_argument(
"-p",
action="store_false",
dest="parent",
default=True,
help="Do no print .. link for parent folder.",
)
parser.add_argument(
"--password",
type=str,
dest="password",
default=None,
help="Set a password to view page. The file list will be written to a randomly generated filename. Note: this is not secure, as the target link will still be unprotected if known.",
)
parser.add_argument(
"-r",
action="store_true",
dest="recursive",
default=False,
help="Include all files recursively in the list. Do not include any folders.",
)
parser.add_argument(
"--images",
action="store_true",
dest="images",
default=False,
help="Show images with <img> tags",
)
parser.add_argument(
"--include",
"-i",
action="store",
dest="includes",
default=["*"],
help="Glob match for files to be included in the table. ex. *.jpg. You can pass several includes.",
nargs="*",
)
parser.add_argument("--version", action="version", version=VERSION)
parser.add_argument(
"path",
type=str,
action="store",
default=os.path.abspath("."),
nargs="?",
help="Root path of the index",
)
options = parser.parse_args()
@@ -60,28 +104,36 @@ def setup():
options.title = os.path.basename(options.path)
return options
def setup2HTML(opts):
return '<meta name="SimpleWebPageSetup" content="%s"/>'%";".join([
'hidden=%s'%opts.hidden,
'parent=%s'%opts.parent,
'title=%s'%urllib.parse.quote(opts.title),
'images=%s'%opts.images
])
return '<meta name="SimpleWebPageSetup" content="%s"/>' % ";".join(
[
"hidden=%s" % opts.hidden,
"parent=%s" % opts.parent,
"title=%s" % urllib.parse.quote(opts.title),
"images=%s" % opts.images,
]
)
def HTML2setup(opts):
""" returns new opts and was it able to read HTML """
try:
read_config = False
with open(os.path.join(opts.path,opts.filename), 'rt') as f:
with open(os.path.join(opts.path, opts.filename), "rt") as f:
for l in f.readlines():
if l.find('<meta name="SimpleWebPageSetup"') > -1:
content = l[l.find('name="SimpleWebPageSetup"'):]
content = l[l.find('name="SimpleWebPageSetup"') :]
for s in content.split('"')[3].split(";"):
(k,v) = s.split('=',1)
if k == 'hidden': opts.hidden = v == "True"
if k == 'parent': opts.parent = v == "True"
if k == 'title': opts.title = urllib.parse.unquote(v)
if k == 'images': opts.images = v == "True"
(k, v) = s.split("=", 1)
if k == "hidden":
opts.hidden = v == "True"
if k == "parent":
opts.parent = v == "True"
if k == "title":
opts.title = urllib.parse.unquote(v)
if k == "images":
opts.images = v == "True"
read_config = True
print("Reading options from existing " + opts.filename)
break
@@ -89,6 +141,7 @@ def HTML2setup(opts):
except:
return (opts, False)
def get_files_and_folders(opts):
if opts.recursive:
@@ -99,10 +152,10 @@ def get_files_and_folders(opts):
if rpath == None:
rpath = path
if not opts.hidden:
files = [ f for f in files if not f.startswith(".")]
dirs[:] = [ d for d in dirs if not d.startswith(".")]
files = [f for f in files if not f.startswith(".")]
dirs[:] = [d for d in dirs if not d.startswith(".")]
files = [os.path.join(os.path.relpath(path, opts.path), f) for f in files]
files = [f[2:] if f.startswith("./") else f for f in files ]
files = [f[2:] if f.startswith("./") else f for f in files]
rfiles.extend(files)
return rdirs, rfiles, rpath
@@ -110,8 +163,8 @@ def get_files_and_folders(opts):
else:
for path, dirs, files in os.walk(opts.path):
if not opts.hidden:
files = [ f for f in files if not f.startswith(".")]
dirs = [ d for d in dirs if not d.startswith(".")]
files = [f for f in files if not f.startswith(".")]
dirs = [d for d in dirs if not d.startswith(".")]
return dirs, files, path
@@ -125,18 +178,21 @@ def generate_index(opts):
if opts.filename in files:
opts, existing_config = HTML2setup(opts)
if not existing_config and not opts.overwrite:
print(opts.filename + " exists, and not generated with SimpleWebPage. Exiting.")
print(
opts.filename
+ " exists, and not generated with SimpleWebPage. Exiting."
)
sys.exit(1)
files = [ f for f in files if f != opts.filename]
files = [ f for f in files if f != opts.password_filename]
files = [f for f in files if f != opts.filename]
files = [f for f in files if f != opts.password_filename]
files = match_files(files, opts.includes)
dirs.sort()
files.sort()
files.sort(key = lambda x: x.find("/") > 0)
with open(os.path.join(path,opts.filename), 'wt') as f:
files.sort(key=lambda x: x.find("/") > 0)
with open(os.path.join(path, opts.filename), "wt") as f:
f.write(get_header(opts))
if opts.parent:
f.write(get_pathlink(path, '..'))
f.write(get_pathlink(path, ".."))
for di in dirs:
f.write(get_pathlink(path, di))
for fi in files:
@@ -151,55 +207,42 @@ def generate_password_page(path, password_file, password):
def ha(p):
h = 0
for c in p:
h = ((h<<5) - h) + ord(c)
h = h & 0xffffffff
if h > (1<<31) - 1:
h -= (1<<32)
h = ((h << 5) - h) + ord(c)
h = h & 0xFFFFFFFF
if h > (1 << 31) - 1:
h -= 1 << 32
return h
def scramble(p,t):
def scramble(p, t):
p += t
s = ''
s = ""
for i in range(len(t)):
s += chr(ord(p[i]) + ord(t[i]))
return s
def enc(s):
return base64.b64encode(s.encode('latin1')).decode('ascii')
return base64.b64encode(s.encode("latin1")).decode("ascii")
def random_string(stringLength=16):
letters = string.ascii_lowercase + string.digits
return ''.join(random.choice(letters) for i in range(stringLength))
return "".join(random.choice(letters) for i in range(stringLength))
def get_target(filename):
splitted = os.path.splitext(filename)
return (
splitted[0],
random_string(),
splitted[1]
)
return (splitted[0], random_string(), splitted[1])
target_base, target_middle, target_ext = get_target(password_file)
secret = "{}:{}".format(
ha(password),
enc(scramble(password, target_middle))
)
with open(os.path.join(path, password_file), 'wt') as f:
secret = "{}:{}".format(ha(password), enc(scramble(password, target_middle)))
with open(os.path.join(path, password_file), "wt") as f:
f.write(get_password_page(secret, target_base, target_ext))
return "{}.{}{}".format(
target_base,
target_middle,
target_ext
)
return "{}.{}{}".format(target_base, target_middle, target_ext)
def get_filelink(path,fname,images=False):
if os.path.islink(os.path.join(path, fname)) and not os.path.exists(os.path.join(path, fname)):
(fsize, fsstr, fsstrb, fdstr)=(0, "NA", "NA", "NA")
def get_filelink(path, fname, images=False):
if os.path.islink(os.path.join(path, fname)) and not os.path.exists(
os.path.join(path, fname)
):
(fsize, fsstr, fsstrb, fdstr) = (0, "NA", "NA", "NA")
else:
fsize = os.path.getsize(os.path.join(path, fname))
fsstr = sizeof(fsize)
@@ -210,42 +253,37 @@ def get_filelink(path,fname,images=False):
fname_str = get_imagestr(fname)
else:
fname_str = fname
return '<tr><td><a class="link_file" href="%s">%s</a><td class="right">%s</td><td class="right bytes">%s</td><td class="right">%s</td></tr>\n'%(
urllib.parse.quote(fname),
fname_str,
fsstr,
fsstrb,
fdstr
return (
'<tr><td><a class="link_file" href="%s">%s</a><td class="right">%s</td><td class="right bytes">%s</td><td class="right">%s</td></tr>\n'
% (urllib.parse.quote(fname), fname_str, fsstr, fsstrb, fdstr)
)
def get_wget_lines(files):
wget = "\n<!--\n"
for f in files:
wget += "#FILE %s\n"%(urllib.parse.quote(f),)
wget += "#FILE %s\n" % (urllib.parse.quote(f),)
wget += "#WGET URL=[insert URL] && curl $URL | grep '^#FILE ' | cut -c7- | sed \"s,^,$URL,\" | xargs -n1 wget\n"
wget += "-->\n"
return wget
def get_imagestr(fname):
return '<img src="%s" title="%s"/>'%(
urllib.parse.quote(fname),
fname
)
return '<img src="%s" title="%s"/>' % (urllib.parse.quote(fname), fname)
def get_pathlink(path,dname):
def get_pathlink(path, dname):
fdate = time.localtime(os.path.getmtime(os.path.join(path, dname)))
fdstr = time.strftime("%Y/%m/%d %H:%M:%S", fdate)
return '<tr><td><a class="link_dir" href="%s">&#8627;&nbsp;%s/</a><td class="right">[DIR]</td><td class="right bytes">0</td><td class="right">%s</td></tr>\n'%(
urllib.parse.quote(dname),
dname,
fdstr
return (
'<tr><td><a class="link_dir" href="%s">&#8627;&nbsp;%s/</a><td class="right">[DIR]</td><td class="right bytes">0</td><td class="right">%s</td></tr>\n'
% (urllib.parse.quote(dname), dname, fdstr)
)
def get_password_page(secret, target_base, target_ext):
return """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
return (
"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
@@ -334,27 +372,28 @@ function newPass(password, target) {
</div>
</body>
</html>""".replace(
"TARGET_EXT",
target_ext,
1
).replace(
"TARGET_BASE",
target_base,
1
).replace(
"SECRET",
secret,
1
)
"TARGET_EXT", target_ext, 1
)
.replace("TARGET_BASE", target_base, 1)
.replace("SECRET", secret, 1)
)
def get_header(opts):
header='''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
header = (
"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="SimpleWebPage ''' + VERSION + '''" />
''' + setup2HTML(opts) + '''
<title>''' + opts.title + '''</title>
<meta name="generator" content="SimpleWebPage """
+ VERSION
+ """" />
"""
+ setup2HTML(opts)
+ """
<title>"""
+ opts.title
+ """</title>
<style>
/* Style modified from: https://css-tricks.com/snippets/php/display-styled-directory-contents/ */
* {
@@ -754,14 +793,19 @@ function alternate(table) {
</head>
<body>
<div class="container">
<h1>''' + opts.title + '''</h1>
<h1>"""
+ opts.title
+ """</h1>
<table class="sortable" id="fileList"><thead><tr><th>Name</th><th class="right">Size</th><th class="right">Size B</th><th class="right">Modified</th></tr></thead><tbody>
'''
"""
)
return header
def get_footer():
return '''</tbody></table></div>
</body></html>'''
return """</tbody></table></div>
</body></html>"""
def is_imagefile(fname):
for ext in IMAGE_EXTENSIONS:
@@ -771,23 +815,24 @@ def is_imagefile(fname):
def match_files(files, glob_list):
matched = []
for f in files:
for g in glob_list:
if fnmatch.fnmatch(f,g):
matched.append(f)
break
return matched
matched = []
for f in files:
for g in glob_list:
if fnmatch.fnmatch(f, g):
matched.append(f)
break
return matched
def sizeof(num):
for x in ['&nbsp;B','KB','MB','GB','TB']:
for x in ["&nbsp;B", "KB", "MB", "GB", "TB"]:
if num < 1024.0:
if x=='&nbsp;B':
if x == "&nbsp;B":
return "%d&nbsp;%s" % (num, x)
return "%3.1f&nbsp;%s" % (num, x)
num /= 1024.0
if __name__ == "__main__":
opts = setup()
generate_index(opts)