|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import argparse |
|
import os |
|
import re |
|
|
|
import packaging.version |
|
|
|
|
|
PATH_TO_EXAMPLES = "examples/" |
|
REPLACE_PATTERNS = { |
|
"examples": (re.compile(r'^check_min_version\("[^"]+"\)\s*$', re.MULTILINE), 'check_min_version("VERSION")\n'), |
|
"init": (re.compile(r'^__version__\s+=\s+"([^"]+)"\s*$', re.MULTILINE), '__version__ = "VERSION"\n'), |
|
"setup": (re.compile(r'^(\s*)version\s*=\s*"[^"]+",', re.MULTILINE), r'\1version="VERSION",'), |
|
"doc": (re.compile(r'^(\s*)release\s*=\s*"[^"]+"$', re.MULTILINE), 'release = "VERSION"\n'), |
|
} |
|
REPLACE_FILES = { |
|
"init": "src/diffusers/__init__.py", |
|
"setup": "setup.py", |
|
} |
|
README_FILE = "README.md" |
|
|
|
|
|
def update_version_in_file(fname, version, pattern): |
|
"""Update the version in one file using a specific pattern.""" |
|
with open(fname, "r", encoding="utf-8", newline="\n") as f: |
|
code = f.read() |
|
re_pattern, replace = REPLACE_PATTERNS[pattern] |
|
replace = replace.replace("VERSION", version) |
|
code = re_pattern.sub(replace, code) |
|
with open(fname, "w", encoding="utf-8", newline="\n") as f: |
|
f.write(code) |
|
|
|
|
|
def update_version_in_examples(version): |
|
"""Update the version in all examples files.""" |
|
for folder, directories, fnames in os.walk(PATH_TO_EXAMPLES): |
|
|
|
if "research_projects" in directories: |
|
directories.remove("research_projects") |
|
if "legacy" in directories: |
|
directories.remove("legacy") |
|
for fname in fnames: |
|
if fname.endswith(".py"): |
|
update_version_in_file(os.path.join(folder, fname), version, pattern="examples") |
|
|
|
|
|
def global_version_update(version, patch=False): |
|
"""Update the version in all needed files.""" |
|
for pattern, fname in REPLACE_FILES.items(): |
|
update_version_in_file(fname, version, pattern) |
|
if not patch: |
|
update_version_in_examples(version) |
|
|
|
|
|
def clean_main_ref_in_model_list(): |
|
"""Replace the links from main doc tp stable doc in the model list of the README.""" |
|
|
|
_start_prompt = "🤗 Transformers currently provides the following architectures" |
|
_end_prompt = "1. Want to contribute a new model?" |
|
with open(README_FILE, "r", encoding="utf-8", newline="\n") as f: |
|
lines = f.readlines() |
|
|
|
|
|
start_index = 0 |
|
while not lines[start_index].startswith(_start_prompt): |
|
start_index += 1 |
|
start_index += 1 |
|
|
|
index = start_index |
|
|
|
while not lines[index].startswith(_end_prompt): |
|
if lines[index].startswith("1."): |
|
lines[index] = lines[index].replace( |
|
"https://huggingface.co/docs/diffusers/main/model_doc", |
|
"https://huggingface.co/docs/diffusers/model_doc", |
|
) |
|
index += 1 |
|
|
|
with open(README_FILE, "w", encoding="utf-8", newline="\n") as f: |
|
f.writelines(lines) |
|
|
|
|
|
def get_version(): |
|
"""Reads the current version in the __init__.""" |
|
with open(REPLACE_FILES["init"], "r") as f: |
|
code = f.read() |
|
default_version = REPLACE_PATTERNS["init"][0].search(code).groups()[0] |
|
return packaging.version.parse(default_version) |
|
|
|
|
|
def pre_release_work(patch=False): |
|
"""Do all the necessary pre-release steps.""" |
|
|
|
default_version = get_version() |
|
if patch and default_version.is_devrelease: |
|
raise ValueError("Can't create a patch version from the dev branch, checkout a released version!") |
|
if default_version.is_devrelease: |
|
default_version = default_version.base_version |
|
elif patch: |
|
default_version = f"{default_version.major}.{default_version.minor}.{default_version.micro + 1}" |
|
else: |
|
default_version = f"{default_version.major}.{default_version.minor + 1}.0" |
|
|
|
|
|
version = input(f"Which version are you releasing? [{default_version}]") |
|
if len(version) == 0: |
|
version = default_version |
|
|
|
print(f"Updating version to {version}.") |
|
global_version_update(version, patch=patch) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def post_release_work(): |
|
"""Do all the necesarry post-release steps.""" |
|
|
|
current_version = get_version() |
|
dev_version = f"{current_version.major}.{current_version.minor + 1}.0.dev0" |
|
current_version = current_version.base_version |
|
|
|
|
|
version = input(f"Which version are we developing now? [{dev_version}]") |
|
if len(version) == 0: |
|
version = dev_version |
|
|
|
print(f"Updating version to {version}.") |
|
global_version_update(version) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") |
|
parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") |
|
args = parser.parse_args() |
|
if not args.post_release: |
|
pre_release_work(patch=args.patch) |
|
elif args.patch: |
|
print("Nothing to do after a patch :-)") |
|
else: |
|
post_release_work() |
|
|