@azure-tools/typespec-python 0.28.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/dist/scripts/eng/format.d.ts +2 -0
  2. package/dist/scripts/eng/format.d.ts.map +1 -0
  3. package/dist/scripts/eng/format.js +4 -0
  4. package/dist/scripts/eng/format.js.map +1 -0
  5. package/dist/scripts/eng/lint.d.ts +5 -0
  6. package/dist/scripts/eng/lint.d.ts.map +1 -0
  7. package/dist/scripts/eng/lint.js +66 -0
  8. package/dist/scripts/eng/lint.js.map +1 -0
  9. package/dist/scripts/{regenerate.d.ts.map → eng/regenerate.d.ts.map} +1 -1
  10. package/dist/scripts/{regenerate.js → eng/regenerate.js} +33 -30
  11. package/dist/scripts/eng/regenerate.js.map +1 -0
  12. package/dist/scripts/eng/run-tests.d.ts +2 -0
  13. package/dist/scripts/eng/run-tests.d.ts.map +1 -0
  14. package/dist/scripts/eng/run-tests.js +71 -0
  15. package/dist/scripts/eng/run-tests.js.map +1 -0
  16. package/dist/scripts/eng/utils.d.ts +3 -0
  17. package/dist/scripts/eng/utils.d.ts.map +1 -0
  18. package/dist/scripts/eng/utils.js +39 -0
  19. package/dist/scripts/eng/utils.js.map +1 -0
  20. package/dist/scripts/run-python3.d.ts +2 -0
  21. package/dist/scripts/run-python3.d.ts.map +1 -0
  22. package/dist/scripts/run-python3.js +23 -0
  23. package/dist/scripts/run-python3.js.map +1 -0
  24. package/dist/scripts/system-requirements.d.ts +17 -0
  25. package/dist/scripts/system-requirements.d.ts.map +1 -0
  26. package/{scripts/system-requirements.cjs → dist/scripts/system-requirements.js} +80 -97
  27. package/dist/scripts/system-requirements.js.map +1 -0
  28. package/dist/src/code-model.d.ts.map +1 -1
  29. package/dist/src/code-model.js +23 -17
  30. package/dist/src/code-model.js.map +1 -1
  31. package/dist/src/emitter.d.ts.map +1 -1
  32. package/dist/src/emitter.js +14 -3
  33. package/dist/src/emitter.js.map +1 -1
  34. package/dist/src/external-process.js +1 -1
  35. package/dist/src/external-process.js.map +1 -1
  36. package/dist/src/http.js +34 -23
  37. package/dist/src/http.js.map +1 -1
  38. package/dist/src/lib.d.ts +1 -0
  39. package/dist/src/lib.d.ts.map +1 -1
  40. package/dist/src/lib.js +1 -0
  41. package/dist/src/lib.js.map +1 -1
  42. package/dist/src/types.d.ts +7 -1
  43. package/dist/src/types.d.ts.map +1 -1
  44. package/dist/src/types.js +42 -7
  45. package/dist/src/types.js.map +1 -1
  46. package/dist/src/utils.d.ts +3 -3
  47. package/dist/src/utils.d.ts.map +1 -1
  48. package/dist/src/utils.js +11 -13
  49. package/dist/src/utils.js.map +1 -1
  50. package/generator/pygen/black.py +2 -3
  51. package/generator/pygen/codegen/models/__init__.py +2 -0
  52. package/generator/pygen/codegen/models/code_model.py +2 -4
  53. package/generator/pygen/codegen/models/combined_type.py +1 -1
  54. package/generator/pygen/codegen/models/credential_types.py +7 -14
  55. package/generator/pygen/codegen/models/enum_type.py +1 -1
  56. package/generator/pygen/codegen/models/lro_operation.py +0 -1
  57. package/generator/pygen/codegen/models/lro_paging_operation.py +1 -1
  58. package/generator/pygen/codegen/models/model_type.py +6 -9
  59. package/generator/pygen/codegen/models/operation.py +13 -16
  60. package/generator/pygen/codegen/models/paging_operation.py +0 -1
  61. package/generator/pygen/codegen/models/parameter_list.py +2 -5
  62. package/generator/pygen/codegen/models/primitive_types.py +35 -3
  63. package/generator/pygen/codegen/models/property.py +1 -9
  64. package/generator/pygen/codegen/serializers/__init__.py +1 -1
  65. package/generator/pygen/codegen/serializers/builder_serializer.py +12 -13
  66. package/generator/pygen/codegen/serializers/general_serializer.py +2 -2
  67. package/generator/pygen/codegen/serializers/model_serializer.py +2 -0
  68. package/generator/pygen/codegen/serializers/sample_serializer.py +20 -15
  69. package/generator/pygen/codegen/templates/model_base.py.jinja2 +30 -18
  70. package/generator/pygen/codegen/templates/vendor.py.jinja2 +0 -2
  71. package/generator/pygen/m2r.py +1 -1
  72. package/generator/pygen/postprocess/__init__.py +2 -2
  73. package/generator/pygen/postprocess/venvtools.py +1 -3
  74. package/generator/pygen/preprocess/__init__.py +1 -1
  75. package/generator/pygen/utils.py +1 -3
  76. package/generator/setup.py +1 -1
  77. package/package.json +18 -25
  78. package/scripts/__pycache__/venvtools.cpython-310.pyc +0 -0
  79. package/scripts/eng/format.ts +5 -0
  80. package/scripts/eng/lint.ts +75 -0
  81. package/scripts/eng/mypy.ini +38 -0
  82. package/scripts/eng/pylintrc +58 -0
  83. package/scripts/eng/pyrightconfig.json +6 -0
  84. package/scripts/{regenerate.ts → eng/regenerate.ts} +45 -33
  85. package/scripts/eng/run-tests.ts +80 -0
  86. package/scripts/eng/utils.ts +38 -0
  87. package/scripts/run-python3.ts +25 -0
  88. package/scripts/run_tsp.py +7 -5
  89. package/scripts/system-requirements.ts +253 -0
  90. package/dist/scripts/regenerate.js.map +0 -1
  91. package/scripts/run-python3.cjs +0 -22
  92. /package/dist/scripts/{regenerate.d.ts → eng/regenerate.d.ts} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-tools/typespec-python",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
