def extract_if_is_tgz(tgz_fp, output_path): try: extract_from_tgz(tgz_fp, output_path) except ValueError, e: # The input if str(e) == ERROR_MSG: copyfile(tgz_fp, output_path) else: raise ValueError, e
def extract_if_is_tgz(tgz_fp, output_path): """Extracts the contents of tgz_fp if it is a tgz file Inputs: tgz_fp: path to the tgz file output_path: path to the output directory or file If tgz_fp is not a tgz file, copies it to output_path Note: propagates the errors from 'extract_from_tgz' """ try: extract_from_tgz(tgz_fp, output_path) except ValueError, e: # The input tgz_fp was a single file, copy it as 'output_path' if str(e) == ERROR_MSG: copyfile(tgz_fp, output_path) else: raise ValueError, e
def test_extract_from_tgz(self): #test with a tgz file which contains only one file filename = get_tmp_filename(tmp_dir=self.tmp_dir) copyfile(self.tgz_file, filename) path_name = get_tmp_filename(tmp_dir=self.tmp_dir) self._paths_to_clean_up = [filename, path_name] extract_from_tgz(filename, path_name) self.assertTrue(path.exists(path_name), 'The output file was not created in the appropiate location') self.assertTrue(path.isfile(path_name), 'The output was not a file') #test with a tgz file which contains multiple files filename = get_tmp_filename(tmp_dir=self.tmp_dir) copyfile(self.tgz_dir, filename) path_name = get_tmp_filename(tmp_dir=self.tmp_dir) self._paths_to_clean_up.append(filename) self._dirs_to_clean_up = [path_name] extract_from_tgz(filename, path_name) self.assertTrue(path.exists(path_name), 'The output directory was not created in the appropiate location') self.assertTrue(path.isdir(path_name), 'The output was not a directory') self.assertTrue(path.exists(path.join(path_name, self.tgz_dir_file1)), 'The tgz contents were not extracted correctly') self.assertTrue(path.exists(path.join(path_name, self.tgz_dir_file2)), 'The tgz contents were not extracted correctly') self.assertTrue(path.exists(path.join(path_name, self.tgz_dir_file3)), 'The tgz contents were not extracted correctly') #test passing a file which is not a tgz filename = get_tmp_filename(tmp_dir=self.tmp_dir) f = open(filename, 'w') f.write("A") f.close() self._paths_to_clean_up.append(filename) self.assertRaises(ValueError, extract_from_tgz, filename, "")