Skip to content

Commit

Permalink
Introduce shadow_copy parameter to PathToResourceAsFile
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 703182436
  • Loading branch information
MediaPipe Team authored and copybara-github committed Dec 5, 2024
1 parent b956ea0 commit 6683244
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
1 change: 0 additions & 1 deletion mediapipe/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ cc_library(
"//mediapipe/framework/port:ret_check",
"//mediapipe/framework/port:singleton",
"//mediapipe/framework/port:status",
"//mediapipe/framework/port:statusor",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/status",
Expand Down
12 changes: 6 additions & 6 deletions mediapipe/util/resource_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ namespace mediapipe {
// - If the input path is an absolute path, it is returned as-is.
// - If the input path is relative, it is searched in a platform-specific
// location:
// - On Android, we look for an asset with the given relative path; if
// it exists, it is copied to the file system (using the AssetCache),
// and a path to that file is returned.
// - On Android with `shadow_copy`, we look for an asset with the given
// relative path; if it exists, it is copied to the file system (using
// the AssetCache), and a path to that file is returned.
// - On iOS, we look for a resource with the given relative path in the
// application bundle.
//
// Note: The exact search algorithm is subject to change.
// Note: This function should be used by code that needs a resource to be
// accessible as a normal file, usually to call an existing API that only
// accepts file paths. Code that can access data as a stream or as a buffer
// should read from an asset directly on Android; an API for this will be
// provided later. TODO.
absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path);
// should use the Resources API (see below).
absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path,
bool shadow_copy = true);

// DEPRECATED: use `CalculatorContext::GetResources` and
// `SubgraphContext::GetResources` which allow for fine grained per graph
Expand Down
17 changes: 12 additions & 5 deletions mediapipe/util/resource_util_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@
#include <vector>

#include "absl/log/absl_log.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/ret_check.h"
#include "mediapipe/framework/port/singleton.h"
#include "mediapipe/framework/port/statusor.h"
#include "mediapipe/framework/port/status_builder.h"
#include "mediapipe/util/android/asset_manager_util.h"
#include "mediapipe/util/android/file/base/helpers.h"

namespace mediapipe {

namespace {
absl::StatusOr<std::string> PathToResourceAsFileInternal(
const std::string& path) {
const std::string& path, bool shadow_copy) {
if (!shadow_copy) {
return absl::UnavailableError(absl::StrCat(
"Not copying asset '", path, "' due to `shadow_copy == false`"));
}
return Singleton<AssetManager>::get()->CachedFileFromAsset(path);
}
} // namespace
Expand Down Expand Up @@ -65,15 +71,16 @@ absl::Status DefaultGetResourceContents(const std::string& path,
}
} // namespace internal

absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path,
bool shadow_copy) {
// Return full path.
if (absl::StartsWith(path, "/")) {
return path;
}

// Try to load a relative path or a base filename as is.
{
auto status_or_path = PathToResourceAsFileInternal(path);
auto status_or_path = PathToResourceAsFileInternal(path, shadow_copy);
if (status_or_path.ok()) {
ABSL_LOG(INFO) << "Successfully loaded: " << path;
return status_or_path;
Expand All @@ -86,7 +93,7 @@ absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
RET_CHECK(last_slash_idx != std::string::npos)
<< path << " doesn't have a slash in it"; // Make sure it's a path.
auto base_name = path.substr(last_slash_idx + 1);
auto status_or_path = PathToResourceAsFileInternal(base_name);
auto status_or_path = PathToResourceAsFileInternal(base_name, shadow_copy);
if (status_or_path.ok()) {
ABSL_LOG(INFO) << "Successfully loaded: " << base_name;
return status_or_path;
Expand Down
5 changes: 3 additions & 2 deletions mediapipe/util/resource_util_apple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#include <sstream>

#include "absl/log/absl_log.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/ret_check.h"
#include "mediapipe/framework/port/statusor.h"
#include "mediapipe/util/resource_util.h"

namespace mediapipe {
Expand Down Expand Up @@ -55,7 +55,8 @@ absl::Status DefaultGetResourceContents(const std::string& path,
}
} // namespace internal

absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path,
bool /*shadow_copy*/) {
// Return full path.
if (absl::StartsWith(path, "/")) {
return path;
Expand Down
5 changes: 3 additions & 2 deletions mediapipe/util/resource_util_default.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include <fstream>

#include "absl/flags/flag.h"
#include "absl/status/statusor.h"
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/statusor.h"

ABSL_FLAG(
std::string, resource_root_dir, "",
Expand All @@ -38,7 +38,8 @@ absl::Status DefaultGetResourceContents(const std::string& path,
}
} // namespace internal

absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path,
bool /*shadow_copy*/) {
if (absl::StartsWith(path, "/")) {
return path;
}
Expand Down
6 changes: 4 additions & 2 deletions mediapipe/util/resource_util_emscripten.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
#include <iterator>

#include "absl/log/absl_log.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/ret_check.h"
#include "mediapipe/framework/port/statusor.h"
#include "mediapipe/util/resource_util.h"

namespace mediapipe {

absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path,
bool /*shadow_copy*/) {
if (absl::StartsWith(path, "/")) {
return path;
}
Expand Down
5 changes: 3 additions & 2 deletions mediapipe/util/resource_util_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include <fstream>

#include "absl/flags/flag.h"
#include "absl/status/statusor.h"
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/singleton.h"
#include "mediapipe/framework/port/statusor.h"
#include "tools/cpp/runfiles/runfiles.h"

ABSL_FLAG(
Expand Down Expand Up @@ -70,7 +70,8 @@ absl::Status DefaultGetResourceContents(const std::string& path,

} // namespace internal

absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path,
bool /*shadow_copy*/) {
std::string qualified_path = path;
if (absl::StartsWith(qualified_path, "./")) {
qualified_path = "mediapipe" + qualified_path.substr(1);
Expand Down

0 comments on commit 6683244

Please sign in to comment.