Skip to content

Commit

Permalink
Note: This change is to support a project that I am working on. You s…
Browse files Browse the repository at this point in the history
…hould see no change in the behavior of your current Autotest installations.

-----

Implement the models and set up the RPC framework for the Test Planner

Signed-off-by: James Ren <[email protected]>
  • Loading branch information
James Ren committed Dec 21, 2009
1 parent 0478e3a commit f4e03a9
Show file tree
Hide file tree
Showing 17 changed files with 859 additions and 57 deletions.
5 changes: 3 additions & 2 deletions frontend/afe/frontend_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ def _setup_dummy_user(self):
thread_local.set_user(self.user)


def _frontend_common_setup(self):
def _frontend_common_setup(self, fill_data=True):
self.god = mock.mock_god()
self._open_test_db()
self._setup_dummy_user()
self._fill_in_test_data()
if fill_data:
self._fill_in_test_data()


def _frontend_common_teardown(self):
Expand Down
47 changes: 47 additions & 0 deletions frontend/afe/model_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,3 +943,50 @@ def set_or_delete_attribute(self, attribute, value):
self.delete_attribute(attribute)
else:
self.set_attribute(attribute, value)


class ModelWithHashManager(dbmodels.Manager):
"""Manager for use with the ModelWithHash abstract model class"""

def create(self, **kwargs):
raise Exception('ModelWithHash manager should use get_or_create() '
'instead of create()')


def get_or_create(self, **kwargs):
kwargs['the_hash'] = self.model._compute_hash(**kwargs)
return super(ModelWithHashManager, self).get_or_create(**kwargs)


class ModelWithHash(dbmodels.Model):
"""Superclass with methods for dealing with a hash column"""

the_hash = dbmodels.CharField(max_length=40, unique=True)

objects = ModelWithHashManager()

class Meta:
abstract = True


@classmethod
def _compute_hash(cls, **kwargs):
raise NotImplementedError('Subclasses must override _compute_hash()')


def save(self, force_insert=False, **kwargs):
"""Prevents saving the model in most cases
We want these models to be immutable, so the generic save() operation
will not work. These models should be instantiated through their the
model.objects.get_or_create() method instead.
The exception is that save(force_insert=True) will be allowed, since
that creates a new row. However, the preferred way to make instances of
these models is through the get_or_create() method.
"""
if not force_insert:
# Allow a forced insert to happen; if it's a duplicate, the unique
# constraint will catch it later anyways
raise Exception('ModelWithHash is immutable')
super(ModelWithHash, self).save(force_insert=force_insert, **kwargs)
18 changes: 17 additions & 1 deletion frontend/afe/rpc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

__author__ = '[email protected] (Steve Howard)'

import datetime, os
import datetime, os, sys
import django.http
from autotest_lib.frontend.afe import models, model_logic

Expand Down Expand Up @@ -620,3 +620,19 @@ def interleave_entries(queue_entries, special_tasks):
special_task_index += 1

return interleaved_entries


def get_sha1_hash(source):
"""Gets the SHA-1 hash of the source string
@param source The string to hash
"""
if sys.version_info < (2,5):
import sha
digest = sha.new()
else:
import hashlib
digest = hashlib.sha1()

digest.update(source)
return digest.hexdigest()
31 changes: 8 additions & 23 deletions frontend/afe/urls.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
from django.conf.urls.defaults import *
import os
from autotest_lib.frontend import settings
import common
from autotest_lib.frontend import settings, urls_common
from autotest_lib.frontend.afe.feeds import feed

feeds = {
'jobs' : feed.JobFeed
}

pattern_list = [
(r'^(?:|noauth/)rpc/', 'frontend.afe.views.handle_rpc'),
(r'^rpc_doc', 'frontend.afe.views.rpc_documentation'),
]
pattern_list, debug_pattern_list = (
urls_common.generate_pattern_lists('frontend.afe', 'AfeClient'))

debug_pattern_list = [
(r'^model_doc/', 'frontend.afe.views.model_documentation'),
# for GWT hosted mode
(r'^(?P<forward_addr>autotest.*)', 'frontend.afe.views.gwt_forward'),
# for GWT compiled files
(r'^client/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': os.path.join(os.path.dirname(__file__), '..', 'client',
'www')}),
# redirect / to compiled client
(r'^$', 'django.views.generic.simple.redirect_to',
{'url': 'client/autotest.AfeClient/AfeClient.html'}),

# Job feeds
(r'^feeds/(?P<url>.*)/$', 'frontend.afe.feeds.feed.feed_view',
{'feed_dict': feeds})

]
# Job feeds
debug_pattern_list.append((
r'^feeds/(?P<url>.*)/$', 'frontend.afe.feeds.feed.feed_view',
{'feed_dict': feeds}))

if settings.DEBUG:
pattern_list += debug_pattern_list
Expand Down
11 changes: 4 additions & 7 deletions frontend/afe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.http import HttpResponseServerError
from django.template import Context, loader
from autotest_lib.client.common_lib import utils
from autotest_lib.frontend import views_common
from autotest_lib.frontend.afe import models, rpc_handler, rpc_interface
from autotest_lib.frontend.afe import rpc_utils

Expand All @@ -26,13 +27,9 @@ def rpc_documentation(request):


def model_documentation(request):
doc = '<h2>Models</h2>\n'
for model_name in ('Label', 'Host', 'Test', 'User', 'AclGroup', 'Job',
'AtomicGroup'):
model_class = getattr(models, model_name)
doc += '<h3>%s</h3>\n' % model_name
doc += '<pre>\n%s</pre>\n' % model_class.__doc__
return HttpResponse(doc)
model_names = ('Label', 'Host', 'Test', 'User', 'AclGroup', 'Job',
'AtomicGroup')
return views_common.model_documentation(models, model_names)


def redirect_with_extra_data(request, url, **kwargs):
Expand Down
Loading

0 comments on commit f4e03a9

Please sign in to comment.