Skip to content

Commit

Permalink
Bug 1275903 - Convert logslice API to use requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wlach committed May 27, 2016
1 parent 9a0f745 commit 10e812d
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions treeherder/webapp/api/logslice.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import gzip
import json
import urllib2

from django.conf import settings
from django.core.cache import caches
from django.utils.six import BytesIO
from rest_framework import viewsets
from rest_framework.response import Response

from treeherder.etl.common import make_request
from treeherder.model.models import (Job,
JobLog)
from treeherder.webapp.api.utils import with_jobs
Expand All @@ -21,15 +20,6 @@ class LogSliceView(viewsets.ViewSet):
This view serves slices of the log
"""

def get_log_handle(self, url):
"""Hook to get a handle to the log with this url"""
req = urllib2.Request(url)
req.add_header('User-Agent', settings.TREEHERDER_USER_AGENT)
return urllib2.urlopen(
req,
timeout=settings.REQUESTS_TIMEOUT
)

@with_jobs
def list(self, request, project, jm):
"""
Expand All @@ -45,7 +35,6 @@ def list(self, request, project, jm):
log_names = ["buildbot_text", "builds-4h"]
format = 'json' if log_name == 'mozlog_json' else 'text'

handle = None
gz_file = None

start_line = request.query_params.get("start_line")
Expand Down Expand Up @@ -77,16 +66,14 @@ def list(self, request, project, jm):

try:
gz_file = filesystem.get(url)

if not gz_file:
handle = self.get_log_handle(url)
gz_file = gzip.GzipFile(fileobj=BytesIO(handle.read()))
r = make_request(url)
gz_file = gzip.GzipFile(fileobj=BytesIO(r.content))
filesystem.set(url, gz_file.fileobj)
else:
gz_file = gzip.GzipFile(fileobj=gz_file)

lines = []

for i, line in enumerate(gz_file):
if i < start_line:
continue
Expand All @@ -101,7 +88,5 @@ def list(self, request, project, jm):
return Response(lines)

finally:
if handle:
handle.close()
if gz_file:
gz_file.close()

0 comments on commit 10e812d

Please sign in to comment.