This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aurtoo.cpp
395 lines (334 loc) · 11.2 KB
/
aurtoo.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <filesystem>
#include <vector>
#include <stdlib.h>
#include <bits/stdc++.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
namespace fs = std::filesystem;
using namespace std;
// Stuff that can be edited
string tgt_missing = "You didn't include a target! :(\n";
string home = getenv("HOME");
string user = getenv("USER");
string config_dir = home + "/.config/aurtoo/";
string config_fn = "settings.conf";
// Stuff loaded from config
string out_dir = "";
string data_dir = "";
string repo_name = "";
bool proc(const string command) {
int code = system(command.c_str());
if (code == 0) {
return true;
} else {
cout << "Exit code: " << code << "\n";
return false;
}
}
void ensure_dir(const string path) {
if (!fs::is_directory(path)) {
mkdir(path.c_str(), 0777);
}
}
void loadconfig() {
if (fs::is_directory(config_dir) && fs::is_regular_file(config_dir + config_fn)) {
//cout << "Loading config from " << config_dir + config_fn << "\n";
string line;
string lines[3];
ifstream config;
config.open(config_dir + config_fn);
if (config.is_open()) {
int i = 0;
while (getline(config,line)) {
lines[i] = line;
i++;
}
config.close();
} else {
cerr << "Unable to open config file\n";
exit(1);
}
//cout << "Config lines: \n";
for (int i = 0; i < 3; i++) {
string thisline = lines[i];
// cout << thisline << "\n";
if (thisline.find("datadir") != string::npos) {
data_dir = thisline.substr(thisline.find("=")+1, -1);
cout << "Data dir is: " << data_dir << "\n";
} else if (thisline.find("outdir") != string::npos) {
out_dir = thisline.substr(thisline.find("=")+1, -1);
cout << "Out dir is: " << out_dir << "\n";
} else if (thisline.find("repon") != string::npos) {
repo_name = thisline.substr(thisline.find("=")+1, -1);
cout << "Repo name is: " << repo_name << "\n";
}
}
} else {
cout << "Seems like you haven't configured things yet. Let's do that\n";
// guess cpp fs standard is still WIP
// does this still work if the dir exists? guess so
mkdir(config_dir.c_str(), 0777);
cout << "Directory to hold package folders (full path): ";
cin >> data_dir;
cout << "Directory to hold repo (full path): ";
cin >> out_dir;
cout << "Repo name: ";
cin >> repo_name;
ofstream config;
config.open(config_dir + config_fn);
config << "datadir=" << data_dir << "\n";
config << "outdir=" << out_dir << "\n";
config << "repon=" << repo_name << "\n";
config.close();
cout << "Here are your configured settings\n";
cout << "Package storage: " << data_dir << "\n";
cout << "Output repo: " << out_dir << "\n";
cout << "Repo name: " << repo_name << "\n";
}
ensure_dir(data_dir);
ensure_dir(out_dir);
}
bool makepkg(const string target) {
chdir(target.c_str());
string thisline;
ifstream dependsf(".SRCINFO");
vector<string> depends;
if (dependsf.is_open()) {
while (getline(dependsf,thisline)) {
/*
string target = "heeho this is a test";
string remove = "this";
if (target.find(remove) != string::npos) {
target.erase(target.find(remove), remove.length());
cout << target << endl;
}
*/
string remove[2] = {"makedepends = ", "depends = "};
for (string target : remove) {
if (thisline.find(target) != string::npos && thisline.find("optdepends") == string::npos) {
thisline.erase(thisline.find(target), target.length());
depends.push_back(thisline);
}
}
}
dependsf.close();
} else {
cerr << "Failed to open .SRCINFO for " << target << "\n";
exit(1);
}
for (string depend : depends) {
cout << "Checking if " << depend << " is an AUR package itself";
bool res = proc("curl -If https://aur.archlinux.org/packages/" + depend);
if (res) {
cout << depend << " is an AUR package.\nRecursive AUR installing is on the todo list.";
cout << "For now, please install it manually. (or by re-running aurtoo ;) )\n";
} else {
cout << "Installing " + depend + " with pacman, as it's not an AUR package\n";
bool res = proc("sudo pacman -Sy --needed --noconfirm " + depend);
if (!res) {
cerr << "Failed to install " + depend + " with pacman.\n";
exit(1);
}
}
}
bool res = proc("updpkgsums");
if (!res) {
cerr << "Could not update checksums for " << target << "\n";
exit(1);
}
res = proc("makepkg -f --sign");
if (!res) {
cerr << "Could not build package " << target << "\n";
exit(1);
}
cout << "Done building " << target << "\n";
vector<string> files;
for (const auto & entry : fs::directory_iterator(".")) {
string tp = entry.path();
if (tp.find(target) != string::npos && tp.find("pkg") != string::npos) {
files.push_back(tp);
}
}
// we shouldn't have more than one
fs::rename(files[0], out_dir + "/" + files[0]);
chdir("../");
return true;
}
bool updrepo() {
chdir(out_dir.c_str());
vector<string> files;
for (const auto & entry : fs::directory_iterator(".")) {
string tp = entry.path();
if (tp.find("tar") != string::npos && tp.find("pkg") != string::npos) {
files.push_back(tp);
}
}
if (files.size() != 0) {
for (const auto & pkg : files) {
bool code = proc("repo-add " + repo_name + ".db.tar.zst " + pkg);
if (!code) {
cout << "Failed while adding " << pkg << " to repo\n";
exit(1);
}
}
} else {
cout << "Seems like there are no packages here!!!\nThis repo will likely break since it's empty.\n";
}
chdir("../");
return true;
}
// Add package
bool add(const string target) {
chdir(data_dir.c_str());
if (fs::is_directory(target)) {
cerr << "Seems like we've already got " << target << ".\nDid you mean to update?\n";
exit(1);
//return false;
} else {
bool clone = proc("git clone https://aur.archlinux.org/" + target + ".git");
if (clone) {
// we got source
bool done = makepkg(target);
if (done) {
bool repo = updrepo();
if (repo) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
cerr << "Failed to clone from AUR for " + target + "\n";
exit(1);
}
}
return false;
}
// Update all packages
bool update() {
chdir(data_dir.c_str());
for (const auto & thing : fs::directory_iterator(".")) {
string tp = thing.path();
if (fs::is_directory(tp)) {
chdir(tp.c_str());
cout << "Checking: " << tp.erase(0,2) << "\n";
bool gitp = proc("git pull > stat");
if (!gitp) {
cerr << "Git failed while updating " << tp << "\n";
exit(1);
}
vector<string> lines;
string thisline;
ifstream stat("stat");
if (stat.is_open()) {
while (getline(stat,thisline)) {
lines.push_back(thisline);
}
stat.close();
} else {
cerr << "Could not check status of " << tp;
exit(1);
}
//fs::remove("stat");
if (lines.size() == 1) {
cout << "Looks like this package is up to date.\n";
chdir("../");
} else {
cout << "Looks like this package needs an update.\n";
chdir("../");
makepkg(tp);
}
}
}
return true;
}
// Stop serving package
bool remove(const string target) {
chdir(data_dir.c_str());
if (fs::is_directory(target)) {
fs::remove_all(target);
} else {
cerr << "Seems like " << target << " isn't installed?\n";
exit(1);
}
cout << "Removed the package dir for " << target << "\nChecking repo dir\n";
chdir(out_dir.c_str());
for (const auto & entry : fs::directory_iterator(".")) {
string tp = entry.path();
if (tp.find(target) != string::npos && tp.find("pkg") != string::npos) {
fs::remove(tp);
}
}
cout << "Verified that " << target << " is not in the repo. Updating repo db.\n";
updrepo();
cout << "Done.\n";
return true;
}
void listp() {
chdir(data_dir.c_str());
cout << "Installed packages: \n";
for (const auto & dir : fs::directory_iterator(".")) {
string dirn = dir.path();
dirn.erase(0,2);
cout << " - " << dirn << "\n";
}
}
int main(int argc, char *argv[]) {
// This func exits on 1 if failed.
// (it creates non-existent config)
loadconfig();
if (argc == 1) {
cout << "You need to give me instructions. See 'help'\n";
} else {
string command = argv[1];
if (command == "add") {
if (argc != 3) {
cout << tgt_missing;
exit(1);
} else { // this is probably even lazier than I realize
string target = argv[2];
bool res = add(target);
if (res) {
cout << "Added " << target << endl;
return 0;
} else {
return 1;
}
}
} else if (command == "remove") {
if (argc != 3) {
cout << tgt_missing;
exit(1);
} else { // this is probably even lazier than I realize
string target = argv[2];
bool res = remove(target);
if (res) {
return 0;
} else {
return 1;
}
}
} else if (command == "update") {
if (update()) {
return 0;
} else {
return 1;
}
} else if (command == "list") {
listp();
} else {
cout << "Options:\n";
cout << " - add <package> : build <package> from AUR and add it to the custom repo\n";
cout << " - remove <package> : remove <package> from repo and from being built in the future\n";
cout << " - update : update all installed packages to the repo\n";
cout << " - list : show packages in repo\n";
}
}
return 0;
}