2020-03-03 00:58:08 +03:00
|
|
|
#!/usr/bin/env python3
|
2019-10-25 00:02:23 +03:00
|
|
|
|
|
|
|
import os
|
2020-03-03 04:45:13 +03:00
|
|
|
import json
|
2019-10-25 00:02:23 +03:00
|
|
|
import sys
|
2020-04-27 23:35:19 +03:00
|
|
|
from typing import Dict, Optional
|
2019-11-22 23:50:41 +03:00
|
|
|
import argparse
|
2019-10-25 00:02:23 +03:00
|
|
|
|
2019-11-22 03:47:12 +03:00
|
|
|
VERSION_LINE_START = "__version__ = "
|
2019-10-25 00:02:23 +03:00
|
|
|
|
2022-02-03 03:32:23 +03:00
|
|
|
DIRECTORIES = ["ml-agents/mlagents/trainers", "ml-agents-envs/mlagents_envs"]
|
2019-10-25 00:02:23 +03:00
|
|
|
|
2020-07-10 23:49:41 +03:00
|
|
|
MLAGENTS_PACKAGE_JSON_PATH = "com.unity.ml-agents/package.json"
|
|
|
|
MLAGENTS_EXTENSIONS_PACKAGE_JSON_PATH = "com.unity.ml-agents.extensions/package.json"
|
|
|
|
|
2020-03-03 04:45:13 +03:00
|
|
|
ACADEMY_PATH = "com.unity.ml-agents/Runtime/Academy.cs"
|
|
|
|
|
2020-04-27 23:35:19 +03:00
|
|
|
PYTHON_VERSION_FILE_TEMPLATE = """# Version of the library that will be used to upload to pypi
|
|
|
|
__version__ = {version}
|
|
|
|
|
|
|
|
# Git tag that will be checked to determine whether to trigger upload to pypi
|
|
|
|
__release_tag__ = {release_tag}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def _escape_non_none(s: Optional[str]) -> str:
|
|
|
|
"""
|
2024-10-05 04:24:33 +03:00
|
|
|
Returns s escaped in quotes if it is non-None, els e "None"
|
2020-04-27 23:35:19 +03:00
|
|
|
:param s:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if s is not None:
|
|
|
|
return f'"{s}"'
|
|
|
|
else:
|
|
|
|
return "None"
|
|
|
|
|
2019-10-25 00:02:23 +03:00
|
|
|
|
|
|
|
def extract_version_string(filename):
|
|
|
|
with open(filename) as f:
|
2020-05-13 20:57:53 +03:00
|
|
|
for line in f.readlines():
|
|
|
|
if line.startswith(VERSION_LINE_START):
|
|
|
|
return line.replace(VERSION_LINE_START, "").strip()
|
2019-10-25 00:02:23 +03:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def check_versions() -> bool:
|
|
|
|
version_by_dir: Dict[str, str] = {}
|
|
|
|
for directory in DIRECTORIES:
|
2019-11-22 03:47:12 +03:00
|
|
|
path = os.path.join(directory, "__init__.py")
|
2019-10-25 00:02:23 +03:00
|
|
|
version = extract_version_string(path)
|
|
|
|
print(f"Found version {version} for {directory}")
|
|
|
|
version_by_dir[directory] = version
|
|
|
|
|
|
|
|
# Make sure we have exactly one version, and it's not none
|
|
|
|
versions = set(version_by_dir.values())
|
|
|
|
if len(versions) != 1 or None in versions:
|
|
|
|
print("Each setup.py must have the same VERSION string.")
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-04-27 23:35:19 +03:00
|
|
|
def set_version(
|
2021-03-05 23:12:41 +03:00
|
|
|
python_version: str,
|
|
|
|
csharp_version: str,
|
|
|
|
csharp_extensions_version: str,
|
|
|
|
release_tag: Optional[str],
|
2020-04-27 23:35:19 +03:00
|
|
|
) -> None:
|
|
|
|
# Sanity check - make sure test tags have a test or dev version
|
|
|
|
if release_tag and "test" in release_tag:
|
|
|
|
if not ("dev" in python_version or "test" in python_version):
|
|
|
|
raise RuntimeError('Test tags must use a "test" or "dev" version.')
|
|
|
|
|
|
|
|
new_contents = PYTHON_VERSION_FILE_TEMPLATE.format(
|
|
|
|
version=_escape_non_none(python_version),
|
|
|
|
release_tag=_escape_non_none(release_tag),
|
|
|
|
)
|
2019-11-22 23:50:41 +03:00
|
|
|
for directory in DIRECTORIES:
|
|
|
|
path = os.path.join(directory, "__init__.py")
|
2020-04-17 02:37:11 +03:00
|
|
|
print(f"Setting {path} to version {python_version}")
|
2019-11-22 23:50:41 +03:00
|
|
|
with open(path, "w") as f:
|
|
|
|
f.write(new_contents)
|
2020-04-17 02:37:11 +03:00
|
|
|
|
|
|
|
if csharp_version is not None:
|
2024-10-05 04:24:33 +03:00
|
|
|
# removed exp.1 tag from version strings since MLA package is a supported package again
|
|
|
|
# package_version = f"{csharp_version}-exp.1"
|
|
|
|
package_version = f"{csharp_version}"
|
2021-03-05 23:12:41 +03:00
|
|
|
if csharp_extensions_version is not None:
|
2021-03-19 19:40:04 +03:00
|
|
|
# since this has never been promoted we need to keep
|
|
|
|
# it in preview forever or CI will fail
|
2024-10-05 04:24:33 +03:00
|
|
|
extension_version = f"{csharp_extensions_version}-preview"
|
2020-03-03 04:45:13 +03:00
|
|
|
print(
|
2020-07-10 23:49:41 +03:00
|
|
|
f"Setting package version to {package_version} in {MLAGENTS_PACKAGE_JSON_PATH}"
|
|
|
|
f" and {MLAGENTS_EXTENSIONS_PACKAGE_JSON_PATH}"
|
2020-03-03 04:45:13 +03:00
|
|
|
)
|
|
|
|
set_package_version(package_version)
|
2021-03-05 23:12:41 +03:00
|
|
|
set_extension_package_version(package_version, extension_version)
|
2020-03-03 04:45:13 +03:00
|
|
|
print(f"Setting package version to {package_version} in {ACADEMY_PATH}")
|
|
|
|
set_academy_version_string(package_version)
|
|
|
|
|
|
|
|
|
|
|
|
def set_package_version(new_version: str) -> None:
|
2020-07-17 04:00:56 +03:00
|
|
|
with open(MLAGENTS_PACKAGE_JSON_PATH) as f:
|
2020-03-03 04:45:13 +03:00
|
|
|
package_json = json.load(f)
|
|
|
|
if "version" in package_json:
|
|
|
|
package_json["version"] = new_version
|
2020-07-10 23:49:41 +03:00
|
|
|
with open(MLAGENTS_PACKAGE_JSON_PATH, "w") as f:
|
|
|
|
json.dump(package_json, f, indent=2)
|
|
|
|
f.write("\n")
|
|
|
|
|
|
|
|
|
2021-03-05 23:12:41 +03:00
|
|
|
def set_extension_package_version(
|
|
|
|
new_dependency_version: str, new_extension_version
|
|
|
|
) -> None:
|
2020-07-17 04:00:56 +03:00
|
|
|
with open(MLAGENTS_EXTENSIONS_PACKAGE_JSON_PATH) as f:
|
2020-07-10 23:49:41 +03:00
|
|
|
package_json = json.load(f)
|
2021-03-05 23:12:41 +03:00
|
|
|
package_json["dependencies"]["com.unity.ml-agents"] = new_dependency_version
|
|
|
|
if new_extension_version is not None:
|
|
|
|
package_json["version"] = new_extension_version
|
2020-07-10 23:49:41 +03:00
|
|
|
with open(MLAGENTS_EXTENSIONS_PACKAGE_JSON_PATH, "w") as f:
|
2020-03-03 04:45:13 +03:00
|
|
|
json.dump(package_json, f, indent=2)
|
2020-07-10 23:49:41 +03:00
|
|
|
f.write("\n")
|
2020-03-03 04:45:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def set_academy_version_string(new_version):
|
|
|
|
needle = "internal const string k_PackageVersion"
|
|
|
|
found = 0
|
|
|
|
with open(ACADEMY_PATH) as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
for i, l in enumerate(lines):
|
|
|
|
if needle in l:
|
|
|
|
left, right = l.split(" = ")
|
|
|
|
right = f' = "{new_version}";\n'
|
|
|
|
lines[i] = left + right
|
|
|
|
found += 1
|
|
|
|
if found != 1:
|
|
|
|
raise RuntimeError(
|
|
|
|
f'Expected to find search string "{needle}" exactly once, but found it {found} times'
|
|
|
|
)
|
|
|
|
with open(ACADEMY_PATH, "w") as f:
|
|
|
|
f.writelines(lines)
|
2019-11-22 23:50:41 +03:00
|
|
|
|
|
|
|
|
2020-05-21 04:08:29 +03:00
|
|
|
def print_release_tag_commands(
|
|
|
|
python_version: str, csharp_version: str, release_tag: str
|
|
|
|
):
|
|
|
|
python_tag = f"python-packages_{python_version}"
|
|
|
|
csharp_tag = f"com.unity.ml-agents_{csharp_version}"
|
|
|
|
docs_tag = f"{release_tag}_docs"
|
|
|
|
print(
|
|
|
|
f"""
|
|
|
|
###
|
|
|
|
Use these commands to create the tags after the release:
|
|
|
|
###
|
|
|
|
git checkout {release_tag}
|
|
|
|
git tag -f latest_release
|
|
|
|
git push -f origin latest_release
|
|
|
|
git tag -f {docs_tag}
|
|
|
|
git push -f origin {docs_tag}
|
|
|
|
git tag {python_tag}
|
|
|
|
git push -f origin {python_tag}
|
|
|
|
git tag {csharp_tag}
|
|
|
|
git push -f origin {csharp_tag}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-25 00:02:23 +03:00
|
|
|
if __name__ == "__main__":
|
2019-11-22 23:50:41 +03:00
|
|
|
parser = argparse.ArgumentParser()
|
2020-04-17 02:37:11 +03:00
|
|
|
parser.add_argument("--python-version", default=None)
|
|
|
|
parser.add_argument("--csharp-version", default=None)
|
2021-03-05 23:12:41 +03:00
|
|
|
parser.add_argument("--csharp-extensions-version", default=None)
|
2020-04-27 23:35:19 +03:00
|
|
|
parser.add_argument("--release-tag", default=None)
|
2019-11-22 23:50:41 +03:00
|
|
|
# unused, but allows precommit to pass filenames
|
|
|
|
parser.add_argument("files", nargs="*")
|
|
|
|
args = parser.parse_args()
|
2020-04-17 02:37:11 +03:00
|
|
|
|
|
|
|
if args.python_version:
|
|
|
|
print(f"Updating python library to version {args.python_version}")
|
|
|
|
if args.csharp_version:
|
|
|
|
print(f"Updating C# package to version {args.csharp_version}")
|
2021-03-05 23:12:41 +03:00
|
|
|
if args.csharp_extensions_version:
|
|
|
|
print(
|
2024-10-05 04:27:11 +03:00
|
|
|
f"Updating C# extensions package to version {args.csharp_extensions_version}-preview"
|
2021-03-05 23:12:41 +03:00
|
|
|
)
|
|
|
|
set_version(
|
|
|
|
args.python_version,
|
|
|
|
args.csharp_version,
|
|
|
|
args.csharp_extensions_version,
|
|
|
|
args.release_tag,
|
|
|
|
)
|
2020-05-21 04:08:29 +03:00
|
|
|
if args.release_tag is not None:
|
|
|
|
print_release_tag_commands(
|
|
|
|
args.python_version, args.csharp_version, args.release_tag
|
|
|
|
)
|
2019-11-22 23:50:41 +03:00
|
|
|
else:
|
|
|
|
ok = check_versions()
|
|
|
|
return_code = 0 if ok else 1
|
|
|
|
sys.exit(return_code)
|