Skip to content

Commit 43b3858

Browse files
authored
update command (#217)
1 parent bdf167f commit 43b3858

File tree

9 files changed

+127
-57
lines changed

9 files changed

+127
-57
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ To install or update LODA, please follow the [installation instructions](https:/
22

33
## [Unreleased]
44

5+
## v22.12.1
6+
57
### Bugfixes
68

79
* Fix update issues
810

911
### Feature
1012

1113
* Install script
14+
* Update command
1215

1316
### Enhancements
1417

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ Core Commands:
3434
optimize <program> Optimize a program and print it
3535
minimize <program> Minimize a program and print it (see -t)
3636
profile <program> Measure program evaluation time (see -t)
37-
setup Run interactive setup to configure LODA
3837
3938
OEIS Commands:
4039
mine Mine programs for OEIS sequences (see -i,-p,-P,-H)
4140
check <program> Check a program for an OEIS sequence (see -b)
4241
mutate <program> Mutate a program and mine for OEIS sequences
4342
submit <file> [id] Submit a program for an OEIS sequence
4443
44+
Admin Commands:
45+
setup Run interactive setup to configure LODA
46+
update Run non-interactive update of LODA and its data
47+
4548
Targets:
4649
<file> Path to a LODA file (file extension: *.asm)
4750
<id> ID of an OEIS integer sequence (example: A000045)

src/commands.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ void Commands::help() {
5151
<< std::endl;
5252
std::cout << " profile <program> Measure program evaluation time (see -t)"
5353
<< std::endl;
54-
std::cout << " setup Run interactive setup to configure LODA"
55-
<< std::endl;
5654

5755
std::cout << std::endl << "OEIS Commands:" << std::endl;
5856
std::cout << " mine Mine programs for OEIS sequences (see "
@@ -66,6 +64,14 @@ void Commands::help() {
6664
<< std::endl;
6765
std::cout << " submit <file> [id] Submit a program for an OEIS sequence"
6866
<< std::endl;
67+
68+
std::cout << std::endl << "Admin Commands:" << std::endl;
69+
std::cout << " setup Run interactive setup to configure LODA"
70+
<< std::endl;
71+
std::cout
72+
<< " update Run non-interactive update of LODA and its data"
73+
<< std::endl;
74+
6975
std::cout << std::endl << "Targets:" << std::endl;
7076
std::cout
7177
<< " <file> Path to a LODA file (file extension: *.asm)"
@@ -131,7 +137,18 @@ std::pair<std::string, size_t> getProgramPathAndSeqId(std::string arg) {
131137

132138
// official commands
133139

134-
void Commands::setup() { Setup::runWizard(); }
140+
void Commands::setup() {
141+
initLog(true);
142+
Setup::runWizard();
143+
}
144+
145+
void Commands::update() {
146+
initLog(false);
147+
OeisManager manager(settings);
148+
manager.update(true);
149+
manager.getStats();
150+
manager.generateLists();
151+
}
135152

136153
void Commands::evaluate(const std::string& path) {
137154
initLog(true);

src/include/commands.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Commands {
1414

1515
void setup();
1616

17+
void update();
18+
1719
void evaluate(const std::string& path);
1820

1921
void check(const std::string& path);

src/include/oeis_manager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class OeisManager {
2828

2929
void load();
3030

31+
void update(bool force);
32+
3133
void migrate();
3234

3335
const std::vector<OeisSequence>& getSequences() const;
@@ -56,8 +58,6 @@ class OeisManager {
5658

5759
bool shouldMatch(const OeisSequence& seq) const;
5860

59-
void update();
60-
6161
void generateStats(int64_t age_in_days);
6262

6363
void addSeqComments(Program& p) const;

src/include/setup.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Setup {
6363

6464
static std::string getLatestVersion();
6565

66-
static void checkLatestedVersion();
66+
static std::string checkLatestedVersion(bool silent);
6767

6868
static bool hasMemory();
6969

@@ -73,6 +73,8 @@ class Setup {
7373

7474
static void runWizard();
7575

76+
static void performUpdate(const std::string& new_version, bool silent);
77+
7678
private:
7779
static constexpr int64_t UNDEFINED_INT = -2; // cannot use -1
7880
static constexpr int64_t DEFAULT_GITHUB_UPDATE_INTERVAL = 1; // 1 day default
@@ -114,4 +116,6 @@ class Setup {
114116
static bool updateFile(const std::string& local_file, const std::string& url,
115117
const std::string& header, const std::string& marker,
116118
bool executable);
119+
120+
static std::string getExecutable(const std::string& suffix);
117121
};

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ int dispatch(Settings settings, const std::vector<std::string>& args) {
134134
// official commands
135135
if (cmd == "setup") {
136136
commands.setup();
137+
} else if (cmd == "update") {
138+
commands.update();
137139
} else if (cmd == "evaluate" || cmd == "eval") {
138140
commands.evaluate(args.at(1));
139141
} else if (cmd == "check") {

src/oeis_manager.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void OeisManager::load() {
7676
FolderLock lock(Setup::getOeisHome());
7777

7878
// update index if needed
79-
update();
79+
update(false);
8080

8181
// load sequence data, names and deny list
8282
Log::get().info("Loading sequences from the OEIS index");
@@ -303,10 +303,11 @@ bool OeisManager::shouldMatch(const OeisSequence &seq) const {
303303
return true; // unreachable
304304
}
305305

306-
void OeisManager::update() {
306+
void OeisManager::update(bool force) {
307307
std::vector<std::string> files = {"stripped", "names"};
308308

309309
// check whether oeis files need to be updated
310+
update_oeis = false;
310311
auto it = files.begin();
311312
int64_t oeis_age_in_days = -1;
312313
while (it != files.end()) {
@@ -321,6 +322,7 @@ void OeisManager::update() {
321322
}
322323

323324
// check whether programs need to be updated
325+
update_programs = false;
324326
const std::string progs_dir = Setup::getProgramsHome();
325327
const std::string local_dir = progs_dir + "local";
326328
const std::string update_progs_file = local_dir + FILE_SEP + ".update";
@@ -330,9 +332,26 @@ void OeisManager::update() {
330332
update_programs = true;
331333
}
332334

335+
// figure out if we should check for loda update
336+
bool check_loda_update = (update_oeis && (Random::get().gen() % 5 == 0));
337+
338+
// force update?
339+
if (force) {
340+
update_oeis = true;
341+
update_programs = true;
342+
check_loda_update = true;
343+
}
344+
345+
// check and perform loda update
346+
if (check_loda_update) {
347+
auto latest_version = Setup::checkLatestedVersion(false);
348+
if (force && !latest_version.empty()) {
349+
Setup::performUpdate(latest_version, false);
350+
}
351+
}
352+
333353
// perform oeis update
334354
if (update_oeis) {
335-
Setup::checkLatestedVersion();
336355
if (oeis_age_in_days == -1) {
337356
Log::get().info("Creating OEIS index at \"" + Setup::getOeisHome() +
338357
"\"");
@@ -683,24 +702,26 @@ void OeisManager::alert(Program p, size_t id, const std::string &prefix,
683702
const std::string &color,
684703
const std::string &submitted_by, bool tweet) const {
685704
auto &seq = sequences.at(id);
686-
std::stringstream buf;
687-
buf << prefix << " program for " << seq
688-
<< " Terms: " << seq.getTerms(settings.num_terms);
705+
std::string msg, full;
706+
msg = prefix + " program for " + seq.to_string();
707+
full = msg + " Terms: " + seq.getTerms(settings.num_terms).to_string();
689708
FormulaGenerator gen(false);
690709
Formula formula;
691710
if (gen.generate(p, id, formula, false)) {
692-
buf << ". Formula: " << formula.toString(false);
711+
full += ". Formula: " + formula.toString(false);
693712
}
694713
if (!submitted_by.empty()) {
695-
buf << ". " << ProgramUtil::PREFIX_SUBMITTED_BY << " " << submitted_by;
714+
std::string sub = ProgramUtil::PREFIX_SUBMITTED_BY + " " + submitted_by;
715+
msg += " " + sub;
716+
full += ". " + sub;
696717
}
697-
auto msg = buf.str();
698718
Log::AlertDetails details;
699719
details.title = seq.id_str();
700720
details.title_link = seq.url_str();
701721
details.color = color;
702722
details.tweet = tweet;
703-
buf << "\\n\\`\\`\\`\\n";
723+
std::stringstream buf;
724+
buf << full << "\\n\\`\\`\\`\\n";
704725
ProgramUtil::removeOps(p, Operation::Type::NOP);
705726
addSeqComments(p);
706727
ProgramUtil::print(p, buf, "\\n");

src/setup.cpp

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -469,66 +469,84 @@ std::string Setup::getLatestVersion() {
469469
return json["tag_name"].as_string();
470470
}
471471

472-
void Setup::checkLatestedVersion() {
473-
if (Version::IS_RELEASE && (Random::get().gen() % 5 == 0)) {
474-
const auto latest_version = Setup::getLatestVersion();
472+
std::string Setup::checkLatestedVersion(bool silent) {
473+
if (Version::IS_RELEASE) {
474+
const auto latest_version = getLatestVersion();
475475
if (latest_version != Version::BRANCH) {
476-
Log::get().info("LODA " + latest_version +
477-
" is available. Run 'loda setup' to update.");
476+
if (!silent) {
477+
Log::get().info("Update to LODA " + latest_version + " available");
478+
}
479+
return latest_version;
478480
}
479481
}
482+
return "";
480483
}
481484

482-
bool Setup::checkUpdate() {
483-
ensureDir(LODA_HOME + "bin" + FILE_SEP);
485+
std::string Setup::getExecutable(const std::string& suffix) {
486+
std::string exe;
487+
#ifdef _WIN64
488+
exe = ".exe";
489+
#endif
490+
return LODA_HOME + "bin" + FILE_SEP + "loda" + suffix + exe;
491+
}
492+
493+
void Setup::performUpdate(const std::string& new_version, bool silent) {
484494
std::string exe;
485495
#ifdef _WIN64
486496
exe = ".exe";
487497
#endif
488-
const std::string exec_local = LODA_HOME + "bin" + FILE_SEP + "loda" + exe;
489-
const std::string exec_tmp =
490-
LODA_HOME + "bin" + FILE_SEP + "loda-" + Version::PLATFORM + exe;
498+
ensureDir(LODA_HOME + "bin" + FILE_SEP);
499+
const std::string exec_local = getExecutable("");
500+
const std::string exec_tmp = getExecutable("-" + Version::PLATFORM);
491501
#ifdef _WIN64
492502
if (isFile(exec_local) && isFile(exec_tmp)) {
493503
std::string cmd = "del \"" + exec_tmp + "\" " + getNullRedirect();
494504
if (system(cmd.c_str()) != 0) {
495-
std::cout << "Error deleting temporary file: " + exec_tmp << std::endl;
505+
Log::get().error("Cannot delete temporary file: " + exec_tmp, false);
496506
}
497507
}
498508
#endif
499-
auto latest_version = getLatestVersion();
500-
if (Version::IS_RELEASE &&
501-
(latest_version != Version::BRANCH || !isFile(exec_local))) {
502-
std::cout << "LODA " << latest_version << " is available!" << std::endl
503-
<< "Do you want to install the update? (Y/n) ";
504-
std::string line;
505-
std::getline(std::cin, line);
506-
if (line.empty() || line == "y" || line == "Y") {
507-
const std::string exec_url =
508-
"https://github.com/loda-lang/loda-cpp/releases/download/" +
509-
latest_version + "/loda-" + Version::PLATFORM + exe;
510-
WebClient::get(exec_url, exec_tmp, true, true);
509+
const std::string exec_url =
510+
"https://github.com/loda-lang/loda-cpp/releases/download/" + new_version +
511+
"/loda-" + Version::PLATFORM + exe;
512+
WebClient::get(exec_url, exec_tmp, true, true);
511513
#ifdef _WIN64
512-
const std::string cmd = "\"" + exec_tmp +
513-
"\" update-windows-executable \"" + exec_tmp +
514-
"\" \"" + exec_local + "\"";
515-
createWindowsProcess(cmd);
514+
const std::string cmd = "\"" + exec_tmp + "\" update-windows-executable \"" +
515+
exec_tmp + "\" \"" + exec_local + "\"";
516+
createWindowsProcess(cmd);
516517
#else
517-
makeExecutable(exec_tmp);
518-
moveFile(exec_tmp, exec_local);
519-
std::cout << "Update installed. Restarting setup... " << std::endl
520-
<< std::endl;
521-
std::string new_setup = exec_local + " setup";
522-
if (system(new_setup.c_str()) != 0) {
523-
std::cout << "Error running setup of LODA " << latest_version
524-
<< std::endl;
525-
}
518+
makeExecutable(exec_tmp);
519+
moveFile(exec_tmp, exec_local);
520+
if (!silent) {
521+
Log::get().info("Installed update to LODA " + new_version);
522+
}
526523
#endif
527-
// in any case, we must stop the current setup here
528-
return false;
529-
} else {
530-
std::cout << std::endl;
524+
}
525+
526+
bool Setup::checkUpdate() {
527+
auto latest_version = checkLatestedVersion(true);
528+
if (latest_version.empty()) {
529+
return true;
530+
}
531+
std::cout << "LODA " << latest_version << " is available!" << std::endl
532+
<< "Do you want to install the update? (Y/n) ";
533+
std::string line;
534+
std::getline(std::cin, line);
535+
if (line.empty() || line == "y" || line == "Y") {
536+
performUpdate(latest_version, true);
537+
#ifndef _WIN64
538+
std::cout << "Update installed. Restarting setup... " << std::endl
539+
<< std::endl;
540+
std::string new_setup = getExecutable("") + " setup";
541+
if (system(new_setup.c_str()) != 0) {
542+
std::cout << "Error running setup of LODA " << latest_version
543+
<< std::endl;
531544
}
545+
#endif
546+
// in any case, we must stop the current setup here
547+
return false;
548+
} else {
549+
std::cout << std::endl;
532550
}
533551
return true;
534552
}

0 commit comments

Comments
 (0)