[IMP] repository cleanup
[odoo/odoo.git] / odoo.py
1 #!/usr/bin/env python2
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 -f 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             content = open(gitconfig_path).read()
54             if re.search('github.com[:/]odoo/odoo.git', content):
55                 break
56         path = os.path.dirname(path)
57     if path == '/':
58         path = None
59     return path
60
61 def cmd_setup_git_init():
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         git_dir = os.path.join(os.getcwd(), 'odoo')
68     if git_dir:
69         # sane push config for git < 2.0
70         run('git','config','push.default','simple')
71         # merge bzr style
72         run('git','config','merge.ff','no')
73         run('git','config','merge.commit','no')
74         # push hooks
75         pre_push_path = os.path.join(git_dir, '.git/hooks/pre-push')
76         open(pre_push_path,'w').write(GIT_HOOKS_PRE_PUSH.strip())
77         os.chmod(pre_push_path, 0755)
78         # setup odoo remote
79         run('git','config','remote.odoo.url','https://github.com/odoo/odoo.git')
80         run('git','config','remote.odoo.pushurl','git@github.com:odoo/odoo.git')
81         run('git','config','--add','remote.odoo.fetch','dummy')
82         run('git','config','--unset-all','remote.odoo.fetch')
83         run('git','config','--add','remote.odoo.fetch','+refs/heads/*:refs/remotes/odoo/heads/*')
84         # setup odoo-dev remote
85         run('git','config','remote.odoo-dev.url','https://github.com/odoo-dev/odoo.git')
86         run('git','config','remote.odoo-dev.pushurl','git@github.com:odoo-dev/odoo.git')
87         run('git','remote','update')
88         # setup master branch
89         run('git','config','branch.master.remote','odoo')
90         run('git','config','branch.master.merge','refs/heads/master')
91         run('git','checkout','master')
92     else:
93         printf('no git repo found')
94
95 def cmd_setup_git_odoo_dev():
96     git_dir = git_locate()
97     if git_dir:
98         # setup odoo-dev remote
99         run('git','config','--add','remote.odoo-dev.fetch','dummy')
100         run('git','config','--unset-all','remote.odoo-dev.fetch')
101         run('git','config','--add','remote.odoo-dev.fetch','+refs/heads/*:refs/remotes/odoo-dev/heads/*')
102         run('git','config','--add','remote.odoo-dev.fetch','+refs/pull/*:refs/remotes/odoo-dev/pull/*')
103         run('git','remote','update')
104
105 def cmd_setup_git_odoo_review():
106     git_dir = git_locate()
107     if git_dir:
108         # setup odoo-dev remote
109         run('git','config','--add','remote.odoo.fetch','dummy')
110         run('git','config','--unset-all','remote.odoo.fetch')
111         run('git','config','--add','remote.odoo.fetch','+refs/heads/*:refs/remotes/odoo/heads/*')
112         run('git','config','--add','remote.odoo.fetch','+refs/tags/*:refs/remotes/odoo/tags/*')
113         run('git','config','--add','remote.odoo.fetch','+refs/pull/*:refs/remotes/odoo/pull/*')
114
115 def setup_deps_debian(git_dir):
116     debian_control_path = os.path.join(git_dir, 'debian/control')
117     debian_control = open(debian_control_path).read()
118     debs = re.findall('python-[0-9a-z]+',debian_control)
119     proc = subprocess.Popen(['sudo','apt-get','install'] + debs, stdin=open('/dev/tty'))
120     proc.communicate()
121
122 def cmd_setup_deps():
123     git_dir = git_locate()
124     if git_dir:
125         if os.path.isfile('/etc/debian_version'):
126             setup_deps_debian(git_dir)
127
128 def setup_pg_debian(git_dir):
129     cmd = ['sudo','su','-','postgres','-c','createuser -s %s' % os.environ['USER']]
130     subprocess.call(cmd)
131
132 def cmd_setup_pg():
133     git_dir = git_locate()
134     if git_dir:
135         if os.path.isfile('/etc/debian_version'):
136             setup_pg_debian(git_dir)
137
138 def cmd_setup():
139     cmd_setup_git_init()
140     cmd_setup_deps()
141     cmd_setup_pg()
142
143 def main():
144     # regsitry of commands
145     g = globals()
146     cmds = dict([(i[4:],g[i]) for i in g if i.startswith('cmd_')])
147     # if curl URL | python2 then use command setup
148     if len(sys.argv) == 1 and __file__ == '<stdin>':
149         cmd_setup()
150     elif len(sys.argv) == 2 and sys.argv[1] in cmds:
151         cmds[sys.argv[1]]()
152     else:
153         import openerp
154         openerp.cli.main()
155
156 if __name__ == "__main__":
157     main()
158