Perl Script Instead of Blame-notifier on Linux OS
- In conclusion
As you know, our clients are developers. Sometimes developers can surprise you. For example, one of our users wrote his own script. It allows configuring emails about errors found by PVS-Studio. The difference between this script and our blame-notifier is that it does not require .NET Core on Linux. We decided to write a short note about this Perl script, because we believe that it can be useful to someone.
The Perl script, which we'll discuss further, is published on github under the GPL-2.0 license. So, anyone can use it.
Its main function is to notify about problems found by PVS-Studio via email. We can say that it's an equivalent of our blame-notifier. Perl script sends emails generated in HTML. It also sends an email to the commit's author. Information about the commit and its author is extracted from Git. It's worth noting that there are a number of settings for sending emails. We'll discuss the settings further.
The script stores all warnings in a database (which you must configure in advance). So, it can send notifications not about all problems, but only about new detected errors.
You can also attach something to the message. For example, you can attach a full log of errors detected by PVS-Studio to an email.
Of course, several pre-settings precede the script usage.
First, the project must use the Git version control system, since the script extracts all necessary information about the commit's author via git blame / git annotate.
Secondly, we need to configure the database so that the script can automatically save the errors found. Then, send an email describing new errors. It's also possible to receive notifications not only about new errors but about all errors in the project. In the "Use cases" section, I'll explain it in a little more detail.
Thirdly, you need to set up your email if you don't have it set up yet. We'll also discuss this point in detail in the "Email setup" section.
And last but not least, we need to install Perl. But it's not quite right to call this a requirement, because usually Perl is installed on every Linux OS.
Let's review the installation process in more detail. I'll install it on my virtual machine which is running the openSUSE Linux distribution. By the way, in the README file on the git repository, you can read about configuration for Debian, but there is almost no difference.
So, before setting up, we must clone the repository from github.
To use the script, we need to install the following modules (here are the modules for openSUSE):
- perl-DBI – a database-independent Perl tool;
- perl-DBD-mysql – MySQL database driver;
- perl-MIME-Lite – for generating MIME messages;
- perl-YAML – YML-parser for loading configuration files;
- perl-List-MoreUtils – additional utilities;
- perl-FindBin-Real – to search the directory of the Perl script source.
To do it use the following command:
$> sudo zypper install perl-DBI perl-DBD-mysql perl-MIME-Lite perl-YAML perl-List-MoreUtils perl-FindBin-Real
You can install these modules at will. It eases working with the code, but it won't affect the script's performance:
- perl-IPC-Filter – highlights syntax;
- highlight – highlights the GNU source code.
To set up email, use msmtp. To start installation, use the following command:
$> sudo zypper install msmtp msmtp-mta
After installation, you need to create the /etc/msmtprc file. The file must contain mail settings. For example, for Yandex-mail, this file will look as follows:
# Set default values for all following accounts. defaults auth on tls on tls_starttls on tls_certcheck off tls_trust_file /etc/ssl/certs/ca-certificates.crt logfile /var/log/msmtp.log # Account account default host smtp.yandex.ru port 587 auth on keepbcc on from firstname.lastname@example.org user email@example.com password emailpassword # Set a default account account default: default # Map local users to mail addresses (for crontab) aliases /etc/aliases
You will also need to create the msmtp.log file by yourself. The script will write logs to the file.
Also, to send emails, you will need to enable the POP3 and IMAP protocols in your mailbox settings.
At this point, the mail setup is complete. To make sure everything works, you can send a test message:
$> echo "test message" | mail -s "test" firstname.lastname@example.org
Setting up a database
Next, we need to set up any of the databases that Perl will support. Let's take MariaDB, as the script's author suggests.
To install it, use the following command:
$> sudo zypper install mariadb
Then we need to use:
$> sudo rcmysql start $> sudo mysql_secure_installation
After installation, we need to create a new database named blame and a new user with the same name:
$> sudo mysql mysql> CREATE DATABASE blame; mysql> GRANT ALL on blame.* TO 'blame'@'localhost' IDENTIFIED BY 'secret_mysql_password'; mysql> quit
Run the database before running the script.
The script will search for the configuration file in two places:
If it finds both files, the file located in the home directory will override the file with the global settings.
The contents of the file must be in YAML:
--- # database connection string dsn: DBI:mysql:blame # database username username: blame # database password password: secret_mysql_password # smtp sender address smtp_from: email@example.com # smtp subject smtp_subject: New PVS Issues # path substitution path_search: '/root/src/src/' path_replace: ''
Please note that this file contains confidential data. It shouldn't be accessible to all users.
The script takes the path to a file in the errorfile format as an input parameter. This file is obtained as a result of converting the analyzer output via the PlogConverter utility. Read more about this utility here.
For example, we have a PVS-Studio report (let's call it report.plog). To get the file required for the script, issue PlogConverter the following command:
plog-converter report.plog -t errorfile -o report.txt
Now, to run the script, we need to run the following command:
$> ./blame-notifier.pl < report.txt
Then, you will receive the following email:
In addition, there are a number of extra parameters that will allow you to use the script more flexibly. To get more information, you can use the -h option.
Let's take a look at some examples with additional options.
By default, the script groups the analyzer warnings by author and sends them via email. It is extracted by the script with the git annotate command. In addition, you can change the behavior so that you receive notifications from all authors by redefining their email addresses. You can do this by specifying the -r <your.email>at startup:
$> ./blame-notifier.pl -r firstname.lastname@example.org < report.txt
Each time you run the script, the database updates. An email with the latest analyzer warnings is generated. If you want to go beyond mailing which just sends warnings, you can use the -d option. In this case, the SQL DROP TABLE will be applied to the database. The script will send notifications about all PVS-Studio warnings.
$> ./blame-notifier.pl -d -r email@example.com < report.txt
Apart from the regular notification, you can also attach something to the message (for example, the report itself or its html version). To use this feature, pass the -a option to the script at startup. It accepts a comma-separated list of files:
$> ./blame-notifier.pl -a 'report.txt,report.html' < report.txt
Finally, if you want to see a detailed output of what is currently happening in the console, you can pass the -v option to the script.
If you don't want to install .NET Core on Linux, take a closer look at this Perl script. Maybe you'll want to use it instead of the official solution. Anyway, now you know for sure that there is such a way too.
However, please, note that we are not responsible for this solution. We do not guarantee that the script's author will maintain any compatibility with previous versions if logs or file formats change.
And, finally, an important note: the script author does not recommend running it under root. This will require storing configuration files in the user's home directory. Instead, you can run the PVS-Studio analysis in a Docker container.