Skip to content

Commit 54680cd

Browse files
committed
Fix __init__.py when used in a non-Python package context
1 parent bf5b805 commit 54680cd

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ _CPack_Packages/
2222
src/version.h
2323
src/createrepo_c.pc
2424
src/deltarpms.h
25+
src/python/createrepo_c/
2526
_skbuild/
2627

2728
# Python distribution stuff

src/python/createrepo_c/__init__.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,33 @@ def decompress_file(src, dst, comtype, stat=None):
332332
compression_type = _createrepo_c.compression_type
333333

334334

335-
DATA = os.path.join(os.path.dirname(__file__), 'data')
335+
# If we have been built as a Python package, e.g. "setup.py", this is
336+
# where the binaries will be located.
337+
_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
336338

337-
# Support running tests from the source tree
338-
if not os.path.exists(DATA):
339-
from skbuild.constants import CMAKE_INSTALL_DIR as SKBUILD_CMAKE_INSTALL_DIR
340-
_data = os.path.abspath(os.path.join(
341-
os.path.dirname(__file__), '..', SKBUILD_CMAKE_INSTALL_DIR, 'src/python/data'))
342-
if os.path.exists(_data):
343-
DATA = _data
339+
# Where we will look for the binaries. Default to looking on the system PATH.
340+
# If one of the following test succeeds, we can change this to look somewhere else.
341+
# Otherwise, probably, we were not built as a Python package, e.g. RPM, "cmake ..; make"
342+
# In that case, let's just assume that the binary will be on the PATH somewhere.
343+
_BIN_DIR = ""
344344

345-
BIN_DIR = os.path.join(DATA, 'bin')
345+
# Support running tests from the source tree
346+
if not os.path.exists(_DATA_DIR):
347+
# We can't find it. This might mean we've been built as an "editable" Python installation
348+
# Try building the path manually.
349+
try:
350+
from skbuild.constants import CMAKE_INSTALL_DIR as SKBUILD_CMAKE_INSTALL_DIR
351+
_data = os.path.abspath(os.path.join(
352+
os.path.dirname(__file__), '..', SKBUILD_CMAKE_INSTALL_DIR, 'src/python/createrepo_c/data'))
353+
if os.path.exists(_data):
354+
_BIN_DIR = os.path.join(_data, 'bin')
355+
except ImportError:
356+
# Scikit isn't available
357+
pass
346358

347359

348360
def _program(name, args):
349-
return subprocess.call([os.path.join(BIN_DIR, name)] + args)
361+
return subprocess.call([os.path.join(_BIN_DIR, name)] + args)
350362

351363

352364
def createrepo_c():

0 commit comments

Comments
 (0)