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

g.extension: get branch from version #1700

Merged
merged 21 commits into from
Jul 21, 2021
Merged
103 changes: 66 additions & 37 deletions scripts/g.extension/g.extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
# % description: Specific branch to fetch addon from (only used when fetching from git)
# % required: no
# % multiple: no
# % answer: main
# %end

# %flag
Expand Down Expand Up @@ -234,6 +233,21 @@ def urlopen(url, *args, **kwargs):
return urlrequest.urlopen(request, *args, **kwargs)


def get_branches(github_api_url, version_only=False):
ninsbl marked this conversation as resolved.
Show resolved Hide resolved
"""Get ordered list of branch names in repo using github API"""
req = urlrequest.urlopen(github_api_url)
content = json.loads(req.read())
branches = [repo_branch["name"] for repo_branch in content]
if version_only:
branches = [
version_branch
for version_branch in branches
if version_branch.startswith("grass")
]
branches.sort()
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
return branches


def download_addons_paths_file(url, response_format, *args, **kwargs):
"""Generates JSON file containing the download URLs of the official
Addons
Expand Down Expand Up @@ -1403,8 +1417,8 @@ def download_source_code_official_github(url, name, outdev, directory=None):
"""
if not directory:
directory = os.path.join(os.getcwd, name)
classchar = name.split(".", 1)[0]
moduleclass = expand_module_class_name(classchar)
# classchar = name.split(".", 1)[0]
# moduleclass = expand_module_class_name(classchar)
ninsbl marked this conversation as resolved.
Show resolved Hide resolved
if grass.call(["svn", "export", url, directory], stdout=outdev) != 0:
grass.fatal(_("GRASS Addons <%s> not found") % name)
return directory
Expand Down Expand Up @@ -1555,7 +1569,7 @@ def download_source_code(
response = urlopen(url)
except URLError:
# Try download add-on from 'master' branch if default "main" fails
if branch == "main":
if branch is None or branch == "":
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
try:
url = url.replace("main", "master")
gscript.message(
Expand Down Expand Up @@ -1612,7 +1626,9 @@ def install_extension_std_platforms(name, source, url, branch):
"""Install extension on standard platforms"""
gisbase = os.getenv("GISBASE")
# TODO: workaround, https://github.com/OSGeo/grass-addons/issues/528
source_url = "https://github.com/OSGeo/grass-addons/tree/master/grass7/"
# source_url = "https://github.com/OSGeo/grass-addons/tree/grass{}/src".format(
# version[0]
# )
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved

# to hide non-error messages from subprocesses
if grass.verbosity() <= 2:
Expand Down Expand Up @@ -1694,7 +1710,7 @@ def install_extension_std_platforms(name, source, url, branch):
"SCRIPTDIR=%s" % dirs["script"],
"STRINGDIR=%s" % dirs["string"],
"ETC=%s" % os.path.join(dirs["etc"]),
"SOURCE_URL=%s" % source_url,
"SOURCE_URL=%s" % url,
]

install_cmd = [
Expand Down Expand Up @@ -2218,7 +2234,9 @@ def resolve_known_host_service(url, name, branch):
else:
actual_start = ""
if "branch" in match["url_end"]:
suffix = match["url_end"].format(name=name, branch=branch)
suffix = match["url_end"].format(
name=name, branch=branch if branch else "main"
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
)
else:
suffix = match["url_end"].format(name=name)
url = "{prefix}{base}{suffix}".format(
Expand Down Expand Up @@ -2299,46 +2317,54 @@ def resolve_source_code(url=None, name=None, branch=None, fork=False):
>>> resolve_source_code('https://bitbucket.org/joe-user/grass-module') # doctest: +SKIP
('remote_zip', 'https://bitbucket.org/joe-user/grass-module/get/default.zip')
"""
# Handle URL for the offical repo
if not url and name:
module_class = get_module_class_name(name)
# note: 'trunk' is required to make URL usable for 'svn export' call
repo_branches = get_branches(
"https://api.github.com/repos/OSGeo/grass-addons/branches", True
)
# Define branch to fetch from (latest or current version)
version_branch = "grass{}".format(version[0])
if version_branch not in repo_branches:
version_branch = repo_branches[-1]
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved

# Set URL for the given GRASS version
git_url = (
"https://github.com/OSGeo/grass-addons/trunk/"
"grass{version}/{module_class}/{module_name}".format(
version=version[0], module_class=module_class, module_name=name
)
"https://github.com/OSGeo/grass-addons/branches/"
f"{version_branch}/src/{module_class}/{name}"
)
# trac_url = 'https://trac.osgeo.org/grass/browser/grass-addons/' \
# 'grass{version}/{module_class}/{module_name}?format=zip' \
# .format(version=version[0],
# module_class=module_class, module_name=name)
# return 'official', trac_url
return "official", git_url

# Handle URL for a fork of the offical repo
if url and fork:
module_class = get_module_class_name(name)

# note: 'trunk' is required to make URL usable for 'svn export' call
if branch in ["master", "main"]:
if branch is None:
repo_branches = get_branches(
url.rstrip("/").replace("github.com/", "api.github.com/repos/")
+ "/branches",
True,
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
)
# Define branch to fetch from (latest or current version)
if not repo_branches:
svn_reference = "trunk"
else:
version_branch = "grass{}".format(version[0])
if version_branch not in repo_branches:
version_branch = repo_branches[-1]
svn_reference = "branches/{}".format(version_branch)
elif branch in ["master", "main"]:
svn_reference = "trunk"
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
else:
svn_reference = "branches/{}".format(branch)

git_url = (
"{url}/{branch}/"
"grass{version}/{module_class}/{module_name}".format(
url=url,
version=version[0],
module_class=module_class,
module_name=name,
branch=svn_reference,
)
git_url = "{url}/{branch}/src/{module_class}/{module_name}".format(
url=url,
module_class=module_class,
module_name=name,
branch=svn_reference,
)
# trac_url = 'https://trac.osgeo.org/grass/browser/grass-addons/' \
# 'grass{version}/{module_class}/{module_name}?format=zip' \
# .format(version=version[0],
# module_class=module_class, module_name=name)
# return 'official', trac_url
return "official_fork", git_url

# Check if URL can be found
Expand All @@ -2351,20 +2377,20 @@ def resolve_source_code(url=None, name=None, branch=None, fork=False):
open_url = urlopen(url)
open_url.close()
url_validated = True
except:
except URLError:
pass
else:
try:
open_url = urlopen("http://" + url)
open_url.close()
url_validated = True
except:
except URLError:
pass
try:
open_url = urlopen("https://" + url)
open_url.close()
url_validated = True
except:
except URLError:
pass

if not url_validated:
Expand Down Expand Up @@ -2507,8 +2533,11 @@ def main():
version = grass_version["version"].split(".")
# TODO: update temporary workaround of using grass7 subdir of addon-repo, see
# https://github.com/OSGeo/grass-addons/issues/528
version[0] = 7
version[1] = 9

# if version[0] > 7:
# version[0] = 7
# version[1] = 9

build_platform = grass_version["build_platform"].split("-", 1)[0]

sys.exit(main())