4
4
  "author": "Microsoft Corporation",
5
5
  "description": "TypeSpec emitter for Python SDKs",
6
6
  "homepage": "https://github.com/Azure/autorest.python",
@@ -36,60 +36,53 @@
36
36
  "@azure-tools/typespec-azure-core": ">=0.45.0 <1.0.0",
37
37
  "@azure-tools/typespec-azure-resource-manager": ">=0.45.0 <1.0.0",
38
38
  "@azure-tools/typespec-autorest": ">=0.45.0 <1.0.0",
39
- "@azure-tools/typespec-client-generator-core": ">=0.45.1 <1.0.0",
39
+ "@azure-tools/typespec-client-generator-core": ">=0.45.4 <1.0.0",
40
40
  "@azure-tools/typespec-azure-rulesets": ">=0.45.0 <3.0.0",
41
- "@typespec/compiler": ">=0.59.0 <1.0.0",
41
+ "@typespec/compiler": ">=0.59.1 <1.0.0",
42
42
  "@typespec/http": ">=0.59.0 <1.0.0",
43
43
  "@typespec/rest": ">=0.59.0 <1.0.0",
44
44
  "@typespec/versioning": ">=0.59.0 <1.0.0",
45
45
  "@typespec/openapi": ">=0.59.0 <1.0.0"
46
46
  },
47
- "dependenciesMeta": {
48
- "@azure-tools/typespec-client-generator-core": {
49
- "injected": true
50
- }
51
- },
52
47
  "dependencies": {
53
48
  "js-yaml": "~4.1.0",
54
49
  "@typespec/openapi3": "~0.59.0",
55
- "@autorest/system-requirements": "~1.0.2",
56
- "fs-extra": "~11.2.0",
57
- "semver": "~7.6.2"
50
+ "semver": "~7.6.2",
51
+ "tsx": "4.17.0"
58
52
  },
