forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/incu…
- Loading branch information
Showing
11 changed files
with
225 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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. | ||
# | ||
|
||
from airflow.hooks.base_hook import BaseHook | ||
|
||
|
||
class FSHook(BaseHook): | ||
''' | ||
Allows for interaction with an file server. | ||
Connection should have a name and a path specified under extra: | ||
example: | ||
Conn Id: fs_test | ||
Conn Type: File (path) | ||
Host, Shchema, Login, Password, Port: empty | ||
Extra: {"path": "/tmp"} | ||
''' | ||
|
||
def __init__(self, conn_id='fs_default'): | ||
conn = self.get_connection(conn_id) | ||
self.basepath = conn.extra_dejson.get('path', '') | ||
self.conn = conn | ||
|
||
def get_conn(self): | ||
pass | ||
|
||
def get_path(self): | ||
return self.basepath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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. | ||
# | ||
|
||
from os import walk | ||
import logging | ||
|
||
from airflow.operators.sensors import BaseSensorOperator | ||
from airflow.contrib.hooks import FSHook | ||
from airflow.utils.decorators import apply_defaults | ||
|
||
class FileSensor(BaseSensorOperator): | ||
""" | ||
Waits for a file or folder to land in a filesystem | ||
:param fs_conn_id: reference to the File (path) | ||
connection id | ||
:type fs_conn_id: string | ||
:param filepath: File or folder name (relative to | ||
the base path set within the connection) | ||
:type fs_conn_id: string | ||
""" | ||
template_fields = ('filepath',) | ||
|
||
@apply_defaults | ||
def __init__( | ||
self, | ||
filepath, | ||
fs_conn_id='fs_default2', | ||
*args, **kwargs): | ||
super(FileSensor, self).__init__(*args, **kwargs) | ||
self.filepath = filepath | ||
self.fs_conn_id = fs_conn_id | ||
|
||
def poke(self, context): | ||
hook = FSHook(self.fs_conn_id) | ||
basepath = hook.get_path() | ||
full_path = "/".join([basepath, self.filepath]) | ||
logging.info( | ||
'Poking for file {full_path} '.format(**locals())) | ||
try: | ||
files = [f for f in walk(full_path)] | ||
except: | ||
return False | ||
return True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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. | ||
# | ||
|
||
from __future__ import absolute_import | ||
from .ssh_execute_operator import * | ||
from .fs_operator import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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 unittest | ||
from datetime import datetime | ||
|
||
from airflow import configuration | ||
from airflow.settings import Session | ||
from airflow import models, DAG | ||
from airflow.contrib.operators.fs_operator import FileSensor | ||
|
||
TEST_DAG_ID = 'unit_tests' | ||
DEFAULT_DATE = datetime(2015, 1, 1) | ||
configuration.test_mode() | ||
|
||
|
||
def reset(dag_id=TEST_DAG_ID): | ||
session = Session() | ||
tis = session.query(models.TaskInstance).filter_by(dag_id=dag_id) | ||
tis.delete() | ||
session.commit() | ||
session.close() | ||
|
||
reset() | ||
|
||
class FileSensorTest(unittest.TestCase): | ||
def setUp(self): | ||
configuration.test_mode() | ||
from airflow.contrib.hooks.fs_hook import FSHook | ||
hook = FSHook() | ||
args = { | ||
'owner': 'airflow', | ||
'start_date': DEFAULT_DATE, | ||
'provide_context': True | ||
} | ||
dag = DAG(TEST_DAG_ID+'test_schedule_dag_once', default_args=args) | ||
dag.schedule_interval = '@once' | ||
self.hook = hook | ||
self.dag = dag | ||
|
||
def test_simple(self): | ||
task = FileSensor( | ||
task_id="test", | ||
filepath="etc/hosts", | ||
fs_conn_id='fs_default', | ||
_hook=self.hook, | ||
dag=self.dag, | ||
) | ||
task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, force=True) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters