Update PCM log messages

Fixes https://gitlab.com/kicad/code/kicad/-/issues/9753
This commit is contained in:
Josue Huaroto 2023-07-13 00:14:46 +00:00 committed by Seth Hillbrand
parent 5aff8b6a6e
commit 3c12217304
2 changed files with 86 additions and 41 deletions

View File

@ -54,10 +54,10 @@ void compile_keep_on_update_regex( const PCM_PACKAGE& pkg, const PACKAGE_VERSION
} }
void PCM_TASK_MANAGER::DownloadAndInstall( const PCM_PACKAGE& aPackage, const wxString& aVersion, PCM_TASK_MANAGER::STATUS PCM_TASK_MANAGER::DownloadAndInstall( const PCM_PACKAGE& aPackage, const wxString& aVersion,
const wxString& aRepositoryId, const bool isUpdate ) const wxString& aRepositoryId, const bool isUpdate )
{ {
PCM_TASK download_task = [aPackage, aVersion, aRepositoryId, isUpdate, this]() PCM_TASK download_task = [aPackage, aVersion, aRepositoryId, isUpdate, this]() -> PCM_TASK_MANAGER::STATUS
{ {
wxFileName file_path( PATHS::GetUserCachePath(), "" ); wxFileName file_path( PATHS::GetUserCachePath(), "" );
file_path.AppendDir( "pcm" ); file_path.AppendDir( "pcm" );
@ -74,7 +74,7 @@ void PCM_TASK_MANAGER::DownloadAndInstall( const PCM_PACKAGE& aPackage, const wx
m_reporter->PCMReport( wxString::Format( _( "Version %s of package %s not found!" ), m_reporter->PCMReport( wxString::Format( _( "Version %s of package %s not found!" ),
aVersion, aPackage.identifier ), aVersion, aPackage.identifier ),
RPT_SEVERITY_ERROR ); RPT_SEVERITY_ERROR );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
if( !wxDirExists( file_path.GetPath() ) if( !wxDirExists( file_path.GetPath() )
@ -82,7 +82,7 @@ void PCM_TASK_MANAGER::DownloadAndInstall( const PCM_PACKAGE& aPackage, const wx
{ {
m_reporter->PCMReport( _( "Unable to create download directory!" ), m_reporter->PCMReport( _( "Unable to create download directory!" ),
RPT_SEVERITY_ERROR ); RPT_SEVERITY_ERROR );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
int code = downloadFile( file_path.GetFullPath(), *find_pkgver->download_url ); int code = downloadFile( file_path.GetFullPath(), *find_pkgver->download_url );
@ -91,18 +91,21 @@ void PCM_TASK_MANAGER::DownloadAndInstall( const PCM_PACKAGE& aPackage, const wx
{ {
// Cleanup after ourselves and exit // Cleanup after ourselves and exit
wxRemoveFile( file_path.GetFullPath() ); wxRemoveFile( file_path.GetFullPath() );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
PCM_TASK install_task = [aPackage, aVersion, aRepositoryId, file_path, isUpdate, this]() PCM_TASK install_task = [aPackage, aVersion, aRepositoryId, file_path, isUpdate, this]()
{ {
installDownloadedPackage( aPackage, aVersion, aRepositoryId, file_path, isUpdate ); return installDownloadedPackage( aPackage, aVersion, aRepositoryId, file_path, isUpdate );
}; };
m_install_queue.push( install_task ); m_install_queue.push( install_task );
return PCM_TASK_MANAGER::STATUS::SUCCESS;
}; };
m_download_queue.push( download_task ); m_download_queue.push( download_task );
return PCM_TASK_MANAGER::STATUS::SUCCESS;
} }
@ -149,7 +152,7 @@ int PCM_TASK_MANAGER::downloadFile( const wxString& aFilePath, const wxString& u
} }
void PCM_TASK_MANAGER::installDownloadedPackage( const PCM_PACKAGE& aPackage, PCM_TASK_MANAGER::STATUS PCM_TASK_MANAGER::installDownloadedPackage( const PCM_PACKAGE& aPackage,
const wxString& aVersion, const wxString& aVersion,
const wxString& aRepositoryId, const wxString& aRepositoryId,
const wxFileName& aFilePath, const bool isUpdate ) const wxFileName& aFilePath, const bool isUpdate )
@ -165,7 +168,7 @@ void PCM_TASK_MANAGER::installDownloadedPackage( const PCM_PACKAGE& aPackage,
m_reporter->PCMReport( wxString::Format( _( "Version %s of package %s not found!" ), m_reporter->PCMReport( wxString::Format( _( "Version %s of package %s not found!" ),
aVersion, aPackage.identifier ), aVersion, aPackage.identifier ),
RPT_SEVERITY_ERROR ); RPT_SEVERITY_ERROR );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
// wxRegEx is not CopyConstructible hence the weird choice of forward_list // wxRegEx is not CopyConstructible hence the weird choice of forward_list
@ -190,8 +193,10 @@ void PCM_TASK_MANAGER::installDownloadedPackage( const PCM_PACKAGE& aPackage,
"This may indicate a problem with the " "This may indicate a problem with the "
"package, if the issue persists " "package, if the issue persists "
"report this to repository maintainers." ), "report this to repository maintainers." ),
aPackage.identifier ), aPackage.name ),
RPT_SEVERITY_ERROR ); RPT_SEVERITY_ERROR );
wxRemoveFile( aFilePath.GetFullPath() );
return PCM_TASK_MANAGER::STATUS::FAILED;
} }
else else
{ {
@ -199,14 +204,14 @@ void PCM_TASK_MANAGER::installDownloadedPackage( const PCM_PACKAGE& aPackage,
{ {
m_reporter->PCMReport( m_reporter->PCMReport(
wxString::Format( _( "Removing previous version of package '%s'." ), wxString::Format( _( "Removing previous version of package '%s'." ),
aPackage.identifier ), aPackage.name ),
RPT_SEVERITY_INFO ); RPT_SEVERITY_INFO );
deletePackageDirectories( aPackage.identifier, keep_on_update ); deletePackageDirectories( aPackage.identifier, keep_on_update );
} }
m_reporter->PCMReport( m_reporter->PCMReport(
wxString::Format( _( "Extracting package '%s'." ), aPackage.identifier ), wxString::Format( _( "Installing package '%s'." ), aPackage.name ),
RPT_SEVERITY_INFO ); RPT_SEVERITY_INFO );
if( extract( aFilePath.GetFullPath(), aPackage.identifier, true ) ) if( extract( aFilePath.GetFullPath(), aPackage.identifier, true ) )
@ -223,11 +228,8 @@ void PCM_TASK_MANAGER::installDownloadedPackage( const PCM_PACKAGE& aPackage,
m_changed_package_types.insert( aPackage.type ); m_changed_package_types.insert( aPackage.type );
} }
m_reporter->PCMReport(
wxString::Format( _( "Removing downloaded archive '%s'." ), aFilePath.GetFullName() ),
RPT_SEVERITY_INFO );
wxRemoveFile( aFilePath.GetFullPath() ); wxRemoveFile( aFilePath.GetFullPath() );
return PCM_TASK_MANAGER::STATUS::SUCCESS;
} }
@ -267,8 +269,6 @@ bool PCM_TASK_MANAGER::extract( const wxString& aFilePath, const wxString& aPack
continue; continue;
} }
// m_reporter->Report( wxString::Format( _( "Extracting file '%s'\n" ), entry->GetName() ),
// RPT_SEVERITY_INFO );
// Transform paths from // Transform paths from
// <PackageRoot>/$folder/$contents // <PackageRoot>/$folder/$contents
@ -313,21 +313,21 @@ bool PCM_TASK_MANAGER::extract( const wxString& aFilePath, const wxString& aPack
return false; return false;
} }
m_reporter->PCMReport( _( "Extracted package\n" ), RPT_SEVERITY_INFO );
m_reporter->SetPackageProgress( entries, entries ); m_reporter->SetPackageProgress( entries, entries );
return true; return true;
} }
void PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent, const wxString& aFilePath ) PCM_TASK_MANAGER::STATUS PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent,
const wxString& aFilePath )
{ {
wxFFileInputStream stream( aFilePath ); wxFFileInputStream stream( aFilePath );
if( !stream.IsOk() ) if( !stream.IsOk() )
{ {
wxLogError( _( "Could not open archive file." ) ); wxLogError( _( "Could not open archive file." ) );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
wxZipInputStream zip( stream ); wxZipInputStream zip( stream );
@ -335,7 +335,7 @@ void PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent, const wxString& aFile
if( !zip.IsOk() ) if( !zip.IsOk() )
{ {
wxLogError( _( "Invalid archive file format." ) ); wxLogError( _( "Invalid archive file format." ) );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
nlohmann::json metadata; nlohmann::json metadata;
@ -367,7 +367,7 @@ void PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent, const wxString& aFile
if( metadata.empty() ) if( metadata.empty() )
{ {
wxLogError( _( "Archive does not contain a valid metadata.json file" ) ); wxLogError( _( "Archive does not contain a valid metadata.json file" ) );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
PCM_PACKAGE package = metadata.get<PCM_PACKAGE>(); PCM_PACKAGE package = metadata.get<PCM_PACKAGE>();
@ -375,7 +375,7 @@ void PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent, const wxString& aFile
if( package.versions.size() != 1 ) if( package.versions.size() != 1 )
{ {
wxLogError( _( "Archive metadata must have a single version defined" ) ); wxLogError( _( "Archive metadata must have a single version defined" ) );
return; return PCM_TASK_MANAGER::STATUS::FAILED;
} }
bool isUpdate = false; bool isUpdate = false;
@ -397,7 +397,7 @@ void PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent, const wxString& aFile
package.identifier ), package.identifier ),
_( "Update package" ), wxICON_EXCLAMATION | wxYES_NO, aParent ) _( "Update package" ), wxICON_EXCLAMATION | wxYES_NO, aParent )
== wxNO ) == wxNO )
return; return PCM_TASK_MANAGER::STATUS::INITIALIZED;
isUpdate = true; isUpdate = true;
@ -414,7 +414,7 @@ void PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent, const wxString& aFile
if( isUpdate ) if( isUpdate )
{ {
m_reporter->PCMReport( wxString::Format( _( "Removing previous version of package '%s'." ), m_reporter->PCMReport( wxString::Format( _( "Removing previous version of package '%s'." ),
package.identifier ), package.name ),
RPT_SEVERITY_INFO ); RPT_SEVERITY_INFO );
deletePackageDirectories( package.identifier, keep_on_update ); deletePackageDirectories( package.identifier, keep_on_update );
@ -432,6 +432,7 @@ void PCM_TASK_MANAGER::InstallFromFile( wxWindow* aParent, const wxString& aFile
std::unique_lock lock( m_changed_package_types_guard ); std::unique_lock lock( m_changed_package_types_guard );
m_changed_package_types.insert( package.type ); m_changed_package_types.insert( package.type );
return PCM_TASK_MANAGER::STATUS::SUCCESS;
} }
@ -529,9 +530,6 @@ void PCM_TASK_MANAGER::deletePackageDirectories( const wxString&
if( re.Matches( tmp ) ) if( re.Matches( tmp ) )
{ {
// m_reporter->PCMReport( wxString::Format( _( "Keeping file '%s'." ), tmp ),
// RPT_SEVERITY_INFO );
del = false; del = false;
break; break;
} }
@ -552,7 +550,7 @@ void PCM_TASK_MANAGER::deletePackageDirectories( const wxString&
} }
void PCM_TASK_MANAGER::Uninstall( const PCM_PACKAGE& aPackage ) PCM_TASK_MANAGER::STATUS PCM_TASK_MANAGER::Uninstall( const PCM_PACKAGE& aPackage )
{ {
PCM_TASK task = [aPackage, this] PCM_TASK task = [aPackage, this]
{ {
@ -564,11 +562,13 @@ void PCM_TASK_MANAGER::Uninstall( const PCM_PACKAGE& aPackage )
m_changed_package_types.insert( aPackage.type ); m_changed_package_types.insert( aPackage.type );
m_reporter->PCMReport( m_reporter->PCMReport(
wxString::Format( _( "Package %s uninstalled" ), aPackage.identifier ), wxString::Format( _( "Package %s uninstalled" ), aPackage.name ),
RPT_SEVERITY_INFO ); RPT_SEVERITY_INFO );
return PCM_TASK_MANAGER::STATUS::SUCCESS;
}; };
m_install_queue.push( task ); m_install_queue.push( task );
return PCM_TASK_MANAGER::STATUS::SUCCESS;
} }
@ -588,6 +588,9 @@ void PCM_TASK_MANAGER::RunQueue( wxWindow* aParent )
std::mutex mutex; std::mutex mutex;
std::condition_variable condvar; std::condition_variable condvar;
bool download_complete = false; bool download_complete = false;
int count_tasks = 0;
int count_failed_tasks = 0;
int count_success_tasks = 0;
std::thread download_thread( std::thread download_thread(
[&]() [&]()
@ -625,7 +628,15 @@ void PCM_TASK_MANAGER::RunQueue( wxWindow* aParent )
{ {
PCM_TASK task; PCM_TASK task;
m_install_queue.pop( task ); m_install_queue.pop( task );
task(); PCM_TASK_MANAGER::STATUS task_status = task();
count_tasks++;
if( task_status == PCM_TASK_MANAGER::STATUS::SUCCESS )
count_success_tasks++;
else if( task_status != PCM_TASK_MANAGER::STATUS::INITIALIZED )
count_failed_tasks++;
m_reporter->AdvancePhase(); m_reporter->AdvancePhase();
} }
@ -634,7 +645,26 @@ void PCM_TASK_MANAGER::RunQueue( wxWindow* aParent )
} while( ( !m_install_queue.empty() || !download_complete ) } while( ( !m_install_queue.empty() || !download_complete )
&& !m_reporter->IsCancelled() ); && !m_reporter->IsCancelled() );
m_reporter->PCMReport( _( "Done." ), RPT_SEVERITY_INFO ); if( count_failed_tasks != 0 )
{
m_reporter->PCMReport(
wxString::Format( _( "%d out of %d operations failed." ), count_failed_tasks, count_tasks ),
RPT_SEVERITY_INFO );
}
else
{
if( count_success_tasks == count_tasks )
{
m_reporter->PCMReport( _( "All operations completed successfully." ), RPT_SEVERITY_INFO );
}
else
{
m_reporter->PCMReport(
wxString::Format( _( "%d out of %d operations were initialized but not successful." ),
count_tasks - count_success_tasks, count_tasks ),
RPT_SEVERITY_INFO );
}
}
m_reporter->SetFinished(); m_reporter->SetFinished();
} ); } );

