99from pathlib import Path
1010import re
1111import shutil
12- import subprocess
1312import zipfile
1413
15- import requests
14+ import pygit2
15+ from github import Github
1616from circup .commands import main as circup_cli
1717
1818ASSET_DIRS = (
2626 "metadata.json"
2727)
2828
29- def run (cmd ):
30- result = subprocess .run (cmd , shell = True , check = True , capture_output = True )
31- return result .stdout .decode ('utf-8' ).strip ()
32-
33- def get_latest_repository_release_assets (name :str | dict ) -> list :
34- request_url = "https://api.github.com/repos/{}/releases/latest" .format (name )
35- release_response = requests .get (request_url , allow_redirects = True )
36- release_data = release_response .json ()
37- return release_data ["assets" ]
29+ def get_latest_repository_release_assets (name :str ) -> list :
30+ gh = Github ()
31+ repo = gh .get_repo (name )
32+ release = repo .get_latest_release ()
33+ gh .close ()
34+ return release .assets
3835
3936def replace_tags (file :Path , data :dict ) -> None :
4037 with open (file , "r" ) as f :
@@ -47,16 +44,15 @@ def replace_tags(file:Path, data:dict) -> None:
4744def main ():
4845
4946 # get github repository details
50- git_remote = run ("git config --get remote.origin.url" )
47+ git_repo = pygit2 .Repository (pygit2 .discover_repository (os .getcwd ()))
48+
49+ git_remote = git_repo .remotes ["origin" ].url
5150 git_remote = re .sub (r'^git@github\.com:' , "https://github.com/" , git_remote )
5251 git_remote = re .sub (r'\.git$' , "" , git_remote )
5352
5453 git_owner , git_name = re .findall (r'^https:\/\/github\.com\/([^\/]+)\/([^\/]+)$' , git_remote )[0 ]
5554
56- try :
57- git_commit = run ('git rev-parse --short HEAD' )
58- except subprocess .CalledProcessError :
59- git_commit = "NO_COMMIT"
55+ git_commit = str (git_repo .revparse_single ("HEAD" ).id )
6056
6157 # get the project root directory
6258 build_dir = Path (__file__ ).parent
@@ -100,7 +96,7 @@ def main():
10096
10197 try :
10298 for asset in get_latest_repository_release_assets ("adafruit/Adafruit_CircuitPython_Bundle" ):
103- bundle_version = re .findall (r'^adafruit-circuitpython-bundle-(\d+.x)-mpy-\d{8}.zip$' , asset [ " name" ] )
99+ bundle_version = re .findall (r'^adafruit-circuitpython-bundle-(\d+.x)-mpy-\d{8}.zip$' , asset . name )
104100 if not len (bundle_version ):
105101 continue
106102 bundle_version = bundle_version [0 ]
0 commit comments