Example #1
0
    def test_open_maybe_zipped_archive(self, mocked_ZipFile, mocked_is_zipfile):
        mocked_is_zipfile.return_value = True
        instance = mocked_ZipFile.return_value
        instance.open.return_value = mock.mock_open(read_data="data")

        utils.open_maybe_zipped('/path/to/archive.zip/deep/path/to/file.txt')

        mocked_is_zipfile.assert_called_once_with('/path/to/archive.zip')
        mocked_ZipFile.assert_called_once_with('/path/to/archive.zip', mode='r')
        instance.open.assert_called_once_with('deep/path/to/file.txt')
Example #2
0
    def test_open_maybe_zipped_archive(self, mocked_ZipFile, mocked_is_zipfile):
        mocked_is_zipfile.return_value = True
        instance = mocked_ZipFile.return_value
        instance.open.return_value = mock.mock_open(read_data="data")

        utils.open_maybe_zipped('/path/to/archive.zip/deep/path/to/file.txt')

        assert mocked_is_zipfile.call_count == 1
        (args, kwargs) = mocked_is_zipfile.call_args_list[0]
        self.assertEqual('/path/to/archive.zip', args[0])

        assert mocked_ZipFile.call_count == 1
        (args, kwargs) = mocked_ZipFile.call_args_list[0]
        self.assertEqual('/path/to/archive.zip', args[0])

        assert instance.open.call_count == 1
        (args, kwargs) = instance.open.call_args_list[0]
        self.assertEqual('deep/path/to/file.txt', args[0])
Example #3
0
    def test_open_maybe_zipped_archive(self, mocked_ZipFile, mocked_is_zipfile):
        mocked_is_zipfile.return_value = True
        instance = mocked_ZipFile.return_value
        instance.open.return_value = mock.mock_open(read_data="data")

        utils.open_maybe_zipped('/path/to/archive.zip/deep/path/to/file.txt')

        mocked_is_zipfile.assert_called_once()
        (args, kwargs) = mocked_is_zipfile.call_args_list[0]
        self.assertEqual('/path/to/archive.zip', args[0])

        mocked_ZipFile.assert_called_once()
        (args, kwargs) = mocked_ZipFile.call_args_list[0]
        self.assertEqual('/path/to/archive.zip', args[0])

        instance.open.assert_called_once()
        (args, kwargs) = instance.open.call_args_list[0]
        self.assertEqual('deep/path/to/file.txt', args[0])
Example #4
0
def get_code(dag_id: str) -> str:
    """Return python code of a given dag_id.

    :param dag_id: DAG id
    :return: code of the DAG
    """
    dag = check_and_get_dag(dag_id=dag_id)

    try:
        with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as file:
            code = file.read()
            return code
    except IOError as exception:
        error_message = "Error {} while reading Dag id {} Code".format(str(exception), dag_id)
        raise AirflowException(error_message)
Example #5
0
def get_code(dag_id):
    """Return python code of a given dag_id."""
    session = settings.Session()
    DM = models.DagModel
    dag = session.query(DM).filter(DM.dag_id == dag_id).first()
    session.close()
    # Check DAG exists.
    if dag is None:
        error_message = "Dag id {} not found".format(dag_id)
        raise DagNotFound(error_message)

    try:
        with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as f:
            code = f.read()
            return code
    except IOError as e:
        error_message = "Error {} while reading Dag id {} Code".format(str(e), dag_id)
        raise AirflowException(error_message)
Example #6
0
def get_code(dag_id):
    """Return python code of a given dag_id."""
    session = settings.Session()
    DM = models.DagModel
    dag = session.query(DM).filter(DM.dag_id == dag_id).first()
    session.close()
    # Check DAG exists.
    if dag is None:
        error_message = "Dag id {} not found".format(dag_id)
        raise DagNotFound(error_message)

    try:
        with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as f:
            code = f.read()
            return code
    except IOError as e:
        error_message = "Error {} while reading Dag id {} Code".format(
            str(e), dag_id)
        raise AirflowException(error_message)
Example #7
0
 def test_open_maybe_zipped_normal_file_with_zip_in_name(self):
     path = '/path/to/fakearchive.zip.other/file.txt'
     with mock.patch('io.open',
                     mock.mock_open(read_data="data")) as mock_file:
         utils.open_maybe_zipped(path)
         mock_file.assert_called_with(path, mode='r')
Example #8
0
 def test_open_maybe_zipped_normal_file(self):
     with mock.patch('io.open',
                     mock.mock_open(read_data="data")) as mock_file:
         utils.open_maybe_zipped('/path/to/some/file.txt')
         mock_file.assert_called_with('/path/to/some/file.txt', mode='r')
Example #9
0
 def test_open_maybe_zipped_normal_file_with_zip_in_name(self):
     path = '/path/to/fakearchive.zip.other/file.txt'
     with mock.patch(
             'io.open', mock.mock_open(read_data="data")) as mock_file:
         utils.open_maybe_zipped(path)
         mock_file.assert_called_with(path, mode='r')
Example #10
0
 def test_open_maybe_zipped_normal_file(self):
     with mock.patch(
             'io.open', mock.mock_open(read_data="data")) as mock_file:
         utils.open_maybe_zipped('/path/to/some/file.txt')
         mock_file.assert_called_with('/path/to/some/file.txt', mode='r')