|
| 1 | +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""An ast.NodeTransformer subclass for updating SageMaker Python SDK code.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import ast |
| 17 | + |
| 18 | +from sagemaker.cli.compatibility.v2 import modifiers |
| 19 | + |
| 20 | +FUNCTION_CALL_MODIFIERS = [ |
| 21 | + modifiers.framework_version.FrameworkVersionEnforcer(), |
| 22 | + modifiers.tf_legacy_mode.TensorFlowLegacyModeConstructorUpgrader(), |
| 23 | + modifiers.tf_legacy_mode.TensorBoardParameterRemover(), |
| 24 | + modifiers.deprecated_params.TensorFlowScriptModeParameterRemover(), |
| 25 | + modifiers.tfs.TensorFlowServingConstructorRenamer(), |
| 26 | +] |
| 27 | + |
| 28 | +IMPORT_MODIFIERS = [modifiers.tfs.TensorFlowServingImportRenamer()] |
| 29 | + |
| 30 | +IMPORT_FROM_MODIFIERS = [modifiers.tfs.TensorFlowServingImportFromRenamer()] |
| 31 | + |
| 32 | + |
| 33 | +class ASTTransformer(ast.NodeTransformer): |
| 34 | + """An ``ast.NodeTransformer`` subclass that walks the abstract syntax tree and |
| 35 | + modifies nodes to upgrade the given SageMaker Python SDK code. |
| 36 | + """ |
| 37 | + |
| 38 | + def visit_Call(self, node): |
| 39 | + """Visits an ``ast.Call`` node and returns a modified node, if needed. |
| 40 | + See https://docs.python.org/3/library/ast.html#ast.NodeTransformer. |
| 41 | +
|
| 42 | + Args: |
| 43 | + node (ast.Call): a node that represents a function call. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + ast.Call: a node that represents a function call, which has |
| 47 | + potentially been modified from the original input. |
| 48 | + """ |
| 49 | + for function_checker in FUNCTION_CALL_MODIFIERS: |
| 50 | + function_checker.check_and_modify_node(node) |
| 51 | + |
| 52 | + ast.fix_missing_locations(node) |
| 53 | + return node |
| 54 | + |
| 55 | + def visit_Import(self, node): |
| 56 | + """Visits an ``ast.Import`` node and returns a modified node, if needed. |
| 57 | + See https://docs.python.org/3/library/ast.html#ast.NodeTransformer. |
| 58 | +
|
| 59 | + Args: |
| 60 | + node (ast.Import): a node that represents an import statement. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + ast.Import: a node that represents an import statement, which has |
| 64 | + potentially been modified from the original input. |
| 65 | + """ |
| 66 | + for import_checker in IMPORT_MODIFIERS: |
| 67 | + import_checker.check_and_modify_node(node) |
| 68 | + |
| 69 | + ast.fix_missing_locations(node) |
| 70 | + return node |
| 71 | + |
| 72 | + def visit_ImportFrom(self, node): |
| 73 | + """Visits an ``ast.ImportFrom`` node and returns a modified node, if needed. |
| 74 | + See https://docs.python.org/3/library/ast.html#ast.NodeTransformer. |
| 75 | +
|
| 76 | + Args: |
| 77 | + node (ast.ImportFrom): a node that represents an import statement. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + ast.ImportFrom: a node that represents an import statement, which has |
| 81 | + potentially been modified from the original input. |
| 82 | + """ |
| 83 | + for import_checker in IMPORT_FROM_MODIFIERS: |
| 84 | + import_checker.check_and_modify_node(node) |
| 85 | + |
| 86 | + ast.fix_missing_locations(node) |
| 87 | + return node |
0 commit comments