Permalink
Browse files

Reviewer changes.

  • Loading branch information...
1 parent 15860cf commit 2d7f9dbae0e4657baa359130ad5f7c5e1f0e3103 @brandonsavage brandonsavage committed Jan 30, 2013
Showing with 40 additions and 24 deletions.
  1. +31 −16 dragnet/dll/cron.py
  2. +8 −6 dragnet/dll/models.py
  3. +1 −2 migrations/07-add-cron.sql
View
@@ -2,37 +2,52 @@
import csv
import urllib
import datetime
+import logging
from dragnet.dll.models import File
from django.contrib.auth.models import User
-from django.db import IntegrityError
+
+logger = logging.getLogger("import")
-
class ImproperStatusCode(Exception):
pass
@cronjobs.register
-def updateModuleData():
- last_report = datetime.date.today() - datetime.timedelta(days=2)
- reportdatestring = last_report.strftime('%Y%m%d')
- baseurl = ('https://crash-analysis.mozilla.com/crash_analysis/modulelist/'
- '%s-modulelist.txt' % reportdatestring)
+def update_module_data():
+ last_report = (datetime.datetime.utcnow() -
+ datetime.timedelta(days=2)).date()
+ report_date_string = last_report.strftime('%Y%m%d')
+ baseurl = (
+ 'https://crash-analysis.mozilla.com/crash_analysis/modulelist/'
+ '%s-modulelist.txt' % report_date_string
+ )
webpage = urllib.urlopen(baseurl)
code = webpage.getcode()
if code != 200:
- raise ImproperStatusCode('Status code for %s was: %s' % (baseurl, code))
+ raise ImproperStatusCode('Status code for %s was: %s' %
+ (baseurl, code))
datareader = csv.reader(webpage)
# System user is created when the database is created.
- sysuser = User.objects.get(username='system')
+ sys_user, sys_user_created = User.objects.get_or_create(
+ username='system',
+ first_name='System',
+ )
+
+ if sys_user_created:
+ sys_user.save()
+
for row in datareader:
try:
- File.objects.create(created_by=sysuser, modified_by=sysuser, file_name=row[0], debug_filename=row[1], debug=row[2], version=row[3])
- except IntegrityError:
- # When a module is already in the database it cannot be added
- # again. It will raise an IntegrityError on the unique index,
- # which is expected behavior. This is a safe exception to ignore.
- pass
-
+ File.objects.create(
+ created_by=sys_user,
+ modified_by=sys_user,
+ file_name=row[0],
+ debug_filename=row[1],
+ debug=row[2],
+ version=row[3]
+ )
+ except Exception, err:
+ logger.info('Import error: %s', err)
return 0
View
@@ -15,7 +15,7 @@ class File(models.Model):
(STATUS_VALID, 'Valid'),
(STATUS_MALWARE, 'Malware')
)
-
+
PLATFORM_WINDOWS = 'Windows'
PLATFORM_LINUX = 'Linux'
PLATFORM_MAC = 'Mac OS X'
@@ -24,7 +24,7 @@ class File(models.Model):
(PLATFORM_LINUX, 'Linux'),
(PLATFORM_MAC, 'Mac OS X')
)
-
+
date_created = models.DateTimeField(default=datetime.datetime.utcnow)
date_modified = models.DateTimeField(default=datetime.datetime.utcnow,
auto_now=True)
@@ -33,7 +33,8 @@ class File(models.Model):
file_name = models.CharField(max_length=200)
common_name = models.CharField(max_length=200, blank=True, null=True)
version = models.CharField(max_length=100, blank=True, null=True)
- platform = models.CharField(max_length=10, choices=PLATFORM_CHOICES, blank=True)
+ platform = models.CharField(max_length=10, choices=PLATFORM_CHOICES,
+ blank=True)
vendor = models.CharField(max_length=200, blank=True, null=True)
distributors = models.CharField(max_length=200, blank=True, null=True)
md5_hash = models.CharField(max_length=32, blank=True, null=True)
@@ -47,9 +48,9 @@ class File(models.Model):
def __unicode__(self):
return self.file_name
-
+
class Meta:
- unique_together = ('file_name', 'md5_hash', 'debug')
+ unique_together = ('file_name', 'debug_filename', 'debug')
class Comment(models.Model):
@@ -82,7 +83,8 @@ def compare_history(sender, instance, **kwargs):
existing = File.objects.get(pk=instance.id)
for key in EVALUATE:
- if getattr(existing, key) != getattr(instance, key) and any([getattr(existing, key), getattr(instance, key)]):
+ if (getattr(existing, key) != getattr(instance, key) and
+ any([getattr(existing, key), getattr(instance, key)])):
user = User.objects.get(pk=instance.modified_by_id)
FileHistory.objects.create(user=user,
dll=existing,
@@ -1,3 +1,2 @@
DROP INDEX `unique_index` on dll_file;
-CREATE UNIQUE INDEX `unique_index` ON dll_file (file_name, debug, debug_filename);
-INSERT INTO `auth_user` (username, first_name, is_staff, is_active, is_superuser, last_login, date_joined) VALUES ('system', 'System', 0, 0, 0, NOW(), NOW());
+CREATE UNIQUE INDEX `unique_index_3489dh2` ON dll_file (file_name, debug, debug_filename);

0 comments on commit 2d7f9db

Please sign in to comment.