59
53
  "devDependencies": {
60
54
  "@azure-tools/typespec-azure-resource-manager": "~0.45.0",
61
55
  "@azure-tools/typespec-autorest": "~0.45.0",
62
- "@azure-tools/cadl-ranch-expect": "~0.14.1",
63
- "@azure-tools/cadl-ranch-specs": "~0.34.5",
56
+ "@azure-tools/cadl-ranch-expect": "~0.15.1",
57
+ "@azure-tools/cadl-ranch-specs": "~0.36.1",
64
58
  "@types/js-yaml": "~4.0.5",
65
- "@types/mocha": "~10.0.1",
66
59
  "@types/node": "^18.16.3",
67
60
  "@types/yargs": "17.0.32",
61
+ "@types/semver": "7.5.8",
68
62
  "@typespec/eslint-config-typespec": "~0.55.0",
69
63
  "@typespec/openapi": "~0.59.0",
70
64
  "c8": "~7.13.0",
71
- "eslint": "^8.57.0",
72
- "mocha": "~10.2.0",
73
65
  "rimraf": "~5.0.0",
74
66
  "typescript": "~5.1.3",
75
67
  "@azure-tools/typespec-azure-core": "~0.45.0",
76
- "@azure-tools/typespec-client-generator-core": "0.45.1",
77
- "@typespec/compiler": "~0.59.0",
68
+ "@azure-tools/typespec-client-generator-core": "0.45.4",
69
+ "@typespec/compiler": "~0.59.1",
78
70
  "@typespec/http": "~0.59.0",
79
71
  "@typespec/rest": "~0.59.0",
80
72
  "@typespec/versioning": "~0.59.0",
81
73
  "@azure-tools/typespec-azure-rulesets": "0.45.0",
82
- "yargs": "~17.2.1"
74
+ "yargs": "~17.2.1",
75
+ "chalk": "5.3.0"
83
76
  },
84
77
  "scripts": {
85
- "clean": "rimraf ./dist ./temp",
78
+ "clean": "rimraf ./dist ./temp ./venv ./node_modules",
86
79
  "build": "tsc -p .",
87
80
  "watch": "tsc -p . --watch",
88
- "test": "mocha",
89
- "test-official": "c8 mocha --forbid-only",
90
- "lint": "eslint . --ext .ts --max-warnings=0",
81
+ "lint": "tsx ./scripts/eng/lint.ts",
91
82
  "lint:fix": "eslint . --fix --ext .ts",
92
- "install": "node ./scripts/run-python3.cjs ./scripts/install.py",
93
- "regenerate": "node ./dist/scripts/regenerate.js"
83
+ "format": "npx prettier **/*.ts --write && tsx ./scripts/eng/format.ts",
84
+ "install": "tsx ./scripts/run-python3.ts ./scripts/install.py",
85
+ "regenerate": "tsx ./scripts/eng/regenerate.ts",
86
+ "test": "tsx ./scripts/eng/run-tests.ts"
94
87
  }
95
88
  }
