KiCad PCB EDA Suite
Loading...
Searching...
No Matches
jobs_runner.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <common.h>
22#include <cli/exit_codes.h>
23#include <jobs_runner.h>
24#include <jobs/job_registry.h>
25#include <jobs/jobset.h>
28#include <kiway.h>
29#include <kiway_mail.h>
30#include <reporter.h>
31#include <optional>
32#include <wx/process.h>
33#include <wx/txtstrm.h>
34#include <wx/sstream.h>
35#include <wx/wfstream.h>
36#include <gestfich.h>
37
38JOBS_RUNNER::JOBS_RUNNER( KIWAY* aKiway, JOBSET* aJobsFile, PROJECT* aProject,
39 REPORTER& aReporter, JOBS_PROGRESS_REPORTER* aProgressReporter ) :
40 m_kiway( aKiway ),
41 m_jobsFile( aJobsFile ),
42 m_reporter( aReporter ),
43 m_progressReporter( aProgressReporter ),
44 m_project( aProject )
45{
46}
47
48
50{
51 bool success = true;
52
53 for( JOBSET_DESTINATION& destination : m_jobsFile->GetDestinations() )
54 success &= RunJobsForDestination( &destination, aBail );
55
56 return success;
57}
58
59
60int JOBS_RUNNER::runSpecialExecute( const JOBSET_JOB* aJob, REPORTER* aReporter, PROJECT* aProject )
61{
62 JOB_SPECIAL_EXECUTE* specialJob = static_cast<JOB_SPECIAL_EXECUTE*>( aJob->m_job.get() );
63 wxString cmd = ExpandEnvVarSubstitutions( specialJob->m_command, m_project );
64
65 aReporter->Report( cmd, RPT_SEVERITY_INFO );
66 aReporter->Report( wxEmptyString, RPT_SEVERITY_INFO );
67
68 wxProcess process;
69 process.Redirect();
70
72
73 wxInputStream* inputStream = process.GetInputStream();
74 wxInputStream* errorStream = process.GetErrorStream();
75
76 if( inputStream && errorStream )
77 {
78 wxTextInputStream inputTextStream( *inputStream );
79 wxTextInputStream errorTextStream( *errorStream );
80
81 while( !inputStream->Eof() )
82 aReporter->Report( inputTextStream.ReadLine(), RPT_SEVERITY_INFO );
83
84 while( !errorStream->Eof() )
85 aReporter->Report( errorTextStream.ReadLine(), RPT_SEVERITY_ERROR );
86
87 if( specialJob->m_recordOutput )
88 {
89 if( specialJob->GetConfiguredOutputPath().IsEmpty() )
90 {
91 wxFileName fn( aJob->m_id );
92 fn.SetExt( wxT( "log" ) );
93 specialJob->SetConfiguredOutputPath( fn.GetFullPath() );
94 }
95
96 wxFFileOutputStream procOutput( specialJob->GetFullOutputPath( aProject ) );
97
98 if( !procOutput.IsOk() )
100
101 inputStream->Reset();
102 *inputStream >> procOutput;
103 }
104 }
105
106 if( specialJob->m_ignoreExitcode )
107 return CLI::EXIT_CODES::OK;
108
109 return result;
110}
111
112
114 std::vector<wxString>& aPathsWritten )
115{
116 wxString source = ExpandEnvVarSubstitutions( aJob->m_source, aProject );
117
118 if( source.IsEmpty() )
120
121 wxString projectPath = aProject->GetProjectPath();
122 wxFileName sourceFn( source );
123 sourceFn.MakeAbsolute( projectPath );
124
125 wxFileName destFn( aJob->GetFullOutputPath( aProject ) );
126
127 if( !aJob->m_dest.IsEmpty() )
128 destFn.AppendDir( ExpandEnvVarSubstitutions( aJob->m_dest, aProject ) );
129
130 wxString errors;
131 bool success = CopyFilesOrDirectory( sourceFn.GetFullPath(), destFn.GetFullPath(), aJob->m_overwriteDest,
132 errors, aPathsWritten );
133
134 if( !success )
136
137 if( aJob->m_generateErrorOnNoCopy && aPathsWritten.empty() )
139
140 return CLI::EXIT_CODES::OK;
141}
142
143
145{
146 bool genOutputs = true;
147 bool success = true;
148 std::vector<JOBSET_JOB> jobsForDestination = m_jobsFile->GetJobsForDestination( aDestination );
149 wxString msg;
150
151 wxFileName tmp;
152 tmp.AssignDir( wxFileName::GetTempDir() );
153 tmp.AppendDir( KIID().AsString() );
154
155 aDestination->m_lastRunSuccessMap.clear();
156 aDestination->m_lastRunReporters.clear();
157 aDestination->m_lastResolvedOutputPath.reset();
158
159 wxString tempDirPath = tmp.GetFullPath();
160
161 if( !wxFileName::Mkdir( tempDirPath, wxS_DIR_DEFAULT ) )
162 {
163 msg = wxString::Format( wxT( "Failed to create temporary directory %s" ), tempDirPath );
164 m_reporter.Report( msg, RPT_SEVERITY_ERROR );
165
166 aDestination->m_lastRunSuccess = false;
167
168 return false;
169 }
170
171 bool continueOuput = aDestination->m_outputHandler->OutputPrecheck();
172
173 if( !continueOuput )
174 {
175 msg = wxString::Format( wxT( "Destination precheck failed for destination %s" ),
176 aDestination->m_id );
177 m_reporter.Report( msg, RPT_SEVERITY_ERROR );
178
179 aDestination->m_lastRunSuccess = false;
180 return false;
181 }
182
183 msg += wxT( "|--------------------------------\n" );
184 msg += wxT( "| " );
185 msg += wxString::Format( wxT( "Running jobs for destination %s" ), aDestination->m_id );
186 msg += wxT( "\n" );
187 msg += wxT( "|--------------------------------\n" );
188
189 msg += wxString::Format( wxT( "|%-5s | %-50s\n" ), wxT( "No." ), wxT( "Description" ) );
190
191 int jobNum = 1;
192
193 for( const JOBSET_JOB& job : jobsForDestination )
194 {
195 msg += wxString::Format( wxT( "|%-5d | %-50s\n" ), jobNum, job.GetDescription() );
196 jobNum++;
197 }
198
199 msg += wxT( "|--------------------------------\n" );
200 msg += wxT( "\n" );
201 msg += wxT( "\n" );
202
203 m_reporter.Report( msg, RPT_SEVERITY_INFO );
204
205 std::vector<wxString> pathsWithOverwriteDisallowed;
206 std::vector<JOB_OUTPUT> outputs;
207
208 jobNum = 1;
209 int failCount = 0;
210 int successCount = 0;
211
212 wxSetEnv( OUTPUT_TMP_PATH_VAR_NAME, tempDirPath );
213
214 for( const JOBSET_JOB& job : jobsForDestination )
215 {
216 msg = wxT( "|--------------------------------\n" );
217
218 msg += wxString::Format( wxT( "| Running job %d: %s" ), jobNum, job.GetDescription() );
219
220 msg += wxT( "\n" );
221 msg += wxT( "|--------------------------------\n" );
222
223 m_reporter.Report( msg, RPT_SEVERITY_INFO );
224
226 {
227 msg.Printf( _( "Running job %d: %s" ), jobNum, job.GetDescription() );
228 m_progressReporter->AdvanceJob( msg );
229 m_progressReporter->KeepRefreshing();
230 }
231
232 jobNum++;
233
234 KIWAY::FACE_T iface = JOB_REGISTRY::GetKifaceType( job.m_type );
235
236 job.m_job->SetTempOutputDirectory( tempDirPath );
237
238 REPORTER* targetReporter = &m_reporter;
239
240 if( targetReporter == &NULL_REPORTER::GetInstance() )
241 {
242 aDestination->m_lastRunReporters[job.m_id] =
243 std::make_shared<JOBSET_OUTPUT_REPORTER>( tempDirPath, m_progressReporter );
244
245 targetReporter = aDestination->m_lastRunReporters[job.m_id].get();
246 }
247
248 // Use a redirect reporter so we don't have error flags set after running previous jobs
249 REDIRECT_REPORTER isolatedReporter( targetReporter );
251
252 if( iface < KIWAY::KIWAY_FACE_COUNT )
253 {
254 result = m_kiway->ProcessJob( iface, job.m_job.get(), &isolatedReporter, m_progressReporter );
255 }
256 else
257 {
258 // special jobs
259 if( job.m_job->GetType() == "special_execute" )
260 {
261 result = runSpecialExecute( &job, &isolatedReporter, m_project );
262 }
263 else if( job.m_job->GetType() == "special_copyfiles" )
264 {
265 JOB_SPECIAL_COPYFILES* copyJob = static_cast<JOB_SPECIAL_COPYFILES*>( job.m_job.get() );
266 std::vector<wxString> pathsWritten;
267
268 result = runSpecialCopyFiles( copyJob, m_project, pathsWritten );
269
270 if( !copyJob->m_overwriteDest )
271 {
272 pathsWithOverwriteDisallowed.insert( pathsWithOverwriteDisallowed.end(), pathsWritten.begin(),
273 pathsWritten.end() );
274 }
275 }
276 }
277
278 aDestination->m_lastRunSuccessMap[job.m_id] = ( result == CLI::EXIT_CODES::SUCCESS );
279
281 {
282 wxString msg_fmt = wxT( "\033[32;1m%s\033[0m\n" );
283 msg = wxString::Format( msg_fmt, _( "Job successful" ) );
284
285 successCount++;
286 }
287 else
288 {
289 wxString msg_fmt = wxT( "\033[31;1m%s\033[0m\n" );
290 msg = wxString::Format( msg_fmt, _( "Job failed" ) );
291
292 failCount++;
293 }
294
295 msg += wxT( "\n\n" );
296 m_reporter.Report( msg, RPT_SEVERITY_INFO );
297
299 {
300 success = false;
301
302 if( aBail )
303 break;
304 }
305 else if( result != CLI::EXIT_CODES::SUCCESS )
306 {
307 genOutputs = false;
308 success = false;
309
310 if( aBail )
311 break;
312 }
313 }
314
315 wxUnsetEnv( OUTPUT_TMP_PATH_VAR_NAME );
316
317 if( genOutputs )
318 {
319 success &= aDestination->m_outputHandler->HandleOutputs( tempDirPath, m_project, pathsWithOverwriteDisallowed,
320 outputs, aDestination->m_lastResolvedOutputPath );
321 }
322
323 aDestination->m_lastRunSuccess = success;
324
325 msg = wxString::Format( wxT( "\n\n\033[33;1m%d %s, %d %s\033[0m\n" ),
326 successCount,
327 wxT( "jobs succeeded" ),
328 failCount,
329 wxT( "job failed" ) );
330
331 m_reporter.Report( msg, RPT_SEVERITY_INFO );
332
333 return success;
334}
virtual bool OutputPrecheck()
Checks if the output process can proceed before doing anything else This can include user prompts.
Definition jobs_output.h:45
virtual bool HandleOutputs(const wxString &aBaseTempPath, PROJECT *aProject, const std::vector< wxString > &aPathsWithOverwriteDisallowed, const std::vector< JOB_OUTPUT > &aOutputsToHandle, std::optional< wxString > &aResolvedOutputPath)=0
bool RunJobsAllDestinations(bool aBail=false)
REPORTER & m_reporter
Definition jobs_runner.h:78
KIWAY * m_kiway
Definition jobs_runner.h:76
JOBSET * m_jobsFile
Definition jobs_runner.h:77
JOBS_PROGRESS_REPORTER * m_progressReporter
Definition jobs_runner.h:79
bool RunJobsForDestination(JOBSET_DESTINATION *aDestination, bool aBail=false)
int runSpecialCopyFiles(const JOB_SPECIAL_COPYFILES *aJob, PROJECT *aProject, std::vector< wxString > &aPathsWritten)
PROJECT * m_project
Definition jobs_runner.h:80
int runSpecialExecute(const JOBSET_JOB *aJob, REPORTER *aReporter, PROJECT *aProject)
JOBS_RUNNER(KIWAY *aKiway, JOBSET *aJobsFile, PROJECT *aProject, REPORTER &aReporter, JOBS_PROGRESS_REPORTER *aProgressReporter)
static KIWAY::FACE_T GetKifaceType(const wxString &aName)
void SetConfiguredOutputPath(const wxString &aPath)
Sets the configured output path for the job, this path is always saved to file.
Definition job.cpp:157
wxString GetFullOutputPath(PROJECT *aProject) const
Returns the full output path for the job, taking into account the configured output path,...
Definition job.cpp:150
wxString GetConfiguredOutputPath() const
Returns the configured output path for the job.
Definition job.h:235
Definition kiid.h:44
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:311
FACE_T
Known KIFACE implementations.
Definition kiway.h:317
@ KIWAY_FACE_COUNT
Definition kiway.h:326
static REPORTER & GetInstance()
Definition reporter.cpp:120
Container for project specific data.
Definition project.h:62
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:183
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:100
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:721
The common library.
#define _(s)
int ExecuteCommandThroughShell(const wxString &aCommand, wxProcess *aProcess)
Run a user-supplied command line through the platform shell so glob expansion, pipes,...
Definition gestfich.cpp:271
bool CopyFilesOrDirectory(const wxString &aSourcePath, const wxString &aDestDir, bool aAllowOverwrites, wxString &aErrors, std::vector< wxString > &aPathsWritten)
Definition gestfich.cpp:533
#define OUTPUT_TMP_PATH_VAR_NAME
static const int ERR_ARGS
Definition exit_codes.h:31
static const int OK
Definition exit_codes.h:30
static const int ERR_RC_VIOLATIONS
Rules check violation count was greater than 0.
Definition exit_codes.h:37
static const int SUCCESS
Definition exit_codes.h:29
static const int ERR_INVALID_OUTPUT_CONFLICT
Definition exit_codes.h:34
static const int ERR_UNKNOWN
Definition exit_codes.h:32
static PGM_BASE * process
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
std::unordered_map< wxString, std::shared_ptr< JOBSET_OUTPUT_REPORTER > > m_lastRunReporters
Definition jobset.h:142
std::shared_ptr< JOBS_OUTPUT_HANDLER > m_outputHandler
Definition jobset.h:136
std::optional< wxString > m_lastResolvedOutputPath
Definition jobset.h:143
std::optional< bool > m_lastRunSuccess
Definition jobset.h:140
std::unordered_map< wxString, std::optional< bool > > m_lastRunSuccessMap
Definition jobset.h:141
wxString m_id
Definition jobset.h:133
wxString m_id
Definition jobset.h:87
std::shared_ptr< JOB > m_job
Definition jobset.h:90
wxString result
Test unit parsing edge cases and error handling.