Commit e080dab9 authored by Hanno Schlichting's avatar Hanno Schlichting

flake8 Zope2/utilities.

parent 4903e67c
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
# #
############################################################################## ##############################################################################
def run(): def run():
""" Start a Zope instance """ """ Start a Zope instance """
import Zope2.Startup import Zope2.Startup
...@@ -25,6 +26,7 @@ def run(): ...@@ -25,6 +26,7 @@ def run():
raise raise
starter.run() starter.run()
def configure(configfile): def configure(configfile):
""" Provide an API which allows scripts like zopectl to configure """ Provide an API which allows scripts like zopectl to configure
Zope before attempting to do 'app = Zope2.app(). Should be used as Zope before attempting to do 'app = Zope2.app(). Should be used as
...@@ -38,6 +40,7 @@ def configure(configfile): ...@@ -38,6 +40,7 @@ def configure(configfile):
starter.dropPrivileges() starter.dropPrivileges()
return starter return starter
def _setconfig(configfile=None): def _setconfig(configfile=None):
""" Configure a Zope instance based on ZopeOptions. Optionally """ Configure a Zope instance based on ZopeOptions. Optionally
accept a configfile argument (string path) in order to specify accept a configfile argument (string path) in order to specify
...@@ -56,6 +59,7 @@ def _setconfig(configfile=None): ...@@ -56,6 +59,7 @@ def _setconfig(configfile=None):
App.config.setConfiguration(opts.configroot) App.config.setConfiguration(opts.configroot)
return opts return opts
def make_wsgi_app(global_config, zope_conf): def make_wsgi_app(global_config, zope_conf):
from App.config import setConfiguration from App.config import setConfiguration
from Zope2.Startup import get_starter from Zope2.Startup import get_starter
...@@ -72,6 +76,6 @@ def make_wsgi_app(global_config, zope_conf): ...@@ -72,6 +76,6 @@ def make_wsgi_app(global_config, zope_conf):
starter.prepare() starter.prepare()
return publish_module return publish_module
if __name__ == '__main__': if __name__ == '__main__':
run() run()
...@@ -28,18 +28,18 @@ def main(argv=sys.argv): ...@@ -28,18 +28,18 @@ def main(argv=sys.argv):
try: try:
user, pwd = argv[1], argv[2] user, pwd = argv[1], argv[2]
except IndexError: except IndexError:
print "%s <username> <password>" % argv[0] print("%s <username> <password>" % argv[0])
sys.exit(255) sys.exit(255)
finder = ZopeFinder(argv) finder = ZopeFinder(argv)
finder.filter_warnings() finder.filter_warnings()
app = finder.get_app() app = finder.get_app()
result = adduser(app, user, pwd) result = adduser(app, user, pwd)
if result: if result:
print "User %s created." % user print("User %s created." % user)
else: else:
print "Got no result back. User creation may have failed." print("Got no result back. User creation may have failed.")
print "Maybe the user already exists and nothing is done then." print("Maybe the user already exists and nothing is done then.")
print "Or the implementation does not give info when it succeeds." print("Or the implementation does not give info when it succeeds.")
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -65,18 +65,21 @@ import shutil ...@@ -65,18 +65,21 @@ import shutil
import sys import sys
import getopt import getopt
CVS_DIRS = [os.path.normcase("CVS"), os.path.normcase(".svn")]
def main(): def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], opts, args = getopt.getopt(
sys.argv[1:],
"hs:t:u:g:r:", "hs:t:u:g:r:",
["help", "sourcedir=", "targetdir=", "uid=", "gid=", ["help", "sourcedir=", "targetdir=", "uid=", "gid=",
"replace="] "replace="]
) )
except getopt.GetoptError, msg: except getopt.GetoptError as msg:
usage(sys.stderr, msg) usage(sys.stderr, msg)
sys.exit(2) sys.exit(2)
script = os.path.abspath(sys.argv[0])
sourcedir = None sourcedir = None
targetdir = None targetdir = None
uid = None uid = None
...@@ -120,7 +123,7 @@ def main(): ...@@ -120,7 +123,7 @@ def main():
sys.exit(2) sys.exit(2)
except (ImportError, AttributeError): except (ImportError, AttributeError):
usage(sys.stderr, usage(sys.stderr,
"Your system does not support the gid or uid options") "Your system does not support the gid or uid options")
sys.exit(2) sys.exit(2)
if opt in ("-g", "--gid"): if opt in ("-g", "--gid"):
if not arg: if not arg:
...@@ -143,7 +146,7 @@ def main(): ...@@ -143,7 +146,7 @@ def main():
sys.exit(2) sys.exit(2)
except (ImportError, AttributeError): except (ImportError, AttributeError):
usage(sys.stderr, usage(sys.stderr,
"Your system does not support the gid or uid options") "Your system does not support the gid or uid options")
sys.exit(2) sys.exit(2)
if opt in ("-r", "--replace"): if opt in ("-r", "--replace"):
...@@ -162,6 +165,7 @@ def main(): ...@@ -162,6 +165,7 @@ def main():
copyskel(sourcedir, targetdir, uid, gid, **replacements) copyskel(sourcedir, targetdir, uid, gid, **replacements)
def copyskel(sourcedir, targetdir, uid, gid, **replacements): def copyskel(sourcedir, targetdir, uid, gid, **replacements):
""" This is an independent function because we'd like to """ This is an independent function because we'd like to
import and call it from mkzopeinstance """ import and call it from mkzopeinstance """
...@@ -180,7 +184,7 @@ def copyskel(sourcedir, targetdir, uid, gid, **replacements): ...@@ -180,7 +184,7 @@ def copyskel(sourcedir, targetdir, uid, gid, **replacements):
(targetdir, replacements, uid, gid)) (targetdir, replacements, uid, gid))
finally: finally:
os.chdir(pwd) os.chdir(pwd)
except (IOError, OSError), msg: except (IOError, OSError) as msg:
print >>sys.stderr, msg print >>sys.stderr, msg
sys.exit(1) sys.exit(1)
...@@ -191,9 +195,8 @@ def copyskel(sourcedir, targetdir, uid, gid, **replacements): ...@@ -191,9 +195,8 @@ def copyskel(sourcedir, targetdir, uid, gid, **replacements):
os.chmod(fullname, 0700) os.chmod(fullname, 0700)
CVS_DIRS = [os.path.normcase("CVS"), os.path.normcase(".svn")] def copydir(args, sourcedir, names):
targetdir, replacements, uid, gid = args
def copydir((targetdir, replacements, uid, gid), sourcedir, names):
# Don't recurse into CVS directories: # Don't recurse into CVS directories:
for name in names[:]: for name in names[:]:
if os.path.normcase(name) in CVS_DIRS: if os.path.normcase(name) in CVS_DIRS:
...@@ -227,6 +230,7 @@ def copydir((targetdir, replacements, uid, gid), sourcedir, names): ...@@ -227,6 +230,7 @@ def copydir((targetdir, replacements, uid, gid), sourcedir, names):
if uid is not None: if uid is not None:
os.chown(dn, uid, gid) os.chown(dn, uid, gid)
def copyin(src, dst, replacements, uid, gid): def copyin(src, dst, replacements, uid, gid):
ifp = open(src) ifp = open(src)
text = ifp.read() text = ifp.read()
...@@ -240,12 +244,13 @@ def copyin(src, dst, replacements, uid, gid): ...@@ -240,12 +244,13 @@ def copyin(src, dst, replacements, uid, gid):
if uid is not None: if uid is not None:
os.chown(dst, uid, gid) os.chown(dst, uid, gid)
def usage(stream, msg=None): def usage(stream, msg=None):
if msg: if msg:
print >>stream, msg stream.write(msg)
print >>stream stream.write('\n')
program = os.path.basename(sys.argv[0]) program = os.path.basename(sys.argv[0])
print >>stream, __doc__ % {"program": program} stream.write(__doc__ % {"program": program})
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
import os import os
class ZopeFinder: class ZopeFinder(object):
def __init__(self, argv): def __init__(self, argv):
self.cmd = argv[0] self.cmd = argv[0]
......
...@@ -31,13 +31,18 @@ import os ...@@ -31,13 +31,18 @@ import os
import sys import sys
import copyzopeskel import copyzopeskel
if sys.version_info > (3, 0):
raw_input = input
def main(): def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], opts, args = getopt.getopt(
sys.argv[1:],
"hu:d:s:p:", "hu:d:s:p:",
["help", "user=", "dir=", "skelsrc=", "python="] ["help", "user=", "dir=", "skelsrc=", "python="]
) )
except getopt.GetoptError, msg: except getopt.GetoptError as msg:
usage(sys.stderr, msg) usage(sys.stderr, msg)
sys.exit(2) sys.exit(2)
...@@ -74,7 +79,7 @@ def main(): ...@@ -74,7 +79,7 @@ def main():
if not arg: if not arg:
usage(sys.stderr, "user must not be empty") usage(sys.stderr, "user must not be empty")
sys.exit(2) sys.exit(2)
if not ":" in arg: if ":" not in arg:
usage(sys.stderr, "user must be specified as name:password") usage(sys.stderr, "user must be specified as name:password")
sys.exit(2) sys.exit(2)
user, password = arg.split(":", 1) user, password = arg.split(":", 1)
...@@ -85,10 +90,9 @@ def main(): ...@@ -85,10 +90,9 @@ def main():
# to skeltarget. # to skeltarget.
skeltarget = instancehome = os.path.abspath( skeltarget = instancehome = os.path.abspath(
os.path.expanduser(get_skeltarget()) os.path.expanduser(get_skeltarget())
) )
instancehome = skeltarget instancehome = skeltarget
configfile = os.path.join(instancehome, 'etc', 'zope.conf')
if skelsrc is None: if skelsrc is None:
# default to using stock Zope skeleton source # default to using stock Zope skeleton source
skelsrc = os.path.join(os.path.dirname(__file__), "skel") skelsrc = os.path.join(os.path.dirname(__file__), "skel")
...@@ -105,14 +109,14 @@ def main(): ...@@ -105,14 +109,14 @@ def main():
# need be used. # need be used.
if python is None: if python is None:
python = sys.executable python = sys.executable
psplit = os.path.split(python) psplit = os.path.split(python)
exedir = os.path.join(*psplit[:-1]) exedir = os.path.join(*psplit[:-1])
pythonexe = os.path.join(exedir, 'python.exe') pythonexe = os.path.join(exedir, 'python.exe')
pythonwexe = os.path.join(exedir, 'pythonw.exe') pythonwexe = os.path.join(exedir, 'pythonw.exe')
if ( os.path.isfile(pythonwexe) and os.path.isfile(pythonexe) and if (os.path.isfile(pythonwexe) and os.path.isfile(pythonexe) and
(python in [pythonwexe, pythonexe]) ): (python in [pythonwexe, pythonexe])):
# we're using a Windows build with both python.exe and pythonw.exe # we're using a Windows build with both python.exe and pythonw.exe
# in the same directory # in the same directory
PYTHON = pythonexe PYTHON = pythonexe
...@@ -124,17 +128,18 @@ def main(): ...@@ -124,17 +128,18 @@ def main():
zope2path = get_zope2path(PYTHON) zope2path = get_zope2path(PYTHON)
kw = { kw = {
"PYTHON":PYTHON, "PYTHON": PYTHON,
"PYTHONW":PYTHONW, "PYTHONW": PYTHONW,
"INSTANCE_HOME": instancehome, "INSTANCE_HOME": instancehome,
"ZOPE_SCRIPTS": script_path, "ZOPE_SCRIPTS": script_path,
"ZOPE2PATH": zope2path, "ZOPE2PATH": zope2path,
} }
copyzopeskel.copyskel(skelsrc, skeltarget, None, None, **kw) copyzopeskel.copyskel(skelsrc, skeltarget, None, None, **kw)
if user and password: if user and password:
write_inituser(inituser, user, password) write_inituser(inituser, user, password)
def usage(stream, msg=None): def usage(stream, msg=None):
if msg: if msg:
print >>stream, msg print >>stream, msg
...@@ -142,25 +147,27 @@ def usage(stream, msg=None): ...@@ -142,25 +147,27 @@ def usage(stream, msg=None):
program = os.path.basename(sys.argv[0]) program = os.path.basename(sys.argv[0])
print >>stream, __doc__ % {"program": program} print >>stream, __doc__ % {"program": program}
def get_skeltarget(): def get_skeltarget():
print 'Please choose a directory in which you\'d like to install' print('Please choose a directory in which you\'d like to install')
print 'Zope "instance home" files such as database files, configuration' print('Zope "instance home" files such as database files, configuration')
print 'files, etc.' print('files, etc.')
print print
while 1: while 1:
skeltarget = raw_input("Directory: ").strip() skeltarget = raw_input("Directory: ").strip()
if skeltarget == '': if skeltarget == '':
print 'You must specify a directory' print('You must specify a directory')
continue continue
else: else:
break break
return skeltarget return skeltarget
def get_inituser(): def get_inituser():
import getpass import getpass
print 'Please choose a username and password for the initial user.' print('Please choose a username and password for the initial user.')
print 'These will be the credentials you use to initially manage' print('These will be the credentials you use to initially manage')
print 'your new Zope instance.' print('your new Zope instance.')
print print
user = raw_input("Username: ").strip() user = raw_input("Username: ").strip()
if user == '': if user == '':
...@@ -172,9 +179,10 @@ def get_inituser(): ...@@ -172,9 +179,10 @@ def get_inituser():
break break
else: else:
passwd = verify = '' passwd = verify = ''
print "Password mismatch, please try again..." print("Password mismatch, please try again...")
return user, passwd return user, passwd
def write_inituser(fn, user, password): def write_inituser(fn, user, password):
import binascii import binascii
try: try:
...@@ -185,7 +193,8 @@ def write_inituser(fn, user, password): ...@@ -185,7 +193,8 @@ def write_inituser(fn, user, password):
pw = binascii.b2a_base64(sha(password).digest())[:-1] pw = binascii.b2a_base64(sha(password).digest())[:-1]
fp.write('%s:{SHA}%s\n' % (user, pw)) fp.write('%s:{SHA}%s\n' % (user, pw))
fp.close() fp.close()
os.chmod(fn, 0644) os.chmod(fn, 0o644)
def check_buildout(script_path): def check_buildout(script_path):
""" Are we running from within a buildout which supplies 'zopepy'? """ Are we running from within a buildout which supplies 'zopepy'?
...@@ -197,6 +206,7 @@ def check_buildout(script_path): ...@@ -197,6 +206,7 @@ def check_buildout(script_path):
parser.read(buildout_cfg) parser.read(buildout_cfg)
return 'zopepy' in parser.sections() return 'zopepy' in parser.sections()
def get_zope2path(python): def get_zope2path(python):
""" Get Zope2 path from selected Python interpreter. """ Get Zope2 path from selected Python interpreter.
""" """
......
...@@ -13,14 +13,23 @@ ...@@ -13,14 +13,23 @@
"""Request log profiler script """Request log profiler script
""" """
import sys, time, getopt, math, cPickle import cPickle
import getopt
import math
import sys
import time
from types import StringType from types import StringType
try: import gzip try:
except: pass import gzip
except ImportError:
pass
class ProfileException(Exception): pass
class Request: class ProfileException(Exception):
pass
class Request(object):
def __init__(self): def __init__(self):
self.url = None self.url = None
self.start = None self.start = None
...@@ -98,10 +107,14 @@ class Request: ...@@ -98,10 +107,14 @@ class Request:
def total(self): def total(self):
stage = self.endstage() stage = self.endstage()
if stage == "B": return 0 if stage == "B":
if stage == "I": return self.t_recdinput - self.start return 0
if stage == "A": return self.t_recdoutput - self.start if stage == "I":
if stage == "E": return self.elapsed return self.t_recdinput - self.start
if stage == "A":
return self.t_recdoutput - self.start
if stage == "E":
return self.elapsed
def prettyisize(self): def prettyisize(self):
if self.isize is not None: if self.isize is not None:
...@@ -126,7 +139,7 @@ class Request: ...@@ -126,7 +139,7 @@ class Request:
self.prettystart(), self.win(), self.wout(), self.wend(), self.prettystart(), self.win(), self.wout(), self.wend(),
self.total(), self.endstage(), self.prettyosize(), self.total(), self.endstage(), self.prettyosize(),
self.prettyhttpcode(), self.active, self.url self.prettyhttpcode(), self.active, self.url
) )
return self.fmt % body return self.fmt % body
fmt = "%19s %4s %4s %4s %3s %1s %7s %4s %4s %s" fmt = "%19s %4s %4s %4s %3s %1s %7s %4s %4s %s"
...@@ -136,14 +149,18 @@ class Request: ...@@ -136,14 +149,18 @@ class Request:
'Code', 'Act', 'URL') 'Code', 'Act', 'URL')
return self.fmt % body return self.fmt % body
class StartupRequest(Request): class StartupRequest(Request):
def endstage(self): def endstage(self):
return "U" return "U"
def total(self): def total(self):
return 0 return 0
class Cumulative: class Cumulative:
def __init__(self, url): def __init__(self, url):
self.url = url self.url = url
self.times = [] self.times = []
...@@ -152,11 +169,12 @@ class Cumulative: ...@@ -152,11 +169,12 @@ class Cumulative:
def put(self, request): def put(self, request):
elapsed = request.elapsed elapsed = request.elapsed
if elapsed == "I": self.hangs = self.hangs + 1 if elapsed == "I":
self.hangs = self.hangs + 1
self.times.append(elapsed) self.times.append(elapsed)
def all(self): def all(self):
if self.allelapsed == None: if self.allelapsed is None:
self.allelapsed = [] self.allelapsed = []
for elapsed in self.times: for elapsed in self.times:
self.allelapsed.append(elapsed) self.allelapsed.append(elapsed)
...@@ -167,7 +185,7 @@ class Cumulative: ...@@ -167,7 +185,7 @@ class Cumulative:
body = ( body = (
self.hangs, self.hits(), self.total(), self.max(), self.min(), self.hangs, self.hits(), self.total(), self.max(), self.min(),
self.median(), self.mean(), self.url self.median(), self.mean(), self.url
) )
return self.fmt % body return self.fmt % body
def getheader(self): def getheader(self):
...@@ -191,8 +209,9 @@ class Cumulative: ...@@ -191,8 +209,9 @@ class Cumulative:
return "I" return "I"
else: else:
t = self.total() t = self.total()
if t == "I": return "I" if t == "I":
return t/l return "I"
return t / l
def median(self): def median(self):
all = self.all() all = self.all()
...@@ -203,25 +222,28 @@ class Cumulative: ...@@ -203,25 +222,28 @@ class Cumulative:
if l == 1: if l == 1:
return all[0] return all[0]
elif l % 2 != 0: elif l % 2 != 0:
i = l/2 + 1 i = l / 2 + 1
return all[i] return all[i]
else: else:
i = l/2 - 1 i = l / 2 - 1
i2 = i + 1 i2 = i + 1
v1 = all[i] v1 = all[i]
v2 = all[i2] v2 = all[i2]
if isinstance(v1, StringType) or isinstance(v2, StringType): if isinstance(v1, StringType) or isinstance(v2, StringType):
return "I" return "I"
else: return (v1 + v2) / 2 else:
return (v1 + v2) / 2
def total(self): def total(self):
t = 0 t = 0
all = self.all() all = self.all()
for elapsed in all: for elapsed in all:
if elapsed == "I": continue if elapsed == "I":
continue
t = t + elapsed t = t + elapsed
return t return t
def parsebigmlogline(line): def parsebigmlogline(line):
tup = line.split(None, 3) tup = line.split(None, 3)
if len(tup) == 3: if len(tup) == 3:
...@@ -232,6 +254,7 @@ def parsebigmlogline(line): ...@@ -232,6 +254,7 @@ def parsebigmlogline(line):
else: else:
return None return None
def get_earliest_file_data(files): def get_earliest_file_data(files):
temp = {} temp = {}
earliest_fromepoch = 0 earliest_fromepoch = 0
...@@ -245,7 +268,7 @@ def get_earliest_file_data(files): ...@@ -245,7 +268,7 @@ def get_earliest_file_data(files):
line = line.strip() line = line.strip()
tup = parsebigmlogline(line) tup = parsebigmlogline(line)
if tup is None: if tup is None:
print "Could not interpret line: %s" % line print("Could not interpret line: %s" % line)
continue continue
code, id, timestr, desc = tup code, id, timestr, desc = tup
timestr = timestr.strip() timestr = timestr.strip()
...@@ -262,6 +285,7 @@ def get_earliest_file_data(files): ...@@ -262,6 +285,7 @@ def get_earliest_file_data(files):
return retn return retn
def get_requests(files, start=None, end=None, statsfname=None, def get_requests(files, start=None, end=None, statsfname=None,
writestats=None, readstats=None): writestats=None, readstats=None):
finished = [] finished = []
...@@ -279,8 +303,10 @@ def get_requests(files, start=None, end=None, statsfname=None, ...@@ -279,8 +303,10 @@ def get_requests(files, start=None, end=None, statsfname=None,
if tup is None: if tup is None:
break break
code, id, fromepoch, desc = tup code, id, fromepoch, desc = tup
if start is not None and fromepoch < start: continue if start is not None and fromepoch < start:
if end is not None and fromepoch > end: break continue
if end is not None and fromepoch > end:
break
if code == 'U': if code == 'U':
finished.extend(unfinished.values()) finished.extend(unfinished.values())
unfinished.clear() unfinished.clear()
...@@ -291,7 +317,8 @@ def get_requests(files, start=None, end=None, statsfname=None, ...@@ -291,7 +317,8 @@ def get_requests(files, start=None, end=None, statsfname=None,
continue continue
request = unfinished.get(id) request = unfinished.get(id)
if request is None: if request is None:
if code != "B": continue # garbage at beginning of file if code != "B":
continue # garbage at beginning of file
request = Request() request = Request()
for pending_req in unfinished.values(): for pending_req in unfinished.values():
pending_req.active = pending_req.active + 1 pending_req.active = pending_req.active + 1
...@@ -300,7 +327,7 @@ def get_requests(files, start=None, end=None, statsfname=None, ...@@ -300,7 +327,7 @@ def get_requests(files, start=None, end=None, statsfname=None,
try: try:
request.put(code, t, desc) request.put(code, t, desc)
except: except:
print "Unable to handle entry: %s %s %s"%(code, t, desc) print("Unable to handle entry: %s %s %s" % (code, t, desc))
if request.isfinished(): if request.isfinished():
del unfinished[id] del unfinished[id]
finished.append(request) finished.append(request)
...@@ -318,6 +345,7 @@ def get_requests(files, start=None, end=None, statsfname=None, ...@@ -318,6 +345,7 @@ def get_requests(files, start=None, end=None, statsfname=None,
return requests return requests
def analyze(requests, top, sortf, start=None, end=None, mode='cumulative', def analyze(requests, top, sortf, start=None, end=None, mode='cumulative',
resolution=60, urlfocusurl=None, urlfocustime=60): resolution=60, urlfocusurl=None, urlfocustime=60):
...@@ -334,17 +362,17 @@ def analyze(requests, top, sortf, start=None, end=None, mode='cumulative', ...@@ -334,17 +362,17 @@ def analyze(requests, top, sortf, start=None, end=None, mode='cumulative',
requests.sort(sortf) requests.sort(sortf)
write(requests, top) write(requests, top)
elif mode=='timed': elif mode == 'timed':
computed_start = requests[0].start computed_start = requests[0].start
computed_end = requests[-1].t_end computed_end = requests[-1].t_end
if start and end: if start and end:
timewrite(requests,start,end,resolution) timewrite(requests, start, end, resolution)
if start and not end: if start and not end:
timewrite(requests,start,computed_end,resolution) timewrite(requests, start, computed_end, resolution)
if end and not start: if end and not start:
timewrite(requests,computed_start,end,resolution) timewrite(requests, computed_start, end, resolution)
if not end and not start: if not end and not start:
timewrite(requests,computed_start,computed_end,resolution) timewrite(requests, computed_start, computed_end, resolution)
elif mode == 'urlfocus': elif mode == 'urlfocus':
requests.sort(sortf) requests.sort(sortf)
...@@ -354,11 +382,13 @@ def analyze(requests, top, sortf, start=None, end=None, mode='cumulative', ...@@ -354,11 +382,13 @@ def analyze(requests, top, sortf, start=None, end=None, mode='cumulative',
requests.sort(sortf) requests.sort(sortf)
write(requests, top) write(requests, top)
def urlfocuswrite(requests, url, t): def urlfocuswrite(requests, url, t):
l = [] l = []
i = 0 i = 0
for request in requests: for request in requests:
if request.url == url: l.append(i) if request.url == url:
l.append(i)
i = i + 1 i = i + 1
before = {} before = {}
after = {} after = {}
...@@ -369,18 +399,20 @@ def urlfocuswrite(requests, url, t): ...@@ -369,18 +399,20 @@ def urlfocuswrite(requests, url, t):
start = r.start start = r.start
earliest = start - t earliest = start - t
latest = start + t latest = start + t
print 'URLs invoked %s seconds before and after %s (#%s, %s)' % \ print('URLs invoked %s seconds before and after %s (#%s, %s)' %
(t, url, x, r.shortprettystart()) (t, url, x, r.shortprettystart()))
print '---' print('---')
i = -1 i = -1
for request in requests: for request in requests:
i = i + 1 i = i + 1
if request.start < earliest: continue if request.start < earliest:
if request.start > latest: break continue
if n == i: # current request if request.start > latest:
print '%3d' % (request.start - start), break
print '%s' % (request.shortprettystart()), if n == i: # current request
print request.url print('%3d\n' % (request.start - start))
print('%s\n' % (request.shortprettystart()))
print(request.url)
continue continue
if request.start <= start: if request.start <= start:
if before.get(i): if before.get(i):
...@@ -392,48 +424,55 @@ def urlfocuswrite(requests, url, t): ...@@ -392,48 +424,55 @@ def urlfocuswrite(requests, url, t):
after[i] = after[i] + 1 after[i] = after[i] + 1
else: else:
after[i] = 1 after[i] = 1
print '%3d' % (request.start - start), print('%3d\n' % (request.start - start))
print '%s' % (request.shortprettystart()), print('%s\n' % (request.shortprettystart()))
print request.url print(request.url)
print print
print ('Summary of URLs invoked before (and at the same time as) %s ' print('Summary of URLs invoked before (and at the same time as) %s '
'(times, url)' % url) '(times, url)' % url)
before = before.items() before = before.items()
before.sort() before.sort()
for k,v in before: for k, v in before:
print v, requests[k].url print(v)
print(requests[k].url)
print print
print 'Summary of URLs invoked after %s (times, url)' % url print('Summary of URLs invoked after %s (times, url)' % url)
after = after.items() after = after.items()
after.sort() after.sort()
for k,v in after: for k, v in after:
print v, requests[k].url print(v)
print(requests[k].url)
def write(requests, top=0): def write(requests, top=0):
if len(requests) == 0: if len(requests) == 0:
print "No data.\n" print("No data.\n")
return return
i = 0 i = 0
header = requests[0].getheader() header = requests[0].getheader()
print header print(header)
for stat in requests: for stat in requests:
i = i + 1 i = i + 1
if verbose: if verbose:
print str(stat) print(str(stat))
else: else:
print str(stat)[:78] print(str(stat)[:78])
if i == top: if i == top:
break break
def getdate(val): def getdate(val):
try: try:
val = val.strip() val = val.strip()
year, month, day = int(val[:4]), int(val[5:7]), int(val[8:10]) year, month, day = (
hour,minute,second=int(val[11:13]),int(val[14:16]),int(val[17:19]) int(val[:4]), int(val[5:7]), int(val[8:10]))
hour, minute, second = (
int(val[11:13]), int(val[14:16]), int(val[17:19]))
t = time.mktime((year, month, day, hour, minute, second, 0, 0, -1)) t = time.mktime((year, month, day, hour, minute, second, 0, 0, -1))
return t return t
except: except Exception:
raise ProfileException, "bad date %s" % val raise ProfileException("bad date %s" % val)
def getTimeslice(period, utime): def getTimeslice(period, utime):
low = int(math.floor(utime)) - period + 1 low = int(math.floor(utime)) - period + 1
...@@ -442,22 +481,25 @@ def getTimeslice(period, utime): ...@@ -442,22 +481,25 @@ def getTimeslice(period, utime):
if x % period == 0: if x % period == 0:
return x return x
def timewrite(requests, start, end, resolution): def timewrite(requests, start, end, resolution):
print "Start: %s End: %s Resolution: %d secs" % \ print("Start: %s End: %s Resolution: %d secs" %
(tick2str(start), tick2str(end), resolution) (tick2str(start), tick2str(end), resolution))
print "-" * 78 print("-" * 78)
print print
print "Date/Time #requests requests/second" print("Date/Time #requests requests/second")
d = {} d = {}
max = 0 max = 0
min = None min = None
for r in requests: for r in requests:
t = r.start t = r.start
slice = getTimeslice(resolution,t) slice = getTimeslice(resolution, t)
if slice > max: max = slice if slice > max:
if (min is None) or (slice < min): min = slice max = slice
if d.has_key(slice): if (min is None) or (slice < min):
min = slice
if slice in d:
d[slice] = d[slice] + 1 d[slice] = d[slice] + 1
else: else:
d[slice] = 1 d[slice] = 1
...@@ -468,7 +510,8 @@ def timewrite(requests, start, end, resolution): ...@@ -468,7 +510,8 @@ def timewrite(requests, start, end, resolution):
max_requests = 0 max_requests = 0
for slice in range(min, max, resolution): for slice in range(min, max, resolution):
num = d.get(slice, 0) num = d.get(slice, 0)
if num>max_requests: max_requests = num if num > max_requests:
max_requests = num
hits = hits + num hits = hits + num
if avg_requests is None: if avg_requests is None:
...@@ -477,19 +520,21 @@ def timewrite(requests, start, end, resolution): ...@@ -477,19 +520,21 @@ def timewrite(requests, start, end, resolution):
avg_requests = (avg_requests + num) / 2 avg_requests = (avg_requests + num) / 2
s = tick2str(slice) s = tick2str(slice)
s = s + " %6d %4.2lf" % (num,num*1.0/resolution) s = s + " %6d %4.2lf" % (num, num * 1.0 / resolution)
print s print(s)
print('=' * 78)
print(" Peak: %6d %4.2lf" %
(max_requests, max_requests * 1.0 / resolution))
print(" Avg: %6d %4.2lf" %
(avg_requests, avg_requests * 1.0 / resolution))
print("Total: %6d n/a " % (hits))
print '='*78
print " Peak: %6d %4.2lf" % \
(max_requests,max_requests*1.0/resolution)
print " Avg: %6d %4.2lf" % \
(avg_requests,avg_requests*1.0/resolution)
print "Total: %6d n/a " % (hits)
def tick2str(t): def tick2str(t):
return time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(t)) return time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(t))
def codesort(v1, v2): def codesort(v1, v2):
v1 = v1.endstage() v1 = v1.endstage()
v2 = v2.endstage() v2 = v2.endstage()
...@@ -497,17 +542,22 @@ def codesort(v1, v2): ...@@ -497,17 +542,22 @@ def codesort(v1, v2):
return 0 return 0
if v1 == "B": if v1 == "B":
return -1 # v1 is smaller than v2 return -1 # v1 is smaller than v2
if v1 == "I": if v1 == "I":
if v2 == "B": return 1 # v1 is larger than v2 if v2 == "B":
else: return -1 return 1 # v1 is larger than v2
else:
return -1
if v1 == "A": if v1 == "A":
if v2 in ['B', 'I']: return 1 if v2 in ['B', 'I']:
else: return -1 return 1
else:
return -1
if v1 == "E": if v1 == "E":
return 1 return 1
class Sort:
class Sort(object):
def __init__(self, fname, ascending=0): def __init__(self, fname, ascending=0):
self.fname = fname self.fname = fname
self.ascending = ascending self.ascending = ascending
...@@ -515,16 +565,23 @@ class Sort: ...@@ -515,16 +565,23 @@ class Sort:
def __call__(self, i1, i2): def __call__(self, i1, i2):
f1 = getattr(i1, self.fname) f1 = getattr(i1, self.fname)
f2 = getattr(i2, self.fname) f2 = getattr(i2, self.fname)
if callable(f1): f1 = f1() if callable(f1):
if callable(f2): f2 = f2() f1 = f1()
if callable(f2):
f2 = f2()
if f1 < f2: if f1 < f2:
if self.ascending: return -1 if self.ascending:
else: return 1 return -1
else:
return 1
elif f1 == f2: elif f1 == f2:
return 0 return 0
else: else:
if self.ascending: return 1 if self.ascending:
else: return -1 return 1
else:
return -1
def detailedusage(): def detailedusage():
details = usage(0) details = usage(0)
...@@ -596,7 +653,8 @@ If the 'verbose' argument is specified, do not trim url to fit into 80 cols. ...@@ -596,7 +653,8 @@ If the 'verbose' argument is specified, do not trim url to fit into 80 cols.
If the 'today' argument is specified, limit results to hits received today. If the 'today' argument is specified, limit results to hits received today.
If the 'daysago' argument is specified, limit results to hits received n days ago. If the 'daysago' argument is specified, limit results to hits received n days
ago.
The 'resolution' argument is used only for timed reports and specifies the The 'resolution' argument is used only for timed reports and specifies the
number of seconds between consecutive lines in the report number of seconds between consecutive lines in the report
...@@ -639,7 +697,8 @@ Examples: ...@@ -639,7 +697,8 @@ Examples:
%(pname)s debug.log --cumulative --sort=mean --daysago=3 --verbose %(pname)s debug.log --cumulative --sort=mean --daysago=3 --verbose
Show cumulative report statistics sorted by mean for entries in the log Show cumulative report statistics sorted by mean for entries in the log
which happened three days ago, and do not trim the URL in the resulting report. which happened three days ago, and do not trim the URL in the resulting
report.
%(pname)s debug.log --urlfocus='/manage_main' --urlfocustime=60 %(pname)s debug.log --urlfocus='/manage_main' --urlfocustime=60
...@@ -673,9 +732,10 @@ Examples: ...@@ -673,9 +732,10 @@ Examples:
%(pname)s --readstats='requests.stat' --detailed %(pname)s --readstats='requests.stat' --detailed
Read from 'requests.stat' stats file (instead of actual -M log files) Read from 'requests.stat' stats file (instead of actual -M log files)
and show detailed report against this data.""" % {'pname':pname} and show detailed report against this data.""" % {'pname': pname}
return details return details
def usage(basic=1): def usage(basic=1):
usage = ( usage = (
""" """
...@@ -691,8 +751,8 @@ Usage: %s filename1 [filename2 ...] ...@@ -691,8 +751,8 @@ Usage: %s filename1 [filename2 ...]
[--help] [--help]
Provides a profile of one or more Zope "-M" request log files. Provides a profile of one or more Zope "-M" request log files.
""" % sys.argv[0] """ % sys.argv[0])
)
if basic == 1: if basic == 1:
usage = usage + """ usage = usage + """
If the --help argument is given, detailed usage docs are provided.""" If the --help argument is given, detailed usage docs are provided."""
...@@ -703,7 +763,9 @@ def main(): ...@@ -703,7 +763,9 @@ def main():
if len(sys.argv) == 1: if len(sys.argv) == 1:
print usage() print usage()
sys.exit(0) sys.exit(0)
if sys.argv[1] == '--help': print detailedusage(); sys.exit(0) if sys.argv[1] == '--help':
print(detailedusage())
sys.exit(0)
mode = 'cumulative' mode = 'cumulative'
sortby = None sortby = None
trim = 0 trim = 0
...@@ -711,9 +773,9 @@ def main(): ...@@ -711,9 +773,9 @@ def main():
verbose = 0 verbose = 0
start = None start = None
end = None end = None
resolution=60 resolution = 60
urlfocustime=10 urlfocustime = 10
urlfocusurl=None urlfocusurl = None
statsfname = None statsfname = None
readstats = 0 readstats = 0
writestats = 0 writestats = 0
...@@ -722,8 +784,8 @@ def main(): ...@@ -722,8 +784,8 @@ def main():
i = 1 i = 1
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
if arg[:2] != '--': if arg[:2] != '--':
if arg[-3:] == '.gz' and globals().has_key('gzip'): if arg[-3:] == '.gz' and 'gzip' in globals():
files.append(gzip.GzipFile(arg,'r')) files.append(gzip.GzipFile(arg, 'r'))
else: else:
files.append(open(arg)) files.append(open(arg))
sys.argv.remove(arg) sys.argv.remove(arg)
...@@ -731,60 +793,69 @@ def main(): ...@@ -731,60 +793,69 @@ def main():
try: try:
opts, extra = getopt.getopt( opts, extra = getopt.getopt(
sys.argv[1:], '', ['sort=', 'top=', 'help', 'verbose', 'today', sys.argv[1:], '', [
'cumulative', 'detailed', 'timed','start=', 'sort=', 'top=', 'help', 'verbose', 'today',
'end=','resolution=', 'writestats=','daysago=', 'cumulative', 'detailed', 'timed', 'start=',
'readstats=','urlfocus=','urlfocustime='] 'end=', 'resolution=', 'writestats=', 'daysago=',
) 'readstats=', 'urlfocus=', 'urlfocustime=']
)
for opt, val in opts: for opt, val in opts:
if opt=='--readstats': if opt == '--readstats':
statsfname = val statsfname = val
readstats = 1 readstats = 1
elif opt=='--writestats': elif opt == '--writestats':
statsfname = val statsfname = val
writestats = 1 writestats = 1
if opt=='--sort': sortby = val if opt == '--sort':
if opt=='--top': top=int(val) sortby = val
if opt=='--help': print detailedusage(); sys.exit(0) if opt == '--top':
if opt=='--verbose': top = int(val)
if opt == '--help':
print(detailedusage())
sys.exit(0)
if opt == '--verbose':
verbose = 1 verbose = 1
if opt=='--resolution': if opt == '--resolution':
resolution=int(val) resolution = int(val)
if opt=='--today': if opt == '--today':
now = time.localtime(time.time()) now = time.localtime(time.time())
# for testing - now = (2001, 04, 19, 0, 0, 0, 0, 0, -1) # for testing - now = (2001, 04, 19, 0, 0, 0, 0, 0, -1)
start = list(now) start = list(now)
start[3] = start[4] = start[5] = 0 start[3] = start[4] = start[5] = 0
start = time.mktime(start) start = time.mktime(start)
end = list(now) end = list(now)
end[3] = 23; end[4] = 59; end[5] = 59 end[3] = 23
end[4] = 59
end[5] = 59
end = time.mktime(end) end = time.mktime(end)
if opt=='--daysago': if opt == '--daysago':
now = time.localtime(time.time() - int(val)*3600*24 ) now = time.localtime(time.time() - int(val) * 3600 * 24)
# for testing - now = (2001, 04, 19, 0, 0, 0, 0, 0, -1) # for testing - now = (2001, 04, 19, 0, 0, 0, 0, 0, -1)
start = list(now) start = list(now)
start[3] = start[4] = start[5] = 0 start[3] = start[4] = start[5] = 0
start = time.mktime(start) start = time.mktime(start)
end = list(now) end = list(now)
end[3] = 23; end[4] = 59; end[5] = 59 end[3] = 23
end[4] = 59
end[5] = 59
end = time.mktime(end) end = time.mktime(end)
if opt=='--start': if opt == '--start':
start = getdate(val) start = getdate(val)
if opt=='--end': if opt == '--end':
end = getdate(val) end = getdate(val)
if opt=='--detailed': if opt == '--detailed':
mode='detailed' mode = 'detailed'
d_sortby = sortby d_sortby = sortby
if opt=='--cumulative': if opt == '--cumulative':
mode='cumulative' mode = 'cumulative'
if opt=='--timed': if opt == '--timed':
mode='timed' mode = 'timed'
if opt=='--urlfocus': if opt == '--urlfocus':
mode='urlfocus' mode = 'urlfocus'
urlfocusurl = val urlfocusurl = val
if opt=='--urlfocustime': if opt == '--urlfocustime':
urlfocustime=int(val) urlfocustime = int(val)
validcumsorts = ['url', 'hits', 'hangs', 'max', 'min', 'median', validcumsorts = ['url', 'hits', 'hangs', 'max', 'min', 'median',
'mean', 'total'] 'mean', 'total']
...@@ -793,14 +864,16 @@ def main(): ...@@ -793,14 +864,16 @@ def main():
'active', 'url'] 'active', 'url']
if mode == 'cumulative': if mode == 'cumulative':
if sortby is None: sortby = 'total' if sortby is None:
sortby = 'total'
assert sortby in validcumsorts, (sortby, mode, validcumsorts) assert sortby in validcumsorts, (sortby, mode, validcumsorts)
if sortby in ['url']: if sortby in ['url']:
sortf = Sort(sortby, ascending=1) sortf = Sort(sortby, ascending=1)
else: else:
sortf = Sort(sortby) sortf = Sort(sortby)
elif mode == 'detailed': elif mode == 'detailed':
if sortby is None: sortby = 'start' if sortby is None:
sortby = 'start'
assert sortby in validdetsorts, (sortby, mode, validdetsorts) assert sortby in validdetsorts, (sortby, mode, validdetsorts)
if sortby in ['start', 'url', 'httpcode']: if sortby in ['start', 'url', 'httpcode']:
sortf = Sort(sortby, ascending=1) sortf = Sort(sortby, ascending=1)
...@@ -808,14 +881,15 @@ def main(): ...@@ -808,14 +881,15 @@ def main():
sortf = codesort sortf = codesort
else: else:
sortf = Sort(sortby) sortf = Sort(sortby)
elif mode=='timed': elif mode == 'timed':
sortf = None sortf = None
elif mode=='urlfocus': elif mode == 'urlfocus':
sortf = Sort('start', ascending=1) sortf = Sort('start', ascending=1)
else: else:
raise ValueError('Invalid mode') raise ValueError('Invalid mode')
req=get_requests(files, start, end, statsfname, writestats, readstats) req = get_requests(
files, start, end, statsfname, writestats, readstats)
analyze(req, top, sortf, start, end, mode, resolution, urlfocusurl, analyze(req, top, sortf, start, end, mode, resolution, urlfocusurl,
urlfocustime) urlfocustime)
......
...@@ -37,13 +37,22 @@ for all necessary information. The available options are: ...@@ -37,13 +37,22 @@ for all necessary information. The available options are:
Filename is required and should be the name of the file to store the Filename is required and should be the name of the file to store the
information in (usually "inituser" or "access"). information in (usually "inituser" or "access").
""" """
import sys, sha, binascii, random, getopt, getpass, os import binascii
import getopt
import getpass
import os
import random
import sha
import sys
try: try:
from crypt import crypt from crypt import crypt
except ImportError: except ImportError:
crypt = None crypt = None
if sys.version_info > (3, 0):
raw_input = input
PROGRAM = sys.argv[0] PROGRAM = sys.argv[0]
COMMASPACE = ', ' COMMASPACE = ', '
...@@ -53,10 +62,11 @@ def generate_salt(): ...@@ -53,10 +62,11 @@ def generate_salt():
salt_choices = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" salt_choices = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"0123456789./") "0123456789./")
return random.choice(salt_choices)+random.choice(salt_choices) return random.choice(salt_choices) + random.choice(salt_choices)
def generate_passwd(password, encoding): def generate_passwd(password, encoding):
encoding=encoding.upper() encoding = encoding.upper()
if encoding == 'SHA': if encoding == 'SHA':
pw = '{SHA}' + binascii.b2a_base64(sha.new(password).digest())[:-1] pw = '{SHA}' + binascii.b2a_base64(sha.new(password).digest())[:-1]
elif encoding == 'CRYPT': elif encoding == 'CRYPT':
...@@ -68,26 +78,28 @@ def generate_passwd(password, encoding): ...@@ -68,26 +78,28 @@ def generate_passwd(password, encoding):
return pw return pw
def write_generated_password(home, ac_path, username): def write_generated_password(home, ac_path, username):
pw_choices = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" pw_choices = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"0123456789!") "0123456789!")
acfile=open(ac_path, 'w') acfile = open(ac_path, 'w')
pw = '' pw = ''
for i in range(8): for i in range(8):
pw = pw + random.choice(pw_choices) pw = pw + random.choice(pw_choices)
acfile.write('%s:%s\n' % (username, generate_passwd(pw, 'SHA'))) acfile.write('%s:%s\n' % (username, generate_passwd(pw, 'SHA')))
acfile.close() acfile.close()
os.chmod(ac_path, 0644) os.chmod(ac_path, 0o644)
return pw return pw
def write_access(home, user='', group=''): def write_access(home, user='', group=''):
ac_path=os.path.join(home, 'access') ac_path = os.path.join(home, 'access')
if not os.path.exists(ac_path): if not os.path.exists(ac_path):
print '-'*78 print('-' * 78)
print 'creating default access file' print('creating default access file')
pw = write_generated_password(home, ac_path, 'emergency') pw = write_generated_password(home, ac_path, 'emergency')
print """Note: print("""Note:
The emergency user name and password are 'emergency' The emergency user name and password are 'emergency'
and '%s'. and '%s'.
...@@ -95,9 +107,11 @@ def write_access(home, user='', group=''): ...@@ -95,9 +107,11 @@ def write_access(home, user='', group=''):
zpasswd script. To find out more, type: zpasswd script. To find out more, type:
%s zpasswd.py %s zpasswd.py
""" % (pw, sys.executable) """ % (pw, sys.executable))
import do
do.ch(ac_path, user, group)
import do; do.ch(ac_path, user, group)
def get_password(): def get_password():
while 1: while 1:
...@@ -107,29 +121,31 @@ def get_password(): ...@@ -107,29 +121,31 @@ def get_password():
return password return password
else: else:
password = verify = '' password = verify = ''
print "Password mismatch, please try again..." print("Password mismatch, please try again...")
def write_inituser(home, user='', group=''): def write_inituser(home, user='', group=''):
ac_path=os.path.join(home, 'inituser') ac_path = os.path.join(home, 'inituser')
if not os.path.exists(ac_path): if not os.path.exists(ac_path):
print '-'*78 print('-' * 78)
print 'creating default inituser file' print('creating default inituser file')
pw = write_generated_password(home, ac_path, 'admin') pw = write_generated_password(home, ac_path, 'admin')
print """Note: print("""Note:
The initial user name and password are 'admin' The initial user name and password are 'admin'
and '%s'. and '%s'.
You can change the name and password through the web You can change the name and password through the web
interface or using the 'zpasswd.py' script. interface or using the 'zpasswd.py' script.
""" % pw """ % pw)
import do; do.ch(ac_path, user, group) import do
do.ch(ac_path, user, group)
def usage(code, msg=''): def usage(code, msg=''):
print >> sys.stderr, __doc__ % globals() sys.stderr.write(__doc__ % globals())
if msg: if msg:
print >> sys.stderr, msg sys.stderr.write(msg)
sys.exit(code) sys.exit(code)
...@@ -143,7 +159,7 @@ def main(): ...@@ -143,7 +159,7 @@ def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts) opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
except getopt.error, msg: except getopt.error as msg:
usage(1, msg) usage(1, msg)
# Defaults # Defaults
...@@ -183,15 +199,15 @@ def main(): ...@@ -183,15 +199,15 @@ def main():
break break
password = get_password() password = get_password()
while 1: while 1:
print """ print("""
Please choose a format from: Please choose a format from:
SHA - SHA-1 hashed password (default) SHA - SHA-1 hashed password (default)
CRYPT - UNIX-style crypt password CRYPT - UNIX-style crypt password
CLEARTEXT - no protection CLEARTEXT - no protection
""" """)
encoding = raw_input("Encoding: ") encoding = raw_input("Encoding: ")
if encoding == '': if encoding == '':
encoding = 'SHA' encoding = 'SHA'
...@@ -210,5 +226,5 @@ CLEARTEXT - no protection ...@@ -210,5 +226,5 @@ CLEARTEXT - no protection
# If called from the command line # If called from the command line
if __name__=='__main__': if __name__ == '__main__':
main() main()
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