Example of implementing git hook (hooks) in python

Source: Internet
Author: User
Tags python script in python


Although implemented in python, it can actually be any server script. The hook function of svn is very useful. git can also implement the hook function, but because of the distributed feature, if we want a server to update and submit code, we can notify the target server through a remote database, when the target server receives a notification, execute git pull.
First implementation:
This method is implemented using an intermediate file in two types of scripts.
1. First, the general remote version Library, github I use, has a webhooks function. github is in the configuration options of the version library, and he can set a Payload URL, here, you can select and enter your own notification address based on your needs. I will execute the notification when pushing each version library to notify you of a url, this url is written in php.
Code:
PHP

<? Php
If (@ $ _ POST ['payload']) {
$ P = json_decode ($ _ POST ['payload'], true );
If ('update' = $ p ['commits '] [0] ['message']) {
File_put_contents ('/data/log/git. Log', '123 ');
   }
}
?>
Here I am writing very simple and receive notifications, but I just want git to write the intermediate file when submitting the remarks and filling in the update, so that you can control web updates by yourself,
2. python script:

Python


#! /Usr/bin/python2.6
# Coding = UTF-8
Import OS
Import time
Dir = '/data/www/www.111cn.net /'
File = '/data/log/git. Log'
Nowfile = OS. getcwd () + "/" + _ file __
Stime = OS. stat (nowfile). st_mtime
# Exit when the modification time changes
While stime = OS. stat (nowfile). st_mtime:
Log = OS. popen ("cat" + file). read (). strip ()
If ''! = Log:
OS. system ('echo ""> '+ file)
OS. system ('CD' + dir + '; git pull origin master 1>/dev/null 2> & 1 ')
OS. system ('echo "git update" | mail-s "web update" zhangcunchao_cn@163.com ')
Time. sleep (1)
Else:
OS. system ('echo "python git pull Process end" | mail-s "process check" zhangcunchao_cn@163.com ')

This script monitors the intermediate script. When there is an update, execute the git pull operation and notify me
The principle is very simple. Add this script to the process monitoring script written in the previous article.
Second implementation:
Directly use web. py and python as web servers, and directly provide the corresponding address.
Code:

Python

Import web
Import OS
Import demjson
Urls = (
'/Github', 'hello ',
'/Github/', 'hello ',
)
Class hello:
Def GET (self, a = ''):
Print 'only post'
Def POST (self, name = ''):
I = web. input ()
If not I. has_key ('payload '):
Print 'nodata'
Else:
Data = demjson. decode (I. payload)
If 'update' = data ['commits '] [0] ['message']:
Dir = '/data/www/www.111cn.net /'
OS. system ('CD' + dir + '; git pull origin master 1>/dev/null 2> & 1 ')
OS. system ('echo "git update" | mail-s "web update" zhangcunchao_cn@163.com ')
If _ name _ = "_ main _": web. run (urls, globals ())
 
In this way, I write markdown documents locally, and even modify the code. Then I can modify and submit the code locally to automatically update my server web site.
In the future, I will try to write my article in the markdown document. You will be able to provide more support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.