def parse_xpi(xpi, addon=None, check=True): """Extract and parse an XPI.""" # Extract to /tmp path = tempfile.mkdtemp() try: xpi = get_file(xpi) extract_xpi(xpi, path) xpi_info = Extractor.parse(path) except forms.ValidationError: raise except IOError as e: if len(e.args) < 2: errno, strerror = None, e[0] else: errno, strerror = e log.error('I/O error({0}): {1}'.format(errno, strerror)) raise forms.ValidationError(_('Could not parse install.rdf.')) except Exception: log.error('XPI parse error', exc_info=True) raise forms.ValidationError(_('Could not parse install.rdf.')) finally: rm_local_tmp_dir(path) if check: return check_xpi_info(xpi_info, addon) else: return xpi_info
def test_storage_walk(): tmp = tempfile.mkdtemp() jn = partial(os.path.join, tmp) try: storage.save(jn('file1.txt'), ContentFile('')) storage.save(jn('one/file1.txt'), ContentFile('')) storage.save(jn('one/file2.txt'), ContentFile('')) storage.save(jn('one/two/file1.txt'), ContentFile('')) storage.save(jn('one/three/file1.txt'), ContentFile('')) storage.save(jn('four/five/file1.txt'), ContentFile('')) storage.save(jn(u'four/kristi\u2603/kristi\u2603.txt'), ContentFile('')) results = [(dir, set(subdirs), set(files)) for dir, subdirs, files in sorted(walk_storage(tmp))] yield (eq_, results.pop(0), (tmp, set(['four', 'one']), set(['file1.txt']))) yield (eq_, results.pop(0), (jn('four'), set(['five', 'kristi\xe2\x98\x83']), set([]))) yield (eq_, results.pop(0), (jn('four/five'), set([]), set(['file1.txt']))) yield (eq_, results.pop(0), (jn('four/kristi\xe2\x98\x83'), set([]), set(['kristi\xe2\x98\x83.txt']))) yield (eq_, results.pop(0), (jn('one'), set(['three', 'two']), set(['file1.txt', 'file2.txt']))) yield (eq_, results.pop(0), (jn('one/three'), set([]), set(['file1.txt']))) yield (eq_, results.pop(0), (jn('one/two'), set([]), set(['file1.txt']))) yield (eq_, len(results), 0) finally: rm_local_tmp_dir(tmp)
def test_storage_walk(): tmp = tempfile.mkdtemp() jn = partial(os.path.join, tmp) try: storage.save(jn('file1.txt'), ContentFile('')) storage.save(jn('one/file1.txt'), ContentFile('')) storage.save(jn('one/file2.txt'), ContentFile('')) storage.save(jn('one/two/file1.txt'), ContentFile('')) storage.save(jn('one/three/file1.txt'), ContentFile('')) storage.save(jn('four/five/file1.txt'), ContentFile('')) storage.save(jn(u'four/kristi\u2603/kristi\u2603.txt'), ContentFile('')) results = [(dir, set(subdirs), set(files)) for dir, subdirs, files in sorted(walk_storage(tmp))] yield (eq_, results.pop(0), (tmp, set(['four', 'one']), set(['file1.txt']))) yield (eq_, results.pop(0), (jn('four'), set(['five', 'kristi\xe2\x98\x83']), set([]))) yield (eq_, results.pop(0), (jn('four/five'), set([]), set(['file1.txt']))) yield (eq_, results.pop(0), (jn('four/kristi\xe2\x98\x83'), set([]), set(['kristi\xe2\x98\x83.txt']))) yield (eq_, results.pop(0), (jn('one'), set(['three', 'two']), set(['file1.txt', 'file2.txt']))) yield (eq_, results.pop(0), (jn('one/three'), set([]), set(['file1.txt']))) yield (eq_, results.pop(0), (jn('one/two'), set([]), set(['file1.txt' ]))) yield (eq_, len(results), 0) finally: rm_local_tmp_dir(tmp)
def extract_zip(source, remove=False, fatal=True): """Extracts the zip file. If remove is given, removes the source file.""" tempdir = tempfile.mkdtemp() zip = SafeUnzip(source) try: if zip.is_valid(fatal): zip.extract_to_dest(tempdir) except: rm_local_tmp_dir(tempdir) raise if remove: os.remove(source) return tempdir
def test_zip(self): # This zip contains just one file chrome/ that we expect # to be unzipped as a directory, not a file. xpi = self.xpi_path('directory-test') # This is to work around: http://bugs.python.org/issue4710 # which was fixed in Python 2.6.2. If the required version # of Python for zamboni goes to 2.6.2 or above, this can # be removed. try: dest = tempfile.mkdtemp() zipfile.ZipFile(xpi).extractall(dest) assert os.path.isdir(os.path.join(dest, 'chrome')) finally: rm_local_tmp_dir(dest)
def test_rm_stored_dir(): tmp = tempfile.mkdtemp() jn = partial(os.path.join, tmp) try: storage.save(jn('file1.txt'), ContentFile('<stuff>')) storage.save(jn('one/file1.txt'), ContentFile('')) storage.save(jn('one/two/file1.txt'), ContentFile('moar stuff')) storage.save(jn(u'one/kristi\u0107/kristi\u0107.txt'), ContentFile('')) rm_stored_dir(jn('one')) yield (eq_, storage.exists(jn('one')), False) yield (eq_, storage.exists(jn('one/file1.txt')), False) yield (eq_, storage.exists(jn('one/two')), False) yield (eq_, storage.exists(jn('one/two/file1.txt')), False) yield (eq_, storage.exists(jn(u'one/kristi\u0107/kristi\u0107.txt')), False) yield (eq_, storage.exists(jn('file1.txt')), True) finally: rm_local_tmp_dir(tmp)
def repack(xpi_path, raise_on_failure=True): """Unpack the XPI, yield the temp folder, and repack on exit. Usage: with repack('foo.xpi') as temp_folder: # 'foo.xpi' files are extracted to the temp_folder. modify_files(temp_folder) # Modify the files in the temp_folder. # The 'foo.xpi' extension is now repacked, with the file changes. """ # Unpack. tempdir = extract_zip(xpi_path, remove=False, fatal=raise_on_failure) yield tempdir try: # Repack. repacked = u'{0}.repacked'.format(xpi_path) # Temporary file. zip_folder_content(tempdir, repacked) # Overwrite the initial file with the repacked one. shutil.move(repacked, xpi_path) finally: rm_local_tmp_dir(tempdir)
def tearDown(self): rm_local_tmp_dir(self.tmp)
xpi = get_filepath(xpi) # Extract to /tmp path = tempfile.mkdtemp() try: extract_xpi(xpi, path) rdf = Extractor.parse(path) except forms.ValidationError: raise except IOError as (errno, strerror): log.error('I/O error({0}): {1}'.format(errno, strerror)) raise forms.ValidationError(_('Could not parse install.rdf.')) except Exception: log.error('XPI parse error', exc_info=True) raise forms.ValidationError(_('Could not parse install.rdf.')) finally: rm_local_tmp_dir(path) return check_rdf(rdf, addon) def check_rdf(rdf, addon=None): from addons.models import Addon, BlacklistedGuid if not rdf['guid']: raise forms.ValidationError(_("Could not find a UUID.")) if addon and addon.guid != rdf['guid']: raise forms.ValidationError(_("UUID doesn't match add-on.")) if (not addon and Addon.objects.filter(guid=rdf['guid']).exists() or BlacklistedGuid.objects.filter(guid=rdf['guid']).exists()): raise forms.ValidationError(_('Duplicate UUID found.')) if len(rdf['version']) > 32:
def tearDown(self): rm_local_tmp_dir(self.tmp) super(TestFileOps, self).tearDown()
def tearDown(self): rm_local_tmp_dir(self.tmp) super(TestLocalFileStorage, self).tearDown()
def tearDown(self): rm_local_tmp_dir(self.temp_dir) super(TestIconForm, self).tearDown()
def cleanup(self): if os.path.exists(self.dest): rm_local_tmp_dir(self.dest)