Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python scripts: Use PDM and virtualenv to manage dependencies #23198

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ VERSION
temp_*

*.local.cmake

# Python
__pycache__/
*.pyc
.venv/
.pdm-python
33 changes: 28 additions & 5 deletions buildscripts/ci/learn/make_youtube_playlist_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import sys
def eprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)

if __name__ == '__main__' and sys.prefix == sys.base_prefix:
# Not running inside a virtual environment. Let's try to load one.
import os
old_dir = os.path.realpath(__file__)
new_dir, script_name = os.path.split(old_dir)
rel_pyi = r'.venv\Scripts\python.exe' if sys.platform == 'win32' else '.venv/bin/python'
while new_dir != old_dir:
abs_pyi = os.path.join(new_dir, rel_pyi)
if os.access(abs_pyi, os.X_OK):
eprint(f'{script_name}: Loading virtual environment:\n {abs_pyi}')
if sys.platform == 'win32':
import subprocess
raise SystemExit(subprocess.run([abs_pyi, *sys.argv]).returncode)
os.execl(abs_pyi, abs_pyi, *sys.argv)
old_dir = new_dir
new_dir = os.path.dirname(new_dir)
eprint(f'{script_name}: Not running inside a virtual environment.')
del old_dir, new_dir, rel_pyi, abs_pyi, script_name

import io
import sys
import json
Expand Down Expand Up @@ -103,13 +126,13 @@ def parseVideosInfo(videosInfoDoc):
PLAYLIST_ID = sys.argv[2]
PLAYLIST_FILE = sys.argv[3]

print("=== Read json file ===")
eprint("=== Read json file ===")

json_file = open(PLAYLIST_FILE, "r+")
jsonDict = json.load(json_file)
json_file.close()

print("=== Get playlist items ===")
eprint("=== Get playlist items ===")

headers = {
'Accept': 'application/json',
Expand All @@ -120,11 +143,11 @@ def parseVideosInfo(videosInfoDoc):

playlist_items = json.loads(r.text)

print("=== Parse playlist items ===")
eprint("=== Parse playlist items ===")

playlist_items_ids = parsePlaylistItemsIds(playlist_items)

print("=== Get videos info ===")
eprint("=== Get videos info ===")

params = f"part=snippet,contentDetails&key={YOUTUBE_API_KEY}&maxResults={MAX_NUMBER_OF_RESULT_ITEMS}"
for item_id in playlist_items_ids:
Expand All @@ -135,7 +158,7 @@ def parseVideosInfo(videosInfoDoc):

playlist_videos_info = json.loads(r.text)

print("=== Parse videos info ===")
eprint("=== Parse videos info ===")

playlist = parseVideosInfo(playlist_videos_info)

Expand Down
31 changes: 27 additions & 4 deletions buildscripts/ci/release/correct_release_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,42 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import sys
def eprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)

if __name__ == '__main__' and sys.prefix == sys.base_prefix:
# Not running inside a virtual environment. Let's try to load one.
import os
old_dir = os.path.realpath(__file__)
new_dir, script_name = os.path.split(old_dir)
rel_pyi = r'.venv\Scripts\python.exe' if sys.platform == 'win32' else '.venv/bin/python'
while new_dir != old_dir:
abs_pyi = os.path.join(new_dir, rel_pyi)
if os.access(abs_pyi, os.X_OK):
eprint(f'{script_name}: Loading virtual environment:\n {abs_pyi}')
if sys.platform == 'win32':
import subprocess
raise SystemExit(subprocess.run([abs_pyi, *sys.argv]).returncode)
os.execl(abs_pyi, abs_pyi, *sys.argv)
old_dir = new_dir
new_dir = os.path.dirname(new_dir)
eprint(f'{script_name}: Not running inside a virtual environment.')
del old_dir, new_dir, rel_pyi, abs_pyi, script_name

import sys
import json
import markdown

RELEASE_INFO_FILE = sys.argv[1]

print("=== Load json ===")
eprint("=== Load json ===")

json_file = open(RELEASE_INFO_FILE, "r+")
release_info_json = json.load(json_file)
json_file.close()

print("=== Make html version of body ===")
eprint("=== Make html version of body ===")

release_body_markdown = release_info_json["body"]

Expand All @@ -48,7 +71,7 @@
release_info_json["body"] = "'" + release_body_html + "'"
release_info_json["bodyMarkdown"] = release_body_markdown

print("=== Split release assets ===")
eprint("=== Split release assets ===")

# For backward compatibility, we must adhere to the rule:
# for each platform, there should be one file with the corresponding extension.
Expand All @@ -72,7 +95,7 @@

release_info_json_updated = json.dumps(release_info_json)

print("=== Write json ===")
eprint("=== Write json ===")

json_file = open(RELEASE_INFO_FILE, "w")
json_file.write(release_info_json_updated)
Expand Down
170 changes: 170 additions & 0 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[project]
description = "Python scripts for MuseScore Studio development."
readme = "https://github.com/musescore/MuseScore/wiki/Running-Python-scripts"
requires-python = ">=3.9"
dependencies = [
"requests>=2.32.3",
"markdown>=3.6",
]
# Show files that use these dependencies, e.g. for requests: rg -lt py "import requests"

# Useful PDM commands:
# --------------------
# pdm install Install all dependencies into a virtual environment (venv).
# pdm run <script> Run a Python script with access to venv.
# pdm add <package> Add new dependency on a PyPi package.
# pdm venv remove in-project Remove the venv (e.g. to test installation from scratch).
[tool.pdm]
distribution = false
Loading
Loading