refactor: Use package.json for version management (#5514)

* refactor: Use package.json for version management

- uses package.json file for version management.
- updated the regex pattern.
- removed the logic that creates or updates the version.txt file

* load version through package.json

* fix code duplication

* add changeset

* add changeset

* fixes

* fix

* package version

* fix

* typing

* typing

* changes

* add changeset

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
D V 2023-09-16 04:53:52 +05:30 committed by GitHub
parent 99e87a45df
commit 52f7831751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 72 additions and 51 deletions

View File

@ -82,7 +82,6 @@ ${current_changelog.replace(`# ${pkg_name}`, "").trim()}
});
if (python) {
writeFileSync(join(dirs[0], "version.txt"), version);
bump_local_dependents(pkg_name, version);
}
}

View File

@ -0,0 +1,7 @@
---
"@gradio/app": patch
"gradio": patch
"gradio_client": patch
---
feat:refactor: Use package.json for version management

View File

@ -11,7 +11,7 @@ import { join } from "path";
import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const version_path = join(__dirname, "..", "gradio", "version.txt");
const version_path = join(__dirname, "..", "gradio", "package.json");
const theme_token_path = join(
__dirname,
"..",
@ -21,9 +21,7 @@ const theme_token_path = join(
"tokens.css"
);
const version = readFileSync(version_path, { encoding: "utf-8" })
.trim()
.replace(/\./g, "-");
const version = JSON.parse(readFileSync(version_path, { encoding: 'utf-8' })).version.trim().replace(/\./g, '-');
//@ts-ignore
export default defineConfig(({ mode }) => {

View File

@ -47,7 +47,6 @@ jobs:
fi
- name: Build pr package
run: |
echo ${{ steps.get_pr_number.outputs.GRADIO_VERSION }} > gradio/version.txt
pnpm i --frozen-lockfile --ignore-scripts
pnpm build
python3 -m build -w

View File

@ -3,9 +3,10 @@ set -e
cd "$(dirname ${0})"
# You should update the version in version.txt before running this script
new_version="$(cat gradio/version.txt)"
GRADIO_VERSION=$new_version
# You should update the version in package.json before running this script
FILE="gradio/package.json"
new_version=$(python -c "import json; f = open('$FILE', 'r'); data = json.load(f); print(data['version']); f.close();")
GRADIO_VERSION = $new_version
rm -rf gradio/templates/frontend
rm -rf gradio/templates/cdn

View File

@ -58,7 +58,20 @@ INVALID_RUNTIME = [
SpaceStage.PAUSED,
]
__version__ = (pkgutil.get_data(__name__, "version.txt") or b"").decode("ascii").strip()
def get_package_version() -> str:
try:
package_json_data = (
pkgutil.get_data(__name__, "package.json").decode("utf-8").strip() # type: ignore
)
package_data = json.loads(package_json_data)
version = package_data.get("version", "")
return version
except Exception:
return ""
__version__ = get_package_version()
class TooManyRequestsError(Exception):

View File

@ -1 +0,0 @@
0.5.0

View File

@ -38,8 +38,8 @@ classifiers = [
Homepage = "https://github.com/gradio-app/gradio"
[tool.hatch.version]
path = "gradio_client/version.txt"
pattern = "(?P<version>.+)"
path = "gradio_client/package.json"
pattern = ".*\"version\":\\s*\"(?P<version>[^\"]+)\""
[tool.hatch.metadata.hooks.requirements_txt]
filename = "requirements.txt"

View File

@ -3,8 +3,9 @@ import sys
import urllib.request
from pathlib import Path
version_file = Path(__file__).parent.parent / "gradio_client" / "version.txt"
version = version_file.read_text(encoding="utf8").strip()
version_file = Path(__file__).parent.parent / "gradio_client" / "package.json"
with version_file.open() as f:
version = json.load(f)["version"]
with urllib.request.urlopen("https://pypi.org/pypi/gradio_client/json") as url:
releases = json.load(url)["releases"]

View File

@ -1,4 +1,4 @@
import pkgutil
import json
import gradio.components as components
import gradio.inputs as inputs
@ -103,8 +103,6 @@ from gradio.templates import (
Webcam,
)
from gradio.themes import Base as Theme
from gradio.utils import get_package_version
current_pkg_version = (
(pkgutil.get_data(__name__, "version.txt") or b"").decode("ascii").strip()
)
__version__ = current_pkg_version
__version__ = get_package_version()

View File

@ -16,7 +16,7 @@ import requests
import gradio
from gradio import wasm_utils
from gradio.context import Context
from gradio.utils import GRADIO_VERSION
from gradio.utils import get_package_version
# For testability, we import the pyfetch function into this module scope and define a fallback coroutine object to be patched in tests.
try:
@ -87,10 +87,7 @@ async def _do_wasm_analytics_request(url: str, data: dict[str, Any]) -> None:
def version_check():
try:
version_data = pkgutil.get_data(__name__, "version.txt")
if not version_data:
raise FileNotFoundError
current_pkg_version = version_data.decode("ascii").strip()
current_pkg_version = get_package_version()
latest_pkg_version = requests.get(url=PKG_VERSION_URL, timeout=3).json()[
"version"
]
@ -197,7 +194,7 @@ def launched_analytics(blocks: gradio.Blocks, data: dict[str, Any]) -> None:
str(blocks.blocks[y]) for y in x["outputs"] if y in blocks.blocks
]
additional_data = {
"version": GRADIO_VERSION,
"version": get_package_version(),
"is_kaggle": blocks.is_kaggle,
"is_sagemaker": blocks.is_sagemaker,
"using_auth": blocks.auth is not None,

View File

@ -55,7 +55,6 @@ from gradio.tunneling import (
CURRENT_TUNNELS,
)
from gradio.utils import (
GRADIO_VERSION,
TupleNoPrint,
check_function_inputs_match,
component_or_layout_class,
@ -63,6 +62,7 @@ from gradio.utils import (
delete_none,
get_cancel_function,
get_continuous_fn,
get_package_version,
)
try:
@ -771,7 +771,7 @@ class Blocks(BlockContext):
"custom_css": self.css is not None,
"theme": self.theme.name,
"is_custom_theme": is_custom_theme,
"version": GRADIO_VERSION,
"version": get_package_version(),
}
analytics.initiated_analytics(data)

View File

@ -54,14 +54,19 @@ from gradio.exceptions import Error
from gradio.oauth import attach_oauth
from gradio.queueing import Estimation, Event
from gradio.route_utils import Request # noqa: F401
from gradio.utils import cancel_tasks, run_coro_in_background, set_task_name
from gradio.utils import (
cancel_tasks,
get_package_version,
run_coro_in_background,
set_task_name,
)
mimetypes.init()
STATIC_TEMPLATE_LIB = files("gradio").joinpath("templates").as_posix() # type: ignore
STATIC_PATH_LIB = files("gradio").joinpath("templates", "frontend", "static").as_posix() # type: ignore
BUILD_PATH_LIB = files("gradio").joinpath("templates", "frontend", "assets").as_posix() # type: ignore
VERSION = files("gradio").joinpath("version.txt").read_text()
VERSION = get_package_version()
class ORJSONResponse(JSONResponse):

View File

@ -50,14 +50,23 @@ if TYPE_CHECKING: # Only import for type checking (is False at runtime).
from gradio.routes import App
JSON_PATH = os.path.join(os.path.dirname(gradio.__file__), "launches.json")
GRADIO_VERSION = (
(pkgutil.get_data(__name__, "version.txt") or b"").decode("ascii").strip()
)
P = ParamSpec("P")
T = TypeVar("T")
def get_package_version() -> str:
try:
package_json_data = (
pkgutil.get_data(__name__, "package.json").decode("utf-8").strip() # type: ignore
)
package_data = json.loads(package_json_data)
version = package_data.get("version", "")
return version
except Exception:
return ""
def safe_get_lock() -> asyncio.Lock:
"""Get asyncio.Lock() without fear of getting an Exception.

View File

@ -1 +0,0 @@
3.44.3

View File

@ -9,19 +9,16 @@ import prefixer from "postcss-prefix-selector";
import { readFileSync } from "fs";
import { resolve } from "path";
const version_path = resolve(__dirname, "../../gradio/version.txt");
const version_path = resolve(__dirname, "../../gradio/package.json");
const theme_token_path = resolve(__dirname, "../theme/src/tokens.css");
const version_raw = readFileSync(version_path, { encoding: "utf-8" }).trim();
const version_raw = JSON.parse(readFileSync(version_path, { encoding: "utf-8" })).version.trim();
const version = version_raw.replace(/\./g, "-");
const client_version_path = resolve(
__dirname,
"../../client/python/gradio_client/version.txt"
"../../client/python/gradio_client/package.json"
);
const client_version_raw = readFileSync(client_version_path, {
encoding: "utf-8"
}).trim();
const client_version = client_version_raw.replace(/\./g, "-");
const client_version_raw = JSON.parse(readFileSync(client_version_path, { encoding: "utf-8" })).version.trim();
import {
inject_ejs,

View File

@ -42,8 +42,8 @@ upload_theme = "gradio.themes.upload_theme:main"
Homepage = "https://github.com/gradio-app/gradio"
[tool.hatch.version]
path = "gradio/version.txt"
pattern = "(?P<version>.+)"
path = "gradio/package.json"
pattern = ".*\"version\":\\s*\"(?P<version>[^\"]+)\""
[tool.hatch.metadata.hooks.requirements_txt]
filename = "requirements.txt"

View File

@ -3,9 +3,9 @@ import sys
import urllib.request
from pathlib import Path
root_directory = Path(__file__).parent.parent
version = (root_directory / "gradio" / "version.txt").read_text(
encoding='utf8').strip()
version_file = Path(__file__).parent.parent / "gradio" / "package.json"
with version_file.open() as f:
version = json.load(f)["version"]
with urllib.request.urlopen("https://pypi.org/pypi/gradio/json") as url:
releases = json.load(url)["releases"]

View File

@ -1,23 +1,22 @@
from __future__ import annotations
import argparse
import json
import os
import pathlib
import shutil
import tempfile
import textwrap
import requests
import huggingface_hub
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
VERSION_TXT = os.path.abspath(os.path.join(ROOT, "gradio", "version.txt"))
VERSION_FILE = os.path.abspath(os.path.join(ROOT, "gradio", "package.json"))
DIR = os.path.dirname(__file__)
GRADIO_DEMO_DIR = os.path.abspath(os.path.join(ROOT, "demo"))
with open(VERSION_TXT) as f:
gradio_version=f.read()
gradio_version = gradio_version.strip()
with open(VERSION_FILE) as f:
version = json.load(f)["version"]
# Reasoning:
# 1. all_demos includes all demos and is for testing PRs