Fix git updates

Julien LepillerFri Aug 23 17:36:36+0200 2019

f27460a

Fix git updates

offlate/formats/gettext.py

3939
4040
    def merge(self, older, callback):
4141
        older.po.merge(self.pot)
42-
        self.po.save()
42+
        self.po.merge(self.pot)
43+
        older.po.save()
4344
        for oentry in older.po:
4445
            for nentry in self.po:
4546
                if oentry.msgid == nentry.msgid:

offlate/systems/git.py

1616
""" The git abstract system connector. """
1717
1818
import pygit2
19-
from os import rename, remove
19+
from os import rename
20+
from pathlib import Path
2021
2122
from translation_finder import discover
2223
from ..formats.yaml import YamlFormat
2324
from ..formats.gettext import GettextFormat
2425
26+
def rmdir(dir):
27+
    dir = Path(dir)
28+
    for item in dir.iterdir():
29+
        if item.is_dir():
30+
            rmdir(item)
31+
        else:
32+
            item.unlink()
33+
    dir.rmdir()
34+
2535
class Progress(pygit2.remote.RemoteCallbacks):
2636
    def __init__(self, callback):
2737
        super().__init__()

5464
        raise Exception("Unimplemented method in concrete class: updateURI")
5565
5666
    def updateFiles(self):
57-
        self.translationfiles = []
58-
        translations = discover(self.basedir + '/current')
59-
        path = self.basedir + '/current/'
67+
        self.translationfiles = self.updateFilesFromDirectory(
68+
                self.basedir + '/current')
69+
70+
    def updateFilesFromDirectory(self, directory):
71+
        translations = discover(directory)
72+
        translationfiles = []
73+
        path = directory + '/'
6074
        for resource in translations:
6175
            if resource['file_format'] == 'po':
6276
                popath = resource['filemask'].replace('*', self.lang)
6377
                if 'new_base' in resource:
64-
                    potpath = resource['newbase']
78+
                    potpath = resource['new_base']
6579
                else:
6680
                    # If there is no POT, then we can't really do anything...
6781
                    continue
68-
                self.translationfiles.append({'filename': popath,
82+
                translationfiles.append({'filename': popath,
6983
                    'format': GettextFormat({'file': path + popath,
7084
                        'pot': path + potpath,
7185
                        'version': '0.1',

7387
                        'lang': self.lang})})
7488
            elif resource['file_format'] == 'yaml':
7589
                yamlpath = resource['filemask'].replace('*', self.lang)
76-
                self.translationfiles.append({'filename': yamlpath,
90+
                translationfiles.append({'filename': yamlpath,
7791
                    'format': YamlFormat({'dest': path + yamlpath,
7892
                        'source': path + resource['template']})})
7993
            else:
8094
                raise Exception('Unsupported file format: {}'.format(resource['file_format']))
95+
        return translationfiles
8196
8297
    def clone(self, directory, callback=None):
8398
        pygit2.clone_repository(self.uri, directory, callbacks=Progress(callback))

87102
        repo.checkout(ref)
88103
89104
    def update(self, callback):
90-
        os.rename(self.basedir + "/current", self.basedir + "/old")
105+
        rename(self.basedir + "/current", self.basedir + "/old")
91106
        self.clone(self.basedir + "/current")
92-
        # TODO: merge
93-
        os.remove(self.basedir + "/old")
107+
        oldfiles = self.updateFilesFromDirectory(self.basedir + "/old")
108+
        self.updateFiles()
109+
        newfiles = self.translationfiles
110+
        for mfile in newfiles:
111+
            path = mfile['filename']
112+
            newformat = mfile['format']
113+
            oldformat = None
114+
            for mmfile in oldfiles:
115+
                if mmfile['filename'] == path:
116+
                    oldformat = mmfile['format']
117+
            if oldformat is None:
118+
                continue
119+
            newformat.merge(oldformat, callback)
120+
        rmdir(self.basedir + "/old")
94121
95122
    def send(self, interface):
96123
        raise Exception("Unimplemented method in concrete class: send")