github.com/apache/beam/sdks/v2@v2.48.2/python/scripts/generate_pydoc.sh (about)

     1  #!/bin/bash
     2  #
     3  #    Licensed to the Apache Software Foundation (ASF) under one or more
     4  #    contributor license agreements.  See the NOTICE file distributed with
     5  #    this work for additional information regarding copyright ownership.
     6  #    The ASF licenses this file to You under the Apache License, Version 2.0
     7  #    (the "License"); you may not use this file except in compliance with
     8  #    the License.  You may obtain a copy of the License at
     9  #
    10  #       http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  #    Unless required by applicable law or agreed to in writing, software
    13  #    distributed under the License is distributed on an "AS IS" BASIS,
    14  #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  #    See the License for the specific language governing permissions and
    16  #    limitations under the License.
    17  #
    18  
    19  # This script will run sphinx to create documentation for python sdk
    20  #
    21  # Use "generate_pydocs.sh" to update documentation in the docs directory.
    22  #
    23  # The exit-code of the script indicates success or a failure.
    24  
    25  # Check that the script is running in a known directory.
    26  if [[ $PWD != *sdks/python* ]]; then
    27    echo 'Unable to locate Apache Beam Python SDK root directory'
    28    exit 1
    29  fi
    30  
    31  # Go to the Apache Beam Python SDK root
    32  if [[ $PWD != *sdks/python ]]; then
    33    cd $(pwd | sed 's/sdks\/python.*/sdks\/python/')
    34  fi
    35  
    36  # Quit on any errors
    37  set -e
    38  
    39  # Create docs directory if it does not exist
    40  mkdir -p target/docs
    41  rm -rf target/docs/*
    42  
    43  mkdir -p target/docs/source
    44  
    45  # Sphinx apidoc autodoc options
    46  export SPHINX_APIDOC_OPTIONS=\
    47  members,\
    48  undoc-members,\
    49  show-inheritance
    50  
    51  python_version=`python -V`
    52  current_minor_version=`echo ${python_version} | sed -E "s/Python 3.([0-9])\..*/\1/"`
    53  
    54  # Exclude internal, test, and Cython paths/patterns from the documentation.
    55  excluded_patterns=(
    56      'apache_beam/coders/coder_impl.*'
    57      'apache_beam/coders/stream.*'
    58      'apache_beam/coders/coder_impl_row_encoders.*'
    59      'apache_beam/examples/'
    60      'apache_beam/io/gcp/tests/'
    61      'apache_beam/metrics/execution.*'
    62      'apache_beam/runners/api/'
    63      'apache_beam/runners/common.*'
    64      'apache_beam/runners/portability/'
    65      'apache_beam/runners/test/'
    66      'apache_beam/runners/worker/'
    67      'apache_beam/testing/benchmarks/chicago_taxi/'
    68      'apache_beam/testing/benchmarks/cloudml/'
    69      'apache_beam/testing/benchmarks/inference/'
    70      'apache_beam/testing/benchmarks/data/'
    71      'apache_beam/testing/benchmarks/load_tests/'
    72      'apache_beam/testing/analyzers'
    73      'apache_beam/testing/.*test.py'
    74      'apache_beam/tools/'
    75      'apache_beam/tools/map_fn_microbenchmark.*'
    76      'apache_beam/transforms/cy_combiners.*'
    77      'apache_beam/transforms/cy_dataflow_distribution_counter.*'
    78      'apache_beam/transforms/py_dataflow_distribution_counter.*'
    79      'apache_beam/utils/counters.*'
    80      'apache_beam/utils/windowed_value.*'
    81      'apache_beam/version.py'
    82      '**/internal/*'
    83      '*_it.py'
    84      '*_pb2.py'
    85      '*_py3[0-9]*.py'
    86      '*_test.py'
    87      '*_test_common.py'
    88      '*_test_py3.py'
    89  )
    90  
    91  python $(type -p sphinx-apidoc) -fMeT -o target/docs/source apache_beam \
    92      "${excluded_patterns[@]}"
    93  
    94  # Include inherited memebers for the DataFrame API
    95  echo "    :inherited-members:" >> target/docs/source/apache_beam.dataframe.frames.rst
    96  
    97  # Create the configuration and index files
    98  #=== conf.py ===#
    99  cat > target/docs/source/conf.py <<'EOF'
   100  import os
   101  import sys
   102  from apache_beam import version as beam_version
   103  
   104  import sphinx_rtd_theme
   105  
   106  import numpy
   107  import IPython
   108  
   109  sys.path.insert(0, os.path.abspath('../../..'))
   110  
   111  exclude_patterns = [
   112      '_build',
   113      'apache_beam.rst',
   114  ]
   115  
   116  extensions = [
   117      'sphinx.ext.autodoc',
   118      'sphinx.ext.doctest',
   119      'sphinx.ext.intersphinx',
   120      'sphinx.ext.napoleon',
   121      'sphinx.ext.viewcode',
   122  ]
   123  master_doc = 'index'
   124  html_theme = 'sphinx_rtd_theme'
   125  html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
   126  project = 'Apache Beam'
   127  version = beam_version.__version__
   128  release = version
   129  
   130  autoclass_content = 'both'
   131  autodoc_inherit_docstrings = False
   132  autodoc_member_order = 'bysource'
   133  autodoc_mock_imports = ["tensorrt", "cuda", "torch", "onnxruntime", "onnx", "tensorflow", "tensorflow_hub"]
   134  
   135  # Allow a special section for documenting DataFrame API
   136  napoleon_custom_sections = ['Differences from pandas']
   137  
   138  doctest_global_setup = '''
   139  import apache_beam as beam
   140  '''
   141  
   142  intersphinx_mapping = {
   143    'python': ('https://docs.python.org/{}'.format(sys.version_info.major), None),
   144    'hamcrest': ('https://pyhamcrest.readthedocs.io/en/stable/', None),
   145    'numpy': ('https://numpy.org/doc/stable', None),
   146    'pandas': ('http://pandas.pydata.org/pandas-docs/dev', None),
   147  }
   148  
   149  # Since private classes are skipped by sphinx, if there is any cross reference
   150  # to them, it will be broken. This can happen if a class inherits from a
   151  # private class.
   152  ignore_identifiers = [
   153    # Ignore "custom" builtin types
   154    '',
   155    'Any',
   156    'Dict',
   157    'Iterable',
   158    'List',
   159    'Set',
   160    'Text',
   161    'Tuple',
   162  
   163    # Ignore broken built-in type references
   164    'tuple',
   165  
   166    # Ignore private classes
   167    'apache_beam.coders.coders._PickleCoderBase',
   168    'apache_beam.coders.coders.FastCoder',
   169    'apache_beam.coders.coders.ListLikeCoder',
   170    'apache_beam.io._AvroSource',
   171    'apache_beam.io.fileio.FileSink',
   172    'apache_beam.io.gcp.bigquery.RowAsDictJsonCoder',
   173    'apache_beam.io.gcp.datastore.v1new.datastoreio._Mutate',
   174    'apache_beam.io.gcp.datastore.v1new.datastoreio.DatastoreMutateFn',
   175    'apache_beam.io.gcp.internal.clients.bigquery.'
   176        'bigquery_v2_messages.TableFieldSchema',
   177    'apache_beam.io.gcp.internal.clients.bigquery.'
   178        'bigquery_v2_messages.TableSchema',
   179    'apache_beam.io.iobase.SourceBase',
   180    'apache_beam.io.source_test_utils.ExpectedSplitOutcome',
   181    'apache_beam.metrics.metric.MetricResults',
   182    'apache_beam.pipeline.PipelineVisitor',
   183    'apache_beam.pipeline.PTransformOverride',
   184    'apache_beam.portability.api.schema_pb2.Schema',
   185    'apache_beam.pvalue.AsSideInput',
   186    'apache_beam.pvalue.DoOutputsTuple',
   187    'apache_beam.pvalue.PValue',
   188    'apache_beam.runners.direct.executor.CallableTask',
   189    'apache_beam.runners.portability.local_job_service.BeamJob',
   190    'apache_beam.testing.synthetic_pipeline._Random',
   191    'apache_beam.transforms.combiners.CombinerWithoutDefaults',
   192    'apache_beam.transforms.core.CallableWrapperCombineFn',
   193    'apache_beam.transforms.ptransform.PTransformWithSideInputs',
   194    'apache_beam.transforms.trigger._ParallelTriggerFn',
   195    'apache_beam.transforms.trigger.InMemoryUnmergedState',
   196    'apache_beam.typehints.typehints.AnyTypeConstraint',
   197    'apache_beam.typehints.typehints.CompositeTypeHint',
   198    'apache_beam.typehints.typehints.TypeConstraint',
   199    'apache_beam.typehints.typehints.validate_composite_type_param()',
   200    'apache_beam.utils.windowed_value._IntervalWindowBase',
   201    'apache_beam.coders.coder_impl.StreamCoderImpl',
   202  
   203    # Private classes which are used within the same module
   204    'apache_beam.transforms.external_test.PayloadBase',
   205    'apache_beam.typehints.typehints.WindowedTypeConstraint',
   206  
   207    # stdlib classes without documentation
   208    'unittest.case.TestCase',
   209  
   210    # DoFn param inner classes, due to a Sphinx misparsing of inner classes
   211    '_StateDoFnParam',
   212    '_TimerDoFnParam',
   213    '_BundleFinalizerParam',
   214    '_RestrictionDoFnParam',
   215    '_WatermarkEstimatorParam',
   216  
   217    # Sphinx cannot find this py:class reference target
   218    'callable',
   219    'types.FunctionType',
   220    'typing.Generic',
   221    'typing_extensions.Protocol',
   222    'concurrent.futures._base.Executor',
   223    'uuid',
   224    'google.cloud.datastore.key.Key',
   225    'google.cloud.datastore.entity.Entity',
   226    'google.cloud.datastore.batch.Batch',
   227    'is_in_ipython',
   228    'doctest.TestResults',
   229  
   230    # IPython Magics py:class reference target not found
   231    'IPython.core.magic.Magics',
   232  ]
   233  ignore_references = [
   234    'BeamIOError',
   235    'HttpError',
   236    'ValueError',
   237  ]
   238  # When inferring a base class it will use ':py:class'; if inferring a function
   239  # argument type or return type, it will use ':py:obj'. We'll generate both.
   240  nitpicky = True
   241  nitpick_ignore = []
   242  nitpick_ignore += [('py:class', iden) for iden in ignore_identifiers]
   243  nitpick_ignore += [('py:obj', iden) for iden in ignore_identifiers]
   244  nitpick_ignore += [('py:exc', iden) for iden in ignore_references]
   245  EOF
   246  
   247  #=== index.rst ===#
   248  cat > target/docs/source/index.rst <<'EOF'
   249  .. include:: ./apache_beam.rst
   250     :start-line: 2
   251  EOF
   252  
   253  # Build the documentation using sphinx
   254  # Reference: http://www.sphinx-doc.org/en/stable/man/sphinx-build.html
   255  # Note we cut out warnings from apache_beam.dataframe, this package uses pandas
   256  # documentation verbatim, as do some of the textio transforms.
   257  python $(type -p sphinx-build) -v -a -E -q target/docs/source \
   258    target/docs/_build -c target/docs/source \
   259    2>&1 | grep -E -v 'apache_beam\.dataframe.*WARNING:' \
   260    2>&1 | grep -E -v 'apache_beam\.io\.textio\.(ReadFrom|WriteTo)(Csv|Json).*WARNING:' \
   261    2>&1 | tee "target/docs/sphinx-build.log"
   262  
   263  # Fail if there are errors or warnings in docs
   264  ! grep -q "ERROR:" target/docs/sphinx-build.log || exit 1
   265  ! grep -q "WARNING:" target/docs/sphinx-build.log || exit 1
   266  
   267  # Run tests for code samples, these can be:
   268  # - Code blocks using '.. testsetup::', '.. testcode::' and '.. testoutput::'
   269  # - Interactive code starting with '>>>'
   270  python -msphinx -M doctest target/docs/source \
   271    target/docs/_build -c target/docs/source \
   272    2>&1 | grep -E -v 'apache_beam\.dataframe.*WARNING:' \
   273    2>&1 | grep -E -v 'apache_beam\.io\.textio\.(ReadFrom|WriteTo)(Csv|Json).*WARNING:' \
   274    2>&1 | tee "target/docs/sphinx-doctest.log"
   275  
   276  # Fail if there are errors or warnings in docs
   277  ! grep -q "ERROR:" target/docs/sphinx-doctest.log || exit 1
   278  ! grep -q "WARNING:" target/docs/sphinx-doctest.log || exit 1
   279  
   280  # Message is useful only when this script is run locally.  In a remote
   281  # test environment, this path will be removed when the test completes.
   282  echo "Browse to file://$PWD/target/docs/_build/index.html"