@@ -110,20 +110,14 @@ def get_path(self) -> str:
110
110
111
111
return os .path .abspath (path )
112
112
113
+
113
114
def initialize (self ):
114
115
""" This is called when a backend is enabled """
115
116
116
117
super (Backend , self ).initialize ()
117
118
filepath = self .get_path ()
118
119
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 ):
127
121
root = firstrun_tasks .generate ()
128
122
xml .create_dirs (self .get_path ())
129
123
xml .save_file (self .get_path (), root )
@@ -140,6 +134,7 @@ def initialize(self):
140
134
xml .save_file (self .get_path (), self .data_tree )
141
135
xml .write_backups (self .get_path ())
142
136
137
+
143
138
def this_is_the_first_run (self , _ ) -> None :
144
139
""" Called upon the very first GTG startup.
145
140
@@ -152,16 +147,7 @@ def this_is_the_first_run(self, _) -> None:
152
147
"""
153
148
154
149
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 )
165
151
166
152
167
153
self ._parameters [self .KEY_DEFAULT_BACKEND ] = True
@@ -172,6 +158,46 @@ def this_is_the_first_run(self, _) -> None:
172
158
self .tag_tree = self .data_tree .find ('taglist' )
173
159
xml .backup_used = None
174
160
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
+
175
201
def start_get_tasks (self ) -> None :
176
202
""" This function starts submitting the tasks from the XML file into
177
203
GTG core. It's run as a separate thread.
0 commit comments