检查外部应用程序是否可用的最佳方法是什么?

时间:2022-09-06 00:25:50

I have an application which had optional extras depending on if the user has the software installed.

我有一个应用程序,它具有可选的附加功能,具体取决于用户是否安装了该软件。

On Linux what is the best way to determine if something like python and PyUsb is installed?

在Linux上,确定是否安装了python和PyUsb之类的最佳方法是什么?

I am developing a C++ Qt application if that helps.

我正在开发一个C ++ Qt应用程序,如果这有帮助。

4 个解决方案

#1


This is inefficient (requires forking and exec'ing /bin/sh). There has to be a better way! But as a generic approach... There's always system().

这是低效的(需要分叉和执行/ bin / sh)。一定有更好的方法!但作为一种通用的方法......总有系统()。

(Remember to use WEXITSTATUS()! Watch out for making programs uninterruptable!)

(记得使用WEXITSTATUS()!注意让程序不间断!)

#define SHOW(X)  cout << # X " = " << (X) << endl

int main()
{
  int status;

  SHOW( status = system( "which grep > /dev/null 2>&1" ) );
  SHOW( WEXITSTATUS(status) );

  SHOW( status = system( "which no_matching_file > /dev/null 2>&1" ) );
  SHOW( WEXITSTATUS(status) );
}

There is also popen(), which can be useful for grabbing output from programs to verify version numbers, libraries, or whatnot.

还有popen(),它可以用于从程序中获取输出以验证版本号,库或诸如此类的东西。

If you need bidirectional (read&write) access to a subprocess, best to use pipe(), fork(), exec(), close(), & dup2().

如果您需要对子进程的双向(读写)访问,最好使用pipe(),fork(),exec(),close()和dup2()。

#2


You can require that they be in the path, etc. Check for the existence of the executable files you will require (using which or similar). You can also use the executable files' arguments and check for required versions as well, if needed.

您可以要求它们位于路径等中。检查是否存在您需要的可执行文件(使用哪个或类似的)。如果需要,您还可以使用可执行文件的参数并检查所需的版本。

#3


I'm not aware of a way to do that for Linux in general, since each distro can have its own package manager. But assuming you want to support the most popular distros, you can query their package manager for installed software (I'd suggest as a start to support apt-get, rpm and yum) and parse the output to look for the packages you recognize. Each manager has a way to list the installed packages, my suggestion as a start:

我一般都不知道为Linux做这件事的方法,因为每个发行版都有自己的包管理器。但假设您想要支持最流行的发行版,您可以向他们的软件包管理器查询已安装的软件(我建议首先支持apt-get,rpm和yum)并解析输出以查找您识别的软件包。每个经理都有办法列出已安装的软件包,我的建议是:

apt-get --no-act check
rpm -qa
yum list installed

#4


One other possibility is to present all the features to the user and prompt them to install extras if they try to use them (e.g. see http://0install.net).

另一种可能性是向用户提供所有功能,并在他们尝试使用时提示他们安装附加功能(例如,请参阅http://0install.net)。

#1


This is inefficient (requires forking and exec'ing /bin/sh). There has to be a better way! But as a generic approach... There's always system().

这是低效的(需要分叉和执行/ bin / sh)。一定有更好的方法!但作为一种通用的方法......总有系统()。

(Remember to use WEXITSTATUS()! Watch out for making programs uninterruptable!)

(记得使用WEXITSTATUS()!注意让程序不间断!)

#define SHOW(X)  cout << # X " = " << (X) << endl

int main()
{
  int status;

  SHOW( status = system( "which grep > /dev/null 2>&1" ) );
  SHOW( WEXITSTATUS(status) );

  SHOW( status = system( "which no_matching_file > /dev/null 2>&1" ) );
  SHOW( WEXITSTATUS(status) );
}

There is also popen(), which can be useful for grabbing output from programs to verify version numbers, libraries, or whatnot.

还有popen(),它可以用于从程序中获取输出以验证版本号,库或诸如此类的东西。

If you need bidirectional (read&write) access to a subprocess, best to use pipe(), fork(), exec(), close(), & dup2().

如果您需要对子进程的双向(读写)访问,最好使用pipe(),fork(),exec(),close()和dup2()。

#2


You can require that they be in the path, etc. Check for the existence of the executable files you will require (using which or similar). You can also use the executable files' arguments and check for required versions as well, if needed.

您可以要求它们位于路径等中。检查是否存在您需要的可执行文件(使用哪个或类似的)。如果需要,您还可以使用可执行文件的参数并检查所需的版本。

#3


I'm not aware of a way to do that for Linux in general, since each distro can have its own package manager. But assuming you want to support the most popular distros, you can query their package manager for installed software (I'd suggest as a start to support apt-get, rpm and yum) and parse the output to look for the packages you recognize. Each manager has a way to list the installed packages, my suggestion as a start:

我一般都不知道为Linux做这件事的方法,因为每个发行版都有自己的包管理器。但假设您想要支持最流行的发行版,您可以向他们的软件包管理器查询已安装的软件(我建议首先支持apt-get,rpm和yum)并解析输出以查找您识别的软件包。每个经理都有办法列出已安装的软件包,我的建议是:

apt-get --no-act check
rpm -qa
yum list installed

#4


One other possibility is to present all the features to the user and prompt them to install extras if they try to use them (e.g. see http://0install.net).

另一种可能性是向用户提供所有功能,并在他们尝试使用时提示他们安装附加功能(例如,请参阅http://0install.net)。