app: Add an instance() function to return the global QApplication
[git-cola.git] / setup.py
blob26a688cc98294bfab3d31636d8e40e62865cf3d3
1 #!/usr/bin/env python
2 import re
3 import os
4 import sys
5 import stat
6 from glob import glob
8 from distutils.core import setup
9 from distutils.command import build_scripts
10 build_scripts.first_line_re = re.compile('^should not match$')
12 from cola import version
13 from cola import utils
14 from cola import resources
15 from cola import core
17 def main():
18 # ensure readable files
19 old_mask = os.umask(0022)
20 git_version = version.git_version()
21 if sys.argv[1] in ('install', 'build'):
22 _setup_environment()
23 _check_python_version()
24 _check_git_version(git_version)
25 _check_pyqt_version()
26 _build_translations() # msgfmt: .po -> .qm
28 if os.path.exists('.git'):
29 version.write_builtin_version()
30 _run_setup(git_version)
31 # restore the old mask
32 os.umask(old_mask)
34 def _setup_environment():
35 """Adds win32/ to our path for windows only"""
36 if sys.platform != 'win32':
37 return
38 path = os.environ['PATH']
39 win32 = os.path.join(os.path.dirname(__file__), 'win32')
40 os.environ['PATH'] = win32 + os.pathsep + path
42 def _run_setup(git_version):
43 """Runs distutils.setup()"""
45 scripts = ['bin/git-cola']
47 # git-difftool first moved out of git.git's contrib area in git 1.6.3
48 if (os.environ.get('INSTALL_GIT_DIFFTOOL', '') or
49 not version.check('difftool-builtin', git_version)):
50 scripts.append('bin/difftool/git-difftool')
51 scripts.append('bin/difftool/git-difftool--helper')
53 if sys.platform == 'win32':
54 scripts.append('win32/cola')
55 scripts.append('win32/dirname')
56 scripts.append('win32/py2exe-setup.py')
57 scripts.append('win32/py2exe-setup.cmd')
59 setup(name = 'git-cola',
60 version = version.version(),
61 license = 'GPLv2',
62 author = 'David Aguilar and contributors',
63 author_email = 'davvid@gmail.com',
64 url = 'http://cola.tuxfamily.org/',
65 description = 'git-cola',
66 long_description = 'A highly caffeinated Git GUI',
67 scripts = scripts,
68 packages = [],
69 data_files = cola_data_files())
72 def cola_data_files():
73 data = [_app_path('share/git-cola/qm', '*.qm'),
74 _app_path('share/git-cola/icons', '*.png'),
75 _app_path('share/git-cola/icons', '*.svg'),
76 _app_path('share/git-cola/styles', '*.qss'),
77 _app_path('share/git-cola/styles/images', '*.png'),
78 _app_path('share/applications', '*.desktop'),
79 _app_path('share/doc/git-cola', '*.txt'),
80 _lib_path('cola/*.py'),
81 _lib_path('cola/models/*.py'),
82 _lib_path('cola/controllers/*.py'),
83 _lib_path('cola/views/*.py'),
84 _lib_path('jsonpickle/*.py'),
85 _lib_path('simplejson/*.py')]
87 if sys.platform == 'darwin':
88 data.append(_app_path('share/git-cola/bin', 'ssh-askpass-darwin'))
89 else:
90 data.append(_app_path('share/git-cola/bin', 'ssh-askpass'))
91 return data
93 def _lib_path(entry):
94 dirname = os.path.dirname(entry)
95 app_dir = os.path.join('share/git-cola/lib', dirname)
96 return (app_dir, glob(entry))
99 def _app_path(dirname, entry):
100 return (dirname, glob(os.path.join(dirname, entry)))
103 def _check_python_version():
104 """Check the minimum Python version
106 pyver = '.'.join(map(lambda x: str(x), sys.version_info))
107 if not version.check('python', pyver):
108 print >> sys.stderr, ('Python version %s or newer required. '
109 'Found %s' % (version.get('python'), pyver))
110 sys.exit(1)
113 def _check_git_version(git_ver):
114 """Check the minimum GIT version
116 if not version.check('git', git_ver):
117 print >> sys.stderr, ('GIT version %s or newer required. '
118 'Found %s' % (version.get('git'), git_ver))
119 sys.exit(1)
121 def _check_pyqt_version():
122 """Check the minimum PyQt version
124 has_pyqt = False
125 pyqtver = 'None'
126 try:
127 from PyQt4 import QtCore
128 pyqtver = QtCore.PYQT_VERSION_STR
129 if version.check('pyqt', pyqtver):
130 return
131 except ImportError:
132 pass
133 print >> sys.stderr, ('PyQt4 version %s or newer required. '
134 'Found %s' % (version.get('pyqt'), pyqtver))
135 sys.exit(1)
138 def _dirty(src, dst):
139 if not os.path.exists(dst):
140 return True
141 srcstat = os.stat(src)
142 dststat = os.stat(dst)
143 return srcstat[stat.ST_MTIME] > dststat[stat.ST_MTIME]
146 def _build_translations():
147 print 'running build_translations'
148 sources = glob(resources.share('po', '*.po'))
149 sources = glob('share/git-cola/po/*.po')
150 for src in sources:
151 dst = resources.qm(os.path.basename(src)[:-3])
152 if _dirty(src, dst):
153 print '\tmsgfmt --qt %s -o %s' % (src, dst)
154 utils.run_cmd(['msgfmt', '--qt', src, '-o', dst])
156 def _run_cmd(cmd):
157 """Runs a command and returns its output."""
158 pipe = os.popen(cmd)
159 contents = core.read_nointr(pipe).strip()
160 pipe.close()
161 return contents
164 if __name__ == '__main__':
165 main()