Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: chrome/common/extensions/docs/server2/example_zipper.py

Issue 10689144: Extensions Docs Server: Samples zip files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: clean up and tests Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import os
6 from io import BytesIO
7 import re
8 from zipfile import ZipFile
9
10 class ExampleZipper(object):
11 """This class creates a zip file given a samples directory.
12 """
13 def __init__(self, fetcher, cache_builder, base_path, match_path):
14 self._base_path = base_path
15 self._zip_cache = cache_builder.build(self._MakeZipFile)
16 self._fetcher = fetcher
17 self._match_path = match_path
18
19 def _MakeZipFile(self, files):
20 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2]
21 prefix = zip_path.rsplit('/', 1)[-2]
22 if zip_path + '/manifest.json' not in files:
23 return None
24 zip_bytes = BytesIO()
25 zip_file = ZipFile(zip_bytes, mode='w')
26 try:
27 for filename in files:
28 zip_file.writestr(
29 filename[len(prefix):].strip('/'),
30 self._fetcher.FetchResource(filename).content)
31 finally:
32 zip_file.close()
33 return zip_bytes.getvalue()
34
35 def create(self, path):
36 """ Creates a new zip file from the recursive contents of |path|
37 as returned by |_zip_cache|.
38 Paths within the zip file are given relative to and including |path|.
39 """
40 return self._zip_cache.getFromFileListing(self._base_path + '/' + path,
41 recursive=True)
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/echo_handler.py ('k') | chrome/common/extensions/docs/server2/fetcher_cache.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698