github.com/apache/beam/sdks/v2@v2.48.2/python/build.gradle (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * License); you may not use this file except in compliance
     8   * with 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  plugins { id 'org.apache.beam.module' }
    20  applyPythonNature()
    21  
    22  
    23  /*************************************************************************************************/
    24  // Basic build and Python environment setup/cleanup
    25  
    26  def buildPython = tasks.register("buildPython") {
    27    dependsOn setupVirtualenv
    28  
    29    doLast {
    30      logger.info('Building Python Dependencies')
    31      exec {
    32        executable 'sh'
    33        args '-c', ". ${envdir}/bin/activate && python setup.py build --build-base ${buildDir}"
    34      }
    35    }
    36  }
    37  build.dependsOn tasks.named("buildPython")
    38  
    39  // Create a Python source distribution tarball.
    40  def tarball = "apache-beam.tar.gz"
    41  def sdist = tasks.register("sdist") {
    42    description "Create a Python source distribution tarball"
    43    dependsOn setupVirtualenv
    44  
    45    doLast {
    46      // Build artifact
    47      exec {
    48        executable 'sh'
    49        args '-c', ". ${envdir}/bin/activate && python setup.py -q sdist --formats zip,gztar --dist-dir ${buildDir}"
    50      }
    51  
    52      def collection = fileTree(buildDir){ include "**/*${project.sdk_version}*.tar.gz" exclude 'srcs/**'}
    53  
    54      // we need a fixed name for the artifact
    55      copy { from collection.singleFile; into buildDir; rename { tarball } }
    56      logger.info('Create distribution tar file {} in {}', tarball, buildDir)
    57    }
    58    inputs.files(pythonSdkDeps)
    59        .withPropertyName('pythonSdkDeps')
    60        .withPathSensitivity(PathSensitivity.RELATIVE)
    61    outputs.file "${buildDir}/${tarball}"
    62  }
    63  
    64  artifacts {
    65    distTarBall file: file("${buildDir}/${tarball}"), builtBy: sdist
    66  }
    67  
    68  // Create Python wheels for given platform and Python version
    69  // build identifiers for cibuildwheel
    70  def platform_identifiers_map = [
    71    linux: 'manylinux_*64*', // e.g. manylinux_x86_64, manylinux_aarch64
    72    macos:'macosx_*64*',     // e.g. macosx_x86_64, macosx_arm64
    73    windows: 'win_*64*',     // e.g. win_amd64, win_arm64
    74  ]
    75  
    76  platform_identifiers_map.each { platform, idsuffix ->
    77    def archs = 'auto'
    78    // note: A fix for arm64 platform in gradle environment. For some reason the
    79    // task fails with "Invalid archs option {<Architecture.arm64: 'arm64'>}."
    80    // even though os.arch is 'aarch64'
    81    // Running cibuildwheel command directly in shell it succeeded, however
    82    if (platform == 'linux' && 'aarch64'.equalsIgnoreCase(System.getProperty("os.arch"))) {
    83      archs = 'aarch64'
    84    }
    85    getVersionsAsList('python_versions').each { it ->
    86      def pyversion = it.replace('.', '')
    87  
    88      project.tasks.register("bdistPy${pyversion}${platform}") {
    89        description "Build a Python wheel distribution for Py${pyversion} ${platform}"
    90        dependsOn setupVirtualenv
    91        // need sdist task to generate protos
    92        dependsOn ':sdks:python:sdist'
    93  
    94        // generated installable Python SDK package
    95        doLast {
    96          exec {
    97            environment CIBW_BUILD: "cp${pyversion}-${idsuffix}"
    98            environment CIBW_ENVIRONMENT: "SETUPTOOLS_USE_DISTUTILS=stdlib"
    99            environment CIBW_BEFORE_BUILD: "pip install cython numpy && pip install --upgrade setuptools"
   100            // note: sync cibuildwheel version with GitHub Action
   101            // .github/workflow/build_wheel.yml:build_wheels "Install cibuildwheel" step
   102            executable 'sh'
   103            args '-c', ". ${envdir}/bin/activate && " +
   104                "pip install cibuildwheel==2.9.0 && " +
   105                "cibuildwheel --print-build-identifiers --platform ${platform} --archs ${archs} && " +
   106                "cibuildwheel --output-dir ${buildDir} --platform ${platform} --archs ${archs}"
   107          }
   108        }
   109      }
   110    }
   111  }
   112  
   113  /*************************************************************************************************/
   114  // Non-testing builds and analysis tasks
   115  
   116  // Snapshot of dependency requirements defined in setup.py.
   117  // Results will be stored in files under Gradle build directory.
   118  def depSnapshot = tasks.register("depSnapshot") {
   119    dependsOn installGcpTest
   120  
   121    doLast {
   122      def outputDir = file(buildDir)
   123      if (!outputDir.exists()) {
   124        outputDir.mkdirs()
   125      }
   126      def requirementsFile = "${outputDir}/requirements.txt"
   127  
   128      logger.info('Snapshoting full dependencies requirements with versions info to build/requirements.txt.')
   129      exec {
   130        // Remove useless item "pkg-resources" from file which is introduced by a bug in Ubuntu.
   131        executable 'sh'
   132        args '-c', ". ${envdir}/bin/activate && pip freeze --local --all | grep -v \"pkg-resources\" > ${requirementsFile}"
   133      }
   134    }
   135  }
   136  
   137  tasks.register("dependencyUpdates") {
   138    dependsOn ':dependencyUpdates'
   139    dependsOn ':sdks:python:test-suites:tox:pycommon:dependency-check'
   140  }
   141  
   142  tasks.register("buildSnapshot") {
   143    dependsOn sdist
   144    dependsOn depSnapshot
   145  }
   146  
   147  tasks.register("startPortableRunner") {
   148    dependsOn buildPython
   149  
   150    doLast {
   151      def jobPort = project.findProperty("jobPort") ?: 8099
   152      exec {
   153        executable 'sh'
   154        args '-c', ". ${envdir}/bin/activate && python -m apache_beam.runners.portability.local_job_service_main --job_port ${jobPort}"
   155      }
   156    }
   157  }
   158  
   159  // Run this task to validate the python environment setup for contributors
   160  tasks.register("wordCount") {
   161    description "Run the Python word count example"
   162    dependsOn installGcpTest
   163    doLast {
   164      exec {
   165        executable 'sh'
   166        args '-c', ". ${envdir}/bin/activate && python -m apache_beam.examples.wordcount --runner DirectRunner --output /tmp/output.txt"
   167      }
   168    }
   169  }