Created
December 7, 2012 05:07
-
-
Save butlern/4230936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import logging | |
import mechanize | |
def get_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-host", "--hostname", dest="hostname", default="localhost", | |
help="The hostname of the server") | |
parser.add_argument("-p", "--port", dest="port", default="4502", | |
help="The server port") | |
parser.add_argument("-u", "--username", dest="username", default="admin", | |
help="The username to login as") | |
parser.add_argument("-pass", "--password", dest="password", default="admin", | |
help="The password to login with") | |
parser.add_argument("-m", "--memgc", dest="memgc", action="store_true", | |
help="Whether to enable Memory Garbage collection") | |
parser.add_argument("-d", "--delete", dest="delete", action="store_true", | |
help="Whether to automatically delete unused items") | |
parser.add_argument("-s", "--sleep", dest="sleep", type=int, default=10, | |
help="The time (in ms) to sleep between gc cycles") | |
parser.add_argument("-w", "--workspace", dest="workspace", default="crx.default", | |
help="The CQ workspace to login to and act upon") | |
parser.add_argument("--ssl", dest="ssl", action="store_true", | |
help="Enable for SSL connections") | |
return parser.parse_args() | |
def get_session(args): | |
"""Return a mechanize object with an authenticated session""" | |
url = ( "%s:%s/crx/login.jsp?UserId=%s&Password=%s&workspace=%s" % | |
(args.hostname, args.port, args.username, args.password, args.workspace)) | |
if args.ssl: | |
url = ''.join(["https://", url]) | |
else: | |
url = ''.join(["http://", url]) | |
logging.info("Authenticating with URL %s" % url) | |
sess = mechanize.Browser() | |
sess.open(url) | |
return sess | |
def gc_run(args): | |
"""Run the datastore garbage collection on an authenticated mechanize object""" | |
url = "%s:%s/crx/config/datastore_gc.jsp?action=run" % (args.hostname, args.port) | |
if args.ssl: | |
url = ''.join(["https://", url]) | |
else: | |
url = ''.join(["http://", url]) | |
if args.memgc: | |
url = ''.join([url, "&memGc=checked"]) | |
if args.sleep: | |
url = ''.join([url, "&sleep=%i" % args.sleep]) | |
if args.delete: | |
url = ''.join([url,"&delete=checked"]) | |
args.sess.open(url) | |
logging.info("Running garbage collection with URL %s" % url) | |
def run(): | |
args = get_args() | |
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) | |
args.sess = get_session(args) | |
gc_run(args) | |
for line in args.sess.response().readlines(): | |
print line.strip() | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment