1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/other_tools/just_mr/utils.hpp"
#include "src/buildtool/execution_api/local/local_cas.hpp"
#include "src/buildtool/file_system/file_storage.hpp"
#include "src/utils/cpp/path.hpp"
namespace JustMR::Utils {
auto GetGitCacheRoot() noexcept -> std::filesystem::path {
return LocalExecutionConfig::BuildRoot() / "git";
}
auto GetGitRoot(JustMR::PathsPtr const& just_mr_paths,
std::string const& repo_url) noexcept -> std::filesystem::path {
if (just_mr_paths->git_checkout_locations.contains(repo_url)) {
return std::filesystem::absolute(ToNormalPath(std::filesystem::path(
just_mr_paths->git_checkout_locations[repo_url])));
}
auto repo_url_as_path = std::filesystem::absolute(
ToNormalPath(std::filesystem::path(repo_url)));
if (not repo_url_as_path.empty() and
FileSystemManager::IsAbsolutePath(repo_url_as_path) and
FileSystemManager::IsDirectory(repo_url_as_path)) {
return repo_url_as_path;
}
return GetGitCacheRoot();
}
auto CreateTypedTmpDir(std::string const& type) noexcept -> TmpDirPtr {
// try to create parent dir
auto parent_path =
LocalExecutionConfig::BuildRoot() / "tmp-workspaces" / type;
return TmpDir::Create(parent_path);
}
auto GetArchiveTreeIDFile(std::string const& repo_type,
std::string const& content) noexcept
-> std::filesystem::path {
return LocalExecutionConfig::BuildRoot() / "tree-map" / repo_type / content;
}
auto GetDistdirTreeIDFile(std::string const& content) noexcept
-> std::filesystem::path {
return LocalExecutionConfig::BuildRoot() / "distfiles-tree-map" / content;
}
auto WriteTreeIDFile(std::filesystem::path const& tree_id_file,
std::string const& tree_id) noexcept -> bool {
// needs to be done safely, so use the rename trick
auto tmp_dir = TmpDir::Create(tree_id_file.parent_path());
if (not tmp_dir) {
Logger::Log(LogLevel::Error,
"could not create tmp dir for writing tree id file {}",
tree_id_file.string());
return false;
}
std::filesystem::path tmp_file{tmp_dir->GetPath() / "tmp_file"};
if (not FileSystemManager::WriteFile(tree_id, tmp_file)) {
Logger::Log(LogLevel::Error, "could not create tmp tree id file");
return false;
}
return FileSystemManager::Rename(tmp_file.string(), tree_id_file);
}
auto AddToCAS(std::string const& data) noexcept
-> std::optional<std::filesystem::path> {
// get file CAS instance
auto const& casf = LocalCAS<ObjectType::File>::Instance();
// write to casf
auto digest = casf.StoreBlobFromBytes(data);
if (digest) {
return casf.BlobPath(*digest);
}
return std::nullopt;
}
void AddDistfileToCAS(std::filesystem::path const& distfile,
JustMR::PathsPtr const& just_mr_paths) noexcept {
auto const& casf = LocalCAS<ObjectType::File>::Instance();
for (auto const& dirpath : just_mr_paths->distdirs) {
auto candidate = dirpath / distfile;
if (FileSystemManager::Exists(candidate)) {
// try to add to CAS
[[maybe_unused]] auto digest = casf.StoreBlobFromFile(candidate);
}
}
}
// NOLINTNEXTLINE(misc-no-recursion)
auto ResolveRepo(ExpressionPtr const& repo_desc,
ExpressionPtr const& repos,
gsl::not_null<std::unordered_set<std::string>*> const& seen)
-> std::optional<ExpressionPtr> {
if (not repo_desc->IsString()) {
return repo_desc;
}
auto desc_str = repo_desc->String();
if (seen->contains(desc_str)) {
// cyclic dependency
return std::nullopt;
}
[[maybe_unused]] auto insert_res = seen->insert(desc_str);
return ResolveRepo(repos[desc_str]["repository"], repos, seen);
}
auto ResolveRepo(ExpressionPtr const& repo_desc,
ExpressionPtr const& repos) noexcept
-> std::optional<ExpressionPtr> {
std::unordered_set<std::string> seen = {};
try {
return ResolveRepo(repo_desc, repos, &seen);
} catch (std::exception const& e) {
Logger::Log(LogLevel::Error,
"Config: while resolving dependencies: {}",
e.what());
return std::nullopt;
}
}
} // namespace JustMR::Utils
|