##// END OF EJS Templates
setup: cleanup coding style
Martin Geisler -
r10000:16f49d67 default
parent child Browse files
Show More
@@ -1,286 +1,286
1 1 #!/usr/bin/env python
2 2 #
3 3 # This is the mercurial setup script.
4 4 #
5 5 # 'python setup.py install', or
6 6 # 'python setup.py --help' for more options
7 7
8 8 import sys
9 9 if not hasattr(sys, 'version_info') or sys.version_info < (2, 4, 0, 'final'):
10 10 raise SystemExit("Mercurial requires Python 2.4 or later.")
11 11
12 12 # Solaris Python packaging brain damage
13 13 try:
14 14 import hashlib
15 15 sha = hashlib.sha1()
16 16 except:
17 17 try:
18 18 import sha
19 19 except:
20 20 raise SystemExit(
21 21 "Couldn't import standard hashlib (incomplete Python install).")
22 22
23 23 try:
24 24 import zlib
25 25 except:
26 26 raise SystemExit(
27 27 "Couldn't import standard zlib (incomplete Python install).")
28 28
29 29 import os, subprocess, time
30 30 import shutil
31 31 import tempfile
32 32 from distutils.core import setup, Extension
33 33 from distutils.dist import Distribution
34 34 from distutils.command.install_data import install_data
35 35 from distutils.command.build import build
36 36 from distutils.command.build_py import build_py
37 37 from distutils.spawn import spawn, find_executable
38 38 from distutils.ccompiler import new_compiler
39 39
40 40 extra = {}
41 41 scripts = ['hg']
42 42 if os.name == 'nt':
43 43 scripts.append('contrib/win32/hg.bat')
44 44
45 45 # simplified version of distutils.ccompiler.CCompiler.has_function
46 46 # that actually removes its temporary files.
47 def has_function(cc, funcname):
47 def hasfunction(cc, funcname):
48 48 tmpdir = tempfile.mkdtemp(prefix='hg-install-')
49 49 devnull = oldstderr = None
50 50 try:
51 51 try:
52 52 fname = os.path.join(tmpdir, 'funcname.c')
53 53 f = open(fname, 'w')
54 54 f.write('int main(void) {\n')
55 55 f.write(' %s();\n' % funcname)
56 56 f.write('}\n')
57 57 f.close()
58 58 # Redirect stderr to /dev/null to hide any error messages
59 59 # from the compiler.
60 60 # This will have to be changed if we ever have to check
61 61 # for a function on Windows.
62 62 devnull = open('/dev/null', 'w')
63 63 oldstderr = os.dup(sys.stderr.fileno())
64 64 os.dup2(devnull.fileno(), sys.stderr.fileno())
65 65 objects = cc.compile([fname], output_dir=tmpdir)
66 66 cc.link_executable(objects, os.path.join(tmpdir, "a.out"))
67 67 except:
68 68 return False
69 69 return True
70 70 finally:
71 71 if oldstderr is not None:
72 72 os.dup2(oldstderr, sys.stderr.fileno())
73 73 if devnull is not None:
74 74 devnull.close()
75 75 shutil.rmtree(tmpdir)
76 76
77 77 # py2exe needs to be installed to work
78 78 try:
79 79 import py2exe
80 80
81 81 # Help py2exe to find win32com.shell
82 82 try:
83 83 import modulefinder
84 84 import win32com
85 85 for p in win32com.__path__[1:]: # Take the path to win32comext
86 86 modulefinder.AddPackagePath("win32com", p)
87 87 pn = "win32com.shell"
88 88 __import__(pn)
89 89 m = sys.modules[pn]
90 90 for p in m.__path__[1:]:
91 91 modulefinder.AddPackagePath(pn, p)
92 92 except ImportError:
93 93 pass
94 94
95 95 extra['console'] = ['hg']
96 96
97 97 except ImportError:
98 98 pass
99 99
100 100 def runcmd(cmd, env):
101 101 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
102 102 stderr=subprocess.PIPE, env=env)
103 103 out, err = p.communicate()
104 104 # If root is executing setup.py, but the repository is owned by
105 105 # another user (as in "sudo python setup.py install") we will get
106 106 # trust warnings since the .hg/hgrc file is untrusted. That is
107 107 # fine, we don't want to load it anyway.
108 108 err = [e for e in err.splitlines()
109 109 if not e.startswith('Not trusting file')]
110 110 if err:
111 111 return ''
112 112 return out
113 113
114 114 version = ''
115 115
116 116 if os.path.isdir('.hg'):
117 117 # Execute hg out of this directory with a custom environment which
118 118 # includes the pure Python modules in mercurial/pure. We also take
119 119 # care to not use any hgrc files and do no localization.
120 120 pypath = ['mercurial', os.path.join('mercurial', 'pure')]
121 121 env = {'PYTHONPATH': os.pathsep.join(pypath),
122 122 'HGRCPATH': '',
123 123 'LANGUAGE': 'C'}
124 124 if 'LD_LIBRARY_PATH' in os.environ:
125 125 env['LD_LIBRARY_PATH'] = os.environ['LD_LIBRARY_PATH']
126 126 if 'SystemRoot' in os.environ:
127 127 # Copy SystemRoot into the custom environment for Python 2.6
128 128 # under Windows. Otherwise, the subprocess will fail with
129 129 # error 0xc0150004. See: http://bugs.python.org/issue3440
130 130 env['SystemRoot'] = os.environ['SystemRoot']
131 131 cmd = [sys.executable, 'hg', 'id', '-i', '-t']
132 132 l = runcmd(cmd, env).split()
133 133 while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
134 134 l.pop()
135 135 if len(l) > 1: # tag found
136 136 version = l[-1]
137 137 if l[0].endswith('+'): # propagate the dirty status to the tag
138 138 version += '+'
139 139 elif len(l) == 1: # no tag found
140 140 cmd = [sys.executable, 'hg', 'parents', '--template',
141 141 '{latesttag}+{latesttagdistance}-']
142 142 version = runcmd(cmd, env) + l[0]
143 143 if version.endswith('+'):
144 144 version += time.strftime('%Y%m%d')
145 145 elif os.path.exists('.hg_archival.txt'):
146 146 kw = dict([t.strip() for t in l.split(':', 1)]
147 147 for l in open('.hg_archival.txt'))
148 148 if 'tag' in kw:
149 149 version = kw['tag']
150 150 elif 'latesttag' in kw:
151 151 version = '%(latesttag)s+%(latesttagdistance)s-%(node).12s' % kw
152 152 else:
153 153 version = kw.get('node', '')[:12]
154 154
155 155 if version:
156 156 f = open("mercurial/__version__.py", "w")
157 157 f.write('# this file is autogenerated by setup.py\n')
158 158 f.write('version = "%s"\n' % version)
159 159 f.close()
160 160
161 161
162 162 try:
163 163 from mercurial import __version__
164 164 version = __version__.version
165 165 except ImportError:
166 166 version = 'unknown'
167 167
168 class build_mo(build):
168 class hgbuildmo(build):
169 169
170 170 description = "build translations (.mo files)"
171 171
172 172 def run(self):
173 173 if not find_executable('msgfmt'):
174 174 self.warn("could not find msgfmt executable, no translations "
175 175 "will be built")
176 176 return
177 177
178 178 podir = 'i18n'
179 179 if not os.path.isdir(podir):
180 180 self.warn("could not find %s/ directory" % podir)
181 181 return
182 182
183 183 join = os.path.join
184 184 for po in os.listdir(podir):
185 185 if not po.endswith('.po'):
186 186 continue
187 187 pofile = join(podir, po)
188 188 modir = join('locale', po[:-3], 'LC_MESSAGES')
189 189 mofile = join(modir, 'hg.mo')
190 190 mobuildfile = join('mercurial', mofile)
191 191 cmd = ['msgfmt', '-v', '-o', mobuildfile, pofile]
192 192 if sys.platform != 'sunos5':
193 193 # msgfmt on Solaris does not know about -c
194 194 cmd.append('-c')
195 195 self.mkpath(join('mercurial', modir))
196 196 self.make_file([pofile], mobuildfile, spawn, (cmd,))
197 197
198 # Insert build_mo first so that files in mercurial/locale/ are found
198 # Insert hgbuildmo first so that files in mercurial/locale/ are found
199 199 # when build_py is run next.
200 200 build.sub_commands.insert(0, ('build_mo', None))
201 201
202 202 Distribution.pure = 0
203 203 Distribution.global_options.append(('pure', None, "use pure (slow) Python "
204 204 "code instead of C extensions"))
205 205
206 class hg_build_py(build_py):
206 class hgbuildpy(build_py):
207 207
208 208 def finalize_options(self):
209 209 build_py.finalize_options(self)
210 210
211 211 if self.distribution.pure:
212 212 if self.py_modules is None:
213 213 self.py_modules = []
214 214 for ext in self.distribution.ext_modules:
215 215 if ext.name.startswith("mercurial."):
216 216 self.py_modules.append("mercurial.pure.%s" % ext.name[10:])
217 217 self.distribution.ext_modules = []
218 218
219 219 def find_modules(self):
220 220 modules = build_py.find_modules(self)
221 221 for module in modules:
222 222 if module[0] == "mercurial.pure":
223 223 if module[1] != "__init__":
224 224 yield ("mercurial", module[1], module[2])
225 225 else:
226 226 yield module
227 227
228 cmdclass = {'build_mo': build_mo,
229 'build_py': hg_build_py}
228 cmdclass = {'build_mo': hgbuildmo,
229 'build_py': hgbuildpy}
230 230
231 ext_modules=[
231 extmodules = [
232 232 Extension('mercurial.base85', ['mercurial/base85.c']),
233 233 Extension('mercurial.bdiff', ['mercurial/bdiff.c']),
234 234 Extension('mercurial.diffhelpers', ['mercurial/diffhelpers.c']),
235 235 Extension('mercurial.mpatch', ['mercurial/mpatch.c']),
236 236 Extension('mercurial.parsers', ['mercurial/parsers.c']),
237 237 Extension('mercurial.osutil', ['mercurial/osutil.c']),
238 238 ]
239 239
240 240 packages = ['mercurial', 'mercurial.hgweb', 'hgext', 'hgext.convert',
241 241 'hgext.highlight', 'hgext.zeroconf', ]
242 242
243 243 if sys.platform == 'linux2' and os.uname()[2] > '2.6':
244 244 # The inotify extension is only usable with Linux 2.6 kernels.
245 245 # You also need a reasonably recent C library.
246 246 cc = new_compiler()
247 if has_function(cc, 'inotify_add_watch'):
248 ext_modules.append(Extension('hgext.inotify.linux._inotify',
247 if hasfunction(cc, 'inotify_add_watch'):
248 extmodules.append(Extension('hgext.inotify.linux._inotify',
249 249 ['hgext/inotify/linux/_inotify.c']))
250 250 packages.extend(['hgext.inotify', 'hgext.inotify.linux'])
251 251
252 252 packagedata = {'mercurial': ['locale/*/LC_MESSAGES/hg.mo',
253 253 'help/*.txt']}
254 254
255 255 def ordinarypath(p):
256 256 return p and p[0] != '.' and p[-1] != '~'
257 257
258 258 for root in ('templates', ):
259 259 for curdir, dirs, files in os.walk(os.path.join('mercurial', root)):
260 260 curdir = curdir.split(os.sep, 1)[1]
261 261 dirs[:] = filter(ordinarypath, dirs)
262 262 for f in filter(ordinarypath, files):
263 263 f = os.path.join(curdir, f)
264 264 packagedata['mercurial'].append(f)
265 265
266 266 datafiles = []
267 267
268 268 setup(name='mercurial',
269 269 version=version,
270 270 author='Matt Mackall',
271 271 author_email='mpm@selenic.com',
272 272 url='http://mercurial.selenic.com/',
273 273 description='Scalable distributed SCM',
274 274 license='GNU GPL',
275 275 scripts=scripts,
276 276 packages=packages,
277 ext_modules=ext_modules,
277 ext_modules=extmodules,
278 278 data_files=datafiles,
279 279 package_data=packagedata,
280 280 cmdclass=cmdclass,
281 281 options=dict(py2exe=dict(packages=['hgext', 'email']),
282 282 bdist_mpkg=dict(zipdist=True,
283 283 license='COPYING',
284 284 readme='contrib/macosx/Readme.html',
285 285 welcome='contrib/macosx/Welcome.html')),
286 286 **extra)
General Comments 0
You need to be logged in to leave comments. Login now