示例#1
0
    def test_clone(self):
        testdir = tempfile.mkdtemp()
        if self.acl:
            self.component.project.add_user(self.user, "@VCS")
        try:
            url = (
                get_export_url(self.component)
                .replace("http://example.com", self.live_server_url)
                .replace(
                    "http://",
                    f"http://{self.user.username}:{self.user.auth_token.key}@",
                )
            )
            process = subprocess.Popen(
                ["git", "clone", url],
                cwd=testdir,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                stdin=subprocess.PIPE,
            )
            output = process.communicate()[0]
            retcode = process.poll()
        finally:
            remove_tree(testdir)

        check = self.assertEqual if self.acl else self.assertNotEqual
        check(retcode, 0, f"Failed: {output}")
示例#2
0
def cleanup_stale_repos():
    prefix = data_dir("vcs")
    vcs_mask = os.path.join(prefix, "*", "*")

    yesterday = time() - 86400

    for path in glob(vcs_mask):
        if not os.path.isdir(path):
            continue

        # Skip recently modified paths
        if os.path.getmtime(path) > yesterday:
            continue

        # Parse path
        project, component = os.path.split(path[len(prefix) + 1 :])

        # Find matching components
        objects = Component.objects.with_repo().filter(
            slug=component, project__slug=project
        )

        # Remove stale dirs
        if not objects.exists():
            remove_tree(path)
示例#3
0
 def test_remove(self, callback=None):
     target = os.path.join(settings.DATA_DIR, "test")
     nested = os.path.join(target, "nested")
     filename = os.path.join(target, "file")
     os.makedirs(target)
     os.makedirs(nested)
     with open(filename, "w") as handle:
         handle.write("test")
     if callback:
         callback(target, nested, filename)
     remove_tree(target)
     self.assertFalse(os.path.exists(target))
示例#4
0
 def from_zip(cls, target, zipfile):
     # Create empty repo
     if os.path.exists(target):
         remove_tree(target)
     cls._clone("local:", target, cls.default_branch)
     # Extract zip file content, ignoring some files
     zipobj = ZipFile(zipfile)
     names = [name for name in zipobj.namelist() if not is_excluded(name)]
     zipobj.extractall(path=target, members=names)
     # Add to repository
     repo = cls(target)
     with repo.lock:
         repo.execute(["add", target])
         if repo.needs_commit():
             repo.commit("ZIP file upladed into Weblate")
示例#5
0
    def clone_test_repos(self):
        dirs = ["test-repo.git", "test-repo.hg", "test-repo.svn"]
        # Remove possibly existing directories
        for name in dirs:
            path = self.get_repo_path(name)
            if os.path.exists(path):
                remove_tree(path)

        # Remove cached paths
        keys = ["git_repo_path", "mercurial_repo_path", "subversion_repo_path"]
        for key in keys:
            if key in self.__dict__:
                del self.__dict__[key]

        # Remove possibly existing project directories
        test_repo_path = os.path.join(settings.DATA_DIR, "vcs")
        if os.path.exists(test_repo_path):
            remove_tree(test_repo_path)
        os.makedirs(test_repo_path)
示例#6
0
 def from_files(cls, target, files):
     # Create empty repo
     if os.path.exists(target):
         remove_tree(target)
     cls._clone("local:", target, cls.default_branch)
     # Create files
     for name, content in files.items():
         fullname = os.path.join(target, name)
         dirname = os.path.dirname(fullname)
         if not os.path.exists(dirname):
             os.makedirs(dirname)
         with open(fullname, "wb") as handle:
             handle.write(content)
     # Add to repository
     repo = cls(target)
     with repo.lock:
         repo.execute(["add", target])
         if repo.needs_commit():
             repo.commit("Started tranlation using Weblate")
示例#7
0
    def optional_extract(self, output, tarname):
        """Extract test repository data if needed.

        Checks whether directory exists or is older than archive.
        """
        tarname = get_test_file(tarname)

        if not os.path.exists(output) or os.path.getmtime(
                output) < os.path.getmtime(tarname):

            # Remove directory if outdated
            if os.path.exists(output):
                remove_tree(output)

            # Extract new content
            tar = TarFile(tarname)
            tar.extractall(settings.DATA_DIR)
            tar.close()

            # Update directory timestamp
            os.utime(output, None)
        self.updated_base_repos.add(output)
