Skip to content

Commit 73ba310

Browse files
authored
Merge pull request #1 from ento/sysconfcpus-support
Inherit LD_PRELOAD env vars to support sysconfcpus
2 parents 8d9488b + 0937f0e commit 73ba310

7 files changed

Lines changed: 83 additions & 18 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ script:
5959
if [[ "$CHECK_INSTALL" == 'yes' ]]; then
6060
pip install .
6161
elm-doc --help
62-
python -c 'import elm_doc; assert elm_doc.elm_package_overlayer_path()'
62+
python -c 'import os, elm_doc; assert elm_doc.elm_package_overlayer_env("", "", os.environ)["LD_PRELOAD"]'
6363
else
6464
tox -v
6565
fi

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,10 @@ In a Python (>=3.4) [virtualenv](https://docs.python.org/3.6/library/venv.html#c
5151
$ pip install elm-doc
5252

5353
Dependency on `python-magic` may require you to [install more stuff](https://github.com/ahupp/python-magic#dependencies).
54+
55+
## Development
56+
57+
Running tests:
58+
59+
$ pip install -r dev-requirements.txt
60+
$ tox -e py35,...

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'click',
2828
'doit',
2929
'python-magic',
30+
'retrying',
3031
'typing ; python_version < "3.5"',
3132
],
3233
packages=find_packages('src', exclude=['tests']),

src/elm_doc/__init__.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
1+
from collections import ChainMap
12
import imp
23
from distutils.sysconfig import get_python_lib
34

45

5-
def elm_package_overlayer_path():
6+
def elm_package_overlayer_env(
7+
use_elm_package_path,
8+
instead_of_elm_package_path,
9+
base_environ):
10+
overlayer_path = _elm_package_overlayer_path()
11+
# todo: make overlayer support windows if we want to (can we?)
12+
return dict(ChainMap(
13+
{
14+
'USE_ELM_PACKAGE': use_elm_package_path,
15+
'INSTEAD_OF_ELM_PACKAGE': instead_of_elm_package_path,
16+
'DYLD_INSERT_LIBRARIES': _prepend_overlayer(
17+
base_environ, 'DYLD_INSERT_LIBRARIES', overlayer_path),
18+
'LD_PRELOAD': _prepend_overlayer(base_environ, 'LD_PRELOAD', overlayer_path),
19+
},
20+
base_environ,
21+
))
22+
23+
24+
def _elm_package_overlayer_path():
625
search_paths = __path__ + [get_python_lib()]
726
(_file, overlayer_path, _desc) = imp.find_module(
827
"overlay_elm_package", search_paths)
928
return overlayer_path
29+
30+
31+
def _prepend_overlayer(base_environ, key, overlayer_path):
32+
if key in base_environ:
33+
return '{}:{}'.format(overlayer_path, base_environ[key])
34+
else:
35+
return overlayer_path

src/elm_doc/asset_tasks.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import subprocess
77
import shutil
88

9+
from retrying import retry
10+
911
from elm_doc import elm_platform
1012
from elm_doc import elm_package
1113
from elm_doc import node_modules
@@ -32,10 +34,7 @@ def build_assets(output_path: Path, mount_point: str = ''):
3234
# install elm
3335
elm_platform.install(root_path, package.elm_version)
3436
node_modules.add('jscodeshift', cwd=str(root_path))
35-
subprocess.check_call(
36-
['./node_modules/.bin/elm-package', 'install', '--yes'],
37-
cwd=str(root_path),
38-
)
37+
_install_elm_packages(str(root_path))
3938

4039
# make artifacts
4140
artifacts_path = root_path / 'artifacts'
@@ -82,3 +81,15 @@ def build_assets(output_path: Path, mount_point: str = ''):
8281
# copy assets
8382
shutil.rmtree(str(output_path / 'assets'), ignore_errors=True)
8483
shutil.copytree(str(root_path / 'assets'), str(output_path / 'assets'))
84+
85+
86+
@retry(
87+
retry_on_exception=lambda e: isinstance(e, subprocess.CalledProcessError),
88+
wait_exponential_multiplier=1000, # Wait 2^x * 1000 milliseconds between each retry,
89+
wait_exponential_max=30 * 1000, # up to 30 seconds, then 30 seconds afterwards
90+
stop_max_attempt_number=10)
91+
def _install_elm_packages(root_path):
92+
subprocess.check_call(
93+
['./node_modules/.bin/elm-package', 'install', '--yes'],
94+
cwd=root_path,
95+
)

src/elm_doc/package_tasks.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from doit.tools import create_folder
1313

1414
from elm_doc import elm_platform
15-
from elm_doc import elm_package_overlayer_path
15+
from elm_doc import elm_package_overlayer_env
1616
from elm_doc import elm_package
1717
from elm_doc.elm_package import ElmPackage, ModuleName
1818
from elm_doc import page_template
@@ -59,7 +59,6 @@ def build_package_docs_json(
5959
{'exposed-modules': package_modules},
6060
package.description,
6161
))
62-
overlayer_path = elm_package_overlayer_path()
6362
with TemporaryDirectory() as tmpdir:
6463
root_path = Path(tmpdir)
6564

@@ -75,16 +74,10 @@ def build_package_docs_json(
7574
# todo: support windows if we want to
7675
output_path = '/dev/null'
7776

78-
# todo: make overlayer support windows if we want to (can we?)
79-
env = dict(ChainMap(
80-
{
81-
'USE_ELM_PACKAGE': str(overlayed_elm_package_path),
82-
'INSTEAD_OF_ELM_PACKAGE': str(elm_package.description_path(package)),
83-
'DYLD_INSERT_LIBRARIES': overlayer_path,
84-
'LD_PRELOAD': overlayer_path,
85-
},
86-
os.environ,
87-
))
77+
env = elm_package_overlayer_env(
78+
str(overlayed_elm_package_path),
79+
str(elm_package.description_path(package)),
80+
os.environ)
8881
subprocess.check_call(
8982
[str(elm_make), '--yes', '--docs', str(output_path), '--output', '/dev/null'],
9083
cwd=str(package.path),

tests/test_helpers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from elm_doc import elm_package_overlayer_env
2+
3+
4+
def test_ld_preload_is_preserved():
5+
existing_preload = 'existing.lib'
6+
base_env = {
7+
'DYLD_INSERT_LIBRARIES': existing_preload,
8+
'LD_PRELOAD': existing_preload,
9+
}
10+
11+
env = elm_package_overlayer_env('', '', base_env)
12+
13+
assert existing_preload in env['LD_PRELOAD']
14+
assert existing_preload in env['DYLD_INSERT_LIBRARIES']
15+
16+
17+
def test_overlayer_environment_is_configured():
18+
original_elm_package = 'elm-package.json'
19+
modified_elm_package = 'tmp/elm-package.json'
20+
env = elm_package_overlayer_env(modified_elm_package, original_elm_package, {})
21+
22+
assert env['USE_ELM_PACKAGE'] == modified_elm_package
23+
assert env['INSTEAD_OF_ELM_PACKAGE'] == original_elm_package
24+
# test for existence but not the actual path because the helper function
25+
# to get the overlayer path is private. there might be a better way to test this?
26+
assert len(env['DYLD_INSERT_LIBRARIES']) > 0
27+
assert len(env['LD_PRELOAD']) > 0

0 commit comments

Comments
 (0)