refactor(3p/nix/libutil): Replace chomp() with absl::strings

This commit is contained in:
Vincent Ambo 2020-05-24 01:36:11 +01:00
parent 10481d2586
commit 06d7b4aebd
15 changed files with 40 additions and 26 deletions

View file

@ -381,6 +381,7 @@ absl_deps = [
absl.dependency('spinlock_wait'), absl.dependency('spinlock_wait'),
absl.dependency('stacktrace'), absl.dependency('stacktrace'),
absl.dependency('strings'), absl.dependency('strings'),
absl.dependency('strings_internal'),
absl.dependency('symbolize'), absl.dependency('symbolize'),
absl.dependency('synchronization'), absl.dependency('synchronization'),
absl.dependency('throw_delegate'), absl.dependency('throw_delegate'),

View file

@ -6,6 +6,8 @@
#include <set> #include <set>
#include <tuple> #include <tuple>
#include <absl/strings/ascii.h>
#include <absl/strings/str_cat.h>
#include <glog/logging.h> #include <glog/logging.h>
#include "derivations.hh" #include "derivations.hh"
@ -199,9 +201,10 @@ static int _main(int argc, char* argv[]) {
storeUri = bestMachine->storeUri; storeUri = bestMachine->storeUri;
} catch (std::exception& e) { } catch (std::exception& e) {
auto msg = chomp(drainFD(5, false)); auto msg = absl::StripTrailingAsciiWhitespace(drainFD(5, false));
LOG(ERROR) << "cannot build on '" << bestMachine->storeUri LOG(ERROR) << "cannot build on '" << bestMachine->storeUri
<< "': " << e.what() << (msg.empty() ? "" : ": " + msg); << "': " << e.what()
<< (msg.empty() ? "" : absl::StrCat(": ", msg));
bestMachine->enabled = false; bestMachine->enabled = false;
continue; continue;
} }

View file

