diff --git a/pygit2/__init__.py b/pygit2/__init__.py index 48b15c7..ada4aaf 100644 --- a/pygit2/__init__.py +++ b/pygit2/__init__.py @@ -41,7 +41,7 @@ from .remote import Remote, get_credentials from .repository import Repository from .settings import Settings from .utils import to_bytes -from .version import __version__ +from ._utils import __version__ def init_repository(path, bare=False, diff --git a/pygit2/_utils.py b/pygit2/_utils.py new file mode 100644 index 0000000..2d29ada --- /dev/null +++ b/pygit2/_utils.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2010-2014 The pygit2 contributors +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License, version 2, +# as published by the Free Software Foundation. +# +# In addition to the permissions in the GNU General Public License, +# the authors give you unlimited permission to link the compiled +# version of this file into combinations with other programs, +# and to distribute those combinations without any restriction +# coming from the use of this file. (The General Public License +# restrictions do apply in other respects; for example, they cover +# modification of the file, and distribution when not linked into +# a combined executable.) +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. + +""" +This is an special module, it provides stuff used by setup.py and by +pygit2 at run-time. +""" + +# Import from the Standard Library +import inspect +import codecs +import os +from os import getenv +from os.path import abspath, dirname + + +# +# The version number of pygit2 +# +__version__ = '0.21.3' + + +# +# Utility functions to get the paths required for bulding extensions +# +def _get_libgit2_path(): + # LIBGIT2 environment variable takes precedence + libgit2_path = getenv("LIBGIT2") + if libgit2_path is not None: + return libgit2_path + + # Default + if os.name == 'nt': + return '%s\libgit2' % getenv("ProgramFiles") + return '/usr/local' + + +def get_libgit2_paths(): + libgit2_path = _get_libgit2_path() + return ( + os.path.join(libgit2_path, 'bin'), + os.path.join(libgit2_path, 'include'), + getenv('LIBGIT2_LIB', os.path.join(libgit2_path, 'lib')), + ) + + +# +# Loads the cffi extension +# +def get_ffi(): + from cffi import FFI + + ffi = FFI() + + # Load C definitions + dir_path = dirname(abspath(inspect.getfile(inspect.currentframe()))) + decl_path = os.path.join(dir_path, 'decl.h') + with codecs.open(decl_path, 'r', 'utf-8') as header: + ffi.cdef(header.read()) + + # Load extension module + libgit2_bin, libgit2_include, libgit2_lib = get_libgit2_paths() + C = ffi.verify("#include ", modulename='pygit2_cffi', + libraries=["git2"], + include_dirs=[libgit2_include], library_dirs=[libgit2_lib]) + + # Ok + return ffi, C diff --git a/pygit2/ffi.py b/pygit2/ffi.py index 34ff6c1..ae9e12c 100644 --- a/pygit2/ffi.py +++ b/pygit2/ffi.py @@ -28,54 +28,8 @@ # Import from the future from __future__ import absolute_import -# Import from the Standard Library -import inspect -import codecs -import os -from os import getenv -from os.path import abspath, dirname - -# Import from cffi -from cffi import FFI +# Import from pygit2 +from ._utils import get_ffi -def _get_libgit2_path(): - # LIBGIT2 environment variable takes precedence - libgit2_path = getenv("LIBGIT2") - if libgit2_path is not None: - return libgit2_path - - # Default - if os.name == 'nt': - return '%s\libgit2' % getenv("ProgramFiles") - return '/usr/local' - - -def get_libgit2_paths(): - libgit2_path = _get_libgit2_path() - return ( - os.path.join(libgit2_path, 'bin'), - os.path.join(libgit2_path, 'include'), - getenv('LIBGIT2_LIB', os.path.join(libgit2_path, 'lib')), - ) - - -def init_ffi(): - global C, ffi - - ffi = FFI() - - # Load C definitions - dir_path = dirname(abspath(inspect.getfile(inspect.currentframe()))) - decl_path = os.path.join(dir_path, 'decl.h') - with codecs.open(decl_path, 'r', 'utf-8') as header: - ffi.cdef(header.read()) - - # Load extension module - libgit2_bin, libgit2_include, libgit2_lib = get_libgit2_paths() - C = ffi.verify("#include ", modulename='pygit2_cffi', - libraries=["git2"], - include_dirs=[libgit2_include], library_dirs=[libgit2_lib]) - - -init_ffi() +ffi, C = get_ffi() diff --git a/pygit2/version.py b/pygit2/version.py deleted file mode 100644 index 02c5c51..0000000 --- a/pygit2/version.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright 2010-2014 The pygit2 contributors -# -# This file is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License, version 2, -# as published by the Free Software Foundation. -# -# In addition to the permissions in the GNU General Public License, -# the authors give you unlimited permission to link the compiled -# version of this file into combinations with other programs, -# and to distribute those combinations without any restriction -# coming from the use of this file. (The General Public License -# restrictions do apply in other respects; for example, they cover -# modification of the file, and distribution when not linked into -# a combined executable.) -# -# This file is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; see the file COPYING. If not, write to -# the Free Software Foundation, 51 Franklin Street, Fifth Floor, -# Boston, MA 02110-1301, USA. - -__version__ = '0.21.3' diff --git a/setup.py b/setup.py index f0d802f..ee8973f 100644 --- a/setup.py +++ b/setup.py @@ -43,11 +43,9 @@ from subprocess import Popen, PIPE import sys import unittest -# Read version from local pygit2/version.py without pulling in -# pygit2/__init__.py +# Import stuff from pygit2/_utils.py without loading the whole pygit2 package sys.path.insert(0, 'pygit2') -from version import __version__ -import ffi +from _utils import __version__, get_libgit2_paths, get_ffi del sys.path[0] # Python 2 support @@ -58,7 +56,7 @@ else: u = str -libgit2_bin, libgit2_include, libgit2_lib = ffi.get_libgit2_paths() +libgit2_bin, libgit2_include, libgit2_lib = get_libgit2_paths() pygit2_exts = [os.path.join('src', name) for name in os.listdir('src') if name.endswith('.c')] @@ -100,7 +98,8 @@ class CFFIBuild(build): to add cffi as an extension. """ def finalize_options(self): - self.distribution.ext_modules.append(ffi.ffi.verifier.get_extension()) + ffi, C = get_ffi() + self.distribution.ext_modules.append(ffi.verifier.get_extension()) build.finalize_options(self)