29#include <wx/filename.h>
30#include <wx/tokenzr.h>
49 fprintf( stderr,
"%s\n", aMessage.utf8_string().c_str() );
53static bool shouldKeepTemp(
const std::filesystem::path& aPath,
const wxString& aKeepEnvValue )
55 const wxString envVar = wxT(
"KICAD_QA_KEEP_TEMP" );
58 if( !wxGetEnv( envVar, &keepEnv ) || keepEnv.IsEmpty() )
62 wxStringTokenizer tokenizer( keepEnv, wxT(
"," ) );
64 while( tokenizer.HasMoreTokens() )
66 wxString token = tokenizer.GetNextToken();
68 if( token == aKeepEnvValue || token == wxT(
"ALL" ) )
72 reportTempDir( wxString::Format( wxT(
"Keeping temporary directory '%s' because %s is set to '%s'" ),
73 wxString::FromUTF8( aPath.string() ), envVar, keepEnv ) );
84 wxString tempFile = wxFileName::CreateTempFileName( aPrefix +
"_" );
86 if( tempFile.IsEmpty() )
87 throw std::runtime_error(
"Cannot create a temporary directory name with prefix '"
88 + std::string( aPrefix.utf8_str() ) +
"'" );
90 if( !wxRemoveFile( tempFile ) )
91 throw std::runtime_error(
"Cannot reclaim temporary name '" + std::string( tempFile.utf8_str() ) +
"'" );
93 if( !wxMkdir( tempFile ) )
94 throw std::runtime_error(
"Cannot create temporary directory '" + std::string( tempFile.utf8_str() ) +
"'" );
96 return std::filesystem::path( std::string( tempFile.utf8_str() ) );
119 std::filesystem::remove_all(
m_path );
121 catch(
const std::filesystem::filesystem_error& e )
123 reportTempDir( wxString::Format( wxT(
"Cannot remove temporary directory '%s': %s" ),
124 wxString::FromUTF8(
m_path.string() ), wxString::FromUTF8( e.what() ) ) );
135 wxString::Format( wxT(
"Keeping temporary directory '%s'" ), wxString::FromUTF8(
m_path.string() ) ) );
141 return wxString::FromUTF8( (
m_path / aName.utf8_string() ).string() );
147 std::filesystem::path childPath =
m_path / aName.utf8_string();
149 if( !std::filesystem::create_directory( childPath ) )
150 throw std::runtime_error(
"Cannot create temporary child directory '" + childPath.string() +
"'" );
164 std::filesystem::path childPath =
m_path / aName.utf8_string();
165 wxFFile file( wxString::FromUTF8( childPath.string() ), wxT(
"w" ) );
167 if( !file.IsOpened() )
168 throw std::runtime_error(
"Cannot create temporary child file '" + childPath.string() +
"'" );
181 const wxString& aName ) :
188 m_manager.LoadProject( projectFile.GetFullPath() );
193 throw std::runtime_error(
"Cannot load temporary project '"
194 + std::string( projectFile.GetFullPath().utf8_str() ) +
"'" );
208 const wxString envValue =
m_dir.PathStr();
210 wxSetEnv( wxT(
"TMPDIR" ), envValue );
211 wxSetEnv( wxT(
"TEMP" ), envValue );
212 wxSetEnv( wxT(
"TMP" ), envValue );
SCOPED_PROCESS_TEMP_DIR(const wxString &aPrefix)
~SCOPED_PROCESS_TEMP_DIR()
std::filesystem::path m_path
static bool s_anyTempDirRetained
wxString CreateChildFileStr(const wxString &aName) const
Create and return the path to a direct child file as a wxString.
void Retain()
Keep this directory after all.
static bool AnyTempDirRetained()
wxString ChildPathStr(const wxString &aName) const
Get the path to a direct child of the temporary directory as a wxString, without creating the child.
wxString CreateChildDirStr(const wxString &aName) const
Create and return the path to a direct child directory as a wxString.
SCOPED_TEMP_DIR(const wxString &aPrefix)
Create a temporary directory with a unique name based on the given prefix.
std::filesystem::path CreateChildFile(const wxString &aName) const
Create and return the path to a direct child file.
std::filesystem::path CreateChildDir(const wxString &aName) const
Create and return the path to a direct child directory.
SETTINGS_MANAGER & m_manager
SCOPED_TEMP_PROJECT(SETTINGS_MANAGER &aManager, const wxString &aPrefix, const wxString &aName)
static std::filesystem::path createTempDir(const wxString &aPrefix)
static bool shouldKeepTemp(const std::filesystem::path &aPath, const wxString &aKeepEnvValue)
static void reportTempDir(const wxString &aMessage)
Report a message about a temporary directory.
static const std::string ProjectFileExtension
Definition of file extensions used in Kicad.