damned-lies r1087 - in branches/djamnedlies: . stats templates



Author: claudep
Date: Thu Oct 23 17:47:16 2008
New Revision: 1087
URL: http://svn.gnome.org/viewvc/damned-lies?rev=1087&view=rev

Log:
2008-10-23  Claude Paroz  <claude 2xlibre net>

	* stats/models.py: Finished update-stats implementation.
	* stats/utils.py: Added more functions to generate doc stats.
	* templates/module.html: 'Translation' and 'Documentation' should only
	appear when domain > 1
	* templates/show-stats.html: Missing webroot.

Modified:
   branches/djamnedlies/ChangeLog
   branches/djamnedlies/stats/models.py
   branches/djamnedlies/stats/utils.py
   branches/djamnedlies/templates/module.html
   branches/djamnedlies/templates/show-stats.html

Modified: branches/djamnedlies/stats/models.py
==============================================================================
--- branches/djamnedlies/stats/models.py	(original)
+++ branches/djamnedlies/stats/models.py	Thu Oct 23 17:47:16 2008
@@ -18,7 +18,8 @@
 # along with Damned Lies; if not, write to the Free Software Foundation, Inc.,
 # 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-import os, re, commands
+import os, sys, re, commands
+from datetime import datetime
 from time import tzname
 from django.db import models, connection
 from django.contrib.auth.models import User, Group
@@ -184,9 +185,10 @@
         """ Returns the path of the local checkout for the branch """
         return os.path.join(settings.SCRATCHDIR, self.module.vcs_type, self.module.name + "." + self.name)
     
-    def output_dir(self):
+    def output_dir(self, dom_type):
         """ Directory where generated pot and po files are written on local system """
-        dirname = os.path.join(settings.POTDIR, self.module.name + "." + self.name)
+        subdir = {'ui': '', 'doc': 'docs'}[dom_type]
+        dirname = os.path.join(settings.POTDIR, self.module.name + "." + self.name, subdir)
         if not os.path.isdir(dirname):
             os.makedirs(dirname)
         return dirname
@@ -194,21 +196,32 @@
     def get_stats(self, typ):
         """ Get statistics list of type typ ('ui' or 'doc'), in a dict of lists, key is domain.name (POT in 1st position)"""
         stats = {}
-        for stat in self.statistics_set.order_by('Translated').reverse():
+        for stat in self.statistics_set.all():
             if stat.domain.dtype == typ:
                 if stats.has_key(stat.domain.name):
-                    if stat.Language:
+                    if stat.language:
                         stats[stat.domain.name].append(stat)
                     else:
                         stats[stat.domain.name].insert(0, stat) # This is the POT file
                 else:
                     stats[stat.domain.name] = [stat,]
-        # sort by domain, then translated
-        #templist = [(obj_.domain_id, obj_.Translated, obj_) for obj_ in stats]
-        #templist.sort()
-        #return [obj_ for (key1, key2, obj_) in templist]
+        # Sort
+        for key, doms in stats.items():
+            doms.sort(self.compare_stats)
         return stats
     
+    def compare_stats(self, a, b):
+        """ Sort stats, pot first, then translated (desc), then language name """
+        if not a.language:
+            return -1
+        elif not b.language:
+            return 1
+        else:
+            res = -cmp(a.Translated, b.Translated)
+            if not res:
+                res = cmp(a.get_lang(), b.get_lang())
+        return res  
+    
     def get_doc_stats(self):
         return self.get_stats('doc')
 
@@ -228,11 +241,17 @@
             errors = []
             if dom.dtype == 'ui':
                 # Run intltool-update -m to check for some errors
-                errors.extend(utils.check_pot_regeneration(domain_path))
+                errors.extend(utils.check_potfiles(domain_path))
             
             # Update pot file
-            potfile = utils.generate_pot_file(domain_path, dom.potbase(), settings.DEBUG)
-            previous_pot = os.path.join(self.output_dir(), dom.potbase() + "." + self.name + ".pot")
+            if dom.dtype == 'ui':
+                potfile, errs = utils.generate_ui_pot_file(domain_path, dom.potbase(), settings.DEBUG)
+            elif dom.dtype == 'doc':
+                potfile, errs = utils.generate_doc_pot_file(domain_path, dom.potbase(), self.module.name, settings.DEBUG)
+                doclinguas = utils.read_makefile_variable(domain_path, "DOC_LINGUAS").split()
+            errors.extend(errs)
+            
+            previous_pot = os.path.join(self.output_dir(dom.dtype), dom.potbase() + "." + self.name + ".pot")
             if not potfile:
                 if settings.DEBUG: print >> sys.stderr, "Can't generate POT file for %s/%s." % (self.module.name, dom.directory)
                 if os.access(previous_pot, os.R_OK):
