29 namespace seqan3::detail
42 inline void call_server(std::string
const & command, std::promise<bool> prom)
45 if (system(command.c_str()))
46 prom.set_value(
false);
63 version_checker() =
delete;
64 version_checker(version_checker
const &) =
default;
65 version_checker & operator=(version_checker
const &) =
default;
66 version_checker(version_checker &&) =
default;
67 version_checker & operator=(version_checker &&) =
default;
68 ~version_checker() =
default;
75 version_checker(std::string name_, std::string
const & version_, std::string
const & app_url = std::string{}) :
78 assert(std::regex_match(name, std::regex{
"^[a-zA-Z0-9_-]+$"}));
82 message_app_update.pop_back();
83 message_app_update.append(
"[APP INFO] :: Visit " + app_url +
" for updates.\n\n");
87 timestamp_filename = cookie_path / (name +
"_usr.timestamp");
89 timestamp_filename = cookie_path / (name +
"_dev.timestamp");
91 std::smatch versionMatch;
94 if (!version_.empty() &&
95 std::regex_search(version_, versionMatch, std::regex(
"^([[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+).*")))
97 version = versionMatch.str(1);
128 void operator()(std::promise<bool> prom)
130 std::array<int, 3> empty_version{0, 0, 0};
131 std::array<int, 3> srv_app_version{};
132 std::array<int, 3> srv_seqan_version{};
134 std::ifstream version_file{cookie_path / (name +
".version")};
136 if (version_file.is_open())
139 std::getline(version_file, line);
141 if (line != unregistered_app)
142 srv_app_version = get_numbers_from_version_string(line);
145 std::cerr << message_unregistered_app;
148 std::getline(version_file, line);
149 srv_seqan_version = get_numbers_from_version_string(line);
151 version_file.close();
155 if (srv_seqan_version != empty_version)
159 if (seqan_version < srv_seqan_version)
160 std::cerr << message_seqan3_update;
164 if (srv_app_version != empty_version)
167 if (get_numbers_from_version_string(version) < srv_app_version)
168 std::cerr << message_app_update;
172 if (get_numbers_from_version_string(version) > srv_app_version)
173 std::cerr << message_registered_app_update;
177 std::cerr << std::flush;
179 std::string program = get_program();
183 prom.set_value(
false);
188 std::filesystem::path out_file = cookie_path / (name +
".version");
191 std::string command = program +
195 std::string{
"http://seqan-update.informatik.uni-tuebingen.de/check/SeqAn3_"} +
200 #elif defined(_WIN32)
209 #if __x86_64__ || __ppc64__
218 "; exit [int] -not $?}\" > nul 2>&1";
224 std::thread(call_server, command,
std::move(prom)).detach();
228 static std::filesystem::path get_path()
230 using namespace std::filesystem;
234 tmp_path = std::string{getenv(home_env_name)};
235 tmp_path /=
".config";
239 create_directory(tmp_path, err);
245 create_directory(tmp_path, err);
250 tmp_path = temp_directory_path();
253 path dummy = tmp_path /
"dummy.txt";
254 std::ofstream file{dummy};
255 detail::safe_filesystem_entry file_guard{dummy};
257 bool is_open = file.is_open();
258 bool is_good = file.good();
260 file_guard.remove_no_throw();
262 if (!is_good || !is_open)
294 bool decide_if_check_is_performed(
bool developer_approval, std::optional<bool> user_approval)
296 if (!developer_approval)
299 if (std::getenv(
"SEQAN3_NO_VERSION_CHECK") !=
nullptr)
302 if (user_approval.has_value())
303 return user_approval.value();
306 if (std::filesystem::exists(cookie_path))
308 std::ifstream timestamp_file{timestamp_filename};
309 std::string cookie_line{};
311 if (timestamp_file.is_open())
313 std::getline(timestamp_file, cookie_line);
315 if (get_time_diff_to_current(cookie_line) < 86400)
320 std::getline(timestamp_file, cookie_line);
322 if (cookie_line ==
"NEVER")
326 else if (cookie_line ==
"ALWAYS")
332 timestamp_file.close();
340 if (detail::is_terminal())
343 #######################################################################
344 Automatic Update Notifications
345 #######################################################################
347 This app can look for updates automatically in the background,
348 do you want to do that?
350 [a] Always perform version checks for this app (the default).
351 [n] Never perform version checks for this app.
352 [y] Yes, perform a version check now, and ask again tomorrow.
353 [s] Skip the version check now, but ask again tomorrow.
355 Please enter one of [a, n, y, s] and press [RETURN].
357 For more information, see:
358 https://github.com/seqan/seqan3/wiki/Update-Notifications
360 #######################################################################
364 std::getline(std::cin, line);
379 write_cookie(std::string{
"NEVER"});
384 write_cookie(std::string{
"ALWAYS"});
392 #######################################################################
393 Automatic Update Notifications
394 #######################################################################
395 This app performs automatic checks for updates. For more information
396 see: https://github.com/seqan/seqan3/wiki/Update-Notifications
397 #######################################################################
405 static constexpr std::string_view unregistered_app =
"UNREGISTERED_APP";
407 static constexpr std::string_view message_seqan3_update =
408 "[SEQAN3 INFO] :: A new SeqAn version is available online.\n"
409 "[SEQAN3 INFO] :: Please visit www.github.com/seqan/seqan3.git for an update\n"
410 "[SEQAN3 INFO] :: or inform the developer of this app.\n"
411 "[SEQAN3 INFO] :: If you don't wish to receive further notifications, set --version-check OFF.\n\n";
413 static constexpr std::string_view message_unregistered_app =
414 "[SEQAN3 INFO] :: Thank you for using SeqAn!\n"
415 "[SEQAN3 INFO] :: Do you wish to register your app for update notifications?\n"
416 "[SEQAN3 INFO] :: Just send an email to support@seqan.de with your app name and version number.\n"
417 "[SEQAN3 INFO] :: If you don't wish to receive further notifications, set --version-check OFF.\n\n";
419 static constexpr std::string_view message_registered_app_update =
420 "[APP INFO] :: We noticed the app version you use is newer than the one registered with us.\n"
421 "[APP INFO] :: Please send us an email with the new version so we can correct it (support@seqan.de)\n\n";
423 std::string message_app_update =
424 "[APP INFO] :: A new version of this application is now available.\n"
425 "[APP INFO] :: If you don't wish to receive further notifications, set --version-check OFF.\n\n";
429 static constexpr
char const * home_env_name
441 std::string version{
"0.0.0"};
443 std::regex version_regex{
"^[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+$"};
445 std::filesystem::path cookie_path = get_path();
447 std::filesystem::path timestamp_filename;
451 static std::string get_program()
454 return "powershell.exe -NoLogo -NonInteractive -Command \"& {Invoke-WebRequest -erroraction 'silentlycontinue' -OutFile";
456 if (!system(
"/usr/bin/env -i wget --version > /dev/null 2>&1"))
457 return "/usr/bin/env -i wget --timeout=10 --tries=1 -q -O";
458 else if (!system(
"/usr/bin/env -i curl --version > /dev/null 2>&1"))
459 return "/usr/bin/env -i curl --connect-timeout 10 -o";
462 #if defined(__OpenBSD__)
463 return "/usr/bin/env -i ftp -w10 -Vo";
464 #elif defined(__FreeBSD__)
465 return "/usr/bin/env -i fetch --timeout=10 -o";
473 double get_time_diff_to_current(std::string
const & str_time)
const
475 namespace co = std::chrono;
476 double curr = co::duration_cast<co::seconds>(co::system_clock::now().time_since_epoch()).count();
479 std::from_chars(str_time.data(), str_time.data() + str_time.size(), d_time);
481 return curr - d_time;
487 std::array<int, 3> get_numbers_from_version_string(std::string
const & str)
const
489 std::array<int, 3> result{};
491 if (!std::regex_match(str, version_regex))
494 auto res = std::from_chars(str.data(), str.data() + str.size(), result[0]);
495 res = std::from_chars(res.ptr + 1, str.data() + str.size(), result[1]);
496 res = std::from_chars(res.ptr + 1, str.data() + str.size(), result[2]);
505 template <
typename msg_type>
506 void write_cookie(msg_type && msg)
509 namespace co = std::chrono;
510 auto curr = co::duration_cast<co::seconds>(co::system_clock::now().time_since_epoch()).count();
512 std::ofstream timestamp_file{timestamp_filename};
514 if (timestamp_file.is_open())
516 timestamp_file << curr <<
'\n' << msg;
517 timestamp_file.close();
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
Provides various utility functions.
Provides seqan3::detail::safe_filesystem_entry.
Provides std::from_chars and std::to_chars if not defined in the stl <charconv> header.
Checks if program is run interactively and retrieves dimensions of terminal (Transferred from seqan2)...
Provides SeqAn version macros and global variables.
#define SEQAN3_VERSION_MAJOR
The major version as MACRO.
Definition: version.hpp:20
#define SEQAN3_VERSION_PATCH
The patch version as MACRO.
Definition: version.hpp:24
#define SEQAN3_VERSION_MINOR
The minor version as MACRO.
Definition: version.hpp:22