Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve import of build script #22

Merged
merged 2 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pynt/_pynt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ def build(args):
sys.exit(0)

#load build file as a module
if not path.isfile(args.file):
print("Build file '%s' does not exist. Please specify a build file\n" % args.file)
parser.print_help()
sys.exit(1)

module = imp.load_source(path.splitext(path.basename(args.file))[0], args.file)
module = _load_buildscript(args.file)

# Run task and all its dependencies.
if args.list_tasks:
Expand Down Expand Up @@ -80,6 +75,23 @@ def print_tasks(module, file):
task.doc)
print(task_list + "\n\n"+_CREDIT_LINE)

def _load_buildscript(file_path):
if not path.isfile(file_path):
print("Build file '%s' does not exist. Please specify a build file\n" % file_path)
parser.print_help()
sys.exit(1)

script_dir, script_base = path.split(file_path)

# Append directory of build script to path, to allow importing modules relatively to the script
sys.path.append(path.abspath(script_dir))

module_name, suffix = path.splitext(script_base)
description = (suffix, 'r', imp.PY_SOURCE)

with open(file_path, 'r') as script_file:
return imp.load_module(module_name, script_file, file_path, description)

def _get_default_task(module):
matching_tasks = [task for name,task in inspect.getmembers(module,Task.is_task)
if name == "__DEFAULT__"]
Expand Down
9 changes: 9 additions & 0 deletions pynt/tests/build_scripts/build_with_local_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/python

from pynt import task

from test_module import do_stuff

@task()
def work():
do_stuff()
3 changes: 3 additions & 0 deletions pynt/tests/build_scripts/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

def do_stuff():
pass
19 changes: 17 additions & 2 deletions pynt/tests/test_pynt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ def fpath(mod):

def simulate_dynamic_module_load(mod):
file_path = fpath(mod)
dynamically_loaded_mod = imp.load_source(path.splitext(path.basename(file_path))[0], file_path)
return dynamically_loaded_mod

#sys.path.append(path.abspath(script_dir))

module_name, suffix = path.splitext(path.basename(file_path))
description = (suffix, 'r', imp.PY_SOURCE)

with open(file_path, 'r') as scriptfile:
return imp.load_module(module_name, scriptfile, file_path, description)

def reset_build_file(mod):
mod.tasks_run = []
Expand Down Expand Up @@ -296,3 +302,12 @@ def test_invalid_names_for_kwargs(self):
build(self._mod, ['copy[bar123=2]'])
assert "got an unexpected keyword argument 'bar123'" in str(exc.value)


class TesttaskLocalImports:
def setup_method(self,method):
from .build_scripts import build_with_local_import
self._mod = build_with_local_import
self._mod.tasks_run = []

def test_load_build_with_local_import_does_not_fail(self):
mod = build(self._mod, ["work"])