Example #1
0
    def update_from_templates(self, pootle_path=None):
        """update translation project from templates"""
        if self.is_template_project:
            return
        template_translation_project = self.project.get_template_translationproject(
        )
        if template_translation_project is None or template_translation_project == self:
            return

        monolingual = self.project.is_monolingual()

        if not monolingual:
            self.sync()

        if pootle_path is None:
            oldstats = self.getquickstats()

        for store in template_translation_project.stores.iterator():
            if self.file_style == 'gnu':
                new_pootle_path, new_path = get_translated_name_gnu(
                    self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)
            if pootle_path is not None and new_pootle_path != pootle_path:
                continue
            convert_template(self, store, new_pootle_path, new_path,
                             monolingual)
        self.scan_files()
        #self.update(conservative=False)

        if pootle_path is None:
            newstats = self.getquickstats()
            post_template_update.send(sender=self,
                                      oldstats=oldstats,
                                      newstats=newstats)
Example #2
0
    def update_from_templates(self, pootle_path=None):
        """update translation project from templates"""
        if self.is_template_project:
            return
        template_translation_project = self.project.get_template_translationproject()
        if template_translation_project is None or template_translation_project == self:
            return

        monolingual = self.project.is_monolingual()

        if not monolingual:
            self.sync()

        if pootle_path is None:
            oldstats = self.getquickstats()

        for store in template_translation_project.stores.iterator():
            if self.file_style == "gnu":
                new_pootle_path, new_path = get_translated_name_gnu(self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)
            if pootle_path is not None and new_pootle_path != pootle_path:
                continue
            convert_template(self, store, new_pootle_path, new_path, monolingual)
        self.scan_files()
        self.update(conservative=False)

        if pootle_path is None:
            newstats = self.getquickstats()
            post_template_update.send(sender=self, oldstats=oldstats, newstats=newstats)
Example #3
0
 def test_template_detection(self):
     """test that given a template the correct target file name is generated"""
     template_tp = self.project.get_template_translationproject()
     for template_store in template_tp.stores.iterator():
         for tp in self.project.translationproject_set.exclude(language__code='templates').iterator():
             new_pootle_path, new_path = get_translated_name(tp, template_store)
             store = tp.stores.get(pootle_path=new_pootle_path)
             self.assertEqual(new_pootle_path, store.pootle_path)
             self.assertEqual(new_path, store.abs_real_path)
Example #4
0
    def update_from_templates(self, pootle_path=None):
        """Update translation project from templates."""

        if self.is_template_project:
            return

        template_translation_project = self.project \
            .get_template_translationproject()

        if template_translation_project is None or \
           template_translation_project == self:
            return

        monolingual = self.project.is_monolingual()

        if not monolingual:
            self.sync()

        if pootle_path is None:
            oldstats = self.getquickstats()

        from pootle_app.project_tree import convert_template, \
            get_translated_name, get_translated_name_gnu

        for store in template_translation_project.stores.iterator():

            if self.file_style == 'gnu':
                new_pootle_path, new_path = get_translated_name_gnu(
                    self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)

            if pootle_path is not None and new_pootle_path != pootle_path:
                continue

            convert_template(self, store, new_pootle_path, new_path,
                             monolingual)

        all_files, new_files = self.scan_files()
        #self.update(conservative=False)

        from pootle_misc.versioncontrol import hasversioning
        project_path = self.project.get_real_path()
        if new_files and hasversioning(project_path):
            from translate.storage import versioncontrol
            vcs = versioncontrol.get_versioned_object(project_path)
            output = vcs.add([s.abs_real_path for s in new_files],
                             "New files added from %s based on templates" %
                             (settings.TITLE))

        if pootle_path is None:
            newstats = self.getquickstats()

            from pootle_app.models.signals import post_template_update
            post_template_update.send(sender=self,
                                      oldstats=oldstats,
                                      newstats=newstats)
