Setting the version of the snap is possible via version-script which would look like:
name: example
version: latest
version-script: |
python get-latest-version-number.py
And you’d include in your repository a file called get-latest-version-number.py with similar contents as you already created in your get-latest.py.
import re
import sys
import urllib.request
URL_STUDIO_HOME = 'https://developer.android.com/studio/index.html'
def get_latest_studio_url():
with urllib.request.urlopen(URL_STUDIO_HOME) as response:
html = response.read().decode()
matched = re.findall('"((https)?://.*linux.zip)"', html)
# Ensure unique and then convert to a list of easy access.
links = list(set(matched))
if len(links) == 0:
raise ValueError('Url matching our query not found.')
elif len(links) > 1:
raise RuntimeError('Multiple urls found, expected only one, urls are: {}'.format(
' '.join(links)))
url = links[0][0]
version = url.split('/')[-2]
return version, url
def fetch_latest_version_details():
version, url = get_latest_studio_url()
sys.stdout.write(version) # snapcraft reads the version from the standard output of the version-script
if __name__ == '__main__':
fetch_latest_version_details()