Files
ResInsight/ApplicationLibCode/Application/Tools/RiaWslTools.cpp
T
JJ 727ba200ba Opm Flow Job DATES updates (#12952)
* Split dates keywords with multiple records into single record keywords
Add support for setting environment when running opm flow

* Date handling improvements

* Clean up date and position handling a bit

* Add support for setting a stop date for the simulation

* Use Opm::ParserKeywords keywordName for all keywords

* Re-enable msw support (mark as experimental)

* Bump version to dev-03
2025-09-29 16:57:21 +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 );
}
//--------------------------------------------------------------------------------------------------
/// Returns the input windows path as seen in the wsl world. I.e. c:\tmp -> /mnt/c/tmp
//--------------------------------------------------------------------------------------------------
std::map<QString, QString> RiaWslTools::wslEnvironmentVariables()
{
std::map<QString, QString> env;
env["WSL_UTF8"] = "1";
return env;
}