[FIX] git config for pull
[odoo/odoo.git] / odoo.py
1 #!/usr/bin/env python
2 #----------------------------------------------------------
3 # odoo cli
4 #
5 # To install your odoo development environement type:
6 #
7 # wget -O- https://raw.githubusercontent.com/odoo/odoo/master/odoo.py | python
8 #
9 # The setup_* subcommands used to boostrap odoo are defined here inline and may
10 # only depends on the python 2.7 stdlib
11 #
12 # The rest of subcommands are defined in odoo/cli or in <module>/cli by
13 # subclassing the Command object
14 #
15 # https://raw.githubusercontent.com/odoo-dev/odoo/master-odoo-cmd-fme/odoo.py
16 #
17 #----------------------------------------------------------
18 import os
19 import re
20 import sys
21 import subprocess
22
23 GIT_HOOKS_PRE_PUSH = """
24 #!/usr/bin/env python2
25 import re
26 import sys
27 if re.search('github.com[:/]odoo/odoo.git$', sys.argv[2]):
28     print "Pushing to /odoo/odoo.git is forbidden, please push to odoo-dev, use --no-verify to override"
29     sys.exit(1)
30 """
31
32 def printf(f,*l):
33     print "odoo:" + f % l
34
35 def run(*l):
36     if isinstance(l[0], list):
37         l = l[0]
38     printf("running %s", " ".join(l))
39     subprocess.check_call(l)
40
41 def git_locate():
42     # Locate git dir
43     # TODO add support for os.environ.get('GIT_DIR')
44
45     # check for an odoo child
46     if os.path.isfile('odoo/.git/config'):
47         os.chdir('odoo')
48
49     path = os.getcwd()
50     while path != '/':
51         gitconfig_path = os.path.join(path, '.git/config')
52         if os.path.isfile(gitconfig_path):
53             release_py = os.path.join(path, 'openerp/release.py')
54             if os.path.isfile(release_py):
55                 break
56         path = os.path.dirname(path)
57     if path == '/':
58         path = None
59     return path
60
61 def cmd_setup_git():
62     git_dir = git_locate()
63     if git_dir:
64         printf('git repo found at %s',git_dir)
65     else:
66         run("git", "init", "odoo")
67         os.chdir('odoo')
68         git_dir = os.getcwd()
69     if git_dir:
70         # push sane config for git < 2.0, and hooks
71         run('git','config','push.default','simple')
72         # alias
73         run('git','config','alias.st','status')
74         # merge bzr style
75         run('git','config','merge.commit','no')
76         # pull let me choose between merge or rebase only works in git > 2.0, use an alias for 1
77         run('git','config','pull.ff','only')
78         run('git','config','alias.pl','pull --ff-only')
79         pre_push_path = os.path.join(git_dir, '.git/hooks/pre-push')
80         open(pre_push_path,'w').write(GIT_HOOKS_PRE_PUSH.strip())
81         os.chmod(pre_push_path, 0755)
82         # setup odoo remote
83         run('git','config','remote.odoo.url','https://github.com/odoo/odoo.git')
84         run('git','config','remote.odoo.pushurl','git@github.com:odoo/odoo.git')
85         run('git','config','--add','remote.odoo.fetch','dummy')
86         run('git','config','--unset-all','remote.odoo.fetch')
87         run('git','config','--add','remote.odoo.fetch','+refs/heads/*:refs/remotes/odoo/heads/*')
88         # setup odoo-dev remote
89         run('git','config','remote.odoo-dev.url','https://github.com/odoo-dev/odoo.git')
90         run('git','config','remote.odoo-dev.pushurl','git@github.com:odoo-dev/odoo.git')
91         run('git','remote','update')
92         # setup master branch
93         run('git','config','branch.master.remote','odoo')
94         run('git','config','branch.master.merge','refs/heads/master')
95         run('git','checkout','master')
96     else:
97         printf('no git repo found')
98
99 def cmd_setup_git_dev():
100     git_dir = git_locate()
101     if git_dir:
102         # setup odoo-dev remote
103         run('git','config','--add','remote.odoo-dev.fetch','dummy')
104         run('git','config','--unset-all','remote.odoo-dev.fetch')
105         run('git','config','--add','remote.odoo-dev.fetch','+refs/heads/*:refs/remotes/odoo-dev/heads/*')
106         run('git','config','--add','remote.odoo-dev.fetch','+refs/pull/*:refs/remotes/odoo-dev/pull/*')
107         run('git','remote','update')
108
109 def cmd_setup_git_review():
110     git_dir = git_locate()
111     if git_dir:
112         # setup odoo-dev remote
113         run('git','config','--add','remote.odoo.fetch','dummy')
114         run('git','config','--unset-all','remote.odoo.fetch')
115         run('git','config','--add','remote.odoo.fetch','+refs/heads/*:refs/remotes/odoo/heads/*')
116         run('git','config','--add','remote.odoo.fetch','+refs/tags/*:refs/remotes/odoo/tags/*')
117         run('git','config','--add','remote.odoo.fetch','+refs/pull/*:refs/remotes/odoo/pull/*')
118
119 def setup_deps_debian(git_dir):
120     debian_control_path = os.path.join(git_dir, 'setup/debian/control')
121     debian_control = open(debian_control_path).read()
122     debs = re.findall('python-[0-9a-z]+',debian_control)
123     proc = subprocess.Popen(['sudo','apt-get','install'] + debs, stdin=open('/dev/tty'))
124     proc.communicate()
125
126 def cmd_setup_deps():
127     git_dir = git_locate()
128     if git_dir:
129         if os.path.isfile('/etc/debian_version'):
130             setup_deps_debian(git_dir)
131
132 def setup_pg_debian(git_dir):
133     cmd = ['sudo','su','-','postgres','-c','createuser -s %s' % os.environ['USER']]
134     subprocess.call(cmd)
135
136 def cmd_setup_pg():
137     git_dir = git_locate()
138     if git_dir:
139         if os.path.isfile('/etc/debian_version'):
140             setup_pg_debian(git_dir)
141
142 def cmd_setup():
143     cmd_setup_git()
144     cmd_setup_deps()
145     cmd_setup_pg()
146
147 def main():
148     # regsitry of commands
149     g = globals()
150     cmds = dict([(i[4:],g[i]) for i in g if i.startswith('cmd_')])
151     # if curl URL | python2 then use command setup
152     if len(sys.argv) == 1 and __file__ == '<stdin>':
153         cmd_setup()
154     elif len(sys.argv) == 2 and sys.argv[1] in cmds:
155         cmds[sys.argv[1]]()
156     else:
157         import openerp
158         openerp.cli.main()
159
160 if __name__ == "__main__":
161     main()
162