Skip to content

Commit

Permalink
fix: auto fix ibm_db import issue (Yelp#239)
Browse files Browse the repository at this point in the history
* fix: auto fix ibm_db import issue

Supports https://github.ibm.com/git-defenders/detect-secrets-discuss/issues/306

* fix: support python 2

* fix: cov
  • Loading branch information
XIANJUN ZHU authored and GitHub Enterprise committed Jan 15, 2020
1 parent 914926a commit 47e1778
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion detect_secrets/plugins/db2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
from __future__ import absolute_import

import os
import re
import subprocess
import sys

try:
import ibm_db
except ImportError as ie: # pragma: no cover
# Try to fix the ibm_db dynamic lib import issue
if 'darwin' not in sys.platform:
raise ie

if hasattr(ie, 'msg'):
message = ie.msg
else:
# python 2
message = ie.message

if 'Library not loaded: libdb2.dylib' not in message:
raise ie

if hasattr(ie, 'path'):
ibm_db_darwin_so = ie.path
else:
# python 2
so_file_search = re.search(r'dlopen\((.*),', message, re.IGNORECASE)
if so_file_search:
ibm_db_darwin_so = so_file_search.group(1)

if not ibm_db_darwin_so:
raise ie

site_packages = os.path.dirname(ibm_db_darwin_so)
libdb2_dylib = os.path.join(site_packages, 'clidriver/lib/libdb2.dylib')
if os.path.exists(libdb2_dylib):
subprocess.call(
['install_name_tool', '-change', 'libdb2.dylib', libdb2_dylib, ibm_db_darwin_so],
)

import ibm_db
import ibm_db

from .base import classproperty
from .base import RegexBasedDetector
Expand Down

0 comments on commit 47e1778

Please sign in to comment.