View File

@ -36,9 +36,6 @@
#include <wx/string.h> #include <wx/string.h>
typedef std::function<void()> PCM_TASK;
/** /**
* @brief Helper class that handles package (un)installation * @brief Helper class that handles package (un)installation
* *
@ -50,6 +47,15 @@ typedef std::function<void()> PCM_TASK;
class PCM_TASK_MANAGER class PCM_TASK_MANAGER
{ {
public: public:
enum class STATUS
{
FAILED = -1,
INITIALIZED = 0,
SUCCESS = 1
};
typedef std::function<STATUS()> PCM_TASK;
PCM_TASK_MANAGER( std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) : m_pcm( pcm ){}; PCM_TASK_MANAGER( std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) : m_pcm( pcm ){};
/** /**
@ -66,8 +72,10 @@ public:
* @param aPackage package metadata * @param aPackage package metadata
* @param aVersion version to be installed * @param aVersion version to be installed
* @param aRepositoryId id of the source repository * @param aRepositoryId id of the source repository
*
* @return int status of the process
*/ */
void DownloadAndInstall( const PCM_PACKAGE& aPackage, const wxString& aVersion, PCM_TASK_MANAGER::STATUS DownloadAndInstall( const PCM_PACKAGE& aPackage, const wxString& aVersion,
const wxString& aRepositoryId, const bool isUpdate ); const wxString& aRepositoryId, const bool isUpdate );
/** /**
@ -77,8 +85,10 @@ public:
* as uninstalled. * as uninstalled.
* *
* @param aPackage package metadata * @param aPackage package metadata
*
* @return int status of the process
*/ */
void Uninstall( const PCM_PACKAGE& aPackage ); PCM_TASK_MANAGER::STATUS Uninstall( const PCM_PACKAGE& aPackage );
/** /**
* @brief Run queue of pending actions * @brief Run queue of pending actions
@ -101,8 +111,10 @@ public:
* *
* @param aParent parent dialog for progress window * @param aParent parent dialog for progress window
* @param aFilePath path to the archive file * @param aFilePath path to the archive file
*
* @return int status of the process
*/ */
void InstallFromFile( wxWindow* aParent, const wxString& aFilePath ); PCM_TASK_MANAGER::STATUS InstallFromFile( wxWindow* aParent, const wxString& aFilePath );
/** /**
* @return types of packages that were installed/uninstalled by the task manager * @return types of packages that were installed/uninstalled by the task manager
@ -130,10 +142,13 @@ private:
* @param aRepositoryId id of the source repository * @param aRepositoryId id of the source repository
* @param aFilePath path to the archive * @param aFilePath path to the archive
* @param isUpdate true if this is an update operation * @param isUpdate true if this is an update operation
* @return int status of the operation
*/ */
void installDownloadedPackage( const PCM_PACKAGE& aPackage, const wxString& aVersion, PCM_TASK_MANAGER::STATUS installDownloadedPackage( const PCM_PACKAGE& aPackage,
const wxString& aRepositoryId, const wxFileName& aFilePath, const wxString& aVersion,
const bool isUpdate ); const wxString& aRepositoryId,
const wxFileName& aFilePath,
const bool isUpdate );
/** /**
* @brief Extract package archive * @brief Extract package archive