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

Add Launcher module in component SDK #769

Merged
merged 4 commits into from
Feb 11, 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
4 changes: 3 additions & 1 deletion component_sdk/python/kfp_component/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.

from . import launcher, core
24 changes: 24 additions & 0 deletions component_sdk/python/kfp_component/launcher/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Entrypoint module to launch python module or file dynamically.

This module makes it easier to build kfp component with python code
by defining a dynamic entrypoint and generate command line arg parser
by python-fire module. It can be used as an entrypoint in the
container spec to run arbitary python module or code in the local
image.
"""

from .launcher import launch
34 changes: 34 additions & 0 deletions component_sdk/python/kfp_component/launcher/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import fire
import importlib
import sys
import logging
from .launcher import launch

def main():
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(
prog='launcher',
description='Launch a python module or file.')
parser.add_argument('file_or_module', type=str,
help='Either a python file path or a module name.')
parser.add_argument('args', nargs=argparse.REMAINDER)
args = parser.parse_args()
launch(args.file_or_module, args.args)

if __name__ == '__main__':
main()
45 changes: 45 additions & 0 deletions component_sdk/python/kfp_component/launcher/launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import fire
import importlib
import sys
import logging

def launch(file_or_module, args):
"""Launches a python file or module as a command entrypoint.

Args:
file_or_module: it is either a file path to python file
a module path.
args: the args passed to the entrypoint function.

Returns:
The return value from the launched function.
"""
try:
module = importlib.import_module(file_or_module)
except Exception:
try:
if sys.version_info.major > 2:
gaoning777 marked this conversation as resolved.
Show resolved Hide resolved
spec = importlib.util.spec_from_file_location('module', file_or_module)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
else:
import imp
module = imp.load_source('module', file_or_module)
except Exception:
logging.error('Failed to find the module or file: {}'.format(file_or_module))
sys.exit(1)
return fire.Fire(module, command=args, name=module.__name__)
3 changes: 2 additions & 1 deletion component_sdk/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
kubernetes
gaoning777 marked this conversation as resolved.
Show resolved Hide resolved
kubernetes == 8.0.1
fire == 0.1.3
2 changes: 1 addition & 1 deletion component_sdk/python/run_test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pip install -U tox virtualenv
tox
tox "$@"
13 changes: 13 additions & 0 deletions component_sdk/python/tests/launcher/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
16 changes: 16 additions & 0 deletions component_sdk/python/tests/launcher/echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def echo(message):
gaoning777 marked this conversation as resolved.
Show resolved Hide resolved
return message
31 changes: 31 additions & 0 deletions component_sdk/python/tests/launcher/test_launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import unittest
import math
import os

from kfp_component.launcher import launch

TEST_PY_FILE = os.path.join(os.path.dirname(__file__), 'echo.py')

class LauncherTest(unittest.TestCase):

def test_launch_module_succeed(self):
self.assertEqual(math.pi, launch('math', 'pi'))

def test_launch_py_file_succeed(self):
self.assertEqual('hello',
launch(TEST_PY_FILE, ['echo', 'hello']))
gaoning777 marked this conversation as resolved.
Show resolved Hide resolved