Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#59 from Khan/support_zipimport
Browse files Browse the repository at this point in the history
Add zipimport compatability to ResourceHandler
  • Loading branch information
tkaitchuck committed Jun 24, 2015
2 parents 3bcd874 + a7b0a80 commit 6cf7d4d
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions python/src/mapreduce/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import pkgutil
import time
import zipfile

from google.appengine.api import validation
from google.appengine.api import yaml_builder
Expand Down Expand Up @@ -282,13 +283,22 @@ def get(self, relative):

real_path, content_type = self._RESOURCE_MAP[relative]
path = os.path.join(os.path.dirname(__file__), "static", real_path)

# It's possible we're inside a zipfile (zipimport). If so, path
# will include 'something.zip'.
if ('.zip' + os.sep) in path:
(zip_file, zip_path) = os.path.relpath(path).split('.zip' + os.sep, 1)
content = zipfile.ZipFile(zip_file + '.zip').read(zip_path)
else:
try:
data = pkgutil.get_data(__name__, "static/" + real_path)
except AttributeError: # Python < 2.6.
data = None
content = data or open(path, 'rb').read()

self.response.headers["Cache-Control"] = "public; max-age=300"
self.response.headers["Content-Type"] = content_type
try:
data = pkgutil.get_data(__name__, "static/" + real_path)
except AttributeError: # Python < 2.6.
data = None
self.response.out.write(data or open(path).read())
self.response.out.write(content)


class ListConfigsHandler(base_handler.GetJsonHandler):
Expand Down

0 comments on commit 6cf7d4d

Please sign in to comment.