Skip to content

Generate Python Mgmt SDK from Typespec

Yuchao Yan edited this page Dec 12, 2024 · 9 revisions

Getting Started - Generate MGMT SDK With MPG and Typespec

Prerequisites

Prepare basic environment

  • Python 3.8 or later is required

    • download for windows
    • linux
      • sudo apt install python3
      • sudo apt install python3-pip
      • sudo apt install python3.{?}-venv explicitly if needed
  • Node.js 18.3 LTS or later is required

Setup your repo

Identify tspconfig.yaml

Identify the tspconfig.yaml file for your package in the Rest repo and ensure there is a configuration for the Python SDK similar to that shown below and in this example

parameters:
  "service-dir":
    default: "sdk/SERVICE_DIRECTORY_NAME"
options:
  "@azure-tools/typespec-python":
    package-dir: "azure-mgmt-PACKAGE_NAME"
    package-name: "{package-dir}"
    generate-test: true
    generate-sample: true
    flavor: "azure"

nit: please replace SERVICE_DIRECTORY_NAME/PACKAGE_NAME with real value as needed.

Install necessary dependencies

  1. Install typespec-client-generator-cli globally:
npm install -g @azure-tools/typespec-client-generator-cli
  1. Create Python virtual environments and activate it:
PS C:\dev\azure-sdk-for-python> python -m venv .venv
PS C:\dev\azure-sdk-for-python> .\.venv\Scripts\Activate.ps1 (Windows)
   /C/dev/azure-sdk-for-python> source .venv/bin/activate (Linux)
  1. Install Python dependencies:
(.venv) PS C:\dev\azure-sdk-for-python> python .\scripts\dev_setup.py -p azure-core
(.venv) PS C:\dev\azure-sdk-for-python> pip install tox

Generate SDK

  1. Create a local json file named generatedInput.json outside SDK repo with content similar to that shown below
   {
     "dryRun": false,
     "specFolder": "LOCAL_AZURE-REST-API-SPECS_REPO_ROOT", // e.g. "C:/dev/azure-rest-api-specs"
     "headSha": "SHA_OF_AZURE-REST-API-SPECS_REPO", // use ' git rev-parse HEAD ' on the local rest repo root 
     "repoHttpsUrl": "https://github.com/Azure/azure-rest-api-specs",
     "relatedTypeSpecProjectFolder": [
       "specification/SERVICE_DIRECTORY_NAME/PACKAGE_DIRECTORY_NAME/" // e.g specification/contosowidgetmanager/Contoso.Management
     ]
   }
  1. Run command to generate SDK
(.venv) PS C:\dev\azure-sdk-for-python> python -m packaging_tools.sdk_package ..\generatedInput.json ..\temp.json
(.venv) PS C:\dev\azure-sdk-for-python> python -m packaging_tools.sdk_package ..\temp.json ..\generatedOutput.json
  1. View information about the generated SDK in generateOutput.json

What to do after generating the SDK code

The generated code is not enough to release at once and you need to update it for better usage experience. Please follow What to do after generating the SDK code with MPG to check the code.