Refactor code to get the path to libgit2

This commit is contained in:
J. David Ibáñez
2014-10-28 16:31:53 +01:00
parent 6bb2b369fa
commit e325c51203
3 changed files with 62 additions and 27 deletions

View File

@@ -31,29 +31,23 @@ from __future__ import absolute_import
# Import from the Standard Library
import inspect
import codecs
from os import path, getenv
import os
from os.path import abspath, dirname
# Import from cffi
from cffi import FFI
# Import from pygit2
from libgit2 import get_libgit2_paths
ffi = FFI()
dir_path = path.dirname(path.abspath(inspect.getfile(inspect.currentframe())))
decl_path = path.join(dir_path, 'decl.h')
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())
# if LIBGIT2 exists, set build and link against that version
libgit2_path = getenv('LIBGIT2')
if not libgit2_path:
libgit2_path = '/usr/local'
include_dirs = [path.join(libgit2_path, 'include')]
library_dirs = [path.join(libgit2_path, 'lib')]
libgit2_bin, libgit2_include, libgit2_lib = get_libgit2_paths()
C = ffi.verify("#include <git2.h>", libraries=["git2"],
include_dirs=include_dirs, library_dirs=library_dirs)
include_dirs=[libgit2_include], library_dirs=[libgit2_lib])

49
pygit2/libgit2.py Normal file
View File

@@ -0,0 +1,49 @@
# 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.
# Import from the Standard Library
from os import getenv
import os
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')),
)

View File

@@ -28,15 +28,16 @@
"""Setup file for pygit2."""
# Import from the future
from __future__ import print_function
# Import from the Standard Library
import codecs
from setuptools import setup, Extension, Command
from distutils.command.build import build
from distutils.command.sdist import sdist
from distutils import log
import os
from setuptools import setup, Extension, Command
import shlex
from subprocess import Popen, PIPE
import sys
@@ -46,6 +47,7 @@ import unittest
# pygit2/__init__.py
sys.path.insert(0, 'pygit2')
from version import __version__
from libgit2 import get_libgit2_paths
# Python 2 support
# See https://github.com/libgit2/pygit2/pull/180 for a discussion about this.
@@ -55,18 +57,8 @@ else:
u = str
# Use environment variable LIBGIT2 to set your own libgit2 configuration.
libgit2_path = os.getenv("LIBGIT2")
if libgit2_path is None:
if os.name == 'nt':
program_files = os.getenv("ProgramFiles")
libgit2_path = '%s\libgit2' % program_files
else:
libgit2_path = '/usr/local'
libgit2_bin, libgit2_include, libgit2_lib = get_libgit2_paths()
libgit2_bin = os.path.join(libgit2_path, 'bin')
libgit2_include = os.path.join(libgit2_path, 'include')
libgit2_lib = os.getenv('LIBGIT2_LIB', os.path.join(libgit2_path, 'lib'))
pygit2_exts = [os.path.join('src', name) for name in os.listdir('src')
if name.endswith('.c')]