@@ -0,0 +1,5 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import { runCommand } from "./utils.js";
4
+
5
+ runCommand("black .", "black");
@@ -0,0 +1,75 @@
1
+ /* eslint-disable no-console */
2
+ import yargs from "yargs";
3
+ import { hideBin } from "yargs/helpers";
4
+ import { runCommand, executeCommand } from "./utils.js";
5
+
6
+ interface Arguments {
7
+ folderName: string;
8
+ command?: "pylint" | "mypy" | "pyright" | "eslint";
9
+ skipWarning?: boolean;
10
+ skipEslint?: boolean;
11
+ }
12
+
13
+ const validCommands = ["pylint", "mypy", "pyright", "eslint"];
14
+
15
+ // PARSE INPUT ARGUMENTS
16
+ const argv = yargs(hideBin(process.argv))
17
+ .option("folderName", {
18
+ type: "string",
19
+ choices: ["generator", "autorest"],
20
+ description: "Specify the flavor",
21
+ default: "generator",
22
+ })
23
+ .option("command", {
24
+ alias: "c",
25
+ type: "string",
26
+ choices: validCommands,
27
+ description: "Specify the command to run",
28
+ })
29
+ .option("skipWarning", {
30
+ alias: "s",
31
+ type: "boolean",
32
+ description: "Skip to check warnings",
33
+ })
34
+ .option("skipEslint", {
35
+ alias: "e",
36
+ type: "boolean",
37
+ description: "Skip to check eslint",
38
+ }).argv as Arguments;
39
+
40
+ export function pylint() {
41
+ runCommand(`pylint ${argv.folderName}/ --rcfile ./scripts/eng/pylintrc`, "pylint");
42
+ }
43
+
44
+ export function mypy() {
45
+ runCommand(`mypy ${argv.folderName}/ --config-file ./scripts/eng/mypy.ini`, "mypy");
46
+ }
47
+
48
+ export function pyright() {
49
+ runCommand(`pyright ${argv.folderName}/ -p ./scripts/eng/pyrightconfig.json`, "pyright");
50
+ }
51
+
52
+ export function eslint() {
53
+ // const checkWarning = argv.skipWarning ? "" : "--max-warnings=0";
54
+ const checkWarning = "";
55
+ executeCommand(`npx eslint . --ext .ts ${checkWarning} `, "eslint");
56
+ }
57
+
58
+ if (argv.command === "pylint") {
59
+ pylint();
60
+ } else if (argv.command === "mypy") {
61
+ mypy();
62
+ } else if (argv.command === "pyright") {
63
+ pyright();
64
+ } else if (argv.command === "eslint") {
65
+ if (!argv.skipEslint) {
66
+ eslint();
67
+ }
68
+ } else {
69
+ pylint();
70
+ mypy();
71
+ pyright();
72
+ if (!argv.skipEslint) {
73
+ eslint();
74
+ }
75
+ }
@@ -0,0 +1,38 @@
1
+ # global configurations
2
+ [mypy]
3
+ python_version = 3.8
4
+
5
+
6
+ # module level configuratiohns
7
+ [mypy-jsonrpc.*]
8
+ ignore_missing_imports = True
9
+
10
+ [mypy-ptvsd.*]
11
+ ignore_missing_imports = True
12
+
13
+ [mypy-debugpy.*]
14
+ ignore_missing_imports = True
15
+
16
+ [mypy-m2r2.*]
17
+ ignore_missing_imports = True
18
+
19
+ [mypy-autorest.common.utils.*]
20
+ ignore_missing_imports = True
21
+
22
+ [mypy-autorest.common.python_mappings.*]
23
+ ignore_missing_imports = True
24
+
25
+ [mypy-pygen.codegen.models.*]
26
+ ignore_missing_imports = True
27
+
28
+ [mypy-setuptools]
29
+ ignore_missing_imports = True
30
+
31
+ [mypy-*._patch]
32
+ ignore_missing_imports = True
33
+
34
+ [mypy-pygen.*]
35
+ ignore_missing_imports = True
36
+
37
+ [mypy-yaml.*]
38
+ ignore_missing_imports = True
@@ -0,0 +1,58 @@
1
+ [MASTER]
2
+ py-version=3.8
3
+ ignore-patterns=test_*,conftest,setup
4
+ reports=no
5
+
6
+ # PYLINT DIRECTORY BLACKLIST.
7
+ ignore=_generated,samples,examples,test,tests,doc,.tox,generated_samples
8
+
9
+ [MESSAGES CONTROL]
10
+
11
+ # Add enable for useless disables
12
+ enable=useless-suppression
13
+
14
+ # For all codes, run 'pylint --list-msgs' or go to 'https://pylint.readthedocs.io/en/latest/reference_guide/features.html'
15
+ # locally-disabled: Warning locally suppressed using disable-msg
16
+ # cyclic-import: because of https://github.com/PyCQA/pylint/issues/850
17
+ # too-many-arguments: Due to the nature of the CLI many commands have large arguments set which reflect in large arguments set in corresponding methods.
18
+ # Let's black deal with bad-continuation
19
+ disable=useless-object-inheritance,missing-docstring,locally-disabled,fixme,cyclic-import,too-many-arguments,invalid-name,duplicate-code,too-few-public-methods,consider-using-f-string,super-with-arguments,redefined-builtin,import-outside-toplevel,client-suffix-needed,unnecessary-dunder-call,unnecessary-ellipsis,disallowed-name,consider-using-max-builtin
20
+
21
+ [FORMAT]
22
+ max-line-length=120
23
+
24
+ [VARIABLES]
25
+ # Tells whether we should check for unused import in __init__ files.
26
+ init-import=yes
27
+
28
+ [DESIGN]
29
+ # Maximum number of locals for function / method body
30
+ max-locals=25
31
+ # Maximum number of branch for function / method body
32
+ max-branches=20
33
+ # Maximum number of instance attributes for class
34
+ max-attributes=10
35
+ # Maximum number of ancestors
36
+ max-parents=15
37
+
38
+ [SIMILARITIES]
39
+ min-similarity-lines=10
40
+
41
+ [BASIC]
42
+ # Naming hints based on PEP 8 (https://www.python.org/dev/peps/pep-0008/#naming-conventions).
43
+ # Consider these guidelines and not hard rules. Read PEP 8 for more details.
44
+
45
+ # The invalid-name checker must be **enabled** for these hints to be used.
46
+ include-naming-hint=yes
47
+
48
+ # keep short; underscores are discouraged
49
+ module-naming-style=snake_case
50
+ const-naming-style=UPPER_CASE
51
+ class-naming-style=PascalCase
52
+ class-attribute-naming-style=snake_case
53
+ attr-naming-style=snake_case
54
+ method-naming-style=snake_case
55
+ function-naming-style=snake_case
56
+ argument-naming-style=snake_case
57
+ variable-naming-style=snake_case
58
+ inlinevar-naming-style=snake_case
@@ -0,0 +1,6 @@
1
+ {
2
+ "reportUnnecessaryCast": "warning",
3
+ "reportTypeCommentUsage": true,
4
+ "reportMissingImports": false,
5
+ "pythonVersion": "3.8"
6
+ }
@@ -4,7 +4,7 @@ import { promisify } from "util";
4
4
  import yargs from "yargs";
