[libcalamares] Extend command-list with timeouts

- Replace plain StringList with a list of <String, timeout> pairs,
   and run that instead. All code paths still use the default 10sec
   timeout and there's no way to change that.
This commit is contained in:
Adriaan de Groot 2018-01-29 17:01:28 +01:00
parent ea179eaef4
commit fe2be46d3f
2 changed files with 68 additions and 20 deletions

View File

@ -26,14 +26,17 @@
#include <QVariantList> #include <QVariantList>
static QStringList get_variant_stringlist( const QVariantList& l ) namespace CalamaresUtils
{ {
QStringList retl;
static CommandList_t get_variant_stringlist( const QVariantList& l, int timeout )
{
CommandList_t retl;
unsigned int c = 0; unsigned int c = 0;
for ( const auto& v : l ) for ( const auto& v : l )
{ {
if ( v.type() == QVariant::String ) if ( v.type() == QVariant::String )
retl.append( v.toString() ); retl.append( CommandLine( v.toString(), timeout ) );
else else
cDebug() << "WARNING Bad CommandList element" << c << v.type() << v; cDebug() << "WARNING Bad CommandList element" << c << v.type() << v;
++c; ++c;
@ -41,22 +44,20 @@ static QStringList get_variant_stringlist( const QVariantList& l )
return retl; return retl;
} }
namespace CalamaresUtils CommandList::CommandList( bool doChroot, int timeout )
{
CommandList::CommandList( bool doChroot )
: m_doChroot( doChroot ) : m_doChroot( doChroot )
, m_timeout( timeout )
{ {
} }
CommandList::CommandList::CommandList( const QVariant& v, bool doChroot ) CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, int timeout )
: CommandList( doChroot ) : CommandList( doChroot, timeout )
{ {
if ( v.type() == QVariant::List ) if ( v.type() == QVariant::List )
{ {
const auto v_list = v.toList(); const auto v_list = v.toList();
if ( v_list.count() ) if ( v_list.count() )
append( get_variant_stringlist( v_list ) ); append( get_variant_stringlist( v_list, m_timeout ) );
else else
cDebug() << "WARNING: Empty CommandList"; cDebug() << "WARNING: Empty CommandList";
} }
@ -90,7 +91,7 @@ Calamares::JobResult CommandList::run( const QObject* parent )
for ( CommandList::const_iterator i = cbegin(); i != cend(); ++i ) for ( CommandList::const_iterator i = cbegin(); i != cend(); ++i )
{ {
QString processed_cmd = *i; QString processed_cmd = i->command();
processed_cmd.replace( "@@ROOT@@", root ); // FIXME? processed_cmd.replace( "@@ROOT@@", root ); // FIXME?
bool suppress_result = false; bool suppress_result = false;
if ( processed_cmd.startsWith( '-' ) ) if ( processed_cmd.startsWith( '-' ) )
@ -103,7 +104,7 @@ Calamares::JobResult CommandList::run( const QObject* parent )
shell_cmd << processed_cmd; shell_cmd << processed_cmd;
ProcessResult r = System::runCommand( ProcessResult r = System::runCommand(
location, shell_cmd, QString(), QString(), 10 ); location, shell_cmd, QString(), QString(), i->timeout() );
if ( r.getExitCode() != 0 ) if ( r.getExitCode() != 0 )
{ {
@ -117,4 +118,10 @@ Calamares::JobResult CommandList::run( const QObject* parent )
return Calamares::JobResult::ok(); return Calamares::JobResult::ok();
} }
void
CommandList::append( const QString& s )
{
append( CommandLine( s, m_timeout ) );
}
} // namespace } // namespace

View File

@ -27,11 +27,47 @@
namespace CalamaresUtils namespace CalamaresUtils
{ {
class CommandList : protected QStringList /**
* Each command can have an associated timeout in seconds. The timeout
* defaults to 10 seconds. Provide some convenience naming and construction.
*/
struct CommandLine : public QPair< QString, int >
{
CommandLine( const QString& s )
: QPair< QString, int >( s, 10 )
{
}
CommandLine( const QString& s, int t )
: QPair< QString, int >( s, t)
{
}
QString command() const
{
return first;
}
int timeout() const
{
return second;
}
} ;
/** @brief Abbreviation, used internally. */
using CommandList_t = QList< CommandLine >;
/**
* A list of commands; the list may have its own default timeout
* for commands (which is then applied to each individual command
* that doesn't have one of its own).
*/
class CommandList : protected CommandList_t
{ {
public: public:
CommandList( bool doChroot = true ); /** @brief empty command-list with timeout to apply to entries. */
CommandList( const QVariant& v, bool doChroot = true ); CommandList( bool doChroot = true, int timeout = 10 );
CommandList( const QVariant& v, bool doChroot = true, int timeout = 10 );
~CommandList(); ~CommandList();
bool doChroot() const bool doChroot() const
@ -41,14 +77,19 @@ public:
Calamares::JobResult run( const QObject* parent ); Calamares::JobResult run( const QObject* parent );
using QStringList::isEmpty; using CommandList_t::isEmpty;
using QStringList::count; using CommandList_t::count;
using QStringList::cbegin; using CommandList_t::cbegin;
using QStringList::cend; using CommandList_t::cend;
using QStringList::const_iterator; using CommandList_t::const_iterator;
protected:
using CommandList_t::append;
void append( const QString& );
private: private:
bool m_doChroot; bool m_doChroot;
int m_timeout;
} ; } ;
} // namespace } // namespace