示例#8
0
    def test_rename_project(self):
        # Remove stale dir from previous tests
        target = os.path.join(data_dir("vcs"), "xxxx")
        if os.path.exists(target):
            remove_tree(target)
        self.make_manager()
        self.assertContains(
            self.client.get(reverse("project", kwargs=self.kw_project)), "#rename"
        )
        response = self.client.post(
            reverse("rename", kwargs=self.kw_project), {"slug": "xxxx"}
        )
        self.assertRedirects(response, "/projects/xxxx/")
        project = Project.objects.get(pk=self.project.pk)
        self.assertEqual(project.slug, "xxxx")
        for component in project.component_set.iterator():
            self.assertIsNotNone(component.repository.last_remote_revision)
            response = self.client.get(component.get_absolute_url())
            self.assertContains(response, "/projects/xxxx/")

        # Test rename redirect in middleware
        response = self.client.get(reverse("project", kwargs=self.kw_project))
        self.assertRedirects(response, project.get_absolute_url(), status_code=301)
示例#9
0
    def add_remote_commit(self, conflict=False, rename=False):
        tempdir = tempfile.mkdtemp()
        try:
            repo = self.clone_repo(tempdir)
            self.fixup_repo(repo)

            with repo.lock:
                repo.set_committer("Second Bar", "*****@*****.**")
                if rename:
                    shutil.move(
                        os.path.join(tempdir, "README.md"),
                        os.path.join(tempdir, "READ ME.md"),
                    )
                    if self._vcs == "mercurial":
                        repo.remove(["README.md"], "Removed readme")
                        filenames = ["READ ME.md"]
                    else:
                        filenames = None
                else:
                    if conflict:
                        filename = "testfile"
                    else:
                        filename = "test2"
                    # Create test file
                    with open(os.path.join(tempdir, filename), "w") as handle:
                        handle.write("SECOND TEST FILE\n")
                    filenames = [filename]

                # Commit it
                repo.commit(
                    "Test commit", "Foo Bar <*****@*****.**>", timezone.now(), filenames
                )

                # Push it
                repo.push("")
        finally:
            remove_tree(tempdir)
示例#10
0
 def test_failed_update(self):
     """Test failed remote update."""
     if os.path.exists(self.git_repo_path):
         remove_tree(self.git_repo_path)
     if os.path.exists(self.mercurial_repo_path):
         remove_tree(self.mercurial_repo_path)
     if os.path.exists(self.subversion_repo_path):
         remove_tree(self.subversion_repo_path)
     translation = self.component.translation_set.get(language_code="cs")
     self.assertFalse(translation.do_update(self.request))
示例#11
0
 def test_failed_reset(self):
     # Corrupt Git database so that reset fails
     remove_tree(
         os.path.join(self.component.full_path, ".git", "objects", "pack"))
     self.assertFalse(self.component.do_reset(None))
示例#12
0
    def import_initial(self, project, repo, branch):
        """Import the first repository of a project."""
        # Checkout git to temporary dir
        workdir = self.checkout_tmp(project, repo, branch)
        # Create fake discovery without existing component
        discovery = self.get_discovery(None, workdir)

        components = project.component_set.all()

        component = None

        # Create first component (this one will get full git repo)
        if self.main_component:
            match = None
            for match in discovery.matched_components.values():
                if match["slug"] == self.main_component:
                    break
            if match is None or match["slug"] != self.main_component:
                raise CommandError(
                    "Specified --main-component was not found in matches!")
        else:
            # Try if one is already there
            for match in discovery.matched_components.values():
                try:
                    component = components.get(repo=repo,
                                               filemask=match["mask"])
                except Component.DoesNotExist:
                    continue
            # Pick random
            if component is None:
                match = list(discovery.matched_components.values())[0]

        try:
            if component is None:
                component = components.get(slug=match["slug"])
            self.logger.warning(
                "Component %s already exists, skipping and using it "
                "as a main component",
                match["slug"],
            )
            remove_tree(workdir)
        except Component.DoesNotExist:
            self.logger.info("Creating component %s as main one",
                             match["slug"])

            # Rename gitrepository to new name
            os.rename(workdir, os.path.join(project.full_path, match["slug"]))

            # Create new component
            component = discovery.create_component(
                None,
                match,
                project=project,
                source_language=self.source_language,
                repo=repo,
                branch=branch,
                vcs=self.vcs,
                push_on_commit=self.push_on_commit,
                license=self.license,
            )

        return component
示例#13
0
 def remove_temp(self):
     if self.tempdir:
         remove_tree(self.tempdir)
         self.tempdir = None
示例#14
0
def delete_object_dir(instance):
    """Remove path if it exists."""
    project_path = instance.full_path
    if os.path.exists(project_path):
        remove_tree(project_path)
示例#15
0
 def disable(self):
     super().disable()
     if self._tempdir is not None:
         remove_tree(self._tempdir)
         self._tempdir = None