@@ -247,7 +266,7 @@
 
             # If old pot already exists and we are in string freeze
             if os.access(previous_pot, os.R_OK):
-                if string_freezed:
+                if string_freezed and dom.dtype == 'ui':
                     diff = potdiff.diff(previous_pot, potfile, 1)
                     if len(diff):
                         self.notify_list(out_domain, diff)
@@ -266,29 +285,26 @@
             try:
                 stat = Statistics.objects.get(language=None, branch=self, domain=dom)
                 stat.Untranslated = int(pot_stats['untranslated'])
-                stat.Date = datetime.datetime.now()
-                stat.information_set.clear()
+                stat.Date = datetime.now()
+                Information.objects.filter(Statistics=stat).delete()
             except:
                 stat = Statistics(language = None, branch = self, domain = dom, Translated = 0,
                                   Fuzzy = 0, Untranslated = int(pot_stats['untranslated']))
-            stat.information_set = pot_stats['errors']
+            for err in errors:
+                stat.information_set.add(Information(Type=err[0], Description=err[1]))
             stat.save()
                 
             # Update po files and update DB with new stats
             command = "msgmerge -o %(outpo)s %(pofile)s %(potfile)s"
-            for pofile in os.listdir(domain_path):
-                if pofile[-3:] != ".po":
-                    continue
-                lang = pofile[:-3]
-                outpo = os.path.join(self.output_dir(), dom.potbase() + "." + lang + ".po")
+            for lang, pofile in self.get_lang_files(dom, domain_path):
+                outpo = os.path.join(self.output_dir(dom.dtype), dom.potbase() + "." + lang + ".po")
 
-                srcpo = os.path.join(domain_path, pofile)
-                if not force and not pot_has_changed and os.access(outpo, os.R_OK) and os.stat(srcpo)[8] < os.stat(outpo)[8]:
+                if not force and not pot_has_changed and os.access(outpo, os.R_OK) and os.stat(pofile)[8] < os.stat(outpo)[8]:
                     continue
 
                 realcmd = command % {
                     'outpo' : outpo,
-                    'pofile' : srcpo,
+                    'pofile' : pofile,
                     'potfile' : potfile,
                     }
                 if settings.DEBUG: print >>sys.stderr, realcmd
@@ -296,7 +312,11 @@
                 if settings.DEBUG: print >> sys.stderr, output
 
                 langstats = utils.po_file_stats(outpo, 1)
-                langstats['errors'].extend(utils.check_lang_support(self.co_path(), domain_path, lang))
+                if dom.dtype == "ui":
+                    langstats['errors'].extend(utils.check_lang_support(self.co_path(), domain_path, lang))
+                elif dom.dtype == "doc":
+                    if lang not in doclinguas:
+                        langstats['errors'].append(("warn", N_("DOC_LINGUAS list doesn't include this language.")))
 
                 if settings.DEBUG: print >>sys.stderr, lang + ":\n" + str(langstats)
                 # Save in DB
@@ -305,8 +325,8 @@
                     stat.Translated = int(langstats['untranslated'])
                     stat.Fuzzy = int(langstats['fuzzy'])
                     stat.Untranslated = int(langstats['untranslated'])
-                    stat.Date = datetime.datetime.now()
-                    stat.information_set.clear()
+                    stat.Date = datetime.now()
+                    Information.objects.filter(Statistics=stat).delete()
                 except:
                     try:
                         language = Language.objects.get(locale=lang)
@@ -315,9 +335,26 @@
                         language.save()
                     stat = Statistics(language = language, branch = self, domain = dom, Translated = int(langstats['translated']),
                                       Fuzzy = int(langstats['fuzzy']), Untranslated = int(langstats['untranslated']))
-                stat.information_set = langstats['errors']
                 stat.save()
-                    
+                for err in langstats['errors']:
+                    stat.information_set.add(Information(Type=err[0], Description=err[1])) 
+    
+    def get_lang_files(self, domain, dom_path):
+        """ Returns a list of language files on filesystem, as tuple (lang, lang_file) -> lang_file with complete path """
+        flist = []
+        if domain.dtype == "ui":
+            for f in os.listdir(dom_path):
+                if f[-3:] != ".po":
+                    continue
+                lang = f[:-3]
+                pofile = os.path.join(dom_path, f)
+                flist.append((lang, pofile))
+        if domain.dtype == "doc":
+            for d in os.listdir(dom_path):
+                pofile = os.path.join(dom_path, d, d + ".po")
+                if os.path.isfile(pofile):
+                    flist.append((d, pofile))
+        return flist
     
     def checkout(self):
         """ Do a checkout or an update of the VCS files """
