DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (35de27d72a9a)

VCS Links

Line Code
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import sys
import os
import re
import subprocess
import re

from distutils.core import setup, Extension, Command
from distutils.spawn import find_executable, spawn
from distutils import log
from distutils.filelist import FileList
from distutils.util import subst_vars, change_root
from distutils.command.build_py import build_py as _build_py
from distutils.command.sdist import sdist as _sdist
from sphinx.setup_command import BuildDoc as SphinxBuildDoc

name = 'python-nss'
version = "1.0.1"
release = version

doc_manifest = [
    [['include README LICENSE* doc/ChangeLog',
      'recursive-include doc *.py *.txt',
      'prune doc/examples/pki',
      'prune doc/sphinx'],
     [('^doc/', '')], None],
    [['recursive-include test run_tests setup_certs.py test_*.py util.py *.txt',
      'prune test/pki'],
     None , None],
    [['recursive-include lib *.py *.txt',],
     [('^lib/', '')] , 'examples'],
    [['recursive-include build/sphinx/html *'],
     [('^build/sphinx/', 'api/')], None],
]

def update_version():
    """If the version string in __init__.py doesn't match the current
    version then edit the file replacing the version string
    with the current version."""

    version_file = 'src/__init__.py'
    tmp_file = 'src/__init__.tmp'
    version_re = re.compile("^\s*__version__\s*=\s*['\"]([^'\"]*)['\"]")
    need_to_update = False
    version_found = False
    with open(tmp_file, "w") as t:
        with open(version_file) as v:
            for line in v.readlines():
                match = version_re.search(line)
                if match:
                    version_found = True
                    file_version = match.group(1)
                    if file_version != version:
                        need_to_update = True
                        t.write("__version__ = '%s'\n" % version)
                else:
                    t.write(line)
        if not version_found:
            need_to_update = True
            t.write("__version__ = '%s'\n" % version)

    if need_to_update:
        print("Updating version in \"%s\" to \"%s\"" % (version_file, version))
        os.rename(tmp_file, version_file)
    else:
        os.unlink(tmp_file)

def find_include_dir(dir_names, include_files, include_roots=None):
    '''
    Locate an include directory on the system which contains the specified include files.
    You must provide a list of directory basenames to search. You may optionally provide
    a list of include roots. The search proceeds by iterating over each root and appending
    each directory basename to it. If the resulting directory path contains all the include
    files that directory is returned. If no directory is found containing all the include
    files a ValueError is raised.
    '''
    if not include_roots:
        include_roots = ['/usr/include', '/usr/local/include']
    if len(dir_names) == 0:
        raise ValueError("directory search list is empty")
    if len(include_files) == 0:
        raise ValueError("header file list is empty")
    for include_root in include_roots:
        for dir_name in dir_names:
            include_dir = os.path.join(include_root, dir_name)
            if os.path.isdir(include_dir):
                for include_file in include_files:
                    found = True
                    file_path = os.path.join(include_dir, include_file)
                    if not os.path.exists(file_path):
                        found = False
                        break
                if found:
                    return include_dir
    raise ValueError("unable to locate include directory containing header files %s" % include_files)

class BuildPy(_build_py):
    """Specialized Python source builder."""

    def run(self):
        update_version()
        _build_py.run(self)


class SDist(_sdist):
    """Specialized Python source builder."""

    def run(self):
        update_version()
        _sdist.run(self)


class BuildDoc(Command):
    description = 'generate documentation'
    user_options = [('docdir=', 'd', "directory root for documentation"),
                   ]

    def has_sphinx (self):
        if find_executable('sphinx-build'):
            return True
        else:
            return False

    sub_commands = [('build_sphinx', has_sphinx),
                   ]

    def initialize_options(self):
        self.build_base = None
        self.build_lib = None
        self.docdir = None

    def finalize_options(self):
        self.set_undefined_options('build',
                                   ('build_base', 'build_base'),
                                   ('build_lib', 'build_lib'))
        if self.docdir is None:
            self.docdir = change_root(self.build_base, 'doc')

    def run(self):
        self.run_command('build')
        # Add build directory to Python path so doc builder can import
        # in-tree built modules
        sys.path.insert(0, self.build_lib)
        for cmd_name in self.get_sub_commands():
            self.run_command(cmd_name)
        # Remove the build directory from Python path
        del sys.path[0]



