Files
ResInsight/ApplicationLibCode/Application/Tools/RiaWslTools.cpp
T
JJ 6c39729e77 Support submitting opm flow jobs to Slurm or LSF (#14943)
Add preferences for HPC/Batch scheduler
Add Slurm and LSF support
Add batch job monitor
Support launcher specific user settings
Use internal timer to refresh job state
Add some more options to batch monitor
Improve naming
Add tests for job lists
Change WSL handling for processes.
2026-10-21 18:14:13 +02:00

90 lines
3.4 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2025 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaWslTools.h"
#include "RimProcess.h"
#include <QFile>
//--------------------------------------------------------------------------------------------------
/// Returns path to the wsl command on Windows if found, nothing if not on Windows or not found
//--------------------------------------------------------------------------------------------------
QString RiaWslTools::wslCommand()
{
#ifdef WIN32
QString wslCmd = qEnvironmentVariable( "SystemRoot", "C:\\WINDOWS" ) + "\\System32\\wsl.exe";
if ( QFile::exists( wslCmd ) ) return wslCmd;
#endif
return "";
}
//--------------------------------------------------------------------------------------------------
/// Returns the list of installed wsl distributions on this computer, or empty list if no wsl
//--------------------------------------------------------------------------------------------------
QStringList RiaWslTools::wslDistributionList()
{
QString wslCmd = wslCommand();
if ( !wslCmd.isEmpty() )
{
RimProcess wslProc( false /*no logging*/ );
for ( auto& [key, val] : wslEnvironmentVariables() )
{
wslProc.addEnvironmentVariable( key, val );
}
wslProc.setCommand( wslCmd );
wslProc.addParameter( "--list" ); // list distribution names
wslProc.addParameter( "--quiet" ); // quiet, only show name
if ( wslProc.execute() )
{
return wslProc.stdOut();
}
}
return QStringList();
}
//--------------------------------------------------------------------------------------------------
/// Returns the input windows path as seen in the wsl world. I.e. c:\tmp -> /mnt/c/tmp
//--------------------------------------------------------------------------------------------------
QString RiaWslTools::convertToWslPath( QString windowsPath )
{
if ( windowsPath.size() < 2 ) return windowsPath;
if ( windowsPath.startsWith( '/' ) ) return windowsPath;
if ( windowsPath.at( 1 ) != ':' ) return windowsPath;
QString drive = windowsPath.at( 0 );
drive = drive.toLower();
auto path = windowsPath.replace( '\\', '/' );
path = path.section( ':', -1 );
return QString( "/mnt/%1%2" ).arg( drive ).arg( path );
}
//--------------------------------------------------------------------------------------------------
/// Return environment variables needed for WSL character mapping
//--------------------------------------------------------------------------------------------------
std::map<QString, QString> RiaWslTools::wslEnvironmentVariables()
{
std::map<QString, QString> env;
env["WSL_UTF8"] = "1";
return env;
}