@@ -674,8 +711,8 @@
     
     def __unicode__(self):
         """ String representation of the object """
-        return "%s (%s-%s) %s (%s)" % (self.Module, self.Type, self.Domain,
-                                       self.Branch, self.Language)
+        return "%s (%s-%s) %s (%s)" % (self.branch.module.name, self.domain.dtype, self.domain.name,
+                                       self.branch.name, self.get_lang())
     
     def is_fake(self):
         return False
@@ -725,6 +762,8 @@
         return int(self.Translated) + int(self.Fuzzy) + int(self.Untranslated)
     
     def pot_text(self):
+        """ Return stat table header: 'POT file (n messages) - updated on ??-??-???? tz' """
+        #import pdb; pdb.set_trace()
         msg_text = ungettext(u"%(count)s message", "%(count)s messages", self.pot_size()) % {'count': self.pot_size()}
         upd_text = _(u"updated on %(date)s") % {'date': self.Date.strftime("%Y-%m-%d %H:%M:%S ")+tzname[0]}
         if self.fig_count():
@@ -755,17 +794,18 @@
                 else:
                     fig['path'] = '' # This should not happen
                 fig['translated'] = len(lines[i+2])>9 and not fig['fuzzy']
-                # Check if a translated figure really exists or if the English one is used
-                if os.path.exists(os.path.join(self.branch.co_path(), self.domain.directory, self.language.locale, fig['path'])):
-                    fig['translated_file'] = True
-                else: fig['translated_file'] = False
+                fig['translated_file'] = False
+                if self.language:
+                    # Check if a translated figure really exists or if the English one is used
+                    if os.path.exists(os.path.join(self.branch.co_path(), self.domain.directory, self.language.locale, fig['path'])):
+                        fig['translated_file'] = True
                 self.figures.append(fig)
                 i += 4
         return self.figures
     
     def fig_count(self):
         """ If stat of a document type, get the number of figures in the document """
-        return len(self.figures())
+        return len(self.get_figures() or [])
     
     def fig_stats(self):
         stats = {'fuzzy':0, 'translated':0, 'total':0, 'prc':0}
@@ -814,7 +854,7 @@
     def update(self):
         """ Update stats in database """
         # TODO
-        print("Updating %s/%s (%s-%s)" % (self.branch.module.name, self.branch.name, self.language.name, self.domain.name))
+        print("Updating %s/%s (%s-%s)" % (self.branch.module.name, self.branch.name, self.get_lang(), self.domain.name))
         
 class FakeStatistics(object):
     """ This is a fake statistics class where a summary value is needed for a multi-domain module

Modified: branches/djamnedlies/stats/utils.py
==============================================================================
--- branches/djamnedlies/stats/utils.py	(original)
+++ branches/djamnedlies/stats/utils.py	Thu Oct 23 17:47:16 2008
@@ -44,7 +44,7 @@
     replacements = {"<ul>": "\n", "</ul>": "\n", "<li>": " * ", "\n</li>": "", "</li>": ""}
     return multiple_replace(replacements, string)
 
-def check_pot_regeneration(po_path):
+def check_potfiles(po_path):
     """Check if there were any problems regenerating a POT file (intltool-update -m).
        Return a list of errors """
     errors = []
@@ -77,8 +77,8 @@
                        + "</li>\n</ul>")))
     return errors
 
-def generate_pot_file(vcs_path, potbase, verbose):
-    """ Return the pot file generated or empty string on error """
+def generate_ui_pot_file(vcs_path, potbase, verbose):
+    """ Return the pot file generated, and the error if any """
     
     command = "cd \"%(dir)s\" && intltool-update -g '%(domain)s' -p" % {
         "dir" : vcs_path,
@@ -91,9 +91,80 @@
     potfile = os.path.join(vcs_path, potbase + ".pot")
 
     if error or not os.access(potfile, os.R_OK):
-        return ""
+        return "", (("error", N_("Error regenerating POT file for %s:\n<pre>%s\n%s</pre>")
+                             % (potbase, command, output))
+                   )
     else:
-        return potfile
+        return potfile, ()
+
+def generate_doc_pot_file(vcs_path, potbase, moduleid, verbose):
+    """ Return the pot file for a document-type domain, and the error if any """
+    
+    errors = []
+    modulename = read_makefile_variable(vcs_path, "DOC_MODULE")
+    if not modulename:
+        return "", (("error", N_("Module %s doesn't look like gnome-doc-utils module.") % moduleid))
+    if not os.access(os.path.join(vcs_path, "C", modulename + ".xml"), os.R_OK):
+        if os.access(os.path.join(vcs_path, "C", moduleid + ".xml"), os.R_OK):
+            errors.append(("warn", N_("DOC_MODULE doesn't resolve to a real file, using '%s.xml'.") % (moduleid)))
+            modulename = moduleid
+        else:
+            errors.append(("error", N_("DOC_MODULE doesn't point to a real file, probably a macro.")))
+            return "", errors
+    
+    files = os.path.join("C", modulename + ".xml")
+    includes = read_makefile_variable(vcs_path, "DOC_INCLUDES")
+    for f in includes.split(" "):
+        if f.strip() != "":
+            files += " %s" % (os.path.join("C", f.strip()))
+    
+    potfile = os.path.join(vcs_path, "C", potbase + ".pot")
+    command = "cd \"%s\" && xml2po -o %s -e %s" % (vcs_path, potfile, files)
+    if verbose: print >>sys.stderr, command
+    (error, output) = commands.getstatusoutput(command)
+    if error:
+        errors.append(("error",
+                       N_("Error regenerating POT file for document %s:\n<pre>%s\n%s</pre>")
+                       % (potbase, command, output)))
+    if verbose: print >> sys.stderr, output
+    
+    if error or not os.access(potfile, os.R_OK):
+        return "", errors
+    else:
+        return potfile, errors
+
+def read_makefile_variable(vcs_path, variable):
+    makefileam = os.path.join(vcs_path, "Makefile.am")
+    try:
+        fin = open(makefileam, "r")
+    except IOError:
+        # probably file not found or unreadable
+        return ""
+
+    fullline = ""
+    for line in fin:
+        fullline += " " + line.strip()
+        if len(line)>2 and line[-2] == "\\":
+            fullline = fullline[:-2]
+        else:
+            match = re.match(variable + r"\s*=\s*([^=]*)", fullline.strip())
+            if match:
+                return match.group(1)
+            else:
+                # FIXME: hackish, works for www.gnome.org/tour
+                match = re.match("include\s+(.+)", fullline.strip())
+                if match:
+                    incfile = os.path.join(os.path.dirname(makefileam), os.path.basename(match.group(1)))
+                    if incfile.find("gnome-doc-utils.make")==-1:
+                        if settings.DEBUG:
+                            print >>sys.stderr, "Reading file %s..." % (incfile)
+                        var = read_makefile_variable(incfile, variable)
+                        if var != "":
+                            return var
+
+            fullline = ""
+    return ""
+
 
 def po_file_stats(pofile, msgfmt_checks = 1):
 
@@ -151,6 +222,7 @@
         'errors' : errors,
         }
 
+
 def check_lang_support(module_path, po_path, lang):
     """Checks if language is listed in one of po/LINGUAS, configure.ac or configure.in"""
 

Modified: branches/djamnedlies/templates/module.html
==============================================================================
--- branches/djamnedlies/templates/module.html	(original)
+++ branches/djamnedlies/templates/module.html	Thu Oct 23 17:47:16 2008
@@ -78,16 +78,20 @@
     <table><tr><td valign="top"><!-- split to two columns -->
     {% with branch.get_doc_stats as stats %}
     {% if stats|length %}
-      <h3>{% trans "Documentation" %}</h3>
-        {% include "show-stats.html" %}
+      {% ifnotequal stats|length 1 %}
+        <h3>{% trans "Documentation" %}</h3>
+      {% endifnotequal %}
+      {% include "show-stats.html" %}
     {% endif %}
     {% endwith %}
 
     </td><td valign="top"><!-- split to two columns -->
     {% with branch.get_ui_stats as stats %}
     {% if stats|length %}
-      <h3>{% trans "Translation" %}</h3>
-        {% include "show-stats.html" %}
+      {% ifnotequal stats|length 1 %}
+        <h3>{% trans "Translation" %}</h3>
+      {% endifnotequal %}
+      {% include "show-stats.html" %}
     {% endif %}
     {% endwith %}
 

Modified: branches/djamnedlies/templates/show-stats.html
==============================================================================
--- branches/djamnedlies/templates/show-stats.html	(original)
+++ branches/djamnedlies/templates/show-stats.html	Thu Oct 23 17:47:16 2008
@@ -40,7 +40,7 @@
       </td>
       <td>{{ line.get_translationstat|safe }}</td>
       {% if stat1.fig_count %}
-       <td><a href="/module/{{ module.name }}/{{ stat1.domain.name }}/{{ branch.name }}/{{ line.language.locale}}/images">
+       <td><a href="{{ webroot }}/module/{{ module.name }}/{{ stat1.domain.name }}/{{ branch.name }}/{{ line.language.locale}}/images">
            <img src="/media/img/figure.png" alt="{% trans "Display document figures" %}"></a></td>
       {% endif %}
       <td style="width: 108px; text-align: center;"><div class="graph">



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]