class InstallDoc(Command):
    description = 'install documentation'
    user_options = [('docdir=', 'd', "directory root for documentation"),
                    ('root=', None,
                     "install everything relative to this alternate root directory"),
                    ('skip-build', None,
                     "skip rebuilding everything (for testing/debugging)"),
                   ]

    def initialize_options(self):
        self.root = None
        self.build_base = None
        self.docdir = None
        self.skip_build = False

    def finalize_options(self):
        self.set_undefined_options('install',
                                   ('root', 'root'))
        self.set_undefined_options('build',
                                   ('build_base', 'build_base'))

        if self.docdir is None:
            self.docdir = change_root(self.build_base, 'doc')

    def run(self):
        if not self.skip_build:
            self.run_command('build_doc')


        dst_root = change_root(self.root, self.docdir)
        self.copy_transformed_tree(doc_manifest,
                                   dst_root=dst_root,
                                   substitutions={'docdir' : self.docdir})

    def copy_transformed_tree(self, install_specs, dst_root=None, src_root=None, substitutions={}):
        """
        Copy parts of a source tree to a destination tree with a
        different tree structure and/or names.

        The basic idea: given a set of source files, copy them to a
        destination directory, let's call this operation an
        install_spec. A sequence of install_spec's allows one to build
        up the destrination tree in any structure desired.

        Each install_spec consists of 3 components
        (manifest_template, dst_xforms, dst_dir):

        The manifest_template is a sequence where each item is identical
        to a line in the MANIFEST.in template described in distutils. This
        gives you ability to easily specify a set of source files in a
        compact abstract manner (with recursion, exclusion, etc.) The
        manifest_template yields a sequence of source paths.

        dst_xforms is a sequence of regular expression substitutions
        applied to the each source path to yield a rewritten destination
        path. Each transform is expressed as a two-valued sequence
        (pattern, replacement)

        dst_dir is a destination directory where the destinations paths
        are written to. dst_dir is always relative to the dst_root.

        All input may be parametrized via variable substitutions
        supplied by a substitution dict. Any use of $name will cause
        name to be looked up first in the substitution dict and then
        if its not found there in the enviorment. If found it will be
        replaced with it's value.

        The pseudo code algorithm for processing an install_spec is:

        substitute all variables in manifest template
        src_list = evaluate manifest template
        for each src_path in src_list:
            dst_path = src_path
            for each xform in dst_xform:
                apply xform to dst_path
            copy src_root+src_path to dst_root+dest_dir+dest_path

        This process is repeated for each install spec. The src_root and
        dst_root are also subject to variable substitution.


        Examples:

        Copy all text files in build/doc to doc:

            copy_transformed_tree([[["include build/doc *.txt"], None, 'doc']])

        Copy all html files found under build to doc/html and change the extension from
        .html to .htm

            copy_transformed_tree([[["include build *.html"], [('\.html$','.htm')], 'doc']])

    """

        if src_root is not None: src_root = subst_vars(src_root, substitutions)
        if dst_root is not None: dst_root = subst_vars(dst_root, substitutions)

        filelist = FileList()
        if src_root is None:
            filelist.findall()
        else:
            filelist.findall(src_root)

        for manifest_template, dst_xforms, dst_dir in install_specs:
            if dst_dir is not None: dst_dir = subst_vars(dst_dir, substitutions)

            filelist.files = [] # reinitialize to empty

            for line in manifest_template:
                filelist.process_template_line(subst_vars(line, substitutions))

            for src_path in filelist.files:
                dst_path = src_path
                if dst_xforms:
                    for dst_xform in dst_xforms:
                        dst_path = re.sub(dst_xform[0], dst_xform[1], dst_path)
                if dst_dir is not None:
                    dst_path = change_root(dst_dir, dst_path)
                if dst_root is None:
                    full_dst_path = dst_path
                else:
                    full_dst_path = change_root(dst_root, dst_path)
                full_dst_dir = os.path.dirname(full_dst_path)
                self.mkpath(full_dst_dir)
                self.copy_file(src_path, full_dst_path)



#------------------------------------------------------------------------------

