Skip to content

Commit aaff7ce

Browse files
authoredJul 7, 2021
Merge pull request #43520 from Gustry/version
[CLI] Add --version and -v to know QGIS version
2 parents d3d75a1 + bb9c45d commit aaff7ce

File tree

8 files changed

+254
-2
lines changed

8 files changed

+254
-2
lines changed
 
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgscommandlineutils.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsCommandLineUtils
13+
{
14+
%Docstring(signature="appended")
15+
Utils class for QGIS command line tools.
16+
17+
.. versionadded:: 3.22
18+
%End
19+
20+
%TypeHeaderCode
21+
#include "qgscommandlineutils.h"
22+
%End
23+
public:
24+
25+
static QString allVersions( );
26+
%Docstring
27+
Display all versions in the standard output stream
28+
29+
.. versionadded:: 3.22
30+
%End
31+
32+
};
33+
34+
/************************************************************************
35+
* This file has been generated automatically from *
36+
* *
37+
* src/core/qgscommandlineutils.h *
38+
* *
39+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
40+
************************************************************************/

‎python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
%Include auto_generated/qgscacheindexfeatureid.sip
2121
%Include auto_generated/qgscadutils.sip
2222
%Include auto_generated/qgsclipper.sip
23+
%Include auto_generated/qgscommandlineutils.sip
2324
%Include auto_generated/qgscolorramp.sip
2425
%Include auto_generated/qgscolorscheme.sip
2526
%Include auto_generated/qgscolorschemeregistry.sip

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ set(QGIS_CORE_SRCS
314314
qgscolorramp.cpp
315315
qgscolorscheme.cpp
316316
qgscolorschemeregistry.cpp
317+
qgscommandlineutils.cpp
317318
qgsconditionalstyle.cpp
318319
qgsconnectionregistry.cpp
319320
qgscoordinateformatter.cpp
@@ -924,6 +925,7 @@ set(QGIS_CORE_HDRS
924925
qgscacheindexfeatureid.h
925926
qgscadutils.h
926927
qgsclipper.h
928+
qgscommandlineutils.h
927929
qgscolorramp.h
928930
qgscolorscheme.h
929931
qgscolorschemeregistry.h

‎src/core/qgscommandlineutils.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/***************************************************************************
2+
qgscommandlineutils.cpp
3+
---------------------------
4+
begin : June 2021
5+
copyright : (C) 2021 by Etienne Trimaille
6+
email : etienne dot trimaille at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsapplication.h"
19+
#include "qgscommandlineutils.h"
20+
#include "qgsgeos.h"
21+
#include "qgsprojutils.h"
22+
#include "qgsversion.h"
23+
24+
#include <sqlite3.h>
25+
#include <ogr_api.h>
26+
#include <gdal_version.h>
27+
#include <proj.h>
28+
#include <QSysInfo>
29+
30+
QString QgsCommandLineUtils::allVersions( )
31+
{
32+
33+
// QGIS main version
34+
QString versionString = QStringLiteral( "QGIS %1 '%2' (%3)\n" ).arg( VERSION, RELEASE_NAME, QGSVERSION );
35+
36+
// QGIS code revision
37+
if ( QString( Qgis::devVersion() ) == QLatin1String( "exported" ) )
38+
{
39+
versionString += QLatin1String( "QGIS code branch" );
40+
if ( Qgis::version().endsWith( QLatin1String( "Master" ) ) )
41+
{
42+
versionString += QLatin1String( "master\n" );
43+
}
44+
else
45+
{
46+
versionString += QStringLiteral( "Release %1.%2\n" ).arg( Qgis::versionInt() / 10000 ).arg( Qgis::versionInt() / 100 % 100 );
47+
}
48+
}
49+
else
50+
{
51+
versionString += QStringLiteral( "QGIS code revision %1\n" ).arg( Qgis::devVersion() );
52+
}
53+
54+
// Qt version
55+
const QString qtVersionCompiled{ QT_VERSION_STR };
56+
const QString qtVersionRunning{ qVersion() };
57+
if ( qtVersionCompiled != qtVersionRunning )
58+
{
59+
versionString += QStringLiteral( "Compiled against Qt %1\n" ).arg( qtVersionCompiled );
60+
versionString += QStringLiteral( "Running against Qt %1\n" ).arg( qtVersionRunning );
61+
}
62+
else
63+
{
64+
versionString += QStringLiteral( "Qt version %1\n" ).arg( qtVersionCompiled );
65+
}
66+
67+
// Python version
68+
versionString += QStringLiteral( "Python version %1\n" ).arg( PYTHON_VERSION );
69+
70+
// GDAL version
71+
const QString gdalVersionCompiled { GDAL_RELEASE_NAME };
72+
const QString gdalVersionRunning { GDALVersionInfo( "RELEASE_NAME" ) };
73+
if ( gdalVersionCompiled != gdalVersionRunning )
74+
{
75+
versionString += QStringLiteral( "Compiled against GDAL/OGR %1\n" ).arg( gdalVersionCompiled );
76+
versionString += QStringLiteral( "Running against GDAL/OGR %1\n" ).arg( gdalVersionRunning );
77+
}
78+
else
79+
{
80+
versionString += QStringLiteral( "GDAL/OGR version %1\n" ).arg( gdalVersionCompiled );
81+
}
82+
83+
// proj
84+
PJ_INFO info = proj_info();
85+
const QString projVersionCompiled { QStringLiteral( "%1.%2.%3" ).arg( PROJ_VERSION_MAJOR ).arg( PROJ_VERSION_MINOR ).arg( PROJ_VERSION_PATCH ) };
86+
const QString projVersionRunning { info.version };
87+
if ( projVersionCompiled != projVersionRunning )
88+
{
89+
versionString += QStringLiteral( "Compiled against PROJ %1\n" ).arg( projVersionCompiled );
90+
versionString += QStringLiteral( "Running against PROJ %2\n" ).arg( projVersionRunning );
91+
}
92+
else
93+
{
94+
versionString += QStringLiteral( "PROJ version %1\n" ).arg( projVersionCompiled );
95+
}
96+
97+
// CRS database versions
98+
versionString += QStringLiteral( "EPSG Registry database version %1 (%2)\n" ).arg( QgsProjUtils::epsgRegistryVersion(), QgsProjUtils::epsgRegistryDate().toString( Qt::ISODate ) );
99+
100+
// GEOS version
101+
const QString geosVersionCompiled { GEOS_CAPI_VERSION };
102+
const QString geosVersionRunning { GEOSversion() };
103+
if ( geosVersionCompiled != geosVersionRunning )
104+
{
105+
versionString += QStringLiteral( "Compiled against GEOS %1\n" ).arg( geosVersionCompiled );
106+
versionString += QStringLiteral( "Running against GEOS %1\n" ).arg( geosVersionRunning );
107+
}
108+
else
109+
{
110+
versionString += QStringLiteral( "GEOS version %1\n" ).arg( geosVersionCompiled );
111+
}
112+
113+
// SQLite version
114+
const QString sqliteVersionCompiled { SQLITE_VERSION };
115+
const QString sqliteVersionRunning { sqlite3_libversion() };
116+
if ( sqliteVersionCompiled != sqliteVersionRunning )
117+
{
118+
versionString += QStringLiteral( "Compiled against SQLite %1\n" ).arg( sqliteVersionCompiled );
119+
versionString += QStringLiteral( "Running against SQLite %1\n" ).arg( sqliteVersionRunning );
120+
}
121+
else
122+
{
123+
versionString += QStringLiteral( "SQLite version %1\n" ).arg( sqliteVersionCompiled );
124+
}
125+
126+
// Operating system
127+
versionString += QStringLiteral( "OS %1\n" ).arg( QSysInfo::prettyProductName() );
128+
129+
#ifdef QGISDEBUG
130+
versionString += QStringLiteral( "This copy of QGIS writes debugging output.\n" );
131+
#endif
132+
133+
return versionString;
134+
}

‎src/core/qgscommandlineutils.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/***************************************************************************
2+
qgscommandlineutils.h
3+
---------------------------
4+
begin : June 2021
5+
copyright : (C) 2021 by Etienne Trimaille
6+
email : etienne dot trimaille at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSCOMMANDLINEUTILS_H
19+
#define QGSCOMMANDLINEUTILS_H
20+
21+
#include "qgis.h"
22+
23+
/**
24+
* \ingroup core
25+
* \class QgsCommandLineUtils
26+
* \brief Utils class for QGIS command line tools.
27+
* \since QGIS 3.22
28+
*/
29+
class CORE_EXPORT QgsCommandLineUtils
30+
{
31+
public:
32+
33+
/**
34+
* Display all versions in the standard output stream
35+
* \since QGIS 3.22
36+
*/
37+
static QString allVersions( );
38+
39+
};
40+
41+
#endif // QGSCOMMANDLINEUTILS_H

‎src/process/qgsprocess.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* *
1616
***************************************************************************/
1717

18+
#include "qgscommandlineutils.h"
1819
#include "qgsprocess.h"
1920
#include "qgsprocessingregistry.h"
2021
#include "qgsprocessingalgorithm.h"
@@ -298,6 +299,16 @@ int QgsProcessingExec::run( const QStringList &constArgs )
298299
listAlgorithms( useJson );
299300
return 0;
300301
}
302+
else if ( command == QLatin1String( "--version" ) || command == QLatin1String( "-v" ) )
303+
{
304+
std::cout << QgsCommandLineUtils::allVersions().toStdString();
305+
return 0;
306+
}
307+
else if ( command == QLatin1String( "--help" ) || command == QLatin1String( "-h" ) )
308+
{
309+
showUsage( args.at( 0 ) );
310+
return 0;
311+
}
301312
else if ( command == QLatin1String( "help" ) )
302313
{
303314
if ( args.size() < 3 )
@@ -445,8 +456,10 @@ void QgsProcessingExec::showUsage( const QString &appName )
445456

446457
msg << "QGIS Processing Executor - " << VERSION << " '" << RELEASE_NAME << "' ("
447458
<< Qgis::version() << ")\n"
448-
<< "Usage: " << appName << " [--json] [--verbose] [command] [algorithm id or path to model file] [parameters]\n"
459+
<< "Usage: " << appName << " [--help] [--version] [--json] [--verbose] [command] [algorithm id or path to model file] [parameters]\n"
449460
<< "\nOptions:\n"
461+
<< "\t--help or -h\t\tOutput the help\n"
462+
<< "\t--version or -v\t\tOutput all versions related to QGIS Process\n"
450463
<< "\t--json\t\tOutput results as JSON objects\n"
451464
<< "\t--verbose\tOutput verbose logs\n"
452465
<< "\nAvailable commands:\n"

‎src/server/qgis_map_serv.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsfcgiserverresponse.h"
2323
#include "qgsfcgiserverrequest.h"
2424
#include "qgsapplication.h"
25+
#include "qgscommandlineutils.h"
2526

2627
#include <fcgi_stdio.h>
2728
#include <cstdlib>
@@ -43,6 +44,15 @@ int fcgi_accept()
4344

4445
int main( int argc, char *argv[] )
4546
{
47+
if ( argc >= 2 )
48+
{
49+
if ( argv[1] == QLatin1String( "--version" ) || argv[1] == QLatin1String( "-v" ) )
50+
{
51+
std::cout << QgsCommandLineUtils::allVersions().toStdString();
52+
return 0;
53+
}
54+
}
55+
4656
// Test if the environ variable DISPLAY is defined
4757
// if it's not, the server is running in offscreen mode
4858
// Qt supports using various QPA (Qt Platform Abstraction) back ends

‎src/server/qgis_mapserver.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ while QGIS server internal logging is printed to stderr.
3030
#include <condition_variable>
3131

3232
//for CMAKE_INSTALL_PREFIX
33+
#include "qgscommandlineutils.h"
3334
#include "qgsconfig.h"
3435
#include "qgsserver.h"
3536
#include "qgsbufferserverrequest.h"
@@ -554,7 +555,10 @@ int main( int argc, char *argv[] )
554555
QCommandLineParser parser;
555556
parser.setApplicationDescription( QObject::tr( "QGIS Development Server %1" ).arg( VERSION ) );
556557
parser.addHelpOption();
557-
parser.addVersionOption();
558+
559+
QCommandLineOption versionOption( QStringList() << "v" << "version", QObject::tr( "Version of QGIS and libraries" ) );
560+
parser.addOption( versionOption );
561+
558562
parser.addPositionalArgument( QStringLiteral( "addressAndPort" ),
559563
QObject::tr( "Address and port (default: \"localhost:8000\")\n"
560564
"address and port can also be specified with the environment\n"
@@ -571,6 +575,13 @@ int main( int argc, char *argv[] )
571575
parser.addOption( projectOption );
572576

573577
parser.process( app );
578+
579+
if ( parser.isSet( versionOption ) )
580+
{
581+
std::cout << QgsCommandLineUtils::allVersions().toStdString();
582+
return 0;
583+
}
584+
574585
const QStringList args = parser.positionalArguments();
575586

576587
if ( args.size() == 1 )

0 commit comments

Comments
 (0)
Please sign in to comment.