Skip to content

Instantly share code, notes, and snippets.

@patrislav1
Created April 5, 2024 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrislav1/646e5ec2ae66bd9f93c21137945933b5 to your computer and use it in GitHub Desktop.
Save patrislav1/646e5ec2ae66bd9f93c21137945933b5 to your computer and use it in GitHub Desktop.
go.mod to static bitbake recipe
#!/usr/bin/env python3
import re
import sys
import subprocess
def get_commit_hash(repo_url, version_tag):
command = ["git", "ls-remote", "--tags", "--refs", repo_url]
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode == 0:
for line in result.stdout.splitlines():
ref = line.split('\t')[-1] # Extract the reference part
if version_tag in ref:
# Get the commit hash
return line.split('\t')[0].strip()
else:
raise RuntimeError("Error:", result.stderr)
def parse_go_mod(file_path):
dependencies = []
with open(file_path, 'r') as file:
content = file.read()
# Extract require blocks
require_blocks = re.findall(r'require\s*\(\s*(.*?)\s*\)', content, re.DOTALL)
for block in require_blocks:
# Extract each dependency
dependencies.extend(re.findall(r'\s*(.*?)\s+(v[^\s]+)\s*', block))
return dependencies
def build_src_uri(deps):
def sanitize(url):
url = '/'.join(url.split('/')[:3])
url = url.replace('golang.org/x', 'github.com/golang')
return url
# git://github.com/golang/sys;protocol=https;name=go-sys;destsuffix=${BPN}-${PV}/src/golang.org/x/sys \
print('SRC_URI = "\\')
print('\tgit://${GO_IMPORT};nobranch=1;protocol=https \\')
SRC_URI_FMT = 'git://%s;protocol=https;name=%s;nobranch=1;destsuffix=${BPN}-${PV}/src/%s'
deps_sanitized = []
for url, version in deps:
url_san = sanitize(url)
mod_name = url_san.split('/')[-1]
if not mod_name.startswith('go-'):
mod_name = 'go-' + mod_name
deps_sanitized.append((url_san, url, version, mod_name))
for url, url_orig, version, mod_name in deps_sanitized:
src_uri = SRC_URI_FMT % (url, mod_name, url_orig)
print('\t' + src_uri + ' \\')
print('\"')
for url, _, version, mod_name in deps_sanitized:
try:
sha = get_commit_hash('https://' + url + '.git', version)
except RuntimeError:
sha = get_commit_hash('git://' + url + '.git', version)
print(f'SRCREV_{mod_name} = \"{sha}\"')
if __name__ == "__main__":
go_mod_path = sys.argv[1]
dependencies = parse_go_mod(go_mod_path)
build_src_uri(dependencies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment