Skip to content

Commit ef810bb

Browse files
authored
Make it so we can get the latest release automatically without having to keep updating it by hand (#141)
* Initial work on auto increment pyscript versions * Make sure to ignore exceptions if we cant connect to internet * Revert change to version * Actually make it so it's v0 to follow PSDC * types because mypy wants them
1 parent 5a9a006 commit ef810bb

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ dependencies = [
1515
"rich==13.4.2",
1616
"toml<0.11",
1717
"typer==0.9.0",
18-
"platformdirs<2.7"
18+
"platformdirs<2.7",
19+
"requests<=2.31.0"
1920
]
2021
description = "Command Line Interface for PyScript"
2122
license = {text = "Apache-2.0"}
@@ -29,7 +30,8 @@ dev = [
2930
"coverage<7.3",
3031
"mypy<=1.4.1",
3132
"pytest<7.5",
32-
"types-toml<0.11"
33+
"types-toml<0.11",
34+
"types-requests"
3335
]
3436
docs = [
3537
"Sphinx<5.2",

src/pyscript/_generator.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import datetime
21
import json
32
from pathlib import Path
43
from typing import Optional
54

65
import jinja2
6+
import requests
77
import toml
88

99
from pyscript import LATEST_PYSCRIPT_VERSION, config
@@ -19,7 +19,7 @@ def create_project_html(
1919
python_file_path: str,
2020
config_file_path: str,
2121
output_file_path: Path,
22-
pyscript_version: str = LATEST_PYSCRIPT_VERSION,
22+
pyscript_version: str,
2323
template: str = "basic.html",
2424
) -> None:
2525
"""Write a Python script string to an HTML file template.
@@ -54,9 +54,9 @@ def save_config_file(config_file: Path, configuration: dict):
5454
5555
Params:
5656
57-
- config_file(Path): path configuration file. (i.e.: "pyscript.toml"). Supported
58-
formats: `toml` and `json`.
59-
- configuration(dict): app configuration to be saved
57+
- config_file(Path): path configuration file. (i.e.: "pyscript.toml"). Supported
58+
formats: `toml` and `json`.
59+
- configuration(dict): app configuration to be saved
6060
6161
Return:
6262
(None)
@@ -73,7 +73,7 @@ def create_project(
7373
app_description: str,
7474
author_name: str,
7575
author_email: str,
76-
pyscript_version: str = LATEST_PYSCRIPT_VERSION,
76+
pyscript_version: Optional[str] = None,
7777
project_type: str = "app",
7878
wrap: bool = False,
7979
command: Optional[str] = None,
@@ -86,7 +86,6 @@ def create_project(
8686
main.py - a "Hello world" python starter module
8787
index.html - start page for the project
8888
"""
89-
date_stamp = datetime.date.today()
9089

9190
if wrap:
9291
if command:
@@ -107,13 +106,16 @@ def create_project(
107106
# was complaining so let's add a default
108107
app_name = app_or_file_name or "my-pyscript-app"
109108

109+
if not pyscript_version:
110+
pyscript_version = _get_latest_pyscript_version()
111+
110112
context = {
111113
"name": app_name,
112114
"description": app_description,
113115
"type": "app",
114116
"author_name": author_name,
115117
"author_email": author_email,
116-
"version": f"{date_stamp.year}.{'{:02d}'.format(date_stamp.month)}.1",
118+
"version": "v0",
117119
}
118120

119121
app_dir = Path(".") / app_name
@@ -155,3 +157,21 @@ def create_project(
155157
pyscript_version=pyscript_version,
156158
template=template,
157159
)
160+
161+
162+
def _get_latest_pyscript_version() -> str:
163+
"""Get the latest version of PyScript from GitHub."""
164+
url = "https://api.github.com/repos/pyscript/pyscript/releases/latest"
165+
try:
166+
response = requests.get(url)
167+
168+
if not response.ok:
169+
pyscript_version = LATEST_PYSCRIPT_VERSION
170+
else:
171+
172+
data = response.json()
173+
pyscript_version = data["tag_name"]
174+
except Exception:
175+
pyscript_version = LATEST_PYSCRIPT_VERSION
176+
177+
return pyscript_version

src/pyscript/plugins/create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import typer
44

5-
from pyscript import LATEST_PYSCRIPT_VERSION, app, cli, plugins
5+
from pyscript import app, cli, plugins
66
from pyscript._generator import create_project
77

88

@@ -15,7 +15,7 @@ def create(
1515
author_name: str = typer.Option(None, help="Name of the author"),
1616
author_email: str = typer.Option(None, help="Email of the author"),
1717
pyscript_version: str = typer.Option(
18-
LATEST_PYSCRIPT_VERSION,
18+
None,
1919
"--pyscript-version",
2020
help="If provided, defines what version of pyscript will be used to create the app",
2121
),

tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
from pathlib import Path
22
from typing import Any
3+
from unittest.mock import MagicMock, patch
34

45
import pytest
56
from _pytest.monkeypatch import MonkeyPatch
67

8+
from pyscript import LATEST_PYSCRIPT_VERSION
9+
10+
11+
@pytest.fixture(scope="session", autouse=True)
12+
def requests():
13+
mocked_result = {"tag_name": LATEST_PYSCRIPT_VERSION}
14+
15+
with patch("pyscript._generator.requests") as mocked_requests:
16+
mocked_get = MagicMock()
17+
mocked_get.ok = True
18+
mocked_get.json = MagicMock(return_value=mocked_result)
19+
mocked_requests.get.return_value = mocked_get
20+
yield mocked_requests
21+
722

823
@pytest.fixture
924
def auto_enter(monkeypatch):

0 commit comments

Comments
 (0)