summaryrefslogtreecommitdiff
path: root/subversion/bindings/ctypes-python/setup.py
blob: ca9ffa81d62992b70b7db341742dea16903c723a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env python
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.  The ASF licenses this file
#    to you under the Apache License, Version 2.0 (the
#    "License"); you may not use this file except in compliance
#    with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing,
#    software distributed under the License is distributed on an
#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#    KIND, either express or implied.  See the License for the
#    specific language governing permissions and limitations
#    under the License.

import errno, os, re, sys, tempfile

from distutils import log
from distutils.cmd import Command
from distutils.command.build import build as _build
from distutils.command.clean import clean as _clean
from distutils.core import setup
from distutils.dir_util import remove_tree
from distutils.errors import DistutilsExecError
from distutils.errors import DistutilsOptionError

from glob import glob

class clean(_clean):
  """Special distutils command for cleaning the Subversion ctypes bindings."""
  description = "clean the Subversion ctypes Python bindings"

  def initialize_options (self):
    _clean.initialize_options(self)

  # initialize_options()

  def finalize_options (self):
    _clean.finalize_options(self)

  # finalize_options()

  def run(self):
    functions_py = os.path.join(os.path.dirname(__file__), "csvn", "core",
                                "functions.py")
    functions_pyc = os.path.join(os.path.dirname(__file__), "csvn", "core",
                                 "functions.pyc")
    svn_all_py = os.path.join(os.path.dirname(__file__), "svn_all.py")
    svn_all2_py = os.path.join(os.path.dirname(__file__), "svn_all2.py")

    for f in (functions_py, functions_pyc, svn_all_py, svn_all2_py):
      if os.path.exists(f):
        log.info("removing '%s'", os.path.normpath(f))

        if not self.dry_run:
          os.remove(f)
      else:
        log.debug("'%s' does not exist -- can't clean it", os.path.normpath(f))

    # Run standard clean command
    _clean.run(self)

  # run()

# class clean