Example #5
0
 def test_template_detection(self):
     """test that given a template the correct target file name is generated"""
     template_tp = self.project.get_template_translationproject()
     template_store = template_tp.stores.get(name='test.pot')
     for tp in self.project.translationproject_set.exclude(
             language__code='templates').iterator():
         new_pootle_path, new_path = get_translated_name(tp, template_store)
         store = tp.stores.all()[0]
         self.assertEqual(new_pootle_path, store.pootle_path)
         self.assertEqual(new_path, store.abs_real_path)
Example #6
0
    def update_against_templates(self, pootle_path=None):
        """Update translation project from templates."""

        if self.is_template_project:
            return

        template_translation_project = self.project \
                                           .get_template_translationproject()

        if (template_translation_project is None or
            template_translation_project == self):
            return

        monolingual = self.project.is_monolingual()

        if not monolingual:
            self.sync()

        if pootle_path is None:
            oldstats = self.getquickstats()

        from pootle_app.project_tree import (convert_template,
                                             get_translated_name,
                                             get_translated_name_gnu)

        for store in template_translation_project.stores.iterator():
            if self.file_style == 'gnu':
                new_pootle_path, new_path = get_translated_name_gnu(self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)

            if pootle_path is not None and new_pootle_path != pootle_path:
                continue

            convert_template(self, store, new_pootle_path, new_path,
                             monolingual)

        all_files, new_files = self.scan_files(vcs_sync=False)
        #self.update(conservative=False)

        from pootle_misc import versioncontrol
        project_path = self.project.get_real_path()

        if new_files and versioncontrol.hasversioning(project_path):
            output = versioncontrol.add_files(project_path,
                    [s.file.name for s in new_files],
                    "New files added from %s based on templates" %
                            (settings.TITLE))

        if pootle_path is None:
            newstats = self.getquickstats()

            from pootle_app.models.signals import post_template_update
            post_template_update.send(sender=self, oldstats=oldstats,
                                      newstats=newstats)
Example #7
0
    def update_against_templates(self, pootle_path=None):
        """Update translation project from templates."""

        if self.is_template_project:
            return

        template_translation_project = self.project \
                                           .get_template_translationproject()

        if (template_translation_project is None or
            template_translation_project == self):
            return

        monolingual = self.project.is_monolingual

        if not monolingual:
            self.sync()

        from pootle_app.project_tree import (convert_template,
                                             get_translated_name,
                                             get_translated_name_gnu)

        for store in template_translation_project.stores.iterator():
            if self.file_style == 'gnu':
                new_pootle_path, new_path = get_translated_name_gnu(self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)

            if pootle_path is not None and new_pootle_path != pootle_path:
                continue

            convert_template(self, store, new_pootle_path, new_path,
                             monolingual)

        all_files, new_files = self.scan_files(vcs_sync=False)

        project_path = self.project.get_real_path()

        if new_files and versioncontrol.hasversioning(project_path):
            message = ("New files added from %s based on templates" %
                       get_site_title())

            filestocommit = [f.file.name for f in new_files]
            success = True
            try:
                output = versioncontrol.add_files(project_path, filestocommit,
                                                  message)
            except Exception:
                logging.exception(u"Failed to add files")
                success = False

        if pootle_path is None:
            from pootle_app.signals import post_template_update
            post_template_update.send(sender=self)
Example #8
0
    def update_from_templates(self, pootle_path=None):
        """Update translation project from templates."""

        if self.is_template_project:
            return

        template_translation_project = self.project.get_template_translationproject()

        if template_translation_project is None or template_translation_project == self:
            return

        monolingual = self.project.is_monolingual()

        if not monolingual:
            self.sync()

        if pootle_path is None:
            oldstats = self.getquickstats()

        from pootle_app.project_tree import convert_template, get_translated_name, get_translated_name_gnu

        for store in template_translation_project.stores.iterator():

            if self.file_style == "gnu":
                new_pootle_path, new_path = get_translated_name_gnu(self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)

            if pootle_path is not None and new_pootle_path != pootle_path:
                continue

            convert_template(self, store, new_pootle_path, new_path, monolingual)

        all_files, new_files = self.scan_files()
        # self.update(conservative=False)

        from pootle_misc.versioncontrol import hasversioning

        project_path = self.project.get_real_path()
        if new_files and hasversioning(project_path):
            from translate.storage import versioncontrol

            vcs = versioncontrol.get_versioned_object(project_path)
            output = vcs.add(
                [s.abs_real_path for s in new_files], "New files added from %s based on templates" % (settings.TITLE)
            )

        if pootle_path is None:
            newstats = self.getquickstats()

            from pootle_app.models.signals import post_template_update

            post_template_update.send(sender=self, oldstats=oldstats, newstats=newstats)