def main(argv):

    with open('README') as f:
        long_description = f.read()

    debug_compile_args = ['-O0', '-g']
    extra_compile_args = []
    include_roots = []

    for arg in argv[:]:
        if arg in ('--debug', ):
            print("compiling with debug")
            extra_compile_args += debug_compile_args
            argv.remove(arg)
        if arg in ('-t', '--trace'):
            print("compiling with trace")
            extra_compile_args += ['-DDEBUG']
            argv.remove(arg)
        if arg.startswith('--include-root'):
            include_roots.append(arg.split('--include-root=')[1])
            argv.remove(arg)

    nss_include_dir  = find_include_dir(['nss3', 'nss'],   ['nss.h',  'pk11pub.h'], include_roots=include_roots)
    nspr_include_dir = find_include_dir(['nspr4', 'nspr'], ['nspr.h', 'prio.h'], include_roots=include_roots)

    nss_error_extension = \
        Extension('nss.error',
                  sources            = ['src/py_nspr_error.c'],
                  include_dirs       = [nss_include_dir, nspr_include_dir],
                  depends            = ['src/py_nspr_common.h', 'src/py_nspr_error.h',
                                         'src/NSPRerrs.h', 'src/SSLerrs.h', 'src/SECerrs.h'],
                  libraries          = ['nspr4'],
                  extra_compile_args = extra_compile_args,
                  )

    nss_io_extension = \
        Extension('nss.io',
                  sources            = ['src/py_nspr_io.c'],
                  include_dirs       = [nss_include_dir, nspr_include_dir],
                  depends            = ['src/py_nspr_common.h', 'src/py_nspr_error.h', 'src/py_nspr_io.h'],
                  libraries          = ['nspr4'],
                  extra_compile_args = extra_compile_args,
                  )

    nss_nss_extension = \
        Extension('nss.nss',
                  sources            = ['src/py_nss.c'],
                  include_dirs       = ['src', nss_include_dir, nspr_include_dir],
                  depends            = ['src/py_nspr_common.h', 'src/py_nspr_error.h', 'src/py_nss.h'],
                  libraries          = ['nspr4', 'ssl3', 'nss3', 'smime3'],
                  extra_compile_args = extra_compile_args,
                  )

    nss_ssl_extension = \
        Extension('nss.ssl',
                  sources            = ['src/py_ssl.c'],
                  include_dirs       = ['src', nss_include_dir, nspr_include_dir],
                  depends            = ['src/py_nspr_common.h', 'src/py_nspr_error.h', 'src/py_nspr_io.h',
                                        'src/py_ssl.h', 'src/py_nss.h'],
                  libraries          = ['nspr4', 'ssl3'],
                  extra_compile_args = extra_compile_args,
                  )

          #bug_tracker       = 'https://bugzilla.redhat.com/buglist.cgi?submit&component=python-nss&product=Fedora&classification=Fedora'
          #bug_enter     = 'https://bugzilla.redhat.com/enter_bug.cgi?component=python-nss&product=Fedora&classification=Fedora',
    setup(name             = name,
          version          = version,
          description      = 'Python bindings for Network Security Services (NSS) and Netscape Portable Runtime (NSPR)',
          long_description = long_description,
          author           = 'John Dennis',
          author_email     = 'jdennis@redhat.com',
          maintainer       = 'John Dennis',
          maintainer_email = 'jdennis@redhat.com',
          license          = 'MPLv2.0 or GPLv2+ or LGPLv2+',
          platforms        = 'posix',
          url              = 'http://www.mozilla.org/projects/security/pki/python-nss',
          download_url     = '',
          ext_modules      = [nss_error_extension,
                              nss_io_extension,
                              nss_nss_extension,
                              nss_ssl_extension,
                             ],
          package_dir      = {'nss':'src'},
          packages         = ['nss'],
          cmdclass         = {'build_doc'     : BuildDoc,
                              'build_sphinx'  : SphinxBuildDoc,
                              'install_doc'   : InstallDoc,
                              'build_py'      : BuildPy,
                              'sdist'         : SDist,
                             },
          command_options={
              'build_sphinx': {
                  'project': ('setup.py', name),
                  'version': ('setup.py', version),
                  'release': ('setup.py', release),
                  'source_dir': ('setup.py', 'doc/sphinx/source')}},
    )

    return 0

#------------------------------------------------------------------------------

if __name__ == "__main__":
    sys.exit(main(sys.argv))