From ad3acd9acbb965ad0869349c2741760776d3e66b Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Thu, 14 Apr 2011 20:04:55 +0200 Subject: [PATCH] PEP-008 compat. --- mountain/core/__init__.py | 1 - mountain/core/admin.py | 11 ++++++++--- mountain/core/models.py | 21 ++++++++++++++------- mountain/core/registration.py | 24 +++++++++++++----------- mountain/core/signals.py | 3 ++- mountain/core/utils.py | 13 ++++++++----- mountain/core/views.py | 15 +++++++++------ mountain/monitor/admin.py | 13 ++++++++++--- mountain/monitor/computerinfo.py | 17 +++++++++++------ mountain/monitor/models.py | 3 ++- mountain/monitor/processorinfo.py | 10 +++++++--- mountain/packages/models.py | 1 + mountain/packages/packages.py | 21 +++++++++++++++------ mountain/packages/tests.py | 2 +- mountain/packages/utils.py | 1 + 15 files changed, 102 insertions(+), 54 deletions(-) diff --git a/mountain/core/__init__.py b/mountain/core/__init__.py index a3a46d6..ee702b3 100644 --- a/mountain/core/__init__.py +++ b/mountain/core/__init__.py @@ -1,2 +1 @@ from mountain.core import registration - diff --git a/mountain/core/admin.py b/mountain/core/admin.py index ce45401..cadf9bc 100644 --- a/mountain/core/admin.py +++ b/mountain/core/admin.py @@ -1,24 +1,27 @@ from django.contrib import admin, messages from django.utils.simplejson import dumps -from mountain.core.models import * +from mountain.core.models import Computer, Company, Message from mountain.core.utils import hash_types + def force_resync(modeladmin, request, queryset): for comp in queryset: Message.objects.create(computer=comp, message=dumps({ # TODO: Fix operation-id 'type': 'resynchronize', 'operation-id': 1})) - messages.info(request, 'Queued resyncronisation for the selected computers.') + messages.info(request, + 'Queued resyncronisation for the selected computers.') + def confirm_computer(modeladmin, request, queryset): for comp in queryset: - plugins = comp.company.activated_plugins.values_list('identifier', flat=True).order_by('identifier') Message.objects.create(computer=comp, message=dumps({ 'type': 'registration-done'})) queryset.update(confirmed=True) messages.info(request, 'Queued confirmation for the selected computers.') + def set_intervals(modelsamdin, request, queryset): for comp in queryset: Message.objects.create(computer=comp, message=dumps({ @@ -28,11 +31,13 @@ def set_intervals(modelsamdin, request, queryset): 'urgent-exchange': 10})) messages.info(request, 'Queued set-intervals for the selected computers.') + class ComputerAdmin(admin.ModelAdmin): actions = [force_resync, confirm_computer, set_intervals] exclude = ('client_accepted_types_hash',) readonly_fields = ('confirmed',) + class CompanyAdmin(admin.ModelAdmin): exclude = ('activated_plugins_hash',) diff --git a/mountain/core/models.py b/mountain/core/models.py index 503987e..15ccda9 100644 --- a/mountain/core/models.py +++ b/mountain/core/models.py @@ -4,6 +4,7 @@ from taggit.managers import TaggableManager + class AcceptedTypes(models.Model): """This models holds the message types which are supported by this server. They are selectable through the company model, so each customer can @@ -14,14 +15,19 @@ class AcceptedTypes(models.Model): def __unicode__(self): return self.identifier + class Company(models.Model): """Company is used to define administrator and the activated plugins. """ verbose_name = models.CharField(_('verbose name'), max_length=255) - account_name = models.CharField(_('account name'), max_length=255, unique=True) - registration_password = models.CharField(_('registration password'), max_length=255) - administratos = models.ManyToManyField(User, verbose_name=_('administrators')) - activated_plugins = models.ManyToManyField(AcceptedTypes, verbose_name=_('activated plugins')) + account_name = models.CharField(_('account name'), max_length=255, + unique=True) + registration_password = models.CharField(_('registration password'), + max_length=255) + administratos = models.ManyToManyField(User, + verbose_name=_('administrators')) + activated_plugins = models.ManyToManyField(AcceptedTypes, + verbose_name=_('activated plugins')) activated_plugins_hash = models.CharField(max_length=255) def __unicode__(self): @@ -31,6 +37,7 @@ class Meta: verbose_name = _('Company') verbose_name_plural = _('Companies') + class Computer(models.Model): """Represents one Computer, information is pulled from the client registration request. Secure id is used to identify the computer, @@ -42,7 +49,8 @@ class Computer(models.Model): hostname = models.CharField(_('hostname'), max_length=255, unique=True) # TODO: uniqness of secureid secure_id = models.TextField(_('secure id')) - insecure_id = models.CharField(_('insecure id'), max_length=36, unique=True) + insecure_id = models.CharField(_('insecure id'), max_length=36, + unique=True) client_accepted_types_hash = models.CharField(max_length=255) confirmed = models.BooleanField(_('confirmed'), default=False) @@ -59,6 +67,7 @@ class Meta: verbose_name_plural = _('Computers') unique_together = ('company', 'computer_title') + class Message(models.Model): """Message which is queued and should get send to the client""" computer = models.ForeignKey(Computer, verbose_name=_('computer')) @@ -67,5 +76,3 @@ class Message(models.Model): class Meta: verbose_name = _('Message') verbose_name_plural = _('Messages') - - diff --git a/mountain/core/registration.py b/mountain/core/registration.py index 12b330e..c8a2be3 100644 --- a/mountain/core/registration.py +++ b/mountain/core/registration.py @@ -1,36 +1,38 @@ -import uuid, string +import string +import uuid from random import choice -from django.utils.simplejson import dumps - from taggit.utils import parse_tags -from mountain.core.models import Computer, Company, Message +from mountain.core.models import Computer, Company from mountain.core.signals import message_available from mountain.core.utils import MessageType, hash_types CHARS = string.ascii_letters + string.digits + string.punctuation + def handle_registration(sender, computer, request_data, msg_data, **kwargs): try: company = Company.objects.get(account_name=msg_data['account_name']) except Company.DoesNotExist: - return [{'type':'registration', 'info':'unknown-account'}] + return [{'type':'registration', 'info':'unknown-account'}] if msg_data['registration_password'] != company.registration_password: return [{'type':'registration', 'info':'unknown-account'}] comp = Computer() - comp.hostname=msg_data['hostname'] - comp.company=company - comp.computer_title=msg_data['computer_title'] - comp.secure_id= ''.join([choice(CHARS) for i in range(1600)]) - comp.insecure_id=str(uuid.uuid4()) - comp.client_accepted_types_hash = hash_types(request_data['client-accepted-types']).encode('hex') + comp.hostname = msg_data['hostname'] + comp.company = company + comp.computer_title = msg_data['computer_title'] + comp.secure_id = ''.join([choice(CHARS) for i in range(1600)]) + comp.insecure_id = str(uuid.uuid4()) + comp.client_accepted_types_hash = \ + hash_types(request_data['client-accepted-types']).encode('hex') comp.save() comp.tags.set(*parse_tags(msg_data['tags'])) return [{'type':'set-id', 'id':comp.secure_id, 'insecure-id':comp.insecure_id}] + message_available.connect(handle_registration, sender=MessageType("register")) diff --git a/mountain/core/signals.py b/mountain/core/signals.py index e5911ec..b02da04 100644 --- a/mountain/core/signals.py +++ b/mountain/core/signals.py @@ -1,3 +1,4 @@ import django.dispatch -message_available = django.dispatch.Signal(providing_args=['computer', 'request_data', 'msg_data']) +message_available = django.dispatch.Signal(providing_args=['computer', + 'request_data', 'msg_data']) diff --git a/mountain/core/utils.py b/mountain/core/utils.py index 5b31b75..6129d4c 100644 --- a/mountain/core/utils.py +++ b/mountain/core/utils.py @@ -7,6 +7,7 @@ from mountain.core.settings import SERVER_UUID from mountain.core.models import AcceptedTypes + def render_messages(messages, computer=None, ping_answer=False): """Updates the answer with the server-uuid, pickles it and returns the HttpReponse. @@ -16,17 +17,19 @@ def render_messages(messages, computer=None, ping_answer=False): ret.update({'server-uuid': SERVER_UUID}) if computer: ret.update({ - 'client-accepted-types-hash': computer.client_accepted_types_hash.decode('hex'), - 'next-expected-sequence': computer.next_client_sequence - }) + 'client-accepted-types-hash': + computer.client_accepted_types_hash.decode('hex'), + 'next-expected-sequence': computer.next_client_sequence}) return HttpResponse(dumps(ret)) + def MessageType(type, __instance_cache={}): """Use this to register your receiver function to a string. """ instance = __instance_cache.setdefault(type, object()) return instance + def hash_types(types): """The client only sends the hashed server types, we do the same, compare them and only send new types if the types difer. @@ -35,15 +38,15 @@ def hash_types(types): m.update(";".join(types)) return m.digest() + def register_messagetype(type): from django.db.models.signals import post_syncdb from mountain.core import models def install_type(sender, app, created_models, verbosity=0, **kwargs): - if verbosity>=1: + if verbosity >= 1: obj, created = AcceptedTypes.objects.get_or_create(identifier=type) if created: print "Installed message type %s" % type post_syncdb.connect(install_type, sender=models, weak=False) - diff --git a/mountain/core/views.py b/mountain/core/views.py index cf54491..d0a5205 100644 --- a/mountain/core/views.py +++ b/mountain/core/views.py @@ -17,12 +17,14 @@ message_logger = logging.getLogger('mountain.messaging') + @csrf_exempt def ping(request): id = request.POST.get('insecure_id') msg_count = Message.objects.filter(computer__insecure_id=id).count() return render_messages(bool(msg_count), ping_answer=True) + @csrf_exempt @transaction.commit_on_success def message_system(request): @@ -42,7 +44,8 @@ def message_system(request): computer = Computer.objects.select_related('company')\ .get(secure_id=secure_id) company = computer.company - if not all((computer.next_client_sequence, computer.next_server_sequence)): + if not all((computer.next_client_sequence, + computer.next_server_sequence)): computer.next_client_sequence = data['sequence'] computer.next_server_sequence = data['next-expected-sequence'] computer.save() @@ -52,13 +55,13 @@ def message_system(request): # Special case registration, as we could get a request with nothing, # and without accepting register, we'll never get a registration. if computer is not None and computer.confirmed: - accepted_types = company.activated_plugins.values_list('identifier', flat=True).order_by('identifier') + accepted_types = company.activated_plugins.values_list('identifier', + flat=True).order_by('identifier') accepted_types_hash = company.activated_plugins_hash.decode('hex') else: accepted_types = ['register'] accepted_types_hash = hash_types(['register']) - # Check if sequence numbers match if computer is not None and computer.confirmed and \ ((data['sequence'] != computer.next_client_sequence) or @@ -67,7 +70,8 @@ def message_system(request): # Determine whether we need to notify the client about new/delete types if data.get('accepted-types') != accepted_types_hash: - return_msgs.append({'type':'accepted-types', 'types':list(accepted_types)}) + return_msgs.append({'type': 'accepted-types', + 'types': list(accepted_types)}) for msg in received_msgs: if computer is None and msg['type'] != 'register': @@ -95,7 +99,6 @@ def message_system(request): next_client_sequence = \ F('next_client_sequence') + data['total-messages'], next_server_sequence = \ - F('next_server_sequence') + len(return_msgs) - ) + F('next_server_sequence') + len(return_msgs)) return render_messages(return_msgs, computer=computer) diff --git a/mountain/monitor/admin.py b/mountain/monitor/admin.py index e0376f7..7f1cc39 100644 --- a/mountain/monitor/admin.py +++ b/mountain/monitor/admin.py @@ -1,17 +1,24 @@ from django.contrib import admin -from mountain.monitor.models import ComputerInfo#, ProcessorInfo, HardwarePart +from mountain.monitor.models import ComputerInfo +#, ProcessorInfo, HardwarePart + class ProcessorInfoAdmin(admin.ModelAdmin): - list_display = ('model', 'vendor', 'processor_id', 'cache_size', 'computer') + list_display = ('model', 'vendor', 'processor_id', + 'cache_size', 'computer') list_filter = ('computer',) + class HardwarePartAdmin(admin.ModelAdmin): list_display = ('product', 'vendor', 'computer') list_filter = ('computer',) + class ComputerInfoAdmin(admin.ModelAdmin): - list_display = ('hostname', 'code_name', 'description', 'distributor_id', 'release', 'computer') + list_display = ('hostname', 'code_name', 'description', + 'distributor_id', 'release', 'computer') + #admin.site.register(ProcessorInfo, ProcessorInfoAdmin) #admin.site.register(HardwarePart, HardwarePartAdmin) diff --git a/mountain/monitor/computerinfo.py b/mountain/monitor/computerinfo.py index a20fbd0..7c86744 100644 --- a/mountain/monitor/computerinfo.py +++ b/mountain/monitor/computerinfo.py @@ -1,21 +1,26 @@ from mountain.core.signals import message_available -from mountain.core.utils import MessageType, register_messagetype +from mountain.core.utils import MessageType from mountain.monitor.models import ComputerInfo + def handle_computer_info(sender, computer, request_data, msg_data, **kwargs): comp_info, created = ComputerInfo.objects.get_or_create(computer=computer) for i in ['total-memory', 'total-swap', 'hostname']: - setattr(comp_info, i.replace('-','_'), msg_data[i]) + setattr(comp_info, i.replace('-', '_'), msg_data[i]) computer.hostname = msg_data['hostname'] comp_info.save() return [] + def handle_distribution_info(sender, computer, request_data, msg_data, **kwargs): dist_info, created = ComputerInfo.objects.get_or_create(computer=computer) - for i in ['code-name', 'description', 'distributor-id', 'release'] : - setattr(dist_info, i.replace('-','_'), msg_data[i]) + for i in ['code-name', 'description', 'distributor-id', 'release']: + setattr(dist_info, i.replace('-', '_'), msg_data[i]) dist_info.save() return [] -message_available.connect(handle_computer_info, sender=MessageType('computer-info')) -message_available.connect(handle_distribution_info, sender=MessageType('distribution-info')) + +message_available.connect(handle_computer_info, + sender=MessageType('computer-info')) +message_available.connect(handle_distribution_info, + sender=MessageType('distribution-info')) diff --git a/mountain/monitor/models.py b/mountain/monitor/models.py index 6350acd..e681cec 100644 --- a/mountain/monitor/models.py +++ b/mountain/monitor/models.py @@ -1,7 +1,7 @@ from django.db import models from mountain.core.models import Computer - +# #class ProcessorInfo(models.Model): # computer = models.ForeignKey(Computer) # processor_id = models.SmallIntegerField() @@ -24,6 +24,7 @@ # def __unicode__(self): # return self.product + class ComputerInfo(models.Model): computer = models.ForeignKey(Computer) hostname = models.CharField(max_length=255) diff --git a/mountain/monitor/processorinfo.py b/mountain/monitor/processorinfo.py index 50bb39d..65c871b 100644 --- a/mountain/monitor/processorinfo.py +++ b/mountain/monitor/processorinfo.py @@ -2,17 +2,21 @@ from mountain.core.utils import MessageType from mountain.monitor.models import ProcessorInfo + def handle_proc_info(sender, computer, request_data, msg_data, **kwargs): ProcessorInfo.objects.filter(computer=computer).delete() for p in msg_data['processors']: - p_info = ProcessorInfo(computer=computer, processor_id=p['processor-id'], + p_info = ProcessorInfo(computer=computer, + processor_id=p['processor-id'], model=p['model']) for i in ['cache-size', 'vendor']: if p.get(i): - setattr(p_info, i.replace('-','_'), p.get(i)) + setattr(p_info, i.replace('-', '_'), p.get(i)) p_info.save() return [] -message_available.connect(handle_proc_info, sender=MessageType('processor-info')) + +message_available.connect(handle_proc_info, + sender=MessageType('processor-info')) diff --git a/mountain/packages/models.py b/mountain/packages/models.py index 48c3415..c1c4edf 100644 --- a/mountain/packages/models.py +++ b/mountain/packages/models.py @@ -22,6 +22,7 @@ (3, 'locked'), ) + class PackageManager(models.Manager): def hashes_in_bulk(self, id_list): qs = self.model.objects.filter(hash__in=id_list) diff --git a/mountain/packages/packages.py b/mountain/packages/packages.py index 30f1cb9..e2ae70c 100644 --- a/mountain/packages/packages.py +++ b/mountain/packages/packages.py @@ -1,13 +1,17 @@ from mountain.core.signals import message_available from mountain.core.utils import MessageType -from mountain.packages.models import Package, PackageRelation, PackageStatus, STATUS_CHOICES +from mountain.packages.models import Package, PackageRelation, PackageStatus,\ + STATUS_CHOICES from mountain.packages.utils import get_hash + def handle_unknown_hashes(sender, computer, request_data, msg_data, **kwargs): hashes = [i.encode('hex') for i in msg_data['hashes']] data = Package.objects.hashes_in_bulk(hashes) ids = [(data.get(hash).id if data.get(hash) else None) for hash in hashes] - return [{'type': 'package-ids', 'ids':ids, 'request-id':msg_data['request-id']}] + return [{'type': 'package-ids', 'ids': ids, + 'request-id': msg_data['request-id']}] + def handle_add_packages(sender, computer, request_data, msg_data, **kwargs): ids = [] @@ -23,10 +27,13 @@ def handle_add_packages(sender, computer, request_data, msg_data, **kwargs): target=relation[1]) ids.append(m.id) - return [{'type': 'package-ids', 'ids':ids, 'request-id':msg_data['request-id']}] + return [{'type': 'package-ids', 'ids': ids, + 'request-id':msg_data['request-id']}] + STATUS_TO_ID = dict(map(lambda x: x[::-1], STATUS_CHOICES)) + def handle_packages(sender, computer, request_data, msg_data, **kwargs): for status in ('installed', 'available', 'available-upgrades', 'locked'): ids = [] @@ -37,7 +44,7 @@ def handle_packages(sender, computer, request_data, msg_data, **kwargs): for id_or_range in msg_data.get(s, []): ids_ = ids if not remove else remove_ids if isinstance(id_or_range, tuple): - ids_.extend(range(id_or_range[0], id_or_range[1]+1)) + ids_.extend(range(id_or_range[0], id_or_range[1] + 1)) else: ids_.append(id_or_range) @@ -50,6 +57,8 @@ def handle_packages(sender, computer, request_data, msg_data, **kwargs): return [] -message_available.connect(handle_unknown_hashes, sender=MessageType('unknown-package-hashes')) -message_available.connect(handle_add_packages, sender=MessageType('add-packages')) +message_available.connect(handle_unknown_hashes, + sender=MessageType('unknown-package-hashes')) +message_available.connect(handle_add_packages, + sender=MessageType('add-packages')) message_available.connect(handle_packages, sender=MessageType('packages')) diff --git a/mountain/packages/tests.py b/mountain/packages/tests.py index 2247054..6b60485 100644 --- a/mountain/packages/tests.py +++ b/mountain/packages/tests.py @@ -7,6 +7,7 @@ from django.test import TestCase + class SimpleTest(TestCase): def test_basic_addition(self): """ @@ -20,4 +21,3 @@ def test_basic_addition(self): >>> 1 + 1 == 2 True """} - diff --git a/mountain/packages/utils.py b/mountain/packages/utils.py index e9ad288..f38af13 100644 --- a/mountain/packages/utils.py +++ b/mountain/packages/utils.py @@ -1,5 +1,6 @@ from hashlib import sha1 + def get_hash(type, name, version, relations): digest = sha1("[%d %s %s]" % (type, name, version)) relations.sort()