Skip to content

Commit

Permalink
Crawler: added support for authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury Yurevich authored and ericholscher committed Oct 3, 2010
1 parent 8ab5364 commit 2f7c79a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test_utils/crawler/base.py
Expand Up @@ -5,6 +5,7 @@


from django.conf import settings from django.conf import settings
from django.db import transaction from django.db import transaction
from django.views.debug import cleanse_setting
from django.test.client import Client from django.test.client import Client
from django.test.utils import setup_test_environment, teardown_test_environment from django.test.utils import setup_test_environment, teardown_test_environment


Expand Down Expand Up @@ -55,6 +56,8 @@ def __init__(self, base_url, conf_urls={}, verbosity=1, output_dir=None, ascend=
self.verbosity = verbosity self.verbosity = verbosity
self.ascend = ascend self.ascend = ascend


auth = kwargs.get('auth')

if output_dir: if output_dir:
assert os.path.isdir(output_dir) assert os.path.isdir(output_dir)
self.output_dir = os.path.realpath(output_dir) self.output_dir = os.path.realpath(output_dir)
Expand All @@ -68,6 +71,13 @@ def __init__(self, base_url, conf_urls={}, verbosity=1, output_dir=None, ascend=


self.c = Client(REMOTE_ADDR='127.0.0.1') self.c = Client(REMOTE_ADDR='127.0.0.1')


if auth:
printable_auth = ', '.join(
'%s: %s' % (key, cleanse_setting(key.upper(), value))
for key, value in auth.items())
LOG.info('Log in with %s' % printable_auth)
self.c.login(**auth)

self.plugins = [] self.plugins = []
for plug in Plugin.__subclasses__(): for plug in Plugin.__subclasses__():
active = getattr(plug, 'active', True) active = getattr(plug, 'active', True)
Expand Down
21 changes: 21 additions & 0 deletions test_utils/management/commands/crawlurls.py
Expand Up @@ -33,6 +33,8 @@ class Command(BaseCommand):
help='If specified, store plugin output in the provided directory'), help='If specified, store plugin output in the provided directory'),
make_option('--no-parent', action='store_true', dest="no_parent", default=False, make_option('--no-parent', action='store_true', dest="no_parent", default=False,
help='Do not crawl URLs which do not start with your base URL'), help='Do not crawl URLs which do not start with your base URL'),
make_option('-a', "--auth", action='store', dest='auth', default=None,
help='Authenticate (login:user,password:secret) before crawl')
) )


help = "Displays all of the url matching routes for the project." help = "Displays all of the url matching routes for the project."
Expand All @@ -42,6 +44,8 @@ def handle(self, *args, **options):
verbosity = int(options.get('verbosity', 1)) verbosity = int(options.get('verbosity', 1))
depth = int(options.get('depth', 3)) depth = int(options.get('depth', 3))


auth = _parse_auth(options.get('auth'))

if verbosity > 1: if verbosity > 1:
log_level = logging.DEBUG log_level = logging.DEBUG
elif verbosity: elif verbosity:
Expand Down Expand Up @@ -94,6 +98,7 @@ def handle(self, *args, **options):
verbosity=verbosity, verbosity=verbosity,
output_dir=options.get("output_dir"), output_dir=options.get("output_dir"),
ascend=not options.get("no_parent"), ascend=not options.get("no_parent"),
auth=auth,
) )


# Load plugins: # Load plugins:
Expand Down Expand Up @@ -128,3 +133,19 @@ def handle(self, *args, **options):
sys.exit(1) sys.exit(1)
else: else:
sys.exit(0) sys.exit(0)


def _parse_auth(auth):
"""
Parse auth string and return dict.
>>> _parse_auth('login:user,password:secret')
{'login': 'user', 'password': 'secret'}
>>> _parse_auth('name:user, token:top:secret')
{'name': 'user', 'token': 'top:secret'}
"""
if not auth:
return None
items = auth.split(',')
return dict(i.strip().split(':', 1) for i in items)

0 comments on commit 2f7c79a

Please sign in to comment.