Skip to content

Commit

Permalink
support search with hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcumming committed Feb 24, 2024
1 parent 18c37da commit 7deb90d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
43 changes: 19 additions & 24 deletions img
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def make_argparser():
parser.add_argument("--no-color",
action="store_true",
help="disable color output")
parser.add_argument("-v", "--verbose",
action="store_true",
help="enable verbose output")
parser.add_argument("-r", "--repo",
required=False, default=None, type=str,
help="the local repository")
Expand Down Expand Up @@ -74,16 +77,24 @@ def get_options(args):
return options

def get_filter(args):
# TODO: test "name" to see whether it is a sha/sha256
options = get_options(args)
img_filter = {"system": options["system"]}

for key, value in parse_uenv_string(options["name"]).items():
if value is not None:
img_filter[key] = value
name = options["name"]
if (name is not None) and datastore.DataStore.is_valid_sha(name):
terminal.info(f"get_filter: search term {name} is being treated as a sha256")
img_filter["sha"] = name
else:
terminal.info(f"get_filter: search term {name} is being treated as a name")
for key, value in parse_uenv_string(options["name"]).items():
if value is not None:
img_filter[key] = value

if options["uarch"] is not None:
img_filter["uarch"] = options["uarch"]

terminal.info(f"get_filter: filter {img_filter}")
return img_filter


Expand Down Expand Up @@ -145,39 +156,23 @@ def uenv_repo_path(path: str=None) -> str:

terminal.error("No repository path available: set UENV_REPO_PATH or use the --repo flag")

# return the relative path of an image
def record_path(record):
return f"{record.system}/{record.uarch}/{record.name}/{record.version}/{record.tag}"

# the path of the image corresponding to a specific image in the user cache
def uenv_record_path(record):
store = uenv_image_path()
if store:
return store + "/images/" + relative_path_from_record(record)
return store

def is_valid_sha256(s: str):
pattern = re.compile(r'^[a-fA-F-0-9]{64}$')
return True if pattern.match(s) else False

def is_short_sha256(s: str):
pattern = re.compile(r'^[a-fA-F-0-9]{16}$')
return True if pattern.match(s) else False

if __name__ == "__main__":

parser = make_argparser()
args = parser.parse_args()
terminal.use_colored_output(args.no_color)

if args.command is None:
parser.print_help()
sys.exit(0)

terminal.use_colored_output(args.no_color)
if args.verbose:
terminal.set_debug_level(2)

terminal.info(f"command mode: {args.command}")

if args.command in ["find", "pull"]:
img_filter = get_filter(args)
terminal.info(f"filter for remote repo {img_filter}")
terminal.info(f"using {'build' if args.build else 'deploy'} remote repo")

try:
Expand Down
22 changes: 14 additions & 8 deletions lib/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ def find_records(self, **constraints):
raise ValueError("At least one constraint must be provided")

for field in constraints:
if field not in self._store:
raise ValueError(f"Invalid field: {field}. Must be one of 'system', 'uarch', 'name', 'version', 'tag'")

# Find matching records for each constraint
matching_records_sets = [
set(self._store[field].get(value, [])) for field, value in constraints.items()
]
if (field != "sha") and (field not in self._store):
raise ValueError(f"Invalid field: {field}. Must be one of 'system', 'uarch', 'name', 'version', 'tag', 'sha'")

if "sha" in constraints:
sha = constraints["sha"]
if len(sha)<64:
matching_records_sets = [set([self._short_sha[sha]])]
else:
matching_records_sets = [set([sha])]
else:
# Find matching records for each constraint
matching_records_sets = [
set(self._store[field].get(value, [])) for field, value in constraints.items()
]

# Intersect all sets of matching records
if matching_records_sets:
Expand All @@ -70,7 +77,6 @@ def find_records(self, **constraints):
results = []
for sha in unique:
results += (self._images[sha])
#results = [self._images[sha] for sha in unique]
results.sort(reverse=True)
return results

Expand Down
2 changes: 1 addition & 1 deletion lib/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def colorize(string, color):
else:
return string

def deubg_level(level: int):
def set_debug_level(level: int):
global debug_level
debug_level = level

Expand Down

0 comments on commit 7deb90d

Please sign in to comment.