Commit 3145378b authored by Michael Murtaugh's avatar Michael Murtaugh
Browse files

added cgi-bin, tweaked settings

parent ac96f6d5
import os
DATAPATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data")
def getDataPath (p=None):
if p:
return os.path.join(DATAPATH, p)
else:
return data
#!/usr/bin/env python
print "Content-type: text/html"
print
print "hello"
#!/usr/bin/env python
import os, cgi, urllib
import cgitb; cgitb.enable()
from listing_utils import *
# fs = cgi.FieldStorage()
pwd = os.environ.get("PWD", os.environ.get("PATH_TRANSLATED", "."))
request_url = os.environ.get("REQUEST_URI").rstrip("/")
request_path = urllib.unquote(request_url)
path = os.path.join(pwd, request_path.lstrip("/"))
print "Content-type: text/html;charset=utf-8"
print
# <script src="/cookbook/collagecode.js"></script>
# <script src="/cookbook/clicktoedit.js"></script>
# <script src="/cookbook/relaymessages.js"></script>
title = request_path
if request_path == "":
title = "/"
print """<!DOCTYPE html>
<html>
<head>
<title>{0[title]}</title>
<style>
h1 {{ margin-top : 0 }}
</style>
<link rel="stylesheet" type="text/css" href="/cookbook/listing.css">
<script src="/cookbook/listing.js"></script>
</head>
<body>
<h1>Index of {0[request]}</h1>
<table>
<thead>
<tr><th>&nbsp;</th><th>Name</th><th>Last modified</th><th>Size</th><th>Description</th></tr>
</thead>
<tbody>
""".format({
'title': "Directory listing: {0}".format(title),
'request': title
})
items = []
for f in os.listdir(path):
items.append(f)
items.sort()
request_parts = request_url.strip("/").split("/")
parent_url = None
if len(request_parts) > 1:
parent_url = "/"+("/".join(request_parts[:-1]))
elif request_url and request_url != "/":
parent_url = "/"
if parent_url:
print """<tr><td align="right">&larr;</td><td><a href="{0}">Parent Directory</a></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>""".format(parent_url)
for f in items:
fp = os.path.join(path, f)
file_url = '/' + urllib.quote(os.path.relpath(fp, pwd))
fstat = os.stat(fp)
size = humanize_bytes(fstat.st_size)
label = f
if os.path.isdir(fp):
file_url += "/"
label += "/"
size = "&ndash;"
lastmod = datetime.datetime.fromtimestamp(fstat.st_mtime).strftime("%d-%b-%Y %H:%M:%S")
mode = fmt_mode(fstat.st_mode)
print """ <tr>
<td><input type="checkbox" />{0}</td>
<td>{1}</td>
<td align="right">{2}</td>
<td align="right">{3}</td>
<td align="right">{4}</td>
</tr>""".format(
"",
"<a href=\"{0}\">{1}</a>".format(file_url, label),
lastmod,
size,
mode )
print """ </tbody>
</table>
<p>
<select>
<option selected>&mdash;</option>
<option>select all</option>
<option>select none</option>
<option>delete selected items</option>
<option>copy selected items</option>
<option>move selected items</option>
<option>new file</option>
<option>new folder</option>
</select>
</p>
"""
print """</body>
</html>
"""
import datetime, stat, hashlib, os
def git_hash (fp):
sha1 = hashlib.sha1()
size = os.path.getsize(fp)
sha1.update("blob{0}\0".format(size))
with open(fp) as f:
while True:
bytes = f.read()
if not bytes:
break
sha1.update(bytes)
return sha1.hexdigest()
def humanize_bytes(bytes, precision=1):
"""Return a humanized string representation of a number of bytes.
Assumes `from __future__ import division`.
>>> humanize_bytes(1)
'1 byte'
>>> humanize_bytes(1024)
'1.0 kB'
>>> humanize_bytes(1024*123)
'123.0 kB'
>>> humanize_bytes(1024*12342)
'12.1 MB'
>>> humanize_bytes(1024*12342,2)
'12.05 MB'
>>> humanize_bytes(1024*1234,2)
'1.21 MB'
>>> humanize_bytes(1024*1234*1111,2)
'1.31 GB'
>>> humanize_bytes(1024*1234*1111,1)
'1.3 GB'
"""
abbrevs = (
(1<<50L, 'PB'),
(1<<40L, 'TB'),
(1<<30L, 'GB'),
(1<<20L, 'MB'),
(1<<10L, 'kB'),
(1, 'bytes')
)
if bytes == 1:
return '1 byte'
for factor, suffix in abbrevs:
if bytes >= factor:
break
ret = '%.*f' % (precision, bytes / factor)
if ret.endswith(".0"):
ret = ret[:-2]
ret += " "+suffix
return ret
def fmt_mode (m):
ret = ''
# if stat.S_ISDIR(m):
# ret = "d"
# else:
# ret = " "
# ret += " "
def _rwx(m, flags):
ret = ''
for flag, c in zip(flags, "rwx"):
if m & flag:
ret += c
else:
ret += "&ndash;"
return ret
ret += _rwx(m, (stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR))
ret += "|"
ret += _rwx(m, (stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP))
ret += "|"
ret += _rwx(m, (stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH))
return ret
#!/usr/bin/env python
import cgitb; cgitb.enable()
import cgi, json, sys, os
env = os.environ
referer = env.get("HTTP_REFERER")
pwd = env.get("PWD", env.get("PATH_TRANSLATED"))
fs = cgi.FieldStorage()
path = fs.getvalue("path", "").strip("/")
data = fs.getvalue("data", "")
# print "Content-type: application/json;charset=utf-8"
# print
# print json.dumps({'result': True, 'message': 'saved', 'pwd': pwd, 'path': path, 'referer': referer})
# sys.exit(0)
if path:
abspath = os.path.join(pwd, path)
try:
if os.path.exists(abspath):
if os.path.exists(abspath+"~"): # for windows (rename fails if ~ exists)
os.remove(abspath+"~")
os.rename(abspath, abspath+"~")
f = open(abspath, "w")
f.write(data)
f.close()
print "Content-type: application/json;charset=utf-8"
print
print json.dumps({'result': True, 'message': 'saved'})
except OSError, e:
print "Content-type: application/json;charset=utf-8"
print
print json.dumps({'result': False, 'message': 'OSError: '+str(e)})
......@@ -17,6 +17,8 @@ from time import sleep
DIRECTORY_LISTING = None
from cookbook import getDataPath
class StaticDirectoryListingFile (File):
# def __init__(self, path, *args, **kwargs):
# # self.directoryListingResource = directory_listing
......@@ -67,6 +69,15 @@ def main(args=None):
DIRECTORY_LISTING = listing
resource = StaticDirectoryListingFile(".")
resource.putChild("cookbook", cookbook)
corehtdocspath = getDataPath("htdocs")
corehtdocs = File(corehtdocspath)
resource.putChild("core", corehtdocs)
corecgibinpath = getDataPath("cgi-bin")
corecgibin = CGIDirectory(corecgibinpath)
corehtdocs.putChild("cgi-bin", corecgibin)
factory = Site(resource)
while True:
......
......@@ -42,6 +42,6 @@ setup(
# long_description=open('README.md').read(),
install_requires=[
"twisted"
]
# cmdclass={'install_lib':chmodcgi_install_lib},
],
cmdclass={'install_lib':chmodcgi_install_lib},
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment