Skip to content

Commit

Permalink
typespec and autorest.python working with vendored pygen
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed May 16, 2024
1 parent ead7533 commit 2bbe3ed
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 275 deletions.
49 changes: 0 additions & 49 deletions packages/autorest.python/install.py

This file was deleted.

17 changes: 8 additions & 9 deletions packages/autorest.python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "6.13.17",
"description": "The Python extension for generators in AutoRest.",
"scripts": {
"prepare": "node ./node_modules/@azure-tools/python-client-generator-core/run-python3.js ./node_modules/@azure-tools/python-client-generator-core/prepare.py",
"start": "node ./node_modules/@azure-tools/python-client-generator-core/run-python3.js start.py",
"install": "node ./node_modules/@azure-tools/python-client-generator-core/run-python3.js install.py",
"debug": "node ./node_modules/@azure-tools/python-client-generator-core/run-python3.js start.py --debug"
"prepare": "node ./node_modules/pygen/scripts/run-python3.cjs ./node_modules/pygen/scripts/prepare.py",
"start": "node ./node_modules/pygen/scripts/run-python3.cjs ./scripts/start.py",
"install": "node ./scripts/copy-pygen.js && node ./node_modules/pygen/scripts/run-python3.cjs ./node_modules/pygen/scripts/install.py && node ./node_modules/pygen/scripts/run-python3.cjs ./scripts/install.py",
"debug": "node ./node_modules/pygen/scripts/run-python3.cjs ./scripts/start.py --debug"
},
"main": "index.js",
"repository": {
Expand All @@ -25,7 +25,9 @@
},
"homepage": "https://github.com/Azure/autorest.python/blob/main/README.md",
"dependencies": {
"@autorest/system-requirements": "~1.0.2"
"@autorest/system-requirements": "~1.0.2",
"fs-extra": "^10.0.0",
"semver": "^7.3.5"
},
"devDependencies": {
"@microsoft.azure/autorest.testserver": "^3.3.46",
Expand All @@ -34,12 +36,9 @@
"files": [
"autorest/**/*.py",
"autorest/**/*.jinja2",
"scripts/",
"setup.py",
"install.py",
"prepare.py",
"start.py",
"venvtools.py",
"run-python3.js",
"requirements.txt",
"run_cadl.py"
]
Expand Down
2 changes: 1 addition & 1 deletion packages/autorest.python/run-python3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// path resolution algorithm as AutoRest so that the behavior
// is fully consistent (and also supports AUTOREST_PYTHON_EXE).
//
// Invoke it like so: "node run-python3.js script.py"
// Invoke it like so: "node run-python3.cjs script.py"

const cp = require("child_process");
const extension = require("@autorest/system-requirements");
Expand Down
15 changes: 15 additions & 0 deletions packages/autorest.python/scripts/copy-pygen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs-extra');
const path = require('path');
const url = require('url');

// Define the source and destination directories
const sourceDir = path.join(__dirname, "..", "..", 'pygen');
const destDir = path.join(__dirname, "..", "node_modules", "pygen");

// Define the filter function. Don't want to copy node_modules
const filterFunc = (src) => {
return src.indexOf('node_modules') === -1;
};

// Copy the source directory to the destination directory
fs.copySync(sourceDir, destDir, { filter: filterFunc });
58 changes: 58 additions & 0 deletions packages/autorest.python/scripts/install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import sys

if not sys.version_info >= (3, 8, 0):
raise Exception("Autorest for Python extension requires Python 3.8 at least")

try:
import pip
except ImportError:
raise Exception("Your Python installation doesn't have pip available")

try:
import venv
except ImportError:
raise Exception("Your Python installation doesn't have venv available")


# Now we have pip and Py >= 3.8, go to work

from pathlib import Path
import shutil

from venvtools import python_run

_ROOT_DIR = Path(__file__).parent.parent


def ignore_node_modules(dirname, filenames):
return ["node_modules"] if "node_modules" in filenames else []


def main():
# Define the source and destination directories
source_dir = _ROOT_DIR.parent / "pygen"
dest_dir = _ROOT_DIR / "node_modules" / "pygen"

# Copy the source directory to the destination directory
shutil.copytree(source_dir, dest_dir, dirs_exist_ok=True, ignore=ignore_node_modules)

# we use pygen's venv
venv_path = dest_dir / "venv"
assert venv_path.exists() # Otherwise install was not done

env_builder = venv.EnvBuilder(with_pip=True)
venv_context = env_builder.ensure_directories(venv_path)
# install autorest.python specific stuff into it
python_run(venv_context, "pip", ["install", "-r", "requirements.txt"])
python_run(venv_context, "pip", ["install", "-e", str(_ROOT_DIR)])


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

from venvtools import python_run

_ROOT_DIR = Path(__file__).parent
_ROOT_DIR = Path(__file__).parent.parent


def main():
# we use pygen's venv
venv_path = _ROOT_DIR.parent / "pygen" / "venv"
venv_path = _ROOT_DIR / "node_modules" / "pygen" / "venv"
venv_prexists = venv_path.exists()

assert venv_prexists # Otherwise install was not done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pathlib import Path


_ROOT_DIR = Path(__file__).parent
_ROOT_DIR = Path(__file__).parent.parent


class ExtendedEnvBuilder(venv.EnvBuilder):
Expand Down
4 changes: 2 additions & 2 deletions packages/pygen/dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-e .
-r ../../eng/requirements.txt
-r ../../eng/dev_requirements.txt
-r ../../../../eng/requirements.txt
-r ../../../../eng/dev_requirements.txt
ptvsd==4.3.2
types-PyYAML==6.0.12.8
193 changes: 0 additions & 193 deletions packages/pygen/run-python3.js

This file was deleted.

Loading

0 comments on commit 2bbe3ed

Please sign in to comment.