Skip to content

Commit aacfc8f

Browse files
committed
BUG: Ensure relative paths are resolved when using ctkAppLauncherSettings
This commit is a follow up of 9931394 (ENH: Allow user settings file to be in <APPLAUNCHER_DIR> (#114)) See Slicer/Slicer#5437
1 parent 81ae946 commit aacfc8f

File tree

8 files changed

+122
-63
lines changed

8 files changed

+122
-63
lines changed

Base/Testing/Cpp/ctkAppLauncherSettingsTest.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,20 @@ void ctkAppLauncherSettingsTester::testReadSettings()
200200
"[Paths]\n"
201201
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/regular-settings-path\n"
202202
"2\\path=/path/within/regular-settings\n"
203-
"size=2\n"
203+
"3\\path=relative-path/within/regular-settings\n"
204+
"size=3\n"
204205
"\n"
205206
"[LibraryPaths]\n"
206207
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/regular-settings-librarypath\n"
207208
"2\\path=/librarypath/within/regular-settings\n"
208-
"size=2\n"
209+
"3\\path=relative-librarypath/within/regular-settings\n"
210+
"size=3\n"
209211
"\n"
210212
"[EXTRA_PATH]\n"
211213
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/regular-settings-extrapath\n"
212214
"2\\path=/extrapath/within/regular-settings\n"
213-
"size=2\n";
215+
"3\\path=relative-extrapath/within/regular-settings\n"
216+
"size=3\n";
214217

215218
regularSettingFile.close();
216219
}
@@ -231,17 +234,20 @@ void ctkAppLauncherSettingsTester::testReadSettings()
231234
"[Paths]\n"
232235
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/user-additional-settings-path\n"
233236
"2\\path=/path/within/user-additional-settings\n"
234-
"size=2\n"
237+
"3\\path=relative-path/within/user-additional-settings\n"
238+
"size=3\n"
235239
"\n"
236240
"[LibraryPaths]\n"
237241
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/user-additional-settings-librarypath\n"
238242
"2\\path=/librarypath/within/user-additional-settings\n"
239-
"size=2\n"
243+
"3\\path=relative-librarypath/within/user-additional-settings\n"
244+
"size=3\n"
240245
"\n"
241246
"[EXTRA_PATH]\n"
242247
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/user-additional-settings-extrapath\n"
243248
"2\\path=/extrapath/within/user-additional-settings\n"
244-
"size=2\n";
249+
"3\\path=relative-extrapath/within/user-additional-settings\n"
250+
"size=3\n";
245251

246252
}
247253

@@ -258,17 +264,20 @@ void ctkAppLauncherSettingsTester::testReadSettings()
258264
"[Paths]\n"
259265
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/additional-settings-path\n"
260266
"2\\path=/path/within/additional-settings\n"
261-
"size=2\n"
267+
"3\\path=relative-path/within/additional-settings\n"
268+
"size=3\n"
262269
"\n"
263270
"[LibraryPaths]\n"
264271
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/additional-settings-librarypath\n"
265272
"2\\path=/librarypath/within/additional-settings\n"
266-
"size=2\n"
273+
"3\\path=relative-librarypath/within/additional-settings\n"
274+
"size=3\n"
267275
"\n"
268276
"[EXTRA_PATH]\n"
269277
"1\\path=<APPLAUNCHER_SETTINGS_DIR>/additional-settings-extrapath\n"
270278
"2\\path=/extrapath/within/additional-settings\n"
271-
"size=2\n";
279+
"3\\path=relative-extrapath/within/additional-settings\n"
280+
"size=3\n";
272281

273282
additionalSettingFile.close();
274283
}
@@ -299,25 +308,25 @@ void ctkAppLauncherSettingsTester::testReadSettings()
299308
QCOMPARE_QSTRINGLIST(
300309
appLauncherSettings.paths(),
301310
QStringList()
302-
<< additionalSettingsDir + "/additional-settings-path" << "/path/within/additional-settings"
303-
<< userAdditionalSettingsDir + "/user-additional-settings-path" << "/path/within/user-additional-settings"
304-
<< regularSettingsDir + "/regular-settings-path" << "/path/within/regular-settings"
311+
<< additionalSettingsDir + "/additional-settings-path" << "/path/within/additional-settings" << "/path/to/relative-path/within/additional-settings"
312+
<< userAdditionalSettingsDir + "/user-additional-settings-path" << "/path/within/user-additional-settings" << "/path/to/relative-path/within/user-additional-settings"
313+
<< regularSettingsDir + "/regular-settings-path" << "/path/within/regular-settings" << "/path/to/relative-path/within/regular-settings"
305314
);
306315

307316
QCOMPARE_QSTRINGLIST(
308317
appLauncherSettings.libraryPaths(),
309318
QStringList()
310-
<< additionalSettingsDir + "/additional-settings-librarypath" << "/librarypath/within/additional-settings"
311-
<< userAdditionalSettingsDir + "/user-additional-settings-librarypath" << "/librarypath/within/user-additional-settings"
312-
<< regularSettingsDir + "/regular-settings-librarypath" << "/librarypath/within/regular-settings"
319+
<< additionalSettingsDir + "/additional-settings-librarypath" << "/librarypath/within/additional-settings" << "/path/to/relative-librarypath/within/additional-settings"
320+
<< userAdditionalSettingsDir + "/user-additional-settings-librarypath" << "/librarypath/within/user-additional-settings" << "/path/to/relative-librarypath/within/user-additional-settings"
321+
<< regularSettingsDir + "/regular-settings-librarypath" << "/librarypath/within/regular-settings" << "/path/to/relative-librarypath/within/regular-settings"
313322
);
314323

315324
QCOMPARE_QSTRINGLIST(
316325
appLauncherSettings.pathsEnvVars().value("EXTRA_PATH"),
317326
QStringList()
318-
<< additionalSettingsDir + "/additional-settings-extrapath" << "/extrapath/within/additional-settings"
319-
<< userAdditionalSettingsDir + "/user-additional-settings-extrapath" << "/extrapath/within/user-additional-settings"
320-
<< regularSettingsDir + "/regular-settings-extrapath" << "/extrapath/within/regular-settings"
327+
<< additionalSettingsDir + "/additional-settings-extrapath" << "/extrapath/within/additional-settings" << "/path/to/relative-extrapath/within/additional-settings"
328+
<< userAdditionalSettingsDir + "/user-additional-settings-extrapath" << "/extrapath/within/user-additional-settings" << "/path/to/relative-extrapath/within/user-additional-settings"
329+
<< regularSettingsDir + "/regular-settings-extrapath" << "/extrapath/within/regular-settings" << "/path/to/relative-extrapath/within/regular-settings"
321330
);
322331
}
323332

Base/ctkAppLauncher.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -529,28 +529,8 @@ void ctkAppLauncherPrivate::buildEnvironment(QProcessEnvironment &env)
529529
QHash<QString, QStringList> pathsEnvVars = q->pathsEnvVars();
530530
foreach(const QString& key, pathsEnvVars.keys())
531531
{
532-
// Relative paths are resolved against the launcher directory.
533-
QStringList absolutePaths;
534532
const QStringList& paths = pathsEnvVars[key];
535-
foreach(const QString& path, paths)
536-
{
537-
if (path.isEmpty())
538-
{
539-
continue;
540-
}
541-
QFileInfo fileInfo(path);
542-
if (fileInfo.isRelative())
543-
{
544-
// make absolute path by appending to SlicerHome
545-
absolutePaths << QDir(q->launcherDir()).filePath(path);
546-
}
547-
else
548-
{
549-
// already absolute path
550-
absolutePaths << path;
551-
}
552-
}
553-
QString value = absolutePaths.join(this->PathSep);
533+
QString value = paths.join(this->PathSep);
554534
this->reportInfo(QString("Setting env. variable [%1]:%2").arg(key, value));
555535
if (env.contains(key))
556536
{

Base/ctkAppLauncherSettings.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,20 @@ void ctkAppLauncherSettingsPrivate::readPathSettings(QSettings& settings, const
292292
}
293293
}
294294

295+
// --------------------------------------------------------------------------
296+
QString ctkAppLauncherSettingsPrivate::resolvePath(const QString& path) const
297+
{
298+
QFileInfo fileInfo(path);
299+
if (!this->LauncherDir.isEmpty() && fileInfo.isRelative())
300+
{
301+
return QDir(this->LauncherDir).filePath(path);
302+
}
303+
else
304+
{
305+
return path;
306+
}
307+
}
308+
295309
// --------------------------------------------------------------------------
296310
QString ctkAppLauncherSettingsPrivate::expandValue(const QString& value) const
297311
{
@@ -567,7 +581,7 @@ QStringList ctkAppLauncherSettings::libraryPaths(bool expand /* = true */)const
567581
QStringList expanded;
568582
foreach(const QString& path, d->ListOfLibraryPaths)
569583
{
570-
expanded << (expand ? d->expandValue(path) : path);
584+
expanded << (expand ? d->resolvePath(d->expandValue(path)) : path);
571585
}
572586
return expanded;
573587
}
@@ -579,7 +593,7 @@ QStringList ctkAppLauncherSettings::paths(bool expand /* = true */)const
579593
QStringList expanded;
580594
foreach(const QString& path, d->ListOfPaths)
581595
{
582-
expanded << (expand ? d->expandValue(path) : path);
596+
expanded << (expand ? d->resolvePath(d->expandValue(path)) : path);
583597
}
584598
return expanded;
585599
}
@@ -634,7 +648,7 @@ QStringList ctkAppLauncherSettings::additionalPaths(const QString& variableName,
634648
QStringList expanded;
635649
foreach(const QString& path, d->MapOfPathVars.value(variableName))
636650
{
637-
expanded << (expand ? d->expandValue(path) : path);
651+
expanded << (expand ? d->resolvePath(d->expandValue(path)) : path);
638652
}
639653
return expanded;
640654
}

Base/ctkAppLauncherSettings.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,27 +169,33 @@ class ctkAppLauncherSettings : public QObject
169169

170170
/// Get list of paths associated with the \c Paths group.
171171
///
172-
/// By default, placeholder strings are expanded.
172+
/// By default, placeholder strings are expanded and relative paths are updated
173+
/// prepending the launcher directory.
173174
///
174175
/// \sa ctkAppLauncherSettingsPrivate::expandValue(const QString& value)
176+
/// \sa launcherDir()
175177
QStringList paths(bool expand = true)const;
176178

177179
/// Get environment variable value associated with \c EnvironmentVariables
178180
/// group.
179181
///
180-
/// By default, placeholder strings are expanded.
182+
/// By default, placeholder strings are expanded and relative paths are updated
183+
/// prepending the launcher directory.
181184
///
182185
/// \sa envVars(bool expand)
183186
/// \sa ctkAppLauncherSettingsPrivate::expandValue(const QString& value)
187+
/// \sa launcherDir()
184188
QString envVar(const QString& variableName, bool expand = true) const;
185189

186190
/// Get list of environment variables associated with \c EnvironmentVariables
187191
/// group.
188192
///
189-
/// By default, placeholder strings are expanded.
193+
/// By default, placeholder strings are expanded and relative paths are updated
194+
/// prepending the launcher directory.
190195
///
191196
/// \sa envVar(const QString& variableName, bool expand)
192197
/// \sa ctkAppLauncherSettingsPrivate::expandValue(const QString& value)
198+
/// \sa launcherDir()
193199
QHash<QString, QString> envVars(bool expand = true) const;
194200

195201
/// \brief Get dictionnary of all list of paths.
@@ -211,9 +217,11 @@ class ctkAppLauncherSettings : public QObject
211217
/// `DYLD_LIBRARY_PATH` | NA | libraryPaths() | NA |
212218
/// `PATH` | paths() | paths() | paths() + libraryPaths() |
213219
///
214-
/// By default, placeholder strings are expanded.
220+
/// By default, placeholder strings are expanded and relative paths are updated
221+
/// prepending the launcher directory.
215222
///
216223
/// \sa ctkAppLauncherSettingsPrivate::expandValue(const QString& value)
224+
/// \sa launcherDir()
217225
/// \sa additionalPathsVars(bool expand)
218226
/// \sa libraryPaths(bool expand)
219227
/// \sa paths(bool expand)
@@ -226,17 +234,21 @@ class ctkAppLauncherSettings : public QObject
226234
/// The returned list corresponds to the path list identified by one of
227235
/// the variable associated with \c General/additionalPathVariables.
228236
///
229-
/// By default, placeholder strings are expanded.
237+
/// By default, placeholder strings are expanded and relative paths are updated
238+
/// prepending the launcher directory.
230239
///
231240
/// \sa ctkAppLauncherSettingsPrivate::expandValue(const QString& value)
241+
/// \sa launcherDir()
232242
QStringList additionalPaths(const QString& variableName, bool expand = true) const;
233243

234244
/// \brief Get dictionnary of path list associated with \c General/additionalPathVariables.
235245
///
236-
/// By default, placeholder strings are expanded.
246+
/// By default, placeholder strings are expanded and relative paths are updated
247+
/// prepending the launcher directory.
237248
///
238249
/// \sa additionalPaths(const QString& variableName, bool expand)
239250
/// \sa ctkAppLauncherSettingsPrivate::expandValue(const QString& value)
251+
/// \sa launcherDir()
240252
QHash<QString, QStringList> additionalPathsVars(bool expand = true) const;
241253

242254
/// \brief Get current platform path separator.

Base/ctkAppLauncherSettings_p.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class ctkAppLauncherSettingsPrivate : public QObject
4040
static QString updateSettingDirPlaceHolder(const QString& value, const SettingsType& settingsType);
4141
static QStringList updateSettingDirPlaceHolder(const QStringList& values, const SettingsType& settingsType);
4242

43+
/// \brief Relative path is resolved against the launcher directory.
44+
///
45+
/// If the launcher directory is empty, returns \a path.
46+
QString resolvePath(const QString& path) const;
47+
4348
/// \brief Expand setting \a value
4449
///
4550
/// The following placeholder strings will be updated:

Testing/LauncherLib/AppWithLauncherLib/launcher-settings.ini

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ organizationName=Kitware
1212
2\path=<APPLAUNCHER_DIR>/libB
1313
3\path=/path/to/libC-<env:PET>
1414
4\path=<APPLAUNCHER_SETTINGS_DIR>/libC
15-
size=4
15+
5\path=relative-path/libC
16+
size=5
1617

1718
[Paths]
1819
1\path=<APPLAUNCHER_DIR>/cow/<APPLAUNCHER_NAME>
1920
2\path=/path/to/pig-<env:BOTH>
2021
3\path=/path/to/<env:PET>
2122
4\path=<APPLAUNCHER_SETTINGS_DIR>/sheep/<APPLAUNCHER_NAME>
22-
size=4
23+
5\path=relative-path/sheep/<APPLAUNCHER_NAME>
24+
size=5
2325

2426
[EnvironmentVariables]
2527
BAR=ASSOCIATION
@@ -32,10 +34,12 @@ SETTINGSPLACEHOLDER=<APPLAUNCHER_SETTINGS_DIR>-<APPLAUNCHER_NAME>
3234
1\path=<APPLAUNCHER_DIR>/lib/python/site-packages
3335
2\path=/path/to/site-packages-2
3436
3\path=<APPLAUNCHER_SETTINGS_DIR>/lib/python/site-packages-settings
35-
size=3
37+
4\path=relative-path/to/site-packages-3
38+
size=4
3639

3740
[QT_PLUGIN_PATH]
3841
1\path=<APPLAUNCHER_DIR>/libexec/qt
3942
2\path=<APPLAUNCHER_DIR>/libexec/<env:BAR>
4043
3\path=<APPLAUNCHER_SETTINGS_DIR>/libexec-settings/<env:BAR>
41-
size=3
44+
4\path=relative-path/libexec-settings/<env:BAR>
45+
size=4

Testing/LauncherLib/AppWithLauncherLib/launcher-user-additional-settings.ini

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
1\path=/path/to/libD
55
2\path=<APPLAUNCHER_DIR>/libE-<env:PET>
66
3\path=<APPLAUNCHER_SETTINGS_DIR>/libF
7-
size=3
7+
4\path=relative-path/libF
8+
size=4
89

910
[Paths]
1011
1\path=<APPLAUNCHER_DIR>/fawn
1112
2\path=/path/to/cat-and-<env:PET>
1213
3\path=/path/to/<env:PAINTER>
1314
4\path=<APPLAUNCHER_SETTINGS_DIR>/osprey
14-
size=4
15+
5\path=relative-path/osprey
16+
size=5
1517

1618
[EnvironmentVariables]
1719
BAR=RAB
@@ -22,4 +24,5 @@ PAINTER=Klimt
2224
2\path=<APPLAUNCHER_DIR>/lib/python/site-packages-3
2325
3\path=/path/to/site-packages-<env:BAR>
2426
4\path=<APPLAUNCHER_SETTINGS_DIR>/lib/python/site-packages-settings-2
25-
size=4
27+
5\path=relative-path/lib/python/site-packages-settings-2
28+
size=5

0 commit comments

Comments
 (0)