Skip to content

Commit

Permalink
ENH: Add support for launcher symbolic link added to the PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed May 13, 2013
1 parent b03c2ec commit 162fa8a
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Base/ctkAppLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,17 @@ QString ctkAppLauncherInternal::searchPaths(const QString& executableName, const
}
QDir pathAsDir(expandedPath);
QString executablePath = pathAsDir.filePath(executableNameWithExt);
this->reportInfo(QString("Checking if [%1] exists in [%2]").arg(executableNameWithExt).arg(expandedPath));
QString msg = QString("Checking if [%1] exists in [%2]").arg(executableNameWithExt).arg(expandedPath);
this->reportInfo(msg);
if (QFile::exists(executablePath))
{
this->reportInfo(msg + " - Found");
return executablePath;
}
else
{
this->reportInfo(msg + " - Not found");
}
}
}
return QString();
Expand All @@ -384,6 +390,28 @@ bool ctkAppLauncherInternal::extractLauncherNameAndDir(const QString& applicatio
{
QFileInfo fileInfo(applicationFilePath);

// In case a symlink to the launcher is available from the PATH, resolve its location.
if (!fileInfo.exists())
{
QStringList paths = this->SystemEnvironment.value("PATH").split(this->PathSep);
foreach(const QString& path, paths)
{
QString executablePath = QDir(path).filePath(fileInfo.fileName());
QString msg = QString("Checking if [%1] exists in [%2]").arg(fileInfo.fileName()).arg(path);
this->reportInfo(msg);
if (QFile::exists(executablePath))
{
this->reportInfo(msg + " - Found");
fileInfo = QFileInfo(executablePath);
break;
}
else
{
this->reportInfo(msg + " - Not found");
}
}
}

// Follow symlink if it applies
if (fileInfo.isSymLink())
{
Expand Down
85 changes: 85 additions & 0 deletions Testing/CMake/AppLauncher-TestSymbolicLink.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
include(${TEST_BINARY_DIR}/AppLauncherTestPrerequisites.cmake)

# --------------------------------------------------------------------------
# Debug flags - Set to True to display the command as string
set(PRINT_COMMAND 0)

# --------------------------------------------------------------------------
set(symlink_dir "${launcher_binary_dir}/TestSymbolicLink")
set(symlink_name "SymLinkToLauncher")

# --------------------------------------------------------------------------
# Configure settings file
file(WRITE "${launcher}LauncherSettings.ini" "
[LibraryPaths]
1\\path=${library_path}
size=1
")

# --------------------------------------------------------------------------
# Delete test directory if it exists
execute_process(
COMMAND ${CMAKE_COMMAND} -E remove_directory ${symlink_dir}
)

# Re-create test directory
file(MAKE_DIRECTORY ${symlink_dir})

# .. and add a symlink
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
${launcher_exe} ${symlink_dir}/${symlink_name})

# --------------------------------------------------------------------------
# Attempt to start launcher from the symlink directory
set(cwd ${symlink_dir})
set(command ${symlink_dir}/${symlink_name} --launcher-no-splash --launch ${application} --print-current-working-directory)
execute_process(
COMMAND ${command}
WORKING_DIRECTORY ${cwd}
ERROR_VARIABLE ev
ERROR_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE ov
RESULT_VARIABLE rv
)

print_command_as_string("${command}")

if(rv)
message(FATAL_ERROR "[${symlink_dir}/${symlink_name}] failed to start application [${application}] from "
"directory [${cwd}]\n${ev}")
endif()

set(expected_msg "currentWorkingDirectory=${cwd}")
string(REGEX MATCH ${expected_msg} current_msg ${ov})
if(NOT "${expected_msg}" STREQUAL "${current_msg}")
message(FATAL_ERROR "Working directory associated with ${application_name} is incorrect ! "
"ExpectedWorkingDirectory:${cwd}\n${ov}")
endif()

# --------------------------------------------------------------------------
# Attempt to start launcher from a different directory
set(cwd ${launcher_binary_dir})
set(command ${symlink_dir}/${symlink_name} --launcher-no-splash --launch ${application} --print-current-working-directory)
execute_process(
COMMAND ${command}
WORKING_DIRECTORY ${cwd}
ERROR_VARIABLE ev
ERROR_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE ov
RESULT_VARIABLE rv
)

print_command_as_string("${command}")

if(rv)
message(FATAL_ERROR "[${symlink_dir}/${symlink_name}] failed to start application [${application}] from "
"directory [${cwd}]\n${ev}")
endif()

set(expected_msg "currentWorkingDirectory=${cwd}")
string(REGEX MATCH ${expected_msg} current_msg ${ov})
if(NOT "${expected_msg}" STREQUAL "${current_msg}")
message(FATAL_ERROR "Working directory associated with ${application_name} is incorrect ! "
"ExpectedWorkingDirectory:${cwd}\n${ov}")
endif()

64 changes: 64 additions & 0 deletions Testing/CMake/AppLauncher-TestSymbolicLinkInPATH.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
include(${TEST_BINARY_DIR}/AppLauncherTestPrerequisites.cmake)

# --------------------------------------------------------------------------
# Debug flags - Set to True to display the command as string
set(PRINT_COMMAND 0)

# --------------------------------------------------------------------------
set(symlink_dir "${launcher_binary_dir}/TestSymbolicLinkInPATH")
set(symlink_name "SymLinkToLauncher")

# --------------------------------------------------------------------------
# Configure settings file
file(WRITE "${launcher}LauncherSettings.ini" "
[LibraryPaths]
1\\path=${library_path}
size=1
")

# Set path
set(old_path "$ENV{PATH}")
set(ENV{PATH} "${symlink_dir}:$ENV{PATH}")

# --------------------------------------------------------------------------
# Delete test directory if it exists
execute_process(
COMMAND ${CMAKE_COMMAND} -E remove_directory ${symlink_dir}
)

# Re-create test directory
file(MAKE_DIRECTORY ${symlink_dir})

# .. and add a symlink
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
${launcher_exe} ${symlink_dir}/${symlink_name})

# --------------------------------------------------------------------------
# Attempt to start launcher using symlink added to the PATH
set(cwd ${launcher_binary_dir})
set(command ${symlink_name} --launcher-no-splash --launch ${application} --print-current-working-directory)
execute_process(
COMMAND ${command}
WORKING_DIRECTORY ${cwd}
ERROR_VARIABLE ev
ERROR_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE ov
RESULT_VARIABLE rv
)

# Restore path
set(ENV{PATH} "${old_path}")

print_command_as_string("${command}")

if(rv)
message(FATAL_ERROR "[${symlink_name}] failed to start application [${application}] from "
"directory [${cwd}]\n${ev}")
endif()

set(expected_msg "currentWorkingDirectory=${cwd}")
string(REGEX MATCH ${expected_msg} current_msg ${ov})
if(NOT "${expected_msg}" STREQUAL "${current_msg}")
message(FATAL_ERROR "Working directory associated with ${application_name} is incorrect ! "
"ExpectedWorkingDirectory:${cwd}\n${ov}")
endif()
4 changes: 4 additions & 0 deletions Testing/CMake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ applauncher_add_test(AppLauncher-TestCurrentDirectory)
applauncher_add_test(AppLauncher-TestLauncherAdditionalSettings)
applauncher_add_test(AppLauncher-TestLauncherAdditionalSettings-CheckForSettingsOverwrite)
applauncher_add_test(AppLauncher-TestNoLaunchSettingsNoLaunchParams)
if(UNIX)
applauncher_add_test(AppLauncher-TestSymbolicLink)
applauncher_add_test(AppLauncher-TestSymbolicLinkInPATH)
endif()

0 comments on commit 162fa8a

Please sign in to comment.