Skip to content

Commit

Permalink
generation script, check_call with shell=False (Azure#40583)
Browse files Browse the repository at this point in the history
* set shell=False

fix python

* format

* pin python version

* revert python change
  • Loading branch information
XiaofeiCao authored Jun 13, 2024
1 parent 9f43ca7 commit 4d1114a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
47 changes: 38 additions & 9 deletions eng/mgmt/automation/generate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from utils import update_service_ci_and_pom
from utils import update_root_pom
from utils import update_version
from utils import is_windows

os.chdir(pwd)

Expand Down Expand Up @@ -71,7 +72,15 @@ def generate(
os.path.abspath(sdk_root),
os.path.abspath(output_dir),
namespace,
" ".join((tag_option, version_option, FLUENTLITE_ARGUMENTS, autorest_options, readme)),
" ".join(
(
tag_option,
version_option,
FLUENTLITE_ARGUMENTS,
autorest_options,
readme,
)
),
)
)
logging.info(command)
Expand Down Expand Up @@ -193,22 +202,30 @@ def compare_with_maven_package(sdk_root: str, service: str, stable_version: str,
if beta_version_int > 1:
previous_beta_version_int = beta_version_int - 1
previous_beta_version = current_version.replace(
"-beta." + str(beta_version_int), "-beta." + str(previous_beta_version_int)
"-beta." + str(beta_version_int),
"-beta." + str(previous_beta_version_int),
)
stable_version = previous_beta_version

logging.info(
"[Changelog] Compare stable version {0} with current version {1}".format(stable_version, current_version)
)

r = requests.get(MAVEN_URL.format(group_id=GROUP_ID.replace(".", "/"), artifact_id=module, version=stable_version))
r = requests.get(
MAVEN_URL.format(
group_id=GROUP_ID.replace(".", "/"),
artifact_id=module,
version=stable_version,
)
)
r.raise_for_status()
old_jar_fd, old_jar = tempfile.mkstemp(".jar")
try:
with os.fdopen(old_jar_fd, "wb") as tmp:
tmp.write(r.content)
new_jar = os.path.join(
sdk_root, JAR_FORMAT.format(service=service, artifact_id=module, version=current_version)
sdk_root,
JAR_FORMAT.format(service=service, artifact_id=module, version=current_version),
)
if not os.path.exists(new_jar):
raise Exception("Cannot found built jar in {0}".format(new_jar))
Expand Down Expand Up @@ -345,14 +362,21 @@ def generate_typespec_project(
tspconfig_valid = True
if url_match:
# generate from remote url
tsp_cmd = ["npx", "tsp-client", "init", "--debug", "--tsp-config", tsp_project]
tsp_cmd = [
"npx" + (".cmd" if is_windows() else ""),
"tsp-client",
"init",
"--debug",
"--tsp-config",
tsp_project,
]
else:
# sdk automation
tsp_dir = os.path.join(spec_root, tsp_project) if spec_root else tsp_project
tspconfig_valid = validate_tspconfig(tsp_dir)
repo = remove_prefix(repo_url, "https://github.com/")
tsp_cmd = [
"npx",
"npx" + (".cmd" if is_windows() else ""),
"tsp-client",
"init",
"--debug",
Expand All @@ -367,7 +391,7 @@ def generate_typespec_project(
]

if tspconfig_valid:
check_call(tsp_cmd, sdk_root, shell=True)
check_call(tsp_cmd, sdk_root)

sdk_folder = find_sdk_folder(sdk_root)
logging.info("SDK folder: " + sdk_folder)
Expand All @@ -379,7 +403,12 @@ def generate_typespec_project(
# check require_sdk_integration
cmd = ["git", "add", "."]
check_call(cmd, sdk_root)
cmd = ["git", "status", "--porcelain", os.path.join(sdk_folder, "pom.xml")]
cmd = [
"git",
"status",
"--porcelain",
os.path.join(sdk_folder, "pom.xml"),
]
logging.info("Command line: " + " ".join(cmd))
output = subprocess.check_output(cmd, cwd=sdk_root)
output_str = str(output, "utf-8")
Expand All @@ -393,7 +422,7 @@ def generate_typespec_project(
drop_changes(sdk_root)
remove_generated_source_code(sdk_folder, f"{group_id}.{service}")
# regenerate
check_call(tsp_cmd, sdk_root, shell=True)
check_call(tsp_cmd, sdk_root)
succeeded = True
except subprocess.CalledProcessError as error:
error_message = (
Expand Down
27 changes: 18 additions & 9 deletions eng/mgmt/automation/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import math
import os
import sys
import yaml
import logging
import re
import subprocess
import platform
from typing import Tuple

from parameters import CI_HEADER
Expand Down Expand Up @@ -180,11 +180,11 @@ def update_version(sdk_root: str, output_folder: str):
# find the python command
python_cmd = "python"
try:
subprocess.check_call([python_cmd, "--version"], shell=True)
subprocess.check_call([python_cmd, "--version"])
except subprocess.CalledProcessError:
python_cmd = "python3"
try:
subprocess.check_call([python_cmd, "--version"], shell=True)
subprocess.check_call([python_cmd, "--version"])
except subprocess.CalledProcessError:
raise Exception("python or python3 not found")

Expand All @@ -193,18 +193,23 @@ def update_version(sdk_root: str, output_folder: str):
os.chdir(sdk_root)
print(os.getcwd())
subprocess.run(
"{0} eng/versioning/update_versions.py --ut library --bt client --sr".format(python_cmd),
[python_cmd, "eng/versioning/update_versions.py", "--ut", "library", "--bt", "client", "--sr"],
stdout=subprocess.DEVNULL,
stderr=sys.stderr,
shell=True,
)
subprocess.run(
"{0} eng/versioning/update_versions.py --ut library --bt client --tf {1}/README.md".format(
python_cmd, output_folder
),
[
python_cmd,
"eng/versioning/update_versions.py",
"--ut",
"library",
"--bt",
"client",
"--tf",
"{0}/README.md".format(output_folder),
],
stdout=subprocess.DEVNULL,
stderr=sys.stderr,
shell=True,
)
finally:
os.chdir(pwd)
Expand Down Expand Up @@ -341,3 +346,7 @@ def set_or_increase_version(
write_version(version_file, lines, version_index, project, stable_version, current_version)

return stable_version, current_version


def is_windows():
return platform.system().lower() == "windows"

0 comments on commit 4d1114a

Please sign in to comment.