Example #9
0
    def get_stores_for_path(self, pootle_path):
        """Return the stores for this goal in the given pootle path.

        If this is a project goal then the corresponding stores in the path to
        that ones in the 'templates' TP for this goal are returned instead.

        :param pootle_path: A string with a valid pootle path.
        """
        # Putting the next imports at the top of the file causes circular
        # import issues.
        from pootle_store.models import Store
        from pootle_translationproject.models import TranslationProject

        lang, proj, dir_path, filename = split_pootle_path(pootle_path)

        # Get the translation project for this pootle_path.
        try:
            tp = TranslationProject.objects.get(language__code=lang, project__code=proj)
        except TranslationProject.DoesNotExist:
            return Store.objects.none()

        if self.project_goal and not tp.is_template_project:
            # Get the stores for this goal that are in the 'templates' TP.
            templates_tp = tp.project.get_template_translationproject()

            if templates_tp is None:
                return Store.objects.none()
            else:
                path_in_templates = templates_tp.pootle_path + dir_path + filename
                lookups = {"pootle_path__startswith": path_in_templates, "goals__in": [self]}
                template_stores_in_goal = Store.objects.filter(**lookups)

                # Putting the next imports at the top of the file causes circular
                # import issues.
                if tp.file_style == "gnu":
                    from pootle_app.project_tree import get_translated_name_gnu as get_translated_name
                else:
                    from pootle_app.project_tree import get_translated_name

                # Get the pootle path for the corresponding stores in the given
                # TP for those stores in the 'templates' TP.
                criteria = {"pootle_path__in": [get_translated_name(tp, store)[0] for store in template_stores_in_goal]}
        else:
            # This is a regular goal or the given TP is the 'templates' TP, so
            # just retrieve the goal stores on this TP.
            criteria = {"pootle_path__startswith": pootle_path, "goals__in": [self]}

        # Return the stores.
        return Store.objects.filter(**criteria)
Example #10
0
    def get_stores_for_path(self, pootle_path):
        """Return the stores for this goal in the given pootle path.

        If this is a project goal then the corresponding stores in the path to
        that ones in the 'templates' TP for this goal are returned instead.

        :param pootle_path: A string with a valid pootle path.
        """
        # Putting the next imports at the top of the file causes circular
        # import issues.
        from pootle_store.models import Store
        from pootle_translationproject.models import TranslationProject

        lang, proj, dir_path, filename = split_pootle_path(pootle_path)

        # Get the translation project for this pootle_path.
        try:
            tp = TranslationProject.objects.get(language__code=lang,
                                                project__code=proj)
        except TranslationProject.DoesNotExist:
            return Store.objects.none()

        if self.project_goal and not tp.is_template_project:
            # Get the stores for this goal that are in the 'templates' TP.
            templates_tp = tp.project.get_template_translationproject()

            if templates_tp is None:
                return Store.objects.none()
            else:
                path_in_templates = (templates_tp.pootle_path + dir_path +
                                     filename)
                lookups = {
                    'pootle_path__startswith': path_in_templates,
                    'goals__in': [self],
                }
                template_stores_in_goal = Store.objects.filter(**lookups)

                # Putting the next imports at the top of the file causes circular
                # import issues.
                if tp.file_style == 'gnu':
                    from pootle_app.project_tree import (
                        get_translated_name_gnu as get_translated_name)
                else:
                    from pootle_app.project_tree import get_translated_name

                # Get the pootle path for the corresponding stores in the given
                # TP for those stores in the 'templates' TP.
                criteria = {
                    'pootle_path__in': [get_translated_name(tp, store)[0]
                                        for store in template_stores_in_goal],
                }
        else:
            # This is a regular goal or the given TP is the 'templates' TP, so
            # just retrieve the goal stores on this TP.
            criteria = {
                'pootle_path__startswith': pootle_path,
                'goals__in': [self],
            }

        # Return the stores.
        return Store.objects.filter(**criteria)