class build(_build):
  """Special distutils command for building the Subversion ctypes bindings."""

  description = "build the Subversion ctypes Python bindings"

  _build.user_options.append(("apr=", None, "full path to where apr is "
                              "installed or the full path to the apr-1-config or "
                              "apr-config file"))
  _build.user_options.append(("apr-util=", None, "full path to where apr-util "
                              "is installed or the full path to the apu-1-config or "
                              "apu-config file"))
  _build.user_options.append(("subversion=", None, "full path to where "
                              "Subversion is installed"))
  _build.user_options.append(("svn-headers=", None, "Full path to the "
                              "Subversion header files, if they are not in a "
                              "standard location"))
  _build.user_options.append(("ctypesgen=", None, "full path to where ctypesgen "
                              "is installed, to the ctypesgen source tree or "
                              "the full path to ctypesgen.py"))
  _build.user_options.append(("cppflags=", None, "extra flags to pass to the c "
                              "preprocessor"))
  _build.user_options.append(("ldflags=", None, "extra flags to pass to the "
                              "ctypesgen linker"))
  _build.user_options.append(("lib-dirs=", None, "colon-delimited list of paths "
                              "to append to the search path"))
  _build.user_options.append(("save-preprocessed-headers=", None, "full path to "
                              "where to save the preprocessed headers"))

  def initialize_options (self):
    _build.initialize_options(self)
    self.apr = None
    self.apr_util = None
    self.ctypesgen = None
    self.subversion = None
    self.svn_headers = None
    self.cppflags = ""
    self.ldflags = ""
    self.lib_dirs = None
    self.save_preprocessed_headers = None

  # initialize_options()

  def finalize_options (self):
    _build.finalize_options(self)

    # Distutils doesn't appear to like when you have --dry-run after the build
    # build command so fail out if this is the case.
    if self.dry_run != self.distribution.dry_run:
      raise DistutilsOptionError("The --dry-run flag must be specified " \
                                 "before the 'build' command")

  # finalize_options()

  ##############################################################################
  # Get build configuration
  ##############################################################################
  def get_build_config (self):
    flags = []
    ldflags = []
    library_path = []
    ferr = None
    apr_include_dir = None

    fout = self.run_cmd("%s --includes --cppflags" % self.apr_config)
    if fout:
      flags = fout.split()
      apr_prefix = self.run_cmd("%s --prefix" % self.apr_config)
      apr_prefix = apr_prefix.strip()
      apr_include_dir = self.run_cmd("%s --includedir" % self.apr_config).strip()
      apr_version = self.run_cmd("%s --version" % self.apr_config).strip()
      cpp  = self.run_cmd("%s --cpp" % self.apr_config).strip()
      fout = self.run_cmd("%s --ldflags --link-ld" % self.apr_config)
      if fout:
        ldflags = fout.split()
      else:
        log.error(ferr)
        raise DistutilsExecError("Problem running '%s'.  Check the output " \
                                 "for details" % self.apr_config)

      fout = self.run_cmd("%s --includes" % self.apu_config)
      if fout:
        flags += fout.split()
        fout = self.run_cmd("%s --ldflags --link-ld" % self.apu_config)
        if fout:
          ldflags += fout.split()
        else:
          log.error(ferr)
          raise DistutilsExecError("Problem running '%s'.  Check the output " \
                                   "for details" % self.apr_config)

        subversion_prefixes = [
          self.subversion,
          "/usr/local",
          "/usr"
          ]

        if self.subversion != "/usr":
          ldflags.append("-L%s/lib" % self.subversion)
        if self.svn_include_dir[-18:] == "subversion/include":
          flags.append("-Isubversion/include")
        else:
          flags.append("-I%s" % self.svn_include_dir)

        # List the libraries in the order they should be loaded
        libraries = [
          "svn_subr-1",
          "svn_diff-1",
          "svn_delta-1",
          "svn_fs-1",
          "svn_repos-1",
          "svn_wc-1",
          "svn_ra-1",
          "svn_client-1",
          ]

        for lib in libraries:
          ldflags.append("-l%s" % lib)

        if apr_prefix != '/usr':
          library_path.append("%s/lib" % apr_prefix)
          if self.subversion != '/usr' and self.subversion != apr_prefix:
            library_path.append("%s/lib" % self.subversion)

        return (apr_prefix, apr_include_dir, cpp + " " + self.cppflags,
                " ".join(ldflags) + " " + self.ldflags, " ".join(flags),
                ":".join(library_path))

  # get_build_config()

  ##############################################################################
  # Build csvn/core/functions.py
  ##############################################################################
  def build_functions_py(self):
    (apr_prefix, apr_include_dir, cpp, ldflags, flags,
     library_path) = self.get_build_config()
    cwd = os.getcwd()
    if self.svn_include_dir[-18:] == "subversion/include":
      includes = ('subversion/include/svn_*.h '
                  '%s/ap[ru]_*.h' % apr_include_dir)
      cmd = ["%s %s --cpp '%s %s' %s "
             "%s -o subversion/bindings/ctypes-python/svn_all.py "
             "--no-macro-warnings --strip-build-path=%s" % (sys.executable,
                                                            self.ctypesgen_py, cpp,
                                                            flags, ldflags,
                                                            includes, self.svn_include_dir[:-19])]
      os.chdir(self.svn_include_dir[:-19])
    else:
      includes = ('%s/svn_*.h '
                  '%s/ap[ru]_*.h' % (self.svn_include_dir, apr_include_dir))
      cmd = ["%s %s --cpp '%s %s' %s "
             "%s -o svn_all.py --no-macro-warnings" % (sys.executable,
                                                       self.ctypesgen_py, cpp,
                                                       flags, ldflags,
                                                       includes)]
    if self.lib_dirs:
      cmd.extend('-R ' + x for x in self.lib_dirs.split(":"))
    cmd = ' '.join(cmd)

    if self.save_preprocessed_headers:
      cmd += " --save-preprocessed-headers=%s" % \
          os.path.abspath(self.save_preprocessed_headers)

    if self.verbose or self.dry_run:
      status = self.execute(os.system, (cmd,), cmd)
    else:
      f = os.popen(cmd, 'r')
      f.read() # Required to avoid the 'Broken pipe' error.
      status = f.close() # None is returned for the usual 0 return code

    os.chdir(cwd)

    if os.name == "posix" and status and status != 0:
      if os.WIFEXITED(status):
        status = os.WEXITSTATUS(status)
        if status != 0:
          sys.exit(status)
        elif os.WIFSIGNALED(status):
          log.error("ctypesgen.py killed with signal %d" % os.WTERMSIG(status))
          sys.exit(2)
        elif os.WIFSTOPPED(status):
          log.error("ctypesgen.py stopped with signal %d" % os.WSTOPSIG(status))
          sys.exit(2)
        else:
          log.error("ctypesgen.py exited with invalid status %d", status)
          sys.exit(2)

    if not self.dry_run:
      r = re.compile(r"(\s+\w+)\.restype = POINTER\(svn_error_t\)")
      out = open("svn_all2.py", "w")
      for line in open("svn_all.py"):
        line = r.sub("\\1.restype = POINTER(svn_error_t)\n\\1.errcheck = _svn_errcheck", line)

        if not line.startswith("FILE ="):
          out.write(line)
      out.close()

    cmd = "cat csvn/core/functions.py.in svn_all2.py > csvn/core/functions.py"
    self.execute(os.system, (cmd,), cmd)

    log.info("Generated csvn/core/functions.py successfully")

  # build_functions_py()

  def run_cmd(self, cmd):
    return os.popen(cmd).read()

  # run_cmd()

  def validate_options(self):
    # Validate apr
    if not self.apr:
      self.apr = find_in_path('apr-1-config')

      if not self.apr:
        self.apr = find_in_path('apr-config')

      if self.apr:
        log.info("Found %s" % self.apr)
      else:
        raise DistutilsOptionError("Could not find apr-1-config or " \
                                   "apr-config.  Please rerun with the " \
                                   "--apr option.")

    if os.path.exists(self.apr):
      if os.path.isdir(self.apr):
        if os.path.exists(os.path.join(self.apr, "bin", "apr-1-config")):
          self.apr_config = os.path.join(self.apr, "bin", "apr-1-config")
        elif os.path.exists(os.path.join(self.apr, "bin", "apr-config")):
          self.apr_config = os.path.join(self.apr, "bin", "apr-config")
        else:
          self.apr_config = None
      elif os.path.basename(self.apr) in ("apr-1-config", "apr-config"):
        self.apr_config = self.apr
      else:
        self.apr_config = None
    else:
      self.apr_config = None

    if not self.apr_config:
      raise DistutilsOptionError("The --apr option is not valid.  It must " \
                                 "point to a valid apr installation or " \
                                 "to either the apr-1-config file or the " \
                                 "apr-config file")

    # Validate apr-util
    if not self.apr_util:
      self.apr_util = find_in_path('apu-1-config')

      if not self.apr_util:
        self.apr_util = find_in_path('apu-config')

      if self.apr_util:
        log.info("Found %s" % self.apr_util)
      else:
        raise DistutilsOptionError("Could not find apu-1-config or " \
                                   "apu-config.  Please rerun with the " \
                                   "--apr-util option.")

    if os.path.exists(self.apr_util):
      if os.path.isdir(self.apr_util):
        if os.path.exists(os.path.join(self.apr_util, "bin", "apu-1-config")):
          self.apu_config = os.path.join(self.apr_util, "bin", "apu-1-config")
        elif os.path.exists(os.path.join(self.apr_util, "bin", "apu-config")):
          self.apu_config = os.path.join(self.apr_util, "bin", "apu-config")
        else:
          self.apu_config = None
      elif os.path.basename(self.apr_util) in ("apu-1-config", "apu-config"):
        self.apu_config = self.apr_util
      else:
        self.apu_config = None
    else:
      self.apu_config = None

    if not self.apu_config:
      raise DistutilsOptionError("The --apr-util option is not valid.  It " \
                                 "must point to a valid apr-util " \
                                 "installation or to either the apu-1-config " \
                                 "file or the apu-config file")

    # Validate subversion
    if not self.subversion:
      self.subversion = find_in_path('svn')

      if self.subversion:
        log.info("Found %s" % self.subversion)
        # Get the installation root instead of path to 'svn'
        self.subversion = os.path.normpath(os.path.join(self.subversion, "..",
                                                        ".."))
      else:
        raise DistutilsOptionError("Could not find Subversion.  Please rerun " \
                                   "with the --subversion option.")

    # Validate svn-headers, if present
    if self.svn_headers:
      if os.path.isdir(self.svn_headers):
        if os.path.exists(os.path.join(self.svn_headers, "svn_client.h")):
          self.svn_include_dir = self.svn_headers
        elif os.path.exists(os.path.join(self.svn_headers, "subversion-1",
                                         "svn_client.h")):
          self.svn_include_dir = os.path.join(self.svn_headers, "subversion-1")
        else:
          self.svn_include_dir = None
      else:
        self.svn_include_dir = None
    elif os.path.exists(os.path.join(self.subversion, "include",
                                     "subversion-1")):
      self.svn_include_dir = "%s/include/subversion-1" % self.subversion
    else:
      self.svn_include_dir = None

    if not self.svn_include_dir:
      msg = ""

      if self.svn_headers:
        msg = "The --svn-headers options is not valid.  It must point to " \
              "either a Subversion include directory or the Subversion " \
              "include/subversion-1 directory."
      else:
        msg = "The --subversion option is not valid. " \
              "Could not locate %s/include/" \
              "subversion-1/svn_client.h" % self.subversion

      raise DistutilsOptionError(msg)

    # Validate ctypesgen
    if not self.ctypesgen:
      self.ctypesgen = find_in_path('ctypesgen.py')

      if self.ctypesgen:
        log.info("Found %s" % self.ctypesgen)
      else:
        raise DistutilsOptionError("Could not find ctypesgen.  Please rerun " \
                                   "with the --ctypesgen option.")

    if os.path.exists(self.ctypesgen):
      if os.path.isdir(self.ctypesgen):
        if os.path.exists(os.path.join(self.ctypesgen, "ctypesgen.py")):
          self.ctypesgen_py = os.path.join(self.ctypesgen, "ctypesgen.py")
        elif os.path.exists(os.path.join(self.ctypesgen, "bin",
                                         "ctypesgen.py")):
          self.ctypesgen_py = os.path.join(self.ctypesgen, "bin",
                                           "ctypesgen.py")
        else:
          self.ctypesgen_py = None
      elif os.path.basename(self.ctypesgen) == "ctypesgen.py":
          self.ctypesgen_py = self.ctypesgen
      else:
        self.ctypesgen_py = None
    else:
      self.ctypesgen_py = None

    if not self.ctypesgen_py:
      raise DistutilsOptionError("The --ctypesgen option is not valid.  It " \
                                 "must point to a valid ctypesgen " \
                                 "installation, a ctypesgen source tree or " \
                                 "to the ctypesgen.py script")

  # validate_functions()

  def run (self):
    # We only want to build if functions.py is not present.
    if not os.path.exists(os.path.join(os.path.dirname(__file__),
                                       "csvn", "core",
                                       "functions.py")):
      if 'build' not in self.distribution.commands:
        raise DistutilsOptionError("You must run 'build' explicitly before " \
                                   "you can proceed")

      # Validate the command line options
      self.validate_options()

      # Generate functions.py
      self.build_functions_py()
    else:
      log.info("csvn/core/functions.py was not regenerated (output up-to-date)")

    # Run the standard build command.
    _build.run(self)

  # run()

# class build

def find_in_path(file):
  paths = []
  if os.environ.has_key('PATH'):
    paths = os.environ['PATH'].split(os.pathsep)

  for path in paths:
    file_path = os.path.join(path, file)

    if os.path.exists(file_path):
      # Return the path to the first existing file found in PATH
      return os.path.abspath(file_path)

  return None
# find_in_path()

setup(cmdclass={'build': build, 'clean': clean},
      name='svn-ctypes-python-bindings',
      version='0.1',
      description='Python bindings for the Subversion version control system.',
      author='The Subversion Team',
      author_email='dev@subversion.apache.org',
      url='http://subversion.apache.org',
      packages=['csvn', 'csvn.core', 'csvn.ext'],
      license='Apache License, Version 2.0',
     )

# TODO: We need to create our own bdist_rpm implementation so that we can pass
#       our required arguments to the build command being called by bdist_rpm.