@ -1,6 +1,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <regex> #include <regex>
#include <absl/strings/ascii.h>
#include <glog/logging.h> #include <glog/logging.h>
#include <sys/time.h> #include <sys/time.h>
@ -76,7 +77,8 @@ GitInfo exportGit(ref<Store> store, const std::string& uri,
} }
// clean working tree, but no ref or rev specified. Use 'HEAD'. // clean working tree, but no ref or rev specified. Use 'HEAD'.
rev = chomp(runProgram("git", true, {"-C", uri, "rev-parse", "HEAD"})); rev = absl::StripTrailingAsciiWhitespace(
runProgram("git", true, {"-C", uri, "rev-parse", "HEAD"}));
ref = "HEAD"s; ref = "HEAD"s;
} }
@ -145,7 +147,9 @@ GitInfo exportGit(ref<Store> store, const std::string& uri,
// FIXME: check whether rev is an ancestor of ref. // FIXME: check whether rev is an ancestor of ref.
GitInfo gitInfo; GitInfo gitInfo;
gitInfo.rev = rev != "" ? rev : chomp(readFile(localRefFile)); gitInfo.rev =
rev != "" ? rev
: absl::StripTrailingAsciiWhitespace(readFile(localRefFile));
gitInfo.shortRev = std::string(gitInfo.rev, 0, 7); gitInfo.shortRev = std::string(gitInfo.rev, 0, 7);
DLOG(INFO) << "using revision " << gitInfo.rev << " of repo '" << uri << "'"; DLOG(INFO) << "using revision " << gitInfo.rev << " of repo '" << uri << "'";

View file

@ -1,6 +1,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <regex> #include <regex>
#include <absl/strings/ascii.h>
#include <glog/logging.h> #include <glog/logging.h>
#include <sys/time.h> #include <sys/time.h>
@ -43,7 +44,8 @@ HgInfo exportMercurial(ref<Store> store, const std::string& uri,
HgInfo hgInfo; HgInfo hgInfo;
hgInfo.rev = "0000000000000000000000000000000000000000"; hgInfo.rev = "0000000000000000000000000000000000000000";
hgInfo.branch = chomp(runProgram("hg", true, {"branch", "-R", uri})); hgInfo.branch = absl::StripTrailingAsciiWhitespace(
runProgram("hg", true, {"branch", "-R", uri}));
auto files = tokenizeString<std::set<std::string>>( auto files = tokenizeString<std::set<std::string>>(
runProgram("hg", true, runProgram("hg", true,

View file

@ -12,6 +12,7 @@
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include <absl/strings/ascii.h>
#include <fcntl.h> #include <fcntl.h>
#include <grp.h> #include <grp.h>
#include <netdb.h> #include <netdb.h>
@ -461,7 +462,7 @@ void handleDiffHook(uid_t uid, uid_t gid, Path tryA, Path tryB, Path drvPath,
} }
if (!diffRes.second.empty()) { if (!diffRes.second.empty()) {
LOG(ERROR) << chomp(diffRes.second); LOG(ERROR) << absl::StripTrailingAsciiWhitespace(diffRes.second);
} }
} catch (Error& error) { } catch (Error& error) {
LOG(ERROR) << "diff hook execution failed: " << error.what(); LOG(ERROR) << "diff hook execution failed: " << error.what();
@ -1640,7 +1641,8 @@ MakeError(NotDeterministic, BuildError)
hookEnvironment.emplace("DRV_PATH", drvPath); hookEnvironment.emplace("DRV_PATH", drvPath);
hookEnvironment.emplace("OUT_PATHS", hookEnvironment.emplace("OUT_PATHS",
chomp(concatStringsSep(" ", outputPaths))); absl::StripTrailingAsciiWhitespace(
concatStringsSep(" ", outputPaths)));
RunOptions opts(settings.postBuildHook, {}); RunOptions opts(settings.postBuildHook, {});
opts.environment = hookEnvironment; opts.environment = hookEnvironment;
@ -1788,7 +1790,8 @@ HookReply DerivationGoal::tryBuildHook() {
} catch (SysError& e) { } catch (SysError& e) {
if (e.errNo == EPIPE) { if (e.errNo == EPIPE) {
LOG(ERROR) << "build hook died unexpectedly: " LOG(ERROR) << "build hook died unexpectedly: "
<< chomp(drainFD(worker.hook->fromHook.readSide.get())); << absl::StripTrailingAsciiWhitespace(
drainFD(worker.hook->fromHook.readSide.get()));
worker.hook = nullptr; worker.hook = nullptr;
return rpDecline; return rpDecline;
} }

View file

@ -1,5 +1,7 @@
#include "download.hh" #include "download.hh"
#include <absl/strings/ascii.h>
#include "archive.hh" #include "archive.hh"
#include "compression.hh" #include "compression.hh"
#include "finally.hh" #include "finally.hh"
@ -231,7 +233,9 @@ struct CurlDownloader : public Downloader {
static int debugCallback(CURL* handle, curl_infotype type, char* data, static int debugCallback(CURL* handle, curl_infotype type, char* data,
size_t size, void* userptr) { size_t size, void* userptr) {
if (type == CURLINFO_TEXT) { if (type == CURLINFO_TEXT) {
DLOG(INFO) << "curl: " << chomp(std::string(data, size)); DLOG(INFO) << "curl: "
<< absl::StripTrailingAsciiWhitespace(
std::string(data, size));
} }
return 0; return 0;
} }

View file

@ -80,7 +80,7 @@ libstore_dep_list = [
pthread_dep, pthread_dep,
sqlite3_dep, sqlite3_dep,
libsodium_dep libsodium_dep
] ] + absl_deps
if sys_name.contains('linux') if sys_name.contains('linux')
libstore_dep_list += libseccomp_dep libstore_dep_list += libseccomp_dep

View file

@ -3,6 +3,7 @@
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <absl/strings/ascii.h>
#include <fcntl.h> #include <fcntl.h>
#include <glog/logging.h> #include <glog/logging.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -715,7 +716,7 @@ std::exception_ptr RemoteStore::Connection::processStderr(Sink* sink,
} }
else if (msg == STDERR_NEXT) { else if (msg == STDERR_NEXT) {
LOG(ERROR) << chomp(readString(from)); LOG(ERROR) << absl::StripTrailingAsciiWhitespace(readString(from));
} }
else if (msg == STDERR_START_ACTIVITY) { else if (msg == STDERR_START_ACTIVITY) {

View file

@ -2,6 +2,7 @@
#include "s3-binary-cache-store.hh" #include "s3-binary-cache-store.hh"
#include <absl/strings/ascii.h>
#include <aws/core/Aws.h> #include <aws/core/Aws.h>
#include <aws/core/VersionConfig.h> #include <aws/core/VersionConfig.h>
#include <aws/core/auth/AWSCredentialsProvider.h> #include <aws/core/auth/AWSCredentialsProvider.h>
@ -50,7 +51,7 @@ class AwsLogger : public Aws::Utils::Logging::FormattedLogSystem {
using Aws::Utils::Logging::FormattedLogSystem::FormattedLogSystem; using Aws::Utils::Logging::FormattedLogSystem::FormattedLogSystem;
void ProcessFormattedStatement(Aws::String&& statement) override { void ProcessFormattedStatement(Aws::String&& statement) override {
debug("AWS: %s", chomp(statement)); debug("AWS: %s", absl::StripTrailingAsciiWhitespace(statement));
} }
}; };

View file

@ -1210,11 +1210,6 @@ string concatStringsSep(const string& sep, const StringSet& ss) {
return s; return s;
} }
string chomp(const string& s) {
size_t i = s.find_last_not_of(" \n\r\t");
return i == string::npos ? "" : string(s, 0, i + 1);
}
string trim(const string& s, const string& whitespace) { string trim(const string& s, const string& whitespace) {
auto i = s.find_first_not_of(whitespace); auto i = s.find_first_not_of(whitespace);
if (i == string::npos) { if (i == string::npos) {

View file

@ -324,9 +324,6 @@ MakeError(Interrupted, BaseError)
string concatStringsSep(const string& sep, const Strings& ss); string concatStringsSep(const string& sep, const Strings& ss);
string concatStringsSep(const string& sep, const StringSet& ss); string concatStringsSep(const string& sep, const StringSet& ss);
/* Remove trailing whitespace from a string. */
string chomp(const string& s);
/* Remove whitespace from the start and end of a string. */ /* Remove whitespace from the start and end of a string. */
string trim(const string& s, const string& whitespace = " \n\r\t"); string trim(const string& s, const string& whitespace = " \n\r\t");

View file

@ -5,6 +5,7 @@
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <absl/strings/ascii.h>
#include <glog/logging.h> #include <glog/logging.h>
#include "affinity.hh" #include "affinity.hh"
@ -121,7 +122,7 @@ static void _main(int argc, char** argv) {
} }
args.clear(); args.clear();
for (auto line : lines) { for (auto line : lines) {
line = chomp(line); line = absl::StripTrailingAsciiWhitespace(line);
std::smatch match; std::smatch match;
if (std::regex_match(line, match, if (std::regex_match(line, match,
std::regex("^#!\\s*nix-shell (.*)$"))) { std::regex("^#!\\s*nix-shell (.*)$"))) {

View file

@ -1,5 +1,6 @@
#include <regex> #include <regex>
#include <absl/strings/ascii.h>
#include <fcntl.h> #include <fcntl.h>
#include <pwd.h> #include <pwd.h>
@ -25,7 +26,7 @@ static void readChannels() {
for (const auto& line : for (const auto& line :
tokenizeString<std::vector<string>>(channelsFile, "\n")) { tokenizeString<std::vector<string>>(channelsFile, "\n")) {
chomp(line); absl::StripTrailingAsciiWhitespace(line);
if (std::regex_search(line, std::regex("^\\s*\\#"))) { if (std::regex_search(line, std::regex("^\\s*\\#"))) {
continue; continue;
} }
@ -99,7 +100,7 @@ static void update(const StringSet& channelNames) {
auto dl = getDownloader(); auto dl = getDownloader();
auto result = dl->downloadCached(store, request); auto result = dl->downloadCached(store, request);
auto filename = result.path; auto filename = result.path;
url = chomp(result.effectiveUri); url = absl::StripTrailingAsciiWhitespace(result.effectiveUri);
// If the URL contains a version number, append it to the name // If the URL contains a version number, append it to the name
// attribute (so that "nix-env -q" on the channels profile // attribute (so that "nix-env -q" on the channels profile
@ -136,7 +137,7 @@ static void update(const StringSet& channelNames) {
CachedDownloadRequest(url + "/nixexprs.tar.bz2")) CachedDownloadRequest(url + "/nixexprs.tar.bz2"))
.path; .path;
} }
chomp(filename); absl::StripTrailingAsciiWhitespace(filename);
} }
// Regardless of where it came from, add the expression representing this // Regardless of where it came from, add the expression representing this

View file

@ -58,7 +58,7 @@ nix_dep_list = [
libdl_dep, libdl_dep,
libsodium_dep, libsodium_dep,
pthread_dep, pthread_dep,
] ] + absl_deps
nix_link_list = [ nix_link_list = [
libutil_lib, libutil_lib,

View file

@ -5,6 +5,7 @@
#include <iostream> #include <iostream>
#include <utility> #include <utility>
#include <absl/strings/ascii.h>
#include <glog/logging.h> #include <glog/logging.h>
#ifdef READLINE #ifdef READLINE
@ -122,7 +123,7 @@ void printHelp() {
} }
string removeWhitespace(string s) { string removeWhitespace(string s) {
s = chomp(s); s = absl::StripTrailingAsciiWhitespace(s);
size_t n = s.find_first_not_of(" \n\r\t"); size_t n = s.find_first_not_of(" \n\r\t");
if (n != string::npos) { if (n != string::npos) {
s = string(s, n); s = string(s, n);