Example #11
0
    def update_against_templates(self, pootle_path=None):
        """Update translation project from templates."""

        if self.is_template_project:
            return

        template_translation_project = self.project \
                                           .get_template_translationproject()

        if (template_translation_project is None or
            template_translation_project == self):
            return

        monolingual = self.project.is_monolingual

        if not monolingual:
            self.sync()

        from pootle_app.project_tree import (convert_template,
                                             get_translated_name,
                                             get_translated_name_gnu)

        for store in template_translation_project.stores.iterator():
            if self.file_style == 'gnu':
                new_pootle_path, new_path = get_translated_name_gnu(self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)

            if pootle_path is not None and new_pootle_path != pootle_path:
                continue

            try:
                from pootle.scripts import hooks
                relative_po_path = os.path.relpath(new_path,
                                                   settings.PODIRECTORY)
                if not hooks.hook(self.project.code, "pretemplateupdate",
                                  relative_po_path):
                    continue
            except:
                # Assume hook is not present.
                pass

            convert_template(self, store, new_pootle_path, new_path,
                             monolingual)

        all_files, new_files = self.scan_files(vcs_sync=False)

        from pootle_misc import versioncontrol
        project_path = self.project.get_real_path()

        if new_files and versioncontrol.hasversioning(project_path):
            from pootle.scripts import hooks
            siteconfig = load_site_config()
            message = ("New files added from %s based on templates" %
                       siteconfig.get('TITLE'))

            filestocommit = []
            for new_file in new_files:
                try:
                    hook_files = hooks.hook(self.project.code, "precommit",
                                            new_file.file.name, author=None,
                                            message=message)
                    filestocommit.extend(hook_files)
                except ImportError:
                    # Failed to import the hook - we're going to assume there
                    # just isn't a hook to import. That means we'll commit the
                    # original file.
                    filestocommit.append(new_file.file.name)

            success = True
            try:
                output = versioncontrol.add_files(project_path, filestocommit,
                                                  message)
            except Exception:
                logging.exception(u"Failed to add files")
                success = False

            for new_file in new_files:
                try:
                    hooks.hook(self.project.code, "postcommit",
                               new_file.file.name, success=success)
                except:
                    #FIXME: We should not hide the exception - makes
                    # development impossible
                    pass

        if pootle_path is None:
            from pootle_app.models.signals import post_template_update
            post_template_update.send(sender=self)
Example #12
0
    def update_against_templates(self, pootle_path=None):
        """Update translation project from templates."""

        if self.is_template_project:
            return

        template_translation_project = self.project \
                                           .get_template_translationproject()

        if (template_translation_project is None
                or template_translation_project == self):
            return

        monolingual = self.project.is_monolingual()

        if not monolingual:
            self.sync()

        if pootle_path is None:
            oldstats = self.getquickstats()

        from pootle_app.project_tree import (convert_template,
                                             get_translated_name,
                                             get_translated_name_gnu)

        for store in template_translation_project.stores.iterator():
            if self.file_style == 'gnu':
                new_pootle_path, new_path = get_translated_name_gnu(
                    self, store)
            else:
                new_pootle_path, new_path = get_translated_name(self, store)

            if pootle_path is not None and new_pootle_path != pootle_path:
                continue

            relative_po_path = os.path.relpath(new_path, settings.PODIRECTORY)
            try:
                from pootle.scripts import hooks
                if not hooks.hook(self.project.code, "pretemplateupdate",
                                  relative_po_path):
                    continue
            except:
                # Assume hook is not present.
                pass

            convert_template(self, store, new_pootle_path, new_path,
                             monolingual)

        all_files, new_files = self.scan_files(vcs_sync=False)

        from pootle_misc import versioncontrol
        project_path = self.project.get_real_path()

        if new_files and versioncontrol.hasversioning(project_path):
            from pootle.scripts import hooks
            message = "New files added from %s based on templates" % \
                      (settings.TITLE)

            filestocommit = []
            for new_file in new_files:
                try:
                    filestocommit.extend(
                        hooks.hook(self.project.code,
                                   "precommit",
                                   new_file.file.name,
                                   author=None,
                                   message=message))
                except ImportError:
                    # Failed to import the hook - we're going to assume there
                    # just isn't a hook to import. That means we'll commit the
                    # original file.
                    filestocommit.append(new_file.file.name)

            success = True
            try:
                output = versioncontrol.add_files(project_path, filestocommit,
                                                  message)
            except Exception, e:
                logging.error(u"Failed to add files: %s", e)
                success = False

            for new_file in new_files:
                try:
                    hooks.hook(self.project.code,
                               "postcommit",
                               new_file.file.name,
                               success=success)
                except:
                    #FIXME: We should not hide the exception - makes
                    # development impossible
                    pass
Example #13
0
    def delete_cache_for_path(self, pootle_path):
        """Delete this goal cache for a given path and upper directories.

        The cache is deleted for the given path, for the directories between
        the given path and the translation project, and for the translation
        project itself.

        If the goal is a 'project goal' then delete the cache for all the
        translation projects in the same project for the specified translation
        project.

        :param pootle_path: A string with a valid pootle path.
        """
        # Putting the next imports at the top of the file causes circular
        # import issues.
        from pootle_app.models.directory import Directory
        from pootle_store.models import Store

        try:
            path_obj = Store.objects.get(pootle_path=pootle_path)
        except Store.DoesNotExist:
            try:
                path_obj = Directory.objects.get(pootle_path=pootle_path)
            except Directory.DoesNotExist:
                # If it is not possible to retrieve any path_obj for the
                # provided pootle_path, then abort.
                return

        translation_project = path_obj.translation_project

        if self.project_goal and isinstance(path_obj, Store):
            # Putting the next imports at the top of the file causes circular
            # import issues.
            if translation_project.file_style == 'gnu':
                from pootle_app.project_tree import (get_translated_name_gnu as
                                                     get_translated_name)
            else:
                from pootle_app.project_tree import get_translated_name

        if self.project_goal:
            # Delete the cached stats for all the translation projects in the
            # same project.
            tps = translation_project.project.translationproject_set.all()
        else:
            # Just delete the cached stats for the given translation project.
            tps = [translation_project]

        # For each TP get the pootle_path for all the directories in between
        # the path_obj and TP.
        keys = []
        for tp in tps:
            if isinstance(path_obj, Store):
                if tp != translation_project:
                    store_path = get_translated_name(tp, path_obj)[0]
                    try:
                        tp_store = Store.objects.get(pootle_path=store_path)
                        path_dir = tp_store.parent
                    except Store.DoesNotExist:
                        # If there is no matching store in this TP then just
                        # jump to the next TP.
                        continue
                else:
                    path_dir = path_obj.parent
            elif isinstance(path_obj, Directory):
                path_dir = path_obj

            # Note: Not including path_obj (if it is a store) in path_objs
            # since we still don't support including units in a goal.
            path_objs = chain([tp], path_dir.trail())

            for path_obj in path_objs:
                for function_name in self.CACHED_FUNCTIONS:
                    keys.append(iri_to_uri(self.pootle_path + ":" +
                                           path_obj.pootle_path + ":" +
                                           function_name))
        cache.delete_many(keys)