Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Add basic XML tag generation #2

Merged
merged 20 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
22 changes: 15 additions & 7 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
Copyright 2017 "Shopify inc."

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 3 additions & 5 deletions pyoozie/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

from pyoozie.coordinator import Coordinator, ExecutionOrder
from pyoozie.tags import Parameters, Configuration, Credentials, Shell, SubWorkflow, GlobalConfiguration, \
Email, IdentifierTooLongError
from pyoozie.tags import Parameters, Configuration, Credentials, Shell, SubWorkflow, GlobalConfiguration, Email
from pyoozie.builder import WorkflowBuilder, CoordinatorBuilder

__version__ = '0.0.0'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.0.1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When should we update this; on every intended release? Every PR would become a bit tedious.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not every PR, it just seems like 0.0.1 is a better starting place.

Expand All @@ -13,8 +12,7 @@
'Coordinator', 'ExecutionOrder', 'Configuration', 'Parameters',

# tags
'Parameters', 'Configuration', 'Credentials', 'Shell', 'SubWorkflow', 'GlobalConfiguration', \
'Email', 'IdentifierTooLongError',
'Parameters', 'Configuration', 'Credentials', 'Shell', 'SubWorkflow', 'GlobalConfiguration', 'Email',

# builder
'WorkflowBuilder', 'CoordinatorBuilder',
Expand Down
2 changes: 1 addition & 1 deletion pyoozie/builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
from __future__ import unicode_literals

from pyoozie.coordinator import Coordinator
Expand Down
18 changes: 11 additions & 7 deletions pyoozie/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
from __future__ import unicode_literals

from datetime import timedelta
from enum import Enum

from pyoozie.tags import _validate, Xml, Parameters, Configuration
from pyoozie.tags import _validate, XMLSerializable, Parameters, Configuration


ONE_HUNDRED_YEARS = 100 * 365.24


class ExecutionOrder(Enum):
Expand All @@ -24,18 +27,19 @@ def format_datetime(value):
return value.strftime('%Y-%m-%dT%H:%MZ')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No seconds?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oozie's time grain is minute. You can probably specify seconds, but they'd be ignored.
From https://oozie.apache.org/docs/4.1.0/CoordinatorFunctionalSpec.html#a4._Datetime_Frequency_and_Time-Period_Representation

If the Oozie processing timezone is UTC , all datetime values are always in UTC down to a minute precision, 'YYYY-MM-DDTHH:mmZ'.



class Coordinator(Xml):
class Coordinator(XMLSerializable):

def __init__(self, name, workflow_app_path, frequency, start, end=None, timezone=None,
workflow_configuration=None, timeout=None, concurrency=None, execution_order=None, throttle=None,
parameters=None):
super(Coordinator, self).__init__('coordinator-app')
# Compose and validate dates/frequencies
if end is None:
end = start + timedelta(days=100 * 365.24)
assert end > start, "End time (%s) must be greater than the start time (%s)" % \
(format_datetime(end), format_datetime(start))
assert frequency >= 5, "Frequency (%d min) must be greater than or equal to 5 min" % frequency
end = start + timedelta(days=ONE_HUNDRED_YEARS)
assert end > start, "End time ({end}) must be greater than the start time ({start})".format(
end=format_datetime(end), start=format_datetime(start))
assert frequency >= 5, "Frequency ({frequency} min) must be greater than or equal to 5 min".format(
frequency=frequency)

# Coordinator
self.name = _validate(name)
Expand Down
51 changes: 22 additions & 29 deletions pyoozie/tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
from __future__ import unicode_literals
from abc import ABCMeta, abstractmethod
import re
Expand All @@ -11,20 +11,13 @@
COMPILED_REGEX_IDENTIFIER = re.compile(REGEX_IDENTIFIER)


class IdentifierTooLongError(AssertionError):

def __init__(self, identifier):
AssertionError.__init__(self, "Identifier must be less than {max_length} chars long, '{identifier}' is "
"{length}".format(max_length=MAX_IDENTIFIER_LENGTH,
identifier=identifier,
length=len(identifier)))
self.length = len(identifier)


def _validate(identifier):

if len(identifier) > MAX_IDENTIFIER_LENGTH:
raise IdentifierTooLongError(identifier)
assert len(identifier) <= MAX_IDENTIFIER_LENGTH, \
"Identifier must be less than {max_length} chars long, '{identifier}' is {length}".format(
max_length=MAX_IDENTIFIER_LENGTH,
identifier=identifier,
length=len(identifier))

assert COMPILED_REGEX_IDENTIFIER.match(identifier), \
"Identifier must match {regex}, '{identifier}' does not".format(
Expand All @@ -34,7 +27,7 @@ def _validate(identifier):
return identifier


class Xml(object):
class XMLSerializable(object):
"""An abstract object that can be serialized to XML."""

__metaclass__ = ABCMeta
Expand All @@ -59,7 +52,7 @@ def __str__(self):
return self.xml_tag


class _PropertyList(Xml, dict):
class _PropertyList(XMLSerializable, dict):
"""
Object used to represent Oozie workflow/coordinator property-value sets.

Expand All @@ -75,7 +68,7 @@ class _PropertyList(Xml, dict):
"""

def __init__(self, xml_tag, attributes=None, values=None):
Xml.__init__(self, xml_tag)
XMLSerializable.__init__(self, xml_tag)
if values:
dict.__init__(self, values)
else:
Expand All @@ -87,9 +80,9 @@ def _xml(self, doc, tag, text):
for name, value in sorted(self.items()):
with tag('property'):
with tag('name'):
doc.text('%s' % name)
doc.text('{}'.format(name))
with tag('value'):
doc.text('%s' % value if value is not None else '')
doc.text('{}'.format(value) if value is not None else '')
return doc


Expand Down Expand Up @@ -145,12 +138,12 @@ def __init__(self, values, credential_name, credential_type):
self.name = _validate(credential_name)


class Shell(Xml):
class Shell(XMLSerializable):
"""Workflow shell action (v0.3)."""

def __init__(self, exec_command, job_tracker=None, name_node=None, prepares=None, job_xml_files=None,
configuration=None, arguments=None, env_vars=None, files=None, archives=None, capture_output=False):
Xml.__init__(self, 'shell')
XMLSerializable.__init__(self, 'shell')
self.exec_command = exec_command
self.job_tracker = job_tracker
self.name_node = name_node
Expand Down Expand Up @@ -190,9 +183,9 @@ def _xml(self, doc, tag, text):
with tag('argument'):
doc.text(argument)

for env_var in self.env_vars.items():
for key, value in self.env_vars.items():
with tag('env-var'):
doc.text('%s=%s' % env_var)
doc.text('{key}={value}'.format(key=key, value=value))

for filename in self.files:
with tag('file'):
Expand All @@ -208,15 +201,15 @@ def _xml(self, doc, tag, text):
return doc


class SubWorkflow(Xml):
class SubWorkflow(XMLSerializable):
"""Run another workflow defined in another XML file on HDFS.

An Oozie sub-workflow is an "action [that] runs a child workflow job [...]. The parent workflow job will wait
until the child workflow job has completed."
"""

def __init__(self, app_path, propagate_configuration=True, configuration=None):
Xml.__init__(self, 'sub-workflow')
XMLSerializable.__init__(self, 'sub-workflow')
self.app_path = app_path
self.propagate_configuration = propagate_configuration
self.configuration = Configuration(configuration)
Expand All @@ -233,7 +226,7 @@ def _xml(self, doc, tag, text):
return doc


class GlobalConfiguration(Xml):
class GlobalConfiguration(XMLSerializable):
"""Global configuration values for all actions in a workflow.

"Oozie allows a global section to reduce the redundant job-tracker and name-node declarations for each action.
Expand All @@ -245,8 +238,8 @@ class GlobalConfiguration(Xml):
application."
"""

def __init__(self, job_tracker='', name_node='', job_xml_files=None, configuration=None):
Xml.__init__(self, 'global')
def __init__(self, job_tracker=None, name_node=None, job_xml_files=None, configuration=None):
XMLSerializable.__init__(self, 'global')
self.job_tracker = job_tracker
self.name_node = name_node
self.job_xml_files = job_xml_files if job_xml_files else list()
Expand All @@ -270,11 +263,11 @@ def _xml(self, doc, tag, text):
return doc


class Email(Xml):
class Email(XMLSerializable):
"""Email action for use within a workflow."""

def __init__(self, to, subject, body, cc=None, bcc=None, content_type=None, attachments=None):
Xml.__init__(self, 'email')
XMLSerializable.__init__(self, 'email')
self.to = to
self.subject = subject
self.body = body
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
import re


Expand Down Expand Up @@ -34,12 +34,12 @@
'yattag>=1.7.2',
'setuptools>=0.9',
],
license="BSD",
license="MIT",
keywords=['oozie'],
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
2 changes: 1 addition & 1 deletion tests/pyoozie/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
2 changes: 1 addition & 1 deletion tests/pyoozie/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
import pytest


Expand Down
2 changes: 1 addition & 1 deletion tests/pyoozie/test_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
from __future__ import unicode_literals

from datetime import datetime
Expand Down
2 changes: 1 addition & 1 deletion tests/pyoozie/test_coordinator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
from datetime import datetime, timedelta

import pytest
Expand Down
2 changes: 1 addition & 1 deletion tests/pyoozie/test_package.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2017 "Shopify inc." All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
# Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
import pyoozie


Expand Down
Loading