5
5
  import { hideBin } from "yargs/helpers";
6
6
  import { dirname, join, relative, resolve } from "path";
7
- import { promises } from "fs";
7
+ import { promises, rmSync } from "fs";
8
8
  import { fileURLToPath } from "url";
9
9
 
10
10
  // Promisify the exec function
@@ -13,6 +13,10 @@ const exec = promisify(execCallback);
13
13
  // Get the directory of the current file
14
14
  const PLUGIN_DIR = resolve(fileURLToPath(import.meta.url), "../../../");
15
15
  const CADL_RANCH_DIR = resolve(PLUGIN_DIR, "node_modules/@azure-tools/cadl-ranch-specs/http");
16
+ interface TspCommand {
17
+ outputDir: string;
18
+ command: string;
19
+ }
16
20
 
17
21
  const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<string, string>[]> = {
18
22
  "resiliency/srv-driven/old.tsp": {
@@ -117,9 +121,15 @@ function getEmitterOption(spec: string): Record<string, string>[] {
117
121
  }
118
122
 
119
123
  // Function to execute CLI commands asynchronously
120
- async function executeCommand(command: string): Promise<void> {
124
+ async function executeCommand(tspCommand: TspCommand): Promise<void> {
125
+ try {
126
+ rmSync(tspCommand.outputDir, { recursive: true, force: true });
127
+ } catch (error) {
128
+ console.error(`rm error: ${error}`);
129
+ }
121
130
  try {
122
- const { stdout, stderr } = await exec(command);
131
+ console.log(`exec: ${tspCommand.command}`);
132
+ const { stdout, stderr } = await exec(tspCommand.command);
123
133
  if (stdout) console.log(`stdout: ${stdout}`);
124
134
  if (stderr) console.error(`stderr: ${stderr}`);
125
135
  } catch (error) {
@@ -162,6 +172,12 @@ async function getSubdirectories(baseDir: string, flags: RegenerateFlags): Promi
162
172
  const mainTspRelativePath = toPosix(relative(baseDir, mainTspPath));
163
173
  if (flags.flavor === "unbranded" && mainTspRelativePath.includes("azure")) return;
164
174
 
175
+ // after xml support, remove this check
176
+ if (mainTspRelativePath.includes("xml")) return;
177
+
178
+ // after fix test generation for nested operation group, remove this check
179
+ if (mainTspRelativePath.includes("client-operation-group")) return;
180
+
165
181
  const hasMainTsp = await promises
166
182
  .access(mainTspPath)
167
183
  .then(() => true)
@@ -200,8 +216,13 @@ function defaultPackageName(spec: string): string {
200
216
  .toLowerCase();
201
217
  }
202
218
 
203
- function addOptions(spec: string, generatedFolder: string, flags: RegenerateFlags): string[] {
204
- const emitterConfigs: string[] = [];
219
+ interface EmitterConfig {
220
+ optionsStr: string;
221
+ outputDir: string;
222
+ }
223
+
224
+ function addOptions(spec: string, generatedFolder: string, flags: RegenerateFlags): EmitterConfig[] {
225
+ const emitterConfigs: EmitterConfig[] = [];
205
226
  for (const config of getEmitterOption(spec)) {
206
227
  const options: Record<string, string> = { ...config };
207
228
  options["flavor"] = flags.flavor;
@@ -218,51 +239,42 @@ function addOptions(spec: string, generatedFolder: string, flags: RegenerateFlag
218
239
  if (flags.flavor === "unbranded") {
219
240
  options["company-name"] = "Unbranded";
220
241
  }
221
- options["examples-directory"] = toPosix(join(dirname(spec), "examples"));
242
+ options["examples-dir"] = toPosix(join(dirname(spec), "examples"));
222
243
  const configs = Object.entries(options).flatMap(([k, v]) => {
223
244
  return `--option @azure-tools/typespec-python.${k}=${v}`;
224
245
  });
225
- emitterConfigs.push(configs.join(" "));
246
+ emitterConfigs.push({
247
+ optionsStr: configs.join(" "),
248
+ outputDir: options["emitter-output-dir"],
249
+ });
226
250
  }
227
251
  return emitterConfigs;
228
252
  }
229
-
230
- async function _regenerateSingle(spec: string, flags: RegenerateFlags): Promise<void> {
231
- // Perform some asynchronous operation here
232
- const options = addOptions(spec, PLUGIN_DIR, flags);
233
- const commandPromises = options.map((option) => {
234
- const command = `tsp compile ${spec} --emit=${toPosix(PLUGIN_DIR)} ${option}`;
235
- console.log(command);
236
- return executeCommand(command);
253
+ function _getCmdList(spec: string, flags: RegenerateFlags): TspCommand[] {
254
+ return addOptions(spec, PLUGIN_DIR, flags).map((option) => {
255
+ return {
256
+ outputDir: option.outputDir,
257
+ command: `tsp compile ${spec} --emit=${toPosix(PLUGIN_DIR)} ${option.optionsStr}`,
258
+ };
237
259
  });
238
- await Promise.all(commandPromises);
239
260
  }
240
261
 
241
- async function regenerate(flags: RegenerateFlagsInput): Promise<boolean> {
262
+ async function regenerate(flags: RegenerateFlagsInput): Promise<void> {
242
263
  if (flags.flavor === undefined) {
243
- const azureGeneration = await regenerate({ ...flags, flavor: "azure" });
244
- const unbrandedGeneration = await regenerate({ ...flags, flavor: "unbranded" });
245
- return azureGeneration && unbrandedGeneration;
264
+ await regenerate({ ...flags, flavor: "azure" });
265
+ await regenerate({ ...flags, flavor: "unbranded" });
246
266
  } else {
247
267
  const flagsResolved = { debug: false, flavor: flags.flavor, ...flags };
248
268
  const CADL_RANCH_DIR = resolve(PLUGIN_DIR, "node_modules/@azure-tools/cadl-ranch-specs/http");
249
269
  const subdirectories = await getSubdirectories(CADL_RANCH_DIR, flagsResolved);
250
- const promises = subdirectories.map(async (subdirectory) => {
251
- // Perform additional asynchronous operations on each subdirectory here
252
- await _regenerateSingle(subdirectory, flagsResolved);
253
- });
254
- await Promise.all(promises);
255
- return true;
270
+ const cmdList: TspCommand[] = subdirectories.flatMap((subdirectory) =>
271
+ _getCmdList(subdirectory, flagsResolved),
272
+ );
273
+ const PromiseCommands = cmdList.map((tspCommand) => executeCommand(tspCommand));
274
+ await Promise.all(PromiseCommands);
256
275
  }
257
276
  }
258
277
 
259
- // try {
260
- // const output = await executeCommand('tsp compile');
261
- // console.log(`Command output: ${output}`);
262
- // } catch (error) {
263
- // console.error(`Command failed: ${error}`);
264
- // }
265
-
266
278
  // PARSE INPUT ARGUMENTS
267
279
  const argv = yargs(hideBin(process.argv))
268
280
  .option("flavor", {
@@ -0,0 +1,80 @@
1
+ /* eslint-disable no-console */
2
+ import { execSync } from "child_process";
3
+ import { readFileSync } from "fs";
4
+ import { join } from "path";
5
+ import yargs from "yargs";
6
+ import { hideBin } from "yargs/helpers";
7
+ import { fileURLToPath } from "url";
8
+
9
+ interface Arguments {
10
+ validFolders: string[];
11
+ folder?: string;
12
+ command?: string;
13
+ }
14
+
15
+ const validCommands = ["ci", "lint", "mypy", "pyright", "apiview"];
16
+
17
+ // Parse command-line arguments using yargs
18
+ const argv = yargs(hideBin(process.argv))
19
+ .option("validFolders", {
20
+ alias: "vf",
21
+ describe: "Specify the valid folders",
22
+ type: "array",
23
+ default: ["azure", "unbranded"],
24
+ })
25
+ .option("folder", {
26
+ alias: "f",
27
+ describe: "Specify the folder to use",
28
+ type: "string",
29
+ })
30
+ .option("command", {
31
+ alias: "c",
32
+ describe: "Specify the command to run",
33
+ choices: validCommands,
34
+ type: "string",
35
+ }).argv as Arguments;
36
+
37
+ const foldersToProcess = argv.folder ? [argv.folder] : argv.validFolders;
38
+
39
+ const commandToRun = argv.command || "all";
40
+
41
+ function getCommand(command: string, folder: string) {
42
+ if (!validCommands.includes(command)) throw new Error(`Unknown command '${command}'.`);
43
+ return `FOLDER=${folder} tox -c ./test/${folder}/tox.ini -e ${command}`;
44
+ }
45
+
46
+ function sectionExistsInToxIni(command: string, folder: string): boolean {
47
+ const toxIniPath = join(fileURLToPath(import.meta.url), `../../../test/${folder}/tox.ini`);
48
+ const toxIniContent = readFileSync(toxIniPath, "utf-8");
49
+ const sectionHeader = `[testenv:${command}]`;
50
+ return toxIniContent.includes(sectionHeader);
51
+ }
52
+
53
+ function myExecSync(command: string, folder: string): void {
54
+ if (!sectionExistsInToxIni(command, folder)) {
55
+ console.log(`No section for ${command} in tox.ini for folder ${folder}. Skipping...`);
56
+ return;
57
+ }
58
+ execSync(getCommand(command, folder), { stdio: "inherit" });
59
+ }
60
+
61
+ foldersToProcess.forEach((folder) => {
62
+ try {
63
+ if (commandToRun === "all") {
64
+ for (const key of validCommands) {
65
+ console.log(`Running ${key} for folder ${folder}...`);
66
+ myExecSync(key, folder);
67
+ }
68
+ } else if (getCommand(commandToRun, folder)) {
69
+ console.log(`Running ${commandToRun} for folder ${folder}...`);
70
+ myExecSync(commandToRun, folder);
71
+ } else {
72
+ console.error(`Error: Unknown command '${commandToRun}'.`);
73
+ process.exit(1);
74
+ }
75
+ } catch (error) {
76
+ console.error((error as Error).message);
77
+ console.error(`Error executing command for folder ${folder}: ${(error as Error).message}`);
78
+ process.exit(1);
79
+ }
80
+ });
@@ -0,0 +1,38 @@
1
+ /* eslint-disable no-console */
2
+ import { exec } from "child_process";
3
+ import process from "process";
4
+ import { existsSync } from "fs";
5
+ import { dirname, join } from "path";
6
+ import { fileURLToPath } from "url";
7
+ import chalk from "chalk";
8
+
9
+ // execute the command
10
+ export function executeCommand(command: string, prettyName: string) {
11
+ exec(command, (error, stdout, stderr) => {
12
+ if (error) {
13
+ console.error(chalk.red(`Error executing ${command}(stdout): ${stdout}`));
14
+ console.error(chalk.red(`Error executing ${command}{stderr}: ${stderr}`));
15
+ process.exit(1);
16
+ }
17
+ if (stderr) {
18
+ // Process stderr output
19
+ console.log(chalk.yellow(`${command}:\n${stderr}`));
20
+ return;
21
+ }
22
+ console.log(chalk.green(`${prettyName} passed`));
23
+ });
24
+ }
25
+
26
+ // Function to run a command and log the output
27
+ export function runCommand(command: string, prettyName: string) {
28
+ let pythonPath = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "venv/");
29
+ if (existsSync(join(pythonPath, "bin"))) {
30
+ pythonPath = join(pythonPath, "bin", "python");
31
+ } else if (existsSync(join(pythonPath, "Scripts"))) {
32
+ pythonPath = join(pythonPath, "Scripts", "python");
33
+ } else {
34
+ throw new Error(pythonPath);
35
+ }
36
+ command = `${pythonPath} -m ${command}`;
37
+ executeCommand(command, prettyName);
38
+ }
@@ -0,0 +1,25 @@
1
+ // This script wraps logic in @azure-tools/extension to resolve
2
+ // the path to Python 3 so that a Python script file can be run
3
+ // from an npm script in package.json. It uses the same Python 3
4
+ // path resolution algorithm as AutoRest so that the behavior
5
+ // is fully consistent (and also supports AUTOREST_PYTHON_EXE).
6
+ //
7
+ // Invoke it like so: "tsx run-python3.ts script.py"
8
+
9
+ import cp from "child_process";
10
+ import { patchPythonPath } from "./system-requirements.js";
11
+
12
+ async function runPython3(...args: string[]) {
13
+ const command = await patchPythonPath(["python", ...args], {
14
+ version: ">=3.8",
15
+ environmentVariable: "AUTOREST_PYTHON_EXE",
16
+ });
17
+ cp.execSync(command.join(" "), {
18
+ stdio: [0, 1, 2],
19
+ });
20
+ }
21
+
22
+ runPython3(...process.argv.slice(2)).catch((err) => {
23
+ console.error(err.toString()); // eslint-disable-line no-console
24
+ process.exit(1);
25
+ });
@@ -7,7 +7,8 @@ import sys
7
7
  import venv
8
8
  import logging
9
9
  from pathlib import Path
10
- from venvtools import python_run
10
+ from pygen import m2r, preprocess, codegen, black
11
+ from pygen.utils import parse_args
11
12
 
12
13
  _ROOT_DIR = Path(__file__).parent.parent
13
14
 
@@ -34,7 +35,8 @@ if __name__ == "__main__":
34
35
  breakpoint() # pylint: disable=undefined-variable
35
36
 
36
37
  # run m2r
37
- python_run(venv_context, "generator.pygen.m2r", command=sys.argv[1:])
38
- python_run(venv_context, "generator.pygen.preprocess.__init__", command=sys.argv[1:])
39
- python_run(venv_context, "generator.pygen.codegen.__init__", command=sys.argv[1:])
40
- python_run(venv_context, "generator.pygen.black", command=sys.argv[1:])
38
+ args, unknown_args = parse_args()
39
+ m2r.M2R(output_folder=args.output_folder, cadl_file=args.cadl_file, **unknown_args).process()
40
+ preprocess.PreProcessPlugin(output_folder=args.output_folder, cadl_file=args.cadl_file, **unknown_args).process()
41
+ codegen.CodeGenerator(output_folder=args.output_folder, cadl_file=args.cadl_file, **unknown_args).process()
42
+ black.BlackScriptPlugin(output_folder=args.output_folder, **unknown_args).process()