Skip to content

Commit 25dfa55

Browse files
paulgeardiegogangl
authored andcommitted
Tentative (works for me) fix of versioning detection
1 parent a4683c6 commit 25dfa55

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

GTG/backends/backend_localfile.py

+44-18
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,14 @@ def get_path(self) -> str:
110110

111111
return os.path.abspath(path)
112112

113+
113114
def initialize(self):
114115
""" This is called when a backend is enabled """
115116

116117
super(Backend, self).initialize()
117118
filepath = self.get_path()
118119

119-
if versioning.is_required(filepath):
120-
log.warning('Found old file. Running versioning code.')
121-
old_path = os.path.join(DATA_DIR, 'gtg_tasks.xml')
122-
tree = versioning.convert(old_path, self.datastore)
123-
124-
xml.save_file(filepath, tree)
125-
126-
elif not os.path.isfile(filepath):
120+
if not os.path.isfile(filepath):
127121
root = firstrun_tasks.generate()
128122
xml.create_dirs(self.get_path())
129123
xml.save_file(self.get_path(), root)
@@ -140,6 +134,7 @@ def initialize(self):
140134
xml.save_file(self.get_path(), self.data_tree)
141135
xml.write_backups(self.get_path())
142136

137+
143138
def this_is_the_first_run(self, _) -> None:
144139
""" Called upon the very first GTG startup.
145140
@@ -152,16 +147,7 @@ def this_is_the_first_run(self, _) -> None:
152147
"""
153148

154149
filepath = self.get_path()
155-
if versioning.is_required(filepath):
156-
log.warning('Found old file. Running versioning code.')
157-
old_path = os.path.join(DATA_DIR, 'gtg_tasks.xml')
158-
tree = versioning.convert(old_path, self.datastore)
159-
160-
xml.save_file(filepath, tree)
161-
else:
162-
root = firstrun_tasks.generate()
163-
xml.create_dirs(self.get_path())
164-
xml.save_file(self.get_path(), root)
150+
self.do_first_run_versioning(filepath)
165151

166152

167153
self._parameters[self.KEY_DEFAULT_BACKEND] = True
@@ -172,6 +158,46 @@ def this_is_the_first_run(self, _) -> None:
172158
self.tag_tree = self.data_tree.find('taglist')
173159
xml.backup_used = None
174160

161+
162+
def do_first_run_versioning(self, filepath: str) -> None:
163+
"""If there is an old file around needing versioning, convert it, then rename the old file."""
164+
old_path = self.find_old_path(DATA_DIR)
165+
if old_path is not None:
166+
log.warning('Found old file: %r. Running versioning code.', old_path)
167+
tree = versioning.convert(old_path, self.datastore)
168+
xml.save_file(filepath, tree)
169+
os.rename(old_path, old_path + '.imported')
170+
else:
171+
root = firstrun_tasks.generate()
172+
xml.create_dirs(self.get_path())
173+
xml.save_file(self.get_path(), root)
174+
175+
176+
def find_old_path(self, datadir: str) -> str:
177+
"""Reliably find the old data files."""
178+
# used by which version?
179+
path = os.path.join(datadir, 'gtg_tasks.xml')
180+
if os.path.isfile(path):
181+
return path
182+
# used by (at least) 0.3.1-4
183+
path = os.path.join(datadir, 'projects.xml')
184+
if os.path.isfile(path):
185+
return self.find_old_uuid_path(path)
186+
return None
187+
188+
189+
def find_old_uuid_path(self, path: str) -> str:
190+
"""Find the first backend entry with module='backend_localfile' and return its path."""
191+
old_tree = xml.open_file(path, 'config')
192+
for backend in old_tree.findall('backend'):
193+
module = backend.get('module')
194+
if module == 'backend_localfile':
195+
uuid_path = backend.get('path')
196+
if os.path.isfile(uuid_path):
197+
return uuid_path
198+
return None
199+
200+
175201
def start_get_tasks(self) -> None:
176202
""" This function starts submitting the tasks from the XML file into
177203
GTG core. It's run as a separate thread.

GTG/core/versioning.py

-9
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@
3939
tid_cache = {}
4040

4141

42-
def is_required(path: str) -> bool:
43-
"""Determine if we need to run versioning code."""
44-
45-
# Old filename for versions before 0.5
46-
old_file = os.path.join(DATA_DIR, 'gtg_tasks.xml')
47-
48-
return not os.path.isfile(path) and os.path.isfile(old_file)
49-
50-
5142
def convert(path: str, ds: datastore) -> et.ElementTree:
5243
"""Convert old XML into the new format."""
5344

0 commit comments

Comments
 (0)