Fix git updates
offlate/formats/gettext.py
39 | 39 | ||
40 | 40 | def merge(self, older, callback): | |
41 | 41 | older.po.merge(self.pot) | |
42 | - | self.po.save() | |
42 | + | self.po.merge(self.pot) | |
43 | + | older.po.save() | |
43 | 44 | for oentry in older.po: | |
44 | 45 | for nentry in self.po: | |
45 | 46 | if oentry.msgid == nentry.msgid: |
offlate/systems/git.py
16 | 16 | """ The git abstract system connector. """ | |
17 | 17 | ||
18 | 18 | import pygit2 | |
19 | - | from os import rename, remove | |
19 | + | from os import rename | |
20 | + | from pathlib import Path | |
20 | 21 | ||
21 | 22 | from translation_finder import discover | |
22 | 23 | from ..formats.yaml import YamlFormat | |
23 | 24 | from ..formats.gettext import GettextFormat | |
24 | 25 | ||
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 | + | ||
25 | 35 | class Progress(pygit2.remote.RemoteCallbacks): | |
26 | 36 | def __init__(self, callback): | |
27 | 37 | super().__init__() | |
… | |||
54 | 64 | raise Exception("Unimplemented method in concrete class: updateURI") | |
55 | 65 | ||
56 | 66 | 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 + '/' | |
60 | 74 | for resource in translations: | |
61 | 75 | if resource['file_format'] == 'po': | |
62 | 76 | popath = resource['filemask'].replace('*', self.lang) | |
63 | 77 | if 'new_base' in resource: | |
64 | - | potpath = resource['newbase'] | |
78 | + | potpath = resource['new_base'] | |
65 | 79 | else: | |
66 | 80 | # If there is no POT, then we can't really do anything... | |
67 | 81 | continue | |
68 | - | self.translationfiles.append({'filename': popath, | |
82 | + | translationfiles.append({'filename': popath, | |
69 | 83 | 'format': GettextFormat({'file': path + popath, | |
70 | 84 | 'pot': path + potpath, | |
71 | 85 | 'version': '0.1', | |
… | |||
73 | 87 | 'lang': self.lang})}) | |
74 | 88 | elif resource['file_format'] == 'yaml': | |
75 | 89 | yamlpath = resource['filemask'].replace('*', self.lang) | |
76 | - | self.translationfiles.append({'filename': yamlpath, | |
90 | + | translationfiles.append({'filename': yamlpath, | |
77 | 91 | 'format': YamlFormat({'dest': path + yamlpath, | |
78 | 92 | 'source': path + resource['template']})}) | |
79 | 93 | else: | |
80 | 94 | raise Exception('Unsupported file format: {}'.format(resource['file_format'])) | |
95 | + | return translationfiles | |
81 | 96 | ||
82 | 97 | def clone(self, directory, callback=None): | |
83 | 98 | pygit2.clone_repository(self.uri, directory, callbacks=Progress(callback)) | |
… | |||
87 | 102 | repo.checkout(ref) | |
88 | 103 | ||
89 | 104 | def update(self, callback): | |
90 | - | os.rename(self.basedir + "/current", self.basedir + "/old") | |
105 | + | rename(self.basedir + "/current", self.basedir + "/old") | |
91 | 106 | 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") | |
94 | 121 | ||
95 | 122 | def send(self, interface): | |
96 | 123 | raise Exception("Unimplemented method in concrete class: send") |