[3.0] Bump libgdata to 0.6.4
[jhbuild/xnox.git] / scripts / hg-update.py
blobe64f6c55b7f0693e1d78c35e80b9f3a5c9d42fed
1 #! /usr/bin/env python
3 # hg-update - pull and update a mercurial repository
5 # Copyright (C) 2007 Marco Barisione <marco@barisione.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 import os
22 import sys
23 import re
25 try:
26 from subprocess import Popen, call, PIPE, STDOUT
27 except ImportError: # Python < 2.4 lacks subprocess module
28 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
29 from jhbuild.cut_n_paste import subprocess
30 sys.modules['subprocess'] = subprocess
31 from subprocess import Popen, call, PIPE, STDOUT
33 def get_parent():
34 hg = Popen(['hg', 'parents', '--template', '{rev}'], stdout=PIPE)
35 try:
36 return hg.stdout.read().split()[0]
37 except IndexError:
38 # handle parentless revisions
39 return ''
41 def pull():
42 ret = call(['hg', 'pull'])
43 return ret == 0
45 def update():
46 env = dict(os.environ)
47 env['HGMERGE'] = '/bin/false'
48 env['LANG'] = 'C'
49 hg = Popen(['hg', 'update', '--noninteractive'], stdout=PIPE,
50 stderr=STDOUT, env=env)
51 out = hg.communicate()[0]
52 if hg.returncode != 0:
53 # Use CVS-like format for conflicts.
54 out = re.sub('merging (.*) failed!', r'C \1', out)
55 index = out.find('You can redo the full merge using:')
56 # Remove the instructions to redo the full merge as we are
57 # going to revert the update.
58 if index != -1:
59 out = out[:index]
60 print out
61 return hg.returncode == 0
63 def undo_update(parent):
64 print 'Update failed, updating to parent revision'
65 env = dict(os.environ)
66 env['HGMERGE'] = 'false'
67 hg = call(['hg', 'update', '--noninteractive', '-q', parent], env=env)
69 def pull_and_update():
70 parent = get_parent()
71 if not pull():
72 return False
73 if update():
74 return True
75 else:
76 undo_update(parent)
77 return False
79 if __name__ == '__main__':
80 ret = False
81 try:
82 ret = pull_and_update()
83 except OSError, e:
84 print '%s: %s' % (sys.argv[0], e)
86 if ret:
87 exit_code = 0
88 else:
89 exit_code = 1
90 sys.exit(exit_code)