Skip to content

Commit

Permalink
Remove temp yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
kilian-funk committed Sep 20, 2024
1 parent 102736a commit 2f60c98
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 46 deletions.
53 changes: 24 additions & 29 deletions repos/config/detail/generate_ros2_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import yaml


Expand All @@ -23,9 +22,9 @@
#
load("@bazel_tools//tools/build_defs/repo:utils.bzl", _maybe = "maybe")
load("@rules_ros//repos/config/detail:git_repository.bzl", _git_repository = "git_repository")
load("@rules_ros//repos/config/detail:git_repository.bzl", "git_repository")
load("@rules_ros//repos/config/detail:http_archive.bzl", "http_archive")
load("@rules_ros//repos/config/detail:new_local_repository.bzl", _new_local_repository = "new_local_repository")
load("@rules_ros//repos/config/detail:new_local_repository.bzl", "new_local_repository")
def setup():
pass
Expand All @@ -40,26 +39,32 @@ def print_setup(repos, output_file):


def build_load_command(repo, spec):
if spec.get('type') == "git":
return build_git_load_command(repo, spec)
if spec.get('type') == "http_archive":
return build_http_archive_load_command(repo, spec)
if spec.get('type') == "local":
return build_local_load_command(repo, spec)
else:
builder = {
"git": build_git_load_command,
"http_archive": build_http_archive_load_command,
"local": build_local_load_command,
}
if spec.get('type') not in builder.keys():
return f"""
print("WARNING: Unknown repo type {spec.get('type')} for repo @{repo.replace('/', '.')}")
"""
return builder[spec.get('type')](repo, spec)


def build_build_files_attr(build_files):
if not build_files:
return ""
content = '\n'.join(f" '{k}': '{v}'," for k,v in build_files.items())
return f"""build_files = {{
{content}
}},"""


def build_http_archive_load_command(repo, spec):
build_files = "\n".join([f' "{k}": "{v}",' for k,v in spec['bazel'].items()])
return f"""\
_maybe(
name = "{repo.replace('/','.')}",
build_files = {{
{build_files}
}},
{build_build_files_attr(spec['bazel'])}
url = "{spec['url']}",
sha256 = "{spec['hash']}",
strip_prefix = "{spec['strip_prefix']}",
Expand All @@ -68,31 +73,25 @@ def build_http_archive_load_command(repo, spec):
"""

def build_local_load_command(repo, spec):
build_files = "\n".join([f' "{k}": "{v}",' for k,v in spec['bazel'].items()])
return f"""\
_maybe(
name = "{repo.replace('/','.')}",
build_files = {{
{build_files}
}},
{build_build_files_attr(spec['bazel'])}
path = "{spec['path']}",
sha256 = "{spec['hash']}",
repo_rule = _new_local_repository,
repo_rule = new_local_repository,
)
"""

def build_git_load_command(repo, spec):
build_files = "\n".join([f' "{k}": "{v}",' for k,v in spec['bazel'].items()])
return f"""\
_maybe(
name = "{repo.replace('/','.')}",
branch = "{spec['version']}",
build_files = {{
{build_files}
}},
{build_build_files_attr(spec['bazel'])}
commit = "{spec['hash']}",
remote = "{spec['url']}",
repo_rule = _git_repository,
repo_rule = git_repository,
shallow_since = "{spec['shallow_since']}",
)
"""
Expand All @@ -106,11 +105,7 @@ def merge_dict(origin, to_add):
origin[key]=value


def print_setup_file(yaml_files, output_file):
if len(yaml_files) < 1:
raise ValueError("At least one YAML file is required as input.")

repos = {}
def print_setup_file(repos, yaml_files, output_file):
for input_path in yaml_files:
with (open(input_path,"r")) as repo_file:
merge_dict(repos, yaml.safe_load(repo_file)["repositories"])
Expand Down
34 changes: 17 additions & 17 deletions repos/config/detail/lock_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,45 @@ def main():
add_attributes(repos["repositories"][repo], additional_attributes)
print("{}: {}".format(repo, [*additional_attributes.values()]))

with tempfile.NamedTemporaryFile(mode='w+t', encoding='utf8') as lock_file:
yaml.dump(repos, lock_file, default_flow_style=False, allow_unicode=True)

with open(args.setup_bzl, mode='w', encoding='utf8') as setup_bzl:
print_setup_file(yaml_files=[lock_file.name] + args.overlays, output_file=setup_bzl)
with open(args.setup_bzl, mode='w', encoding='utf8') as setup_bzl:
print_setup_file(repos = repos["repositories"], yaml_files=args.overlays, output_file=setup_bzl)


def fetch_dependency_details(*, use_tar, type, **kwargs):
if type == "tar" or use_tar:
return fetch_archive_details(**kwargs)
return fetch_repo_details(**kwargs)
return fetch_http_details(type = type, **kwargs)
return fetch_git_details(type = type, **kwargs)

def add_attributes(dictionary, additional_attributes):
for k,v in additional_attributes.items():
dictionary[k] = v

def fetch_archive_details(url, version, **kwargs):
cwd = os.getcwd()
def fetch_http_details(*, type, version = None, url = None, **kwargs):
forward_type = "http_archive"
if "sha256" in kwargs:
return { "type": forward_type}
with tempfile.TemporaryDirectory() as tempdir:
url = url.rsplit(".git", 1)[0]
project = url.split('/')[-1]
url += f"/archive/refs/tags/{version}.tar.gz"
archive_filename = Path(tempdir) / "archive.tar.gz"
if type == "git":
url = url.rsplit(".git", 1)[0]
url += f"/archive/refs/tags/{version}.tar.gz"
result = subprocess.run(
["curl", "-L", "-f", "-o", "archive.tar.gz", url],
["curl", "-L", "-f", "-o", archive_filename, url],
stdout=subprocess.PIPE,
encoding='utf8'
)
if result.returncode != 0:
raise ValueError(f"Error loading {url}, {version}: " + (result.stderr or ""))

archive_bytes = Path('archive.tar.gz').read_bytes()
archive_bytes = archive_filename.read_bytes()
hash = hashlib.sha256(archive_bytes).hexdigest()
strip_prefix = extract_archive_root_folder("archive.tar.gz", url)
strip_prefix = extract_archive_root_folder(archive_filename, url)

return {
"hash":hash,
"url": url,
"strip_prefix": strip_prefix,
"type": "http_archive",
"type": forward_type,
}

def extract_archive_root_folder(path, origin):
Expand All @@ -102,7 +102,7 @@ def extract_archive_root_folder(path, origin):
return result.stdout.split('\n')[0].split('/')[0]


def fetch_repo_details(url, version, **kwargs):
def fetch_git_details(url, version, **kwargs):
cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tempdir:
result = subprocess.run(
Expand Down

0 comments on commit 2f60c98

Please sign in to comment.