-
Notifications
You must be signed in to change notification settings - Fork 0
Add external measure manager for non-gem repos #2
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
Open
kflemin
wants to merge
4
commits into
develop
Choose a base branch
from
non-gem-repos
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # External non-gem measure repositories. | ||
| # | ||
| # Precedence in workflow measure lookup is: | ||
| # 1) local_measure_roots below | ||
| # 2) gem-provided measure dirs | ||
| # 3) external repos listed here | ||
| # | ||
| # local_measure_roots are resolved relative to this file. | ||
| # The default shipped configuration keeps this repository's local measures first. | ||
| local_measure_roots: | ||
| - ../lib/measures | ||
|
|
||
| # For each repo: | ||
| # - name: local checkout folder name under vendor/external_measures | ||
| # - url: git repository URL | ||
| # - ref: pinned tag/branch/commit to checkout | ||
| # - measure_roots: one or more paths inside the repo that contain measure directories | ||
| # | ||
|
|
||
| # The below measures are needed for the BOSS L200 workflow. | ||
| repos: | ||
| - name: comstock | ||
| url: https://github.com/NatLabRockies/comstock.git | ||
| ref: main | ||
| measure_roots: | ||
| - measures | ||
| - resources/measures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # ******************************************************************************* | ||
| # OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| # See also https://github.com/BuildingSync/BuildingSync-gem/blob/develop/LICENSE.md | ||
| # ******************************************************************************* | ||
|
|
||
| require 'fileutils' | ||
| require 'open3' | ||
| require 'yaml' | ||
|
|
||
| module BOSS | ||
| # Manages non-gem measure repositories declared in a manifest file. | ||
| class ExternalMeasureRepoManager | ||
| def initialize(manifest_path: EXTERNAL_MEASURE_REPOS_MANIFEST_PATH, install_dir: EXTERNAL_MEASURE_REPOS_INSTALL_DIR) | ||
| @manifest_path = manifest_path | ||
| @install_dir = install_dir | ||
| end | ||
|
|
||
| def manifest_exists? | ||
| File.file?(@manifest_path) | ||
| end | ||
|
|
||
| def configured? | ||
| !repo_entries.empty? | ||
| end | ||
|
|
||
| def local_measure_directories | ||
| return [LOCAL_MEASURES_DIR] if !manifest_exists? | ||
|
|
||
| roots = manifest['local_measure_roots'] | ||
| roots = [LOCAL_MEASURES_DIR] if !roots.is_a?(Array) || roots.empty? | ||
|
|
||
| directories = roots.map do |path| | ||
| File.expand_path(path, File.expand_path('..', @manifest_path)) | ||
| end | ||
|
|
||
| directories.select { |path| Dir.exist?(path) }.uniq | ||
| end | ||
|
|
||
| def install_all | ||
| return [] if !manifest_exists? | ||
|
|
||
| FileUtils.mkdir_p(@install_dir) | ||
|
|
||
| repo_entries.each do |repo_entry| | ||
| install_repo(repo_entry) | ||
| end | ||
|
|
||
| resolved_measure_directories | ||
| end | ||
|
|
||
| def resolved_measure_directories | ||
| return [] if !manifest_exists? | ||
|
|
||
| directories = [] | ||
| repo_entries.each do |repo_entry| | ||
| repo_root = repo_checkout_dir(repo_entry) | ||
| if !Dir.exist?(repo_root) | ||
| message = "Repository '#{repo_entry['name']}' is not installed at #{repo_root}. Run rake measures:install_external." | ||
| if defined?(OpenStudio) && OpenStudio.respond_to?(:logFree) | ||
| OpenStudio.logFree(OpenStudio::Warn, 'BOSS.ExternalMeasureRepoManager.resolved_measure_directories', message) | ||
| else | ||
| warn("BOSS.ExternalMeasureRepoManager.resolved_measure_directories: #{message}") | ||
| end | ||
| next | ||
| end | ||
|
|
||
| repo_entry['measure_roots'].each do |root_rel_path| | ||
| root_abs_path = File.expand_path(File.join(repo_root, root_rel_path)) | ||
| if Dir.exist?(root_abs_path) | ||
| directories << root_abs_path | ||
| else | ||
| message = "Configured measure root '#{root_rel_path}' does not exist for repository '#{repo_entry['name']}' (#{root_abs_path})." | ||
| if defined?(OpenStudio) && OpenStudio.respond_to?(:logFree) | ||
| OpenStudio.logFree(OpenStudio::Warn, 'BOSS.ExternalMeasureRepoManager.resolved_measure_directories', message) | ||
| else | ||
| warn("BOSS.ExternalMeasureRepoManager.resolved_measure_directories: #{message}") | ||
| end | ||
| end | ||
|
Copilot marked this conversation as resolved.
|
||
| end | ||
| end | ||
|
|
||
| ordered_unique(directories) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def manifest | ||
| @manifest ||= begin | ||
| parsed = YAML.safe_load(File.read(@manifest_path), permitted_classes: [], aliases: false) | ||
| parsed.is_a?(Hash) ? parsed : {} | ||
| end | ||
| end | ||
|
|
||
| def repo_entries | ||
| return [] if !manifest_exists? | ||
|
|
||
| repos = manifest.is_a?(Hash) ? manifest['repos'] : nil | ||
| return [] if repos.nil? | ||
| raise StandardError, "Expected 'repos' array in #{@manifest_path}" if !repos.is_a?(Array) | ||
|
|
||
| repos.map do |repo| | ||
| validate_repo_entry(repo) | ||
| end | ||
| end | ||
|
|
||
| def validate_repo_entry(repo) | ||
| if !repo.is_a?(Hash) | ||
| raise StandardError, "Each repo entry in #{@manifest_path} must be a map" | ||
| end | ||
|
|
||
| required = %w[name url ref measure_roots] | ||
| missing = required.select { |key| repo[key].nil? || repo[key].to_s.empty? } | ||
| if !missing.empty? | ||
| raise StandardError, "Repo entry is missing required keys #{missing.join(', ')} in #{@manifest_path}" | ||
| end | ||
|
|
||
| # Prevent path traversal / unexpected nesting in checkout dir. | ||
| if !repo['name'].to_s.match?(/\A[\w.-]+\z/) | ||
| raise StandardError, "Repo name '#{repo['name']}' must match /\\A[\\w.-]+\\z/ in #{@manifest_path}" | ||
| end | ||
|
|
||
| if !repo['measure_roots'].is_a?(Array) || repo['measure_roots'].empty? | ||
| raise StandardError, "Repo '#{repo['name']}' must define a non-empty measure_roots array" | ||
| end | ||
| repo | ||
| end | ||
|
|
||
| def install_repo(repo_entry) | ||
| repo_dir = repo_checkout_dir(repo_entry) | ||
| measure_roots = repo_entry['measure_roots'] | ||
|
|
||
| if Dir.exist?(File.join(repo_dir, '.git')) | ||
| run_git(%W[-C #{repo_dir} remote set-url origin #{repo_entry['url']}]) | ||
| else | ||
| FileUtils.mkdir_p(File.dirname(repo_dir)) | ||
| run_git(%W[clone --filter=blob:none --no-checkout #{repo_entry['url']} #{repo_dir}]) | ||
| end | ||
|
|
||
| run_git(%W[-C #{repo_dir} sparse-checkout init --cone]) | ||
| run_git(['-C', repo_dir, 'sparse-checkout', 'set', *measure_roots]) | ||
| run_git(%W[-C #{repo_dir} fetch --depth 1 origin #{repo_entry['ref']}]) | ||
| run_git(%W[-C #{repo_dir} checkout --force FETCH_HEAD]) | ||
| end | ||
|
|
||
| def repo_checkout_dir(repo_entry) | ||
| File.join(@install_dir, repo_entry['name']) | ||
| end | ||
|
|
||
| def run_git(args) | ||
| stdout, stderr, status = Open3.capture3('git', *args) | ||
| if !status.success? | ||
| raise StandardError, "git #{args.join(' ')} failed:\n#{stdout}\n#{stderr}" | ||
| end | ||
| end | ||
|
|
||
| def ordered_unique(paths) | ||
| seen = {} | ||
| paths.each_with_object([]) do |path, acc| | ||
| next if seen[path] | ||
|
|
||
| seen[path] = true | ||
| acc << path | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.