-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(detect_modifications): quick test based on last write time of files
- Loading branch information
Showing
4 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
// | ||
// 2011-12-10 GONG Chen <[email protected]> | ||
// | ||
#include <algorithm> | ||
#include <boost/algorithm/string.hpp> | ||
#include <boost/filesystem.hpp> | ||
#include <boost/uuid/random_generator.hpp> | ||
|
@@ -30,6 +31,51 @@ namespace fs = boost::filesystem; | |
|
||
namespace rime { | ||
|
||
DetectModifications::DetectModifications(TaskInitializer arg) { | ||
try { | ||
data_dirs_ = boost::any_cast<vector<string>>(arg); | ||
} | ||
catch (const boost::bad_any_cast&) { | ||
LOG(ERROR) << "DetectModifications: invalid arguments."; | ||
} | ||
} | ||
|
||
bool DetectModifications::Run(Deployer* deployer) { | ||
time_t last_modified = 0; | ||
try { | ||
for (auto dir : data_dirs_) { | ||
fs::path p = fs::canonical(dir); | ||
last_modified = (std::max)(last_modified, fs::last_write_time(p)); | ||
if (fs::is_directory(p)) { | ||
for (fs::directory_iterator iter(p), end; iter != end; ++iter) { | ||
fs::path entry(iter->path()); | ||
if (fs::is_regular_file(fs::canonical(entry)) && | ||
entry.extension().string() == ".yaml" && | ||
entry.filename().string() != "user.yaml") { | ||
last_modified = | ||
(std::max)(last_modified, fs::last_write_time(entry)); | ||
} | ||
} | ||
} | ||
} | ||
} catch(const fs::filesystem_error& ex) { | ||
LOG(ERROR) << "Error reading file information: " << ex.what(); | ||
return true; | ||
} | ||
|
||
// TODO: store as 64-bit number to avoid the year 2038 problem | ||
int last_build_time = 0; | ||
{ | ||
the<Config> user_config(Config::Require("config")->Create("user")); | ||
user_config->GetInt("var/last_build_time", &last_build_time); | ||
} | ||
if (last_modified > (time_t)last_build_time) { | ||
LOG(INFO) << "modifications detected. workspace needs update."; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool InstallationUpdate::Run(Deployer* deployer) { | ||
LOG(INFO) << "updating rime installation info."; | ||
fs::path shared_data_path(deployer->shared_data_dir); | ||
|
@@ -188,6 +234,11 @@ bool WorkspaceUpdate::Run(Deployer* deployer) { | |
} | ||
LOG(INFO) << "finished updating schemas: " | ||
<< success << " success, " << failure << " failure."; | ||
|
||
the<Config> user_config(Config::Require("config")->Create("user")); | ||
// TODO: store as 64-bit number to avoid the year 2038 problem | ||
user_config->SetInt("var/last_build_time", (int)time(NULL)); | ||
|
||
return failure == 0; | ||
} | ||
|
||
|
@@ -263,6 +314,7 @@ bool SchemaUpdate::Run(Deployer* deployer) { | |
LOG(INFO) << "patched copy of schema '" << schema_id | ||
<< "' is moved to trash"; | ||
} | ||
// TODO: compile the config file if needs update | ||
|
||
string dict_name; | ||
if (!config->GetString("translator/dictionary", &dict_name)) { | ||
|
@@ -281,6 +333,7 @@ bool SchemaUpdate::Run(Deployer* deployer) { | |
if (verbose_) { | ||
dict_compiler.set_options(DictCompiler::kRebuild | DictCompiler::kDump); | ||
} | ||
// TODO: use compiled schema instead of the YAML file alone | ||
if (!dict_compiler.Compile(schema_file_)) { | ||
LOG(ERROR) << "dictionary '" << dict_name << "' failed to compile."; | ||
return false; | ||
|
@@ -317,6 +370,7 @@ bool ConfigFileUpdate::Run(Deployer* deployer) { | |
trash)) { | ||
LOG(INFO) << "patched copy of '" << file_name_ << "' is moved to trash."; | ||
} | ||
// TODO: compile the config file if needs update | ||
return true; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters