diff --git a/swh/loader/git/loader.py b/swh/loader/git/loader.py --- a/swh/loader/git/loader.py +++ b/swh/loader/git/loader.py @@ -266,7 +266,12 @@ """Compute the project name from the archive's path. """ - return os.path.basename(os.path.dirname(archive_path)) + archive_name = os.path.basename(archive_path) + for ext in ('.zip', '.tar.gz', '.tgz'): + if archive_name.lower().endswith(ext): + archive_name = archive_name[:-len(ext)] + break + return archive_name def prepare_origin_visit(self, origin_url, archive_path, visit_date): self._prepare_origin_visit(origin_url, visit_date) diff --git a/swh/loader/git/tests/test_loader.py b/swh/loader/git/tests/test_loader.py --- a/swh/loader/git/tests/test_loader.py +++ b/swh/loader/git/tests/test_loader.py @@ -4,8 +4,6 @@ # See top-level LICENSE file for more information import os.path -import zipfile -import tempfile import subprocess from swh.loader.git.loader import GitLoader, GitLoaderFromArchive @@ -137,32 +135,10 @@ """ def setUp(self): - super().setUp('testrepo.tgz', True) - self._setup_zip() + super().setUp('testrepo.tgz', False) self.loader = GitLoaderFromArchiveTest() self.storage = self.loader.storage - def _setup_zip(self): - self._zip_file = tempfile.NamedTemporaryFile('ab', suffix='.zip') - dest_dir = os.path.normpath(self.destination_path) + '/' - with zipfile.ZipFile(self._zip_file, 'a') as zip_writer: - for root, dirs, files in os.walk(dest_dir): - assert root.startswith(dest_dir) - relative_root = os.path.join( - 'testrepo', - root[len(dest_dir):]) - for file_ in files: - zip_writer.write( - filename=os.path.join(root, file_), - arcname=os.path.join(relative_root, file_)) - self.destination_path = self._zip_file.name - self.tmp_root_path = None - self.repo_url = 'file://' + self.destination_path - - def tearDown(self): - self._zip_file.close() - super().tearDown() - def load(self): return self.loader.load( origin_url=self.repo_url, diff --git a/swh/loader/git/utils.py b/swh/loader/git/utils.py --- a/swh/loader/git/utils.py +++ b/swh/loader/git/utils.py @@ -10,7 +10,7 @@ import shutil import tempfile -from subprocess import call +from swh.core import tarball def init_git_repo_from_archive(project_name, archive_path, @@ -41,11 +41,18 @@ try: # create the repository that will be loaded with the dump - r = call(['unzip', '-q', '-o', archive_path, '-d', temp_dir]) - if r != 0: - raise ValueError('Failed to uncompress archive %s' % archive_path) - + tarball.uncompress(archive_path, temp_dir) repo_path = os.path.join(temp_dir, project_name) + # tarball content may not be as expected (e.g. no top level directory + # or a top level directory with a name different from project_name), + # so try to make it loadable anyway + if not os.path.exists(repo_path): + os.mkdir(repo_path) + for root, dirs, files in os.walk(temp_dir): + if '.git' in dirs: + shutil.copytree(os.path.join(root, '.git'), + os.path.join(repo_path, '.git')) + break return temp_dir, repo_path except Exception as e: shutil.rmtree(temp_dir)