def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') with codecs.open(target_path, 'w', 'utf-8') as f: f.write("\n") errors = [] unpack_zip(zipname, target_path, errors) assert [("%s exists and isn't a directory, not unzipping a directory over it." % target_path)] == errors
def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name as what's in the zip errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isdir(target_path) assert codecs.open(os.path.join(target_path, 'bar'), 'r', 'utf-8').read() == "hello world\n"
def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name as file in zip os.makedirs(target_path) errors = [] unpack_zip(zipname, target_path, errors) assert os.path.isdir(target_path) assert ["%s exists and is a directory, not unzipping a plain file over it." % target_path] == errors
def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') # different name from file in zip os.makedirs(target_path) errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isdir(target_path) assert codecs.open(os.path.join(target_path, 'foo'), 'r', 'utf-8').read() == "hello world\n"
def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') # different from the file in the zip with codecs.open(target_path, 'w', 'utf-8') as f: f.write("original\n") errors = [] unpack_zip(zipname, target_path, errors) assert [("%s exists and isn't a directory, not unzipping a directory over it." % target_path)] == errors assert os.path.isfile(target_path) assert codecs.open(target_path, 'r', 'utf-8').read() == "original\n"
def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name with codecs.open(target_path, 'w', 'utf-8') as f: f.write("\n") errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isfile(target_path) assert codecs.open(target_path, 'r', 'utf-8').read() == "hello world\n"
def _provide_download(self, requirement, context, errors, logs): filename = context.status.analysis.existing_filename if filename is not None: logs.append("Previously downloaded file located at {}".format(filename)) return filename filename = os.path.abspath(os.path.join(context.environ['PROJECT_DIR'], requirement.filename)) if requirement.unzip: download_filename = filename + ".zip" else: download_filename = filename download = FileDownloader(url=requirement.url, filename=download_filename, hash_algorithm=requirement.hash_algorithm) try: _ioloop = IOLoop(make_current=False) response = _ioloop.run_sync(lambda: download.run(_ioloop)) if response is None: for error in download.errors: errors.append(error) return None elif response.code == 200: if requirement.hash_value is not None and requirement.hash_value != download.hash: errors.append("Error downloading {}: mismatched hashes. Expected: {}, calculated: {}".format( requirement.url, requirement.hash_value, download.hash)) return None if requirement.unzip: if unpack_zip(download_filename, filename, errors): os.remove(download_filename) return filename else: return None return filename else: errors.append("Error downloading {}: response code {}".format(requirement.url, response.code)) return None except Exception as e: errors.append("Error downloading {}: {}".format(requirement.url, str(e))) return None finally: _ioloop.close()
def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') errors = [] unpack_zip(zipname, target_path, errors) assert ['Zip archive was empty.'] == errors assert not os.path.isdir(target_path)
def do_test(workingdir): zipname = os.path.join(workingdir, 'foo') target_path = os.path.join(workingdir, 'boo') errors = [] unpack_zip(zipname, target_path, errors) assert [('Failed to unzip %s: File is not a zip file' % zipname)] == errors