本文整理汇总了Python中mkt.files.utils.SafeUnzip类的典型用法代码示例。如果您正苦于以下问题:Python SafeUnzip类的具体用法?Python SafeUnzip怎么用?Python SafeUnzip使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SafeUnzip类的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: fetch_icon
def fetch_icon(webapp, file_obj=None, **kw):
"""
Downloads a webapp icon from the location specified in the manifest.
Returns False if icon was not able to be retrieved
If `file_obj` is not provided it will use the file from the app's
`current_version`.
"""
log.info(u"[[email protected]] Fetching icon for webapp %s." % webapp.name)
file_obj = file_obj or webapp.current_version and webapp.current_version.all_files[0]
manifest = webapp.get_manifest_json(file_obj)
if not manifest or "icons" not in manifest:
# Set the icon type to empty.
webapp.update(icon_type="")
return
try:
biggest = max(int(size) for size in manifest["icons"])
except ValueError:
log.error('No icon to fetch for webapp "%s"' % webapp.name)
return False
icon_url = manifest["icons"][str(biggest)]
if icon_url.startswith("data:image"):
image_string = icon_url.split("base64,")[1]
content = base64.decodestring(image_string)
else:
if webapp.is_packaged:
# Get icons from package.
if icon_url.startswith("/"):
icon_url = icon_url[1:]
try:
zf = SafeUnzip(file_obj.file_path)
zf.is_valid()
content = zf.extract_path(icon_url)
except (KeyError, forms.ValidationError): # Not found in archive.
log.error(u"[Webapp:%s] Icon %s not found in archive" % (webapp, icon_url))
return False
else:
if not urlparse.urlparse(icon_url).scheme:
icon_url = webapp.origin + icon_url
try:
response = _fetch_content(icon_url)
except Exception, e:
log.error(u"[Webapp:%s] Failed to fetch icon for webapp: %s" % (webapp, e))
# Set the icon type to empty.
webapp.update(icon_type="")
return False
try:
content = get_content_and_check_size(response, settings.MAX_ICON_UPLOAD_SIZE)
except ResponseTooLargeException:
log.warning(u"[Webapp:%s] Icon exceeds maximum size." % webapp)
return False
开发者ID:clouserw,项目名称:zamboni,代码行数:58,代码来源:tasks.py
示例2: test_is_broken
def test_is_broken(self):
zip = SafeUnzip(self.packaged_app_path('signed.zip'))
zip.is_valid()
sf_re = re.compile('^META\-INF/(\w+)\.sf$')
for info in zip.info:
if sf_re.match(info.filename):
info.filename = 'META-INF/foo.foo'
break
assert not zip.is_signed()
开发者ID:atiqueahmedziad,项目名称:zamboni,代码行数:9,代码来源:test_helpers.py
示例3: fetch_icon
def fetch_icon(webapp, **kw):
"""Downloads a webapp icon from the location specified in the manifest.
Returns False if icon was not able to be retrieved
"""
log.info(u'[[email protected]] Fetching icon for webapp %s.' % webapp.name)
manifest = webapp.get_manifest_json()
if not manifest or not 'icons' in manifest:
# Set the icon type to empty.
webapp.update(icon_type='')
return
try:
biggest = max(int(size) for size in manifest['icons'])
except ValueError:
log.error('No icon to fetch for webapp "%s"' % webapp.name)
return False
icon_url = manifest['icons'][str(biggest)]
if icon_url.startswith('data:image'):
image_string = icon_url.split('base64,')[1]
content = base64.decodestring(image_string)
else:
if webapp.is_packaged:
# Get icons from package.
if icon_url.startswith('/'):
icon_url = icon_url[1:]
try:
zf = SafeUnzip(webapp.get_latest_file().file_path)
zf.is_valid()
content = zf.extract_path(icon_url)
except (KeyError, forms.ValidationError): # Not found in archive.
log.error(u'[Webapp:%s] Icon %s not found in archive'
% (webapp, icon_url))
return False
else:
if not urlparse.urlparse(icon_url).scheme:
icon_url = webapp.origin + icon_url
try:
response = _fetch_content(icon_url)
except Exception, e:
log.error(u'[Webapp:%s] Failed to fetch icon for webapp: %s'
% (webapp, e))
# Set the icon type to empty.
webapp.update(icon_type='')
return False
try:
content = get_content_and_check_size(
response, settings.MAX_ICON_UPLOAD_SIZE)
except ResponseTooLargeException:
log.warning(u'[Webapp:%s] Icon exceeds maximum size.' % webapp)
return False
开发者ID:petercpg,项目名称:zamboni,代码行数:53,代码来源:tasks.py
示例4: manifest_contents
def manifest_contents(self):
fp = get_file(self.fileorpath)
if zipfile.is_zipfile(fp):
zf = SafeUnzip(fp)
zf.is_valid() # Raises forms.ValidationError if problems.
try:
data = zf.extract_path('manifest.json')
except KeyError:
raise forms.ValidationError(
_('The file "manifest.json" was not found at the root '
'of the zip archive.'))
else:
raise forms.ValidationError(
_('Addons need to be packaged into a valid zip archive.'))
return self.decode_manifest(data)
开发者ID:demagu-sr,项目名称:zamboni,代码行数:16,代码来源:utils.py
示例5: clean_upload
def clean_upload(self):
upload = self.cleaned_data['upload']
errors = []
if upload.size > self.max_size:
errors.append({
'type': 'error',
'message': _('Packaged app too large for submission. Packages '
'must be smaller than %s.' % filesizeformat(
self.max_size)),
'tier': 1,
})
# Immediately raise an error, do not process the rest of the view,
# which would read the file.
raise self.persist_errors(errors, upload)
manifest = None
try:
# Be careful to keep this as in-memory zip reading.
safe_zip = SafeUnzip(upload, 'r')
safe_zip.is_valid() # Will throw ValidationError if necessary.
manifest = safe_zip.extract_path('manifest.webapp')
except forms.ValidationError as e:
errors.append({
'type': 'error',
'message': ''.join(e.messages),
'tier': 1,
})
except Exception as e:
errors.append({
'type': 'error',
'message': _('Error extracting manifest from zip file.'),
'tier': 1,
})
finally:
safe_zip.close()
origin = None
if manifest:
try:
origin = WebAppParser.decode_manifest(manifest).get('origin')
except forms.ValidationError as e:
errors.append({
'type': 'error',
'message': ''.join(e.messages),
'tier': 1,
})
if origin:
try:
verify_app_domain(origin, packaged=True, exclude=self.addon)
except forms.ValidationError, e:
errors.append({
'type': 'error',
'message': ''.join(e.messages),
'tier': 1,
})
开发者ID:ujdhesa,项目名称:zamboni,代码行数:57,代码来源:forms.py
示例6: validate_file
def validate_file(self, file_obj):
"""
Verify that the upload is a valid zip file that contains a
manifest.json file.
"""
if file_obj.content_type not in self.valid_content_types:
self.error("BAD_CONTENT_TYPE")
try:
self.zipfile = SafeUnzip(file_obj)
try:
# Will throw ValidationError if necessary.
self.zipfile.is_valid()
except ValidationError as e:
raise ParseError(unicode(e))
except (BadZipfile, IOError):
self.error("INVALID_ZIP")
manifest = self.zipfile.extract_path("manifest.json")
except KeyError:
self.error("NO_MANIFEST")
return manifest
开发者ID:washort,项目名称:zamboni,代码行数:20,代码来源:validation.py
示例7: test_is_secure
def test_is_secure(self):
zip = SafeUnzip(self.packaged_app_path('signed.zip'))
zip.is_valid()
assert zip.is_signed()
开发者ID:atiqueahmedziad,项目名称:zamboni,代码行数:4,代码来源:test_helpers.py
示例8: ExtensionValidator
class ExtensionValidator(object):
"""
Firefox OS Add-on validator. If validation fails, will raise an instance of
rest_framework.exceptions.ParseError containing information about the
error.
"""
errors = {
'AUTHOR_NOT_STRING': _(u'The `author` property must be a string.'),
'AUTHOR_TOO_LONG': _(
u'The `author` property cannot be longer than 128 characters.'),
'AUTHOR_TOO_SHORT': _(
u'The `author` property must be at least 1 character'
u' long and can not consist of only whitespace characters.'),
'BAD_CONTENT_TYPE': _(
u'The file sent has an unsupported content-type'),
'DESCRIPTION_NOT_STRING': _(
u'The `description` property must be a string.'),
'DESCRIPTION_TOO_LONG': _(
u'The `description` property cannot be '
u'longer than 132 characters.'),
'ICONS_DO_NOT_EXIST': _(u'Not all specified icons exist.'),
'ICONS_NO_128': _(
u'If defining `icons`, you must include a 128x128 variant.'),
'ICONS_INVALID_FORMAT': _(u'Only PNG icons are permitted.'),
'INVALID_JSON': _(
u"'manifest.json' in the archive is not a valid JSON"
u" file."),
'INVALID_JSON_ENCODING': _(
u"'manifest.json' in the archive is not encoded in UTF-8."),
'INVALID_ZIP': _(u'The file sent is not a valid ZIP file.'),
'NAME_MISSING': _(u'There is no `name` property in the manifest.'),
'NAME_NOT_STRING': _(u'The `name` property must be a string.'),
'NAME_TOO_LONG': _(
u'The `name` property cannot be longer than 45 '
u'characters.'),
'NAME_TOO_SHORT': _(
u'The `name` property must be at least 1 character'
u' long and can not consist of only whitespace characters.'),
'NO_MANIFEST': _(
u"The archive does not contain a 'manifest.json' "
u"file."),
'VERSION_MISSING': _(
u'There is no `version` property in the manifest.'),
'VERSION_NOT_STRING': _(u'The `version` property must be a string.'),
'VERSION_INVALID': _(
u'The `version` property must be a string'
u' containing one to four dot-separated integers each between'
u' 0 and 65535.'),
}
valid_content_types = (
'application/octet-stream',
'application/zip',
)
def __init__(self, file_obj=None):
self.file_obj = file_obj
self.zipfile = None
def error(self, error_key):
raise ParseError(detail={
'key': error_key,
'message': self.errors[error_key],
})
def validate(self):
"""
Run the full validation suite against the uploaded file:
* Ensure that it is a valid zip file.
* Ensure that it contains a valid manifest.json file.
* Validate the manifest fields against the spec.
Return the manifest contents (as dict).
"""
self.manifest = self.validate_file(self.file_obj)
self.data = self.validate_json(self.manifest)
self.validate_name(self.data)
self.validate_description(self.data)
self.validate_version(self.data)
self.validate_author(self.data)
self.validate_icons(self.data)
# self.validate_icon_files(self.data)
return self.data
def validate_file(self, file_obj):
"""
Verify that the upload is a valid zip file that contains a
manifest.json file.
"""
if file_obj.content_type not in self.valid_content_types:
self.error('BAD_CONTENT_TYPE')
try:
self.zipfile = SafeUnzip(file_obj)
try:
# Will throw ValidationError if necessary.
self.zipfile.is_valid()
except ValidationError as e:
raise ParseError(unicode(e))
except (BadZipfile, IOError):
self.error('INVALID_ZIP')
#.........这里部分代码省略.........
开发者ID:elysium001,项目名称:zamboni,代码行数:101,代码来源:validation.py
示例9: test_extract_path
def test_extract_path(self):
zip = SafeUnzip(self.packaged_app_path('mozball.zip'))
assert zip.is_valid()
desc_string = '"description": "Exciting Open Web development action!"'
assert desc_string in zip.extract_path('manifest.webapp')
开发者ID:atiqueahmedziad,项目名称:zamboni,代码行数:5,代码来源:test_helpers.py
示例10: test_not_secure
def test_not_secure(self):
zip = SafeUnzip(self.packaged_app_path('mozball.zip'))
zip.is_valid()
assert not zip.is_signed()
开发者ID:atiqueahmedziad,项目名称:zamboni,代码行数:4,代码来源:test_helpers.py
示例11: test_extract_path
def test_extract_path(self):
zip = SafeUnzip(self.xpi_path('langpack-localepicker'))
assert zip.is_valid()
assert'locale browser de' in zip.extract_path('chrome.manifest')
开发者ID:JaredKerim-Mozilla,项目名称:zamboni,代码行数:4,代码来源:test_helpers.py
示例12: test_unzip_not_fatal
def test_unzip_not_fatal(self):
zip = SafeUnzip(self.manifest_path('mozball.webapp'))
assert not zip.is_valid(fatal=False)
开发者ID:atiqueahmedziad,项目名称:zamboni,代码行数:3,代码来源:test_helpers.py
示例13: test_not_secure
def test_not_secure(self):
zip = SafeUnzip(self.xpi_path('extension'))
zip.is_valid()
assert not zip.is_signed()
开发者ID:JaredKerim-Mozilla,项目名称:zamboni,代码行数:4,代码来源:test_helpers.py
示例14: test_is_secure
def test_is_secure(self):
zip = SafeUnzip(self.xpi_path('signed'))
zip.is_valid()
assert zip.is_signed()
开发者ID:JaredKerim-Mozilla,项目名称:zamboni,代码行数:4,代码来源:test_helpers.py
示例15: test_unzip_not_fatal
def test_unzip_not_fatal(self):
zip = SafeUnzip(self.xpi_path('search.xml'))
assert not zip.is_valid(fatal=False)
开发者ID:JaredKerim-Mozilla,项目名称:zamboni,代码行数:3,代码来源:test_helpers.py
示例16: ExtensionValidator
class ExtensionValidator(object):
"""
Firefox OS Add-on validator. If validation fails, will raise an instance of
rest_framework.exceptions.ParseError containing information about the
error.
"""
errors = {
"AUTHOR_NOT_STRING": _(u"The `author` property must be a string."),
"AUTHOR_TOO_LONG": _(u"The `author` property cannot be longer than 128 characters."),
"AUTHOR_TOO_SHORT": _(
u"The `author` property must be at least 1 character"
u" long and can not consist of only whitespace characters."
),
"BAD_CONTENT_TYPE": _(u"The file sent has an unsupported content-type"),
"DESCRIPTION_NOT_STRING": _(u"The `description` property must be a string."),
"DESCRIPTION_TOO_LONG": _(u"The `description` property cannot be " u"longer than 132 characters."),
"ICON_INCORRECT_DIMENSIONS": _(
u"The icon file `%(icon_path)s` is not the specified dimensions "
u" of %(icon_size)s x %(icon_size)s as defined in the manifest."
),
"ICON_DOES_NOT_EXIST": _(
u"The icon file `%(icon_path)s` is referenced in the manifest but" u" does not exist in the ZIP file."
),
"ICONS_NO_128": _(u"If defining `icons`, you must include a 128x128 variant."),
"ICON_NOT_A_VALID_IMAGE_OR_PNG": _(u"The icon file `%s` is not a valid PNG."),
"ICON_NOT_SQUARE": _(u"The icon file `%(icon_path)s` is not square."),
"ICON_INVALID_SIZE": _(u"The manifest contains an invalid icon size: %(icon_size)s"),
"INVALID_JSON": _(u"'manifest.json' in the archive is not a valid JSON" u" file."),
"INVALID_JSON_ENCODING": _(u"'manifest.json' in the archive is not encoded in UTF-8."),
"INVALID_ZIP": _(u"The file sent is not a valid ZIP file."),
"NAME_MISSING": _(u"There is no `name` property in the manifest."),
"NAME_NOT_STRING": _(u"The `name` property must be a string."),
"NAME_TOO_LONG": _(u"The `name` property cannot be longer than 45 " u"characters."),
"NAME_TOO_SHORT": _(
u"The `name` property must be at least 1 character"
u" long and can not consist of only whitespace characters."
),
"NO_MANIFEST": _(u"The archive does not contain a 'manifest.json' " u"file."),
"VERSION_MISSING": _(u"There is no `version` property in the manifest."),
"VERSION_NOT_STRING": _(u"The `version` property must be a string."),
"VERSION_INVALID": _(
u"The `version` property must be a string"
u" containing one to four dot-separated integers each between"
u" 0 and 65535."
),
}
valid_content_types = ("application/octet-stream", "application/zip")
def __init__(self, file_obj=None):
self.file_obj = file_obj
self.zipfile = None
def error(self, key, **kwargs):
message = self.errors[key]
if kwargs:
message = self.errors[key] % kwargs
raise ParseError(detail={"key": key, "message": message, "params": kwargs})
def validate(self):
"""
Run the full validation suite against the uploaded file:
* Ensure that it is a valid zip file.
* Ensure that it contains a valid manifest.json file.
* Validate the manifest fields against the spec.
Return the manifest contents (as dict).
"""
self.manifest = self.validate_file(self.file_obj)
self.data = self.validate_json(self.manifest)
self.validate_name(self.data)
self.validate_description(self.data)
self.validate_version(self.data)
self.validate_author(self.data)
self.validate_icons(self.data)
return self.data
def validate_file(self, file_obj):
"""
Verify that the upload is a valid zip file that contains a
manifest.json file.
"""
if file_obj.content_type not in self.valid_content_types:
self.error("BAD_CONTENT_TYPE")
try:
self.zipfile = SafeUnzip(file_obj)
try:
# Will throw ValidationError if necessary.
self.zipfile.is_valid()
except ValidationError as e:
raise ParseError(unicode(e))
except (BadZipfile, IOError):
self.error("INVALID_ZIP")
manifest = self.zipfile.extract_path("manifest.json")
except KeyError:
self.error("NO_MANIFEST")
return manifest
def validate_json(self, contents):
#.........这里部分代码省略.........
开发者ID:washort,项目名称:zamboni,代码行数:101,代码来源:validation.py
示例17: test_is_broken
def test_is_broken(self):
zip = SafeUnzip(self.xpi_path('signed'))
zip.is_valid()
zip.info[2].filename = 'META-INF/foo.sf'
assert not zip.is_signed()
开发者ID:JaredKerim-Mozilla,项目名称:zamboni,代码行数:5,代码来源:test_helpers.py
注:本文中的mkt.files.utils.SafeUnzip类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论