def handle(self, *args, **options): "Execute the command" if len(args) != 2: raise CommandError( "export requires two arguments: <tar.gz file> <output path>") source_archive = args[0] output_path = args[1] # Create temp directories to extract the source and create the target archive. temp_source_dir = mkdtemp(dir=settings.DATA_DIR) temp_target_dir = mkdtemp(dir=settings.DATA_DIR) try: extract_source(source_archive, temp_source_dir) desired_version = convert_between_versions(temp_source_dir, temp_target_dir) # New zip up the target directory. parts = os.path.basename(source_archive).split('.') archive_name = path( output_path ) / "{source_name}_version_{desired_version}.tar.gz".format( source_name=parts[0], desired_version=desired_version) with open(archive_name, "w"): tar_file = tarfile.open(archive_name, mode='w:gz') try: for item in os.listdir(temp_target_dir): tar_file.add(path(temp_target_dir) / item, arcname=item) finally: tar_file.close() print("Created archive {0}".format(archive_name)) except ValueError as err: raise CommandError(err) finally: shutil.rmtree(temp_source_dir) shutil.rmtree(temp_target_dir)
def handle(self, *args, **options): "Execute the command" if len(args) != 2: raise CommandError("export requires two arguments: <tar.gz file> <output path>") source_archive = args[0] output_path = args[1] # Create temp directories to extract the source and create the target archive. temp_source_dir = mkdtemp() temp_target_dir = mkdtemp() try: extract_source(source_archive, temp_source_dir) desired_version = convert_between_versions(temp_source_dir, temp_target_dir) # New zip up the target directory. parts = os.path.basename(source_archive).split('.') archive_name = path(output_path) / "{source_name}_version_{desired_version}.tar.gz".format( source_name=parts[0], desired_version=desired_version ) with open(archive_name, "w"): tar_file = tarfile.open(archive_name, mode='w:gz') try: for item in os.listdir(temp_target_dir): tar_file.add(path(temp_target_dir) / item, arcname=item) finally: tar_file.close() print("Created archive {0}".format(archive_name)) except ValueError as err: raise CommandError(err) finally: shutil.rmtree(temp_source_dir) shutil.rmtree(temp_target_dir)
def _verify_conversion(self, source_archive, comparison_archive): """ Helper function for conversion tests. """ convert_between_versions(source_archive, self.result_dir) self.assertTrue(directories_equal(self.result_dir, comparison_archive))
def test_empty_course(self): """ Test error condition of a version 1 archive with no published branch. """ errstring = "source archive does not have single course directory at top level" empty_course = self._expand_archive('EmptyCourse.tar.gz') with self.assertRaisesRegexp(ValueError, errstring): convert_between_versions(empty_course, self.result_dir)
def test_no_published(self): """ Test error condition of a version 1 archive with no published branch. """ errstring = "version 1 archive must contain a published branch" no_published = self._expand_archive('Version1_nopublished.tar.gz') with self.assertRaisesRegexp(ValueError, errstring): convert_between_versions(no_published, self.result_dir)
def test_no_version(self): """ Test error condition of no version number specified. """ errstring = "unknown version" with self.assertRaisesRegexp(ValueError, errstring): convert_between_versions(self.no_version, self.result_dir)