📜 ⬆️ ⬇️

Python using the example of the new git commit notification daemon

Working as a team I like to be aware of the activity of the participants. Therefore, it was decided to write a demon watching new commits to the git 's repository. Since I work in Ubuntu , the notification was implemented in a built-in way - the libnotify library.
Language - Python !

image

The article mentions:
1. Demon in Python ;
2. Logging in Python ;
3. Storage of configuration files of programs in Python ;
4. Working with OS commands from Python scripts;
5. Getting a list of recent changes from git 'a;
6. Ubuntu Standard Popup Notifications.

To implement the task, the Python language was chosen (high-level, interpretable, object-oriented and extensible programming language), because I do not know it.
For a start, two sources helped me a lot:
- official documentation: http://docs.python.org/tutorial/index.html ;
- a series of IBM articles in Russian: https://www.ibm.com/developerworks/ru/library/l-python_part_1/ .
')
While learning the basics, proceed to writing the program.

1. Demon


There are many implementations of demons in the network, chose one of the ready-made with positive reviews and an attractive name: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ . This daemon had problems shutting down the command " daemon.py stop " in this place:
except OSError, err: err = str(err) if err.find("No such process") > 0: if os.path.exists(self.pidfile): os.remove(self.pidfile) 

This apparently arose because of the Russian locale, the phrase " No such process " in my system was returned in Russian. An easy way to fix this is to remove this check:
 except OSError, err: if os.path.exists(self.pidfile): os.remove(self.pidfile) 


2. Logging


The simplest way to notify you when a program is working is to use the print () function. But this program will run as a daemon and does not imply the display of information about its state in the launch console. A convenient option in this case is to write the log to a file. In Python , there is a built-in logging method included in the standard library - the logging module ( http://docs.python.org/library/logging.html ).
The module has many logging options ( handlers , http://docs.python.org/library/logging.handlers.html ): StreamHandler , FileHandler , WatchedFileHandler , RotatingFileHandler , TimedRotatingFileHandler , SocketHandler , DatagramHandler , SysLogHandler , NTEENTLENTler , SocketHandler , DatagramHandler , SysLogHandler , NTEENTLENTler , SocketHandler , DatagramHandler , SysLogHandler , NTEENTLENTler , SocketHandler , NTEHENTler , SocketHandler , NTGENTHandler . HTTPHandler . To control the daemon, FileHandler was used:
 logging.basicConfig(filename = tempfile.gettempdir() + '/gitPushNotify.log', level = logging.DEBUG, format = '%(asctime)s %(levelname)s: %(message)s', datefmt = '%Y-%m-%d %I:%M:%S') logging.info('Daemon start') 


3. Storing Python Program Configuration


To store the configuration of applications, the ini file and the ConfigParser built-in module for working with them ( http://docs.python.org/library/configparser.html ) are used:
 config = ConfigParser.ConfigParser() configPath = os.path.dirname(__file__) + '/config.ini' config.read(configPath) 

Getting the values ​​of parameters by functions (in this case getting integer values):
 timeout = config.getint('daemon', 'timeout') 


4. Working with OS commands from Python scripts


To execute system commands, use the check_output () method of the subprocess module ( http://docs.python.org/library/subprocess.html ):
 sourceOutput = subprocess.check_output('cd ' + repositoryPath, shell=True) 

You can also use the methods of the os module:
 sourceOutput = os.system(commandString) 

Documentation recommends using subprocess .

5. Getting a list of recent changes from git


To view the latest changes to the repository, it is convenient to use the command whatchanged ( http://schacon.github.com/git/git-whatchanged.html ). This command allows you to set the format of the displayed log messages, set the number of changes to be output. An example of using the command:
$ git whatchanged master -10 --date=raw --date-order --pretty=format:"%H %n%cn %n%ce %n%ct %n%s"

Arguments in order:
master - repository branch;
-10 - the number of records displayed;
- date-order — sort by change date;
--pretty = format: "..." - output format.

6. Ubuntu Standard Popup Notifications


Working with pop-up notifications in Ubuntu is done via the libnotify library ( https://wiki.ubuntu.com/NotificationDevelopmentGuidelines ). Check whether it is installed with the command:
$ dpkg -l | grep libnotify-bin

Or immediately execute:
$ sudo apt-get install libnotify-bin

You can display a notification using the command:
$ notify-send "Habr!"
$ notify-send -i notification-message-email "Title" "Message"

The -i flag is an image, it indicates the system name or the path to any image in svg , png or jpg format.

Start daemon


You must make the file executable:
$ chmod +x gitPushNotifyDaemon.py

Before launching in the configuration file, you must specify the path to the repository and the frequency of the survey:
$ vim config.ini

Run:
$ python gitPushNotifyDaemon.py start

If everything went well:
Daemon starting..

Start notification will appear:

image

Now you can see the daemon in the list of processes by running the command:
$ ps uax | grep gitPushNotifyDaemon.py

The process of the daemon can be monitored using the log:
$ tail -f /tmp/gitPushNotify.log

You can also separately run the gitPushNotify.py file for debugging:
$ python gitPushNotify.py


Project repository: https://github.com/antonfisher/gitPushNotify

That's all. Have a good day!

References:
1. http://docs.python.org/tutorial/index.html - official tutorial;
2. https://www.ibm.com/developerworks/ru/library/l-python_part_1/ - IBM article series;
3. http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ - the implementation of the Sander Marechal daemon;
4. http://docs.python.org/library/logging.html - Python , Logging module;
5. http://docs.python.org/library/configparser.html - Python , module ConfigParser ;
6. http://docs.python.org/library/subprocess.html - Python , module Subprocess ;
7. https://wiki.ubuntu.com/NotificationDevelopmentGuidelines - a description of libnotify ;
8. http://schacon.github.com/git/git-whatchanged.html description of the git-whatchanged command .

Source: https://habr.com/ru/post/135408/


All Articles