def test_atomic_write_transparent_on_success(self): _, tempfile_path = mkstemp() os.remove(tempfile_path) # We reuse the path, not the file itself with atomic_write(tempfile_path) as f: f.write('success') with open(tempfile_path, 'rb') as f: self.assertEqual('success', f.read()) os.remove(tempfile_path)
def test_atomic_write_deletes_on_fail(self): _, tempfile_path = mkstemp() os.remove(tempfile_path) # We reuse the path, not the file itself try: with atomic_write(tempfile_path) as f: f.write('fail') raise Exception except Exception: pass self.assertFalse(exists(tempfile_path))
def download_picrust_files(output_path, with_confidence, gg_version, type_of_prediction, force): if not os.path.exists(output_path): print('Creating output directory {path}...'.format(path=output_path)) os.mkdir(output_path) elif not os.path.isdir(output_path): print('Error: {path} already exists but is not a directory!'.format( path=output_path)) exit(1) try: files_to_download = [ FILES[('16s', gg_version)], FILES[(type_of_prediction, gg_version)] ] except KeyError: msg = ('Error: {type_of_prediction} predictions not available for' ' GreenGenes {gg_version}') print( msg.format(gg_version=gg_version, type_of_prediction=type_of_prediction)) exit(1) if with_confidence: try: ci_files = [ FILES[('16s_ci', gg_version)], FILES[(type_of_prediction + '_ci', gg_version)], ] except KeyError: msg = 'Error: CI files not available for {gg_version}' print(msg.format(gg_version=gg_version)) exit(1) files_to_download.extend(ci_files) print('Downloading files to {path}...'.format(path=output_path)) for filename in files_to_download: if 'variances' not in filename: url = urljoin(BASE_URL, gg_version + '/' + filename) else: url = urljoin(BASE_URL, gg_version + '/variances/' + filename) file_path = os.path.join(DATA_DIR, filename) if os.path.exists(file_path) and not force: msg = 'Skipping download of {filename}: file already exists.' print(msg.format(filename=filename)) continue else: print('Downloading {filename}...'.format(filename=filename)) try: response = urlopen(url) with atomic_write(file_path) as f: f.write(response.read()) except HTTPError as e: msg = 'There was a problem downloading {filename}: {exception}.' print(msg.format(filename=filename, exception=e)) exit(1) print('Done.')