-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.pike
executable file
·1481 lines (1277 loc) · 36.8 KB
/
server.pike
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env pike
// Xenofarm server
// By Martin Nilsson
// Made useable on its own by Per Cederqvist
Sql.Sql xfdb;
constant checkin_state_file = "state/checkin.timestamp";
int min_build_distance = 60*60*2;
int fail_build_divisor = 2*6;
int checkin_poll = 60;
int checkin_latency = 60*5;
string project; // --project
string web_dir; // --web-dir
string web_format; // --web-format
string repository; // --repository
string cvs_module; // --cvs-module
string svn_module; // --svn-module
string repo_name; // --repo-name
string remote = "origin"; // --remote
array(string) branches = ({}); // --branch
string tag_format; // --tag
string work_dir; // --work-dir
string source_transformer;
array(string) update_opts = ({});
int(0..1) verbose;
array(string) ignored_globs = ({ });
int(0..1) keep_going = 1;
// This is a global loop variable used to avoid
// having to pass the current branch through in
// the argument lists everywhere.
string branch;
class CommitId
{
int unix_time();
int unix_time_available();
int create_build_id();
int build_needed(CommitId new_commit);
int pending_latency();
string dist_name()
{
object at = Calendar.ISO_UTC.Second("unix", unix_time());
return sprintf("%s-%s-%s", project,
at->format_ymd_short(),
at->format_tod_short());
}
}
class TimeStampCommitId
{
inherit CommitId;
int timestamp;
string export_state;
void create(int timestamp, string export_state)
{
this->timestamp = timestamp;
this->export_state = export_state;
}
int unix_time()
{
return timestamp;
}
int unix_time_available()
{
return true;
}
int build_needed(CommitId new_commit)
{
return new_commit->unix_time() > unix_time();
}
int pending_latency()
{
int rv = unix_time() + checkin_latency - time();
if(rv < 0)
rv = 0;
return rv;
}
int create_build_id()
{
persistent_query("INSERT INTO build\n"
"SET time = %d, export = 'PASS',\n"
"project = %s, remote = %s, branch = %s",
unix_time(), project, remote, branch);
int buildid;
mixed err = catch {
buildid = (int)xfdb->query("SELECT LAST_INSERT_ID() AS id")[0]->id;
};
if(err) {
catch(xfdb->query("DELETE FROM build\n"
"WHERE project=%s AND remote=%s AND branch=%s\n"
" AND time=%d",
project, remote, branch, unix_time()));
return 0;
}
return buildid;
}
}
class Sha1CommitId
{
inherit CommitId;
string commit_id;
int build_time;
string export_state;
void create(string commit_id,
int build_time,
string export_state)
{
this->commit_id = commit_id;
this->build_time = build_time;
this->export_state = export_state;
}
int unix_time()
{
if( !build_time )
error("build_time not yet set on Git commit %s\n", commit_id);
return build_time;
}
int unix_time_available()
{
return !!build_time;
}
int build_needed(CommitId new_commit)
{
return new_commit->commit_id != commit_id;
}
int pending_latency()
{
int rv = build_time + checkin_latency - time();
if(rv < 0)
rv = 0;
return rv;
}
int create_build_id()
{
if(build_time == 0)
build_time = time();
persistent_query("INSERT INTO build\n"
"SET time = %d, export='PASS', commit_id = %s,\n"
" project = %s, remote = %s, branch = %s",
unix_time(), commit_id, project, remote, branch);
int buildid;
mixed err = catch {
buildid = (int)xfdb->query("SELECT LAST_INSERT_ID() AS id")[0]->id;
};
if(err) {
catch(xfdb->query("DELETE FROM build\n"
"WHERE project=%s AND remote=%s AND branch=%s\n"
" AND time=%d AND commit_id=%s",
project, remote, branch, unix_time(), commit_id));
return 0;
}
return buildid;
}
}
//
// Repository classes
//
string client_type;
class RepositoryClient {
// Returns the posix time when the latest checkin was committed.
CommitId get_latest_checkin();
// This method gets called when the local source tree should
// be updated.
void update_source(CommitId commit_id);
// A string with descriptions of the special arguments this repository
// client accepts.
constant arguments = "";
// This method is called during startup and is fed the command line
// arguments for parsing.
void parse_arguments(array(string));
// Should return the name of the repository module.
string module();
// Should return the name of the repository client.
string name();
}
// Base class for version control systems that cannot check out the
// code as it was at a certain time. This class instead performs a
// full checkout in get_latest_checkin() and returns the current time,
// and update_source essentially becomes a no-op or a call to
// get_latest_checkin().
class FakeTimeClient {
inherit RepositoryClient;
protected int latest_checkin;
// The get_latest_checkin function should return the (UTC) unixtime of
// the latest check in. This version actually returns the time we last
// detected that something has been checked in. That is good enough.
TimeStampCommitId get_latest_checkin()
{
check_work_dir();
Calendar.TimeRange now = Calendar.Second();
array(string) log = update_to_current_source();
latest_checkin = (int)Stdio.read_file(checkin_state_file);
latest_checkin = time_of_change(log, checkin_state_file,
latest_checkin, now);
return TimeStampCommitId(latest_checkin, "UNKNOWN");
}
int time_of_change(array(string) log,
string checkin_state_file,
int latest_checkin,
Calendar.TimeRange now)
{
if(sizeof(log))
{
debug("Something changed: \n %s", log * "\n " + "\n");
latest_checkin = now->unix_time();
Stdio.write_file(checkin_state_file, latest_checkin + "\n");
}
else {
debug("Nothing changed\n");
}
// Handle a missing checkin_state_file file. This should only happen
// the first time server.pike is run.
if(latest_checkin == 0)
{
debug("No check in timestamp found; assuming something changed.\n");
latest_checkin = now->unix_time();
Stdio.write_file(checkin_state_file, latest_checkin + "\n");
}
return latest_checkin;
}
void update_source(TimeStampCommitId when) {
if(!latest_checkin || when->unix_time() > latest_checkin)
get_latest_checkin();
}
// Check that we have a working copy of the source tree. Do exit(1)
// otherwise.
void check_work_dir();
// Do "cvs update" or the corresponding command. Return a non-empty
// log file if anything changed.
array(string) update_to_current_source();
}
class CVSClient {
inherit FakeTimeClient;
constant arguments =
"\nCVS specific arguments:\n\n"
"--cvs-module The CVS module the server should use.\n"
"--update-opts CVS options to append to \"cvs -q update\".\n"
" Default: \"-d\". \"--update-opts=-Pd\" also makes sense.\n"
"--repository The CVS repository the server should use.\n";
void parse_arguments(array(string) args) { }
string module() {
return cvs_module;
}
string name() {
return "CVS";
}
void check_work_dir()
{
if(!file_stat(cvs_module) || !file_stat(cvs_module)->isdir) {
write("Please check out %O inside %O and re-run this script.\n",
cvs_module, work_dir);
exit(1);
}
}
array(string) update_to_current_source()
{
debug("Running cvs update.\n");
set_status("Running cvs update.");
object update =
Process.create_process(({ "cvs", "-q", "update",
@update_opts }),
([ "cwd" : cvs_module,
"stdout" : Stdio.File("tmp/update.log", "cwt"),
"stderr" : Stdio.File("/dev/null", "cwt") ]));
if(update->wait())
{
write("Failed to update CVS module %O in %O.\n", cvs_module, getcwd());
exit(1);
}
return filter(Stdio.read_file("tmp/update.log") / "\n" - ({ "" }),
lambda(string row) { return !has_prefix(row, "? "); });
}
}
class GitCommitNode {
string id;
array(string) parents;
array(string) files;
void create(string block)
{
array(string) lines = block / "\n";
if(sizeof(lines) < 1)
error("Broken Git log output: '%O'.\n", block);
array(string) fields = lines[0] / " ";
if(sizeof(fields) < 1)
error("Broken Git log output: '%O'.\n", block);
id = fields[0];
parents = fields[1..];
files = lines[1..] - ({ "" });
}
// Return false if all the files in the node are ignored,
// true otherwise.
int commit_wanted()
{
foreach(files, string file)
{
int ignored = 0;
foreach(ignored_globs, string glb)
if(glob(glb, file))
ignored = 1;
if(!ignored)
return 1;
}
return 0;
}
}
class FirstWanted
{
string last_single = 0;
string result = 0;
multiset(string) pending_commits = (< >);
string feed(GitCommitNode node)
{
if( result )
return result;
pending_commits[node->id] = 0;
if(sizeof(pending_commits) == 0)
last_single = node->id;
foreach( node->parents, string parent )
pending_commits[parent] = 1;
if( node->commit_wanted() )
result = last_single;
return result;
}
}
class GitClient {
inherit RepositoryClient;
constant arguments =
"\nGit specific arguments:\n\n"
"--repo-name The name of the repository (inside workdir).\n"
"--project The project name.\n"
"--remote The remote where the branch is found.\n"
"--branch The branch of the repository to monitor.\n";
string last_commit;
void parse_arguments(array(string) args) {
foreach(Getopt.find_all_options(args, ({
({ "project", Getopt.HAS_ARG, "--project" }),
({ "remote", Getopt.HAS_ARG, "--remote" }),
({ "branch", Getopt.HAS_ARG, "--branch" }),}) ),array opt)
{
switch(opt[0])
{
case "project":
project = opt[1];
break;
case "remote":
remote = opt[1];
break;
case "branch":
branches += ({ opt[1] });
break;
}
}
}
string module() {
return repo_name || branch;
}
string name() {
return "Git";
}
string current_commit_id()
{
return rev_parse("HEAD");
}
string rev_parse(string ref)
{
return git_stdout("rev-parse", ref);
}
// Run a git command. Exit if it fails (after having written the
// output from the command to stdout). Return the stdout output of
// the git command, with any trailing whitespace removed.
string git_stdout(string...args)
{
Stdio.File stdout = Stdio.File();
object stat =
Process.create_process(({ "git" }) + args,
([ "cwd": module(),
"stdout" : stdout.pipe() ]));
string res = stdout.read();
if(stat->wait())
{
write("Failed to run \"git %s\" in %O.\n",
args * " ", combine_path(getcwd(), module()));
exit(1);
}
return String.trim_all_whites(res);
}
void run_git(string...args)
{
git_stdout(@args);
}
Sha1CommitId get_latest_checkin()
{
check_work_dir();
get_current_source();
string commit = first_wanted_commit();
if(!commit)
return 0;
int ctime;
string raw = git_stdout("cat-file", "commit", commit);
foreach(raw/"\n", string line) {
if (!has_prefix(line, "committer ")) continue;
ctime = (int)((line/" ")[-2]);
break;
}
return Sha1CommitId(commit, ctime, "UNKNOWN");
}
// Run "git log" and return the first commit that contains
// "interesting" changes, skipping changes that only changes files
// that match the global ignored_globs variable.
//
// If a merge is found, it will either return the merge commit, or a
// commit from the time before the development forked. If there are
// any interesting changes during the forked development, the merge
// commit will be returned.
string first_wanted_commit()
{
Stdio.File stdout = Stdio.File();
Process.create_process logproc =
Process.create_process( ({ "git", "log", "--name-only",
"--pretty=format:%x00%H %P" }),
([ "cwd": module(),
"stdout": stdout.pipe() ]) );
string buf = "";
FirstWanted wanted = FirstWanted();
string res = 0;
while(string x = stdout.read(8096, 1)) {
if( !strlen(x) )
break;
buf += x;
array(string) blocks = buf / "\0";
[buf, blocks] = Array.pop(blocks);
foreach( blocks, string block ) {
if( strlen(block) > 0 ) {
res = wanted->feed(GitCommitNode(block));
if( res )
break;
}
}
if( res )
break;
}
if( !res && has_value("\n", buf) )
res = wanted->feed(GitCommitNode(buf));
logproc->kill(9);
logproc->wait();
return res;
}
void check_work_dir()
{
if(!file_stat(module()) || !file_stat(module())->isdir
|| !file_stat(combine_path(module(), ".git"))
|| !file_stat(combine_path(module(), ".git"))->isdir) {
write("Please clone %O to the %O directory and re-run this script.\n",
project, combine_path(work_dir, module()));
exit(1);
}
}
int working_on_a_branch()
{
object symref =
Process.create_process( ({ "git", "symbolic-ref", "-q", "HEAD" }),
([ "cwd": module(),
"stdout": Stdio.File("/dev/null", "cwt") ]) );
switch( symref->wait() )
{
case 0:
return 1;
case 1:
return 0;
default:
write("Failed to run git symbolic-ref.\n");
exit(1);
}
}
string remote_for_branch(string branch_ref)
{
string branch;
if( sscanf(branch_ref, "refs/heads/%s", branch) != 1)
{
write("Failed to parse %s as a branch head.\n", branch_ref);
exit(1);
}
return git_stdout("config", sprintf("branch.%s.remote", branch));
}
void get_current_source()
{
// If the latest update_source() put us on a detached head, move
// back to the branch we came from.
if( !working_on_a_branch() )
checkout("@{-1}");
// In most cases, we could to "git pull" instead of running "git
// fetch" and "git reset --hard". But this works if the branch
// has been rebased (or if somebody has done "git commit --amend"
// or something similar). It also makes it possible to do "git
// bisect" and push HEAD to a special bisect branch (that will
// jump all over the place) so that "git bisect" can be used with
// the autobuilder.
string my_branch = git_stdout("symbolic-ref", "HEAD");
if(!my_branch) {
write("Failed to find current branch\n");
exit(1);
}
debug("Running git fetch.\n");
set_status("Running git fetch.");
run_git("fetch", "-p", remote_for_branch(my_branch));
debug("Updating local git tree.\n");
set_status("Updating local git tree.");
string upstream = git_stdout("for-each-ref", "--format=%(upstream)",
my_branch);
run_git("reset", "--hard", upstream);
string after = current_commit_id();
if( after != last_commit )
{
last_commit = after;
debug("HEAD is currently at %s\n", last_commit);
}
}
void checkout(string commit_id)
{
object stat =
Process.create_process(({ "git", "checkout", commit_id }),
([ "cwd": module(),
"stdout" : Stdio.File("tmp/co.log", "cwt"),
"stderr" : Stdio.File("/dev/null", "cwt") ]));
if(stat->wait())
{
write("Failed to check out %O.\n", commit_id);
exit(1);
}
}
void update_source(Sha1CommitId commit_id)
{
// Shortcut for the common case that we already have the requested
// version.
if(current_commit_id() == commit_id->commit_id)
return;
checkout(commit_id->commit_id);
if(current_commit_id() == commit_id->commit_id)
return;
write("FATAL: Failed to update tree to %s: got %s.\n",
commit_id->commit_id, current_commit_id());
exit(1);
}
void tag_source(int buildno) {
debug("Running git tag.\n");
string commit_id = current_commit_id();
object tag =
Process.create_process(({ "git", "tag",
sprintf(tag_format, buildno),
commit_id }),
([ "cwd": module(),
"stdout" : Stdio.File("tmp/tag.log", "cwt"),
"stderr" : Stdio.File("/dev/null", "cwt") ]));
if(tag->wait())
{
write("Failed to tag Git commit %O as %O on branch %O in %O.\n",
commit_id, sprintf(tag_format, buildno), branch||"HEAD", getcwd());
exit(1);
}
// FIXME: Optional push of the tag?
}
}
class SVNClient {
inherit FakeTimeClient;
constant arguments =
"\nSVN specific arguments:\n\n"
"--svn-module The Subversion module the server should use.\n";
void parse_arguments(array(string) args) {
foreach(Getopt.find_all_options(args, ({
({ "svn_module", Getopt.HAS_ARG, "--svn-module" }),}) ),array opt)
{
switch(opt[0])
{
case "svn_module":
svn_module = opt[1];
break;
}
}
}
string module() {
return svn_module;
}
string name() {
return "SVN";
}
void check_work_dir()
{
if(!file_stat(svn_module) || !file_stat(svn_module)->isdir) {
write("Please check out %O inside %O and re-run this script.\n",
svn_module, work_dir);
exit(1);
}
}
array(string) update_to_current_source()
{
debug("Running svn update.\n");
set_status("Running svn update.");
object update =
Process.create_process(({ "svn", "update" }),
([ "cwd" : svn_module,
"stdout" : Stdio.File("tmp/update.log", "cwt"),
"stderr" : Stdio.File("/dev/null", "cwt") ]));
if(update->wait())
{
write("Failed to update SVN module %O in %O.\n", svn_module, getcwd());
exit(1);
}
return filter(Stdio.read_file("tmp/update.log") / "\n" - ({ "" }),
lambda(string row) {
return !(has_prefix(row, "? ")
||has_prefix(row, "At revision"));
});
}
}
class StarTeamClient {
inherit FakeTimeClient;
string st_module;
string st_project;
string st_pwdfile;
constant arguments =
"\nStarteam specific arguments:\n\n"
"--st-module basename of dir where contents of the view folder will reside.\n"
" Similar to the cvs-module option for the CVS client.\n"
"--st-project username:password@host:port/project/view/folder/\n"
"--st-pwdfile password filename\n";
void parse_arguments(array(string) args) {
foreach(Getopt.find_all_options(args, ({
({ "st_module", Getopt.HAS_ARG, "--st-module" }),
({ "st_project", Getopt.HAS_ARG, "--st-project" }),
({ "st_pwdfile", Getopt.HAS_ARG, "--st-pwdfile" }),}) ),array opt)
{
switch(opt[0])
{
case "st_module":
st_module = opt[1];
break;
case "st_project":
st_project = opt[1];
break;
case "st_pwdfile":
st_pwdfile = opt[1];
break;
}
}
}
string module() {
return st_module;
}
string name() {
return "StarTeam";
}
void check_work_dir()
{
//check out into work-dir/st_module
if(!file_stat(module()) || !file_stat(module())->isdir) {
write("Please check out %O inside %O and re-run this script.\n",
module(), work_dir);
exit(1);
}
}
array(string) update_to_current_source()
{
debug("Running stcmd co.\n");
set_status("Running stcmd co.");
object update =
Process.create_process(({ "stcmd", "co", "-nologo", "-is", "-p",
st_project, "-pwdfile", st_pwdfile, "-fp",
work_dir + "/" + module() }),
([ "cwd" : module(),
"stdout" : Stdio.File("tmp/update.log", "cwt"),
"stderr" : Stdio.File("tmp/update.err", "cwt")
]));
debug("Ran stcmd co -nologo -is -p " + st_project + " -pwdfile " +
st_pwdfile + " -fp " + work_dir + "/" + module() + "\n");
if(update->wait())
{
write("Failed to check out module %O in %O.\n", module(), getcwd());
exit(1);
}
return filter(Stdio.read_file("tmp/update.log") / "\n" - ({ "" }),
lambda(string row) {
return has_suffix(row, ": checked out");});
}
}
class CustomClient {
inherit FakeTimeClient;
string custom_module;
string prog;
constant arguments =
"\nCustom client specific arguments:\n\n"
"--custom_module module argument passed to custom program.\n"
"--program the custom program to run.\n"
"(the custom prg will also be passed -D <time>, like CVS)\n";
void parse_arguments(array(string) args) {
foreach(Getopt.find_all_options(args, ({
({ "module", Getopt.HAS_ARG, "--custom_module" }),
({ "program", Getopt.HAS_ARG, "--program" }), }) ),array opt)
{
switch(opt[0])
{
case "module":
custom_module = opt[1];
break;
}
switch(opt[0])
{
case "program":
prog = opt[1];
break;
}
}
}
string module() {
return custom_module;
}
string name() {
return "Custom";
}
void check_work_dir()
{
// We assume that the user knows what he is doing, so no checks here.
}
array(string) update_to_current_source()
{
debug("Running custom client.\n");
Calendar.TimeRange now = Calendar.Second();
array cmd = ({ prog, "-D", now->format_time(), custom_module });
object update =
Process.create_process( cmd,
([ "cwd" : work_dir,
"stdout" : Stdio.File("tmp/update.log", "cwt"),
"stderr" : Stdio.File("/dev/null", "cwt") ]));
string actual_command = sprintf("'%s'", cmd * "' '");
debug("Running custom checker %s\n", actual_command);
set_status("Running custom checker " + actual_command + ".");
if(update->wait())
{
write("Failed to check for updates using: '%s'.\n", actual_command);
exit(1);
}
write("Checked for updates.\n");
return Stdio.read_file("tmp/update.log")/"\n" - ({ "" });
}
}
RepositoryClient client;
//
// Helper functions
//
void debug(string msg, mixed ... args) {
if(verbose)
write("[" + Calendar.ISO.now()->format_tod() + "] "+msg, @args);
}
array persistent_query( string q, mixed ... args ) {
int(0..) try;
mixed err;
array res;
do {
try++;
err = catch {
res = xfdb->query(q, @args);
};
if(err) {
switch(try) {
case 1:
write("Database query failed. Continue to try...\n");
if(arrayp(err) && sizeof(err) && stringp(err[0]))
debug("(%s)\n", err[0][..sizeof(err[0])-2]);
break;
case 2..5:
sleep(1);
break;
default:
sleep(60);
if(!try%10) debug("Continue to try... (try %d)\n", try);
}
}
} while(err);
return res;
}
string fmt_time(int t) {
if(t<60)
return sprintf("%02d seconds", t);
if(t/60 < 60)
return sprintf("%02d:%02d minutes", t/60, t%60);
return sprintf("%02d:%02d:%02d hours", t/3600, (t%3600)/60, t%60);
}
//
// "API" functions
//
// Should return the (UTC) unixtime of the latest build package made for
// this project.
CommitId get_latest_build()
{
array res = persistent_query("SELECT time AS latest_build,\n"
" export, commit_id\n"
"FROM build\n"
"WHERE project = %s AND\n"
" remote = %s AND branch = %s\n"
"ORDER BY time DESC LIMIT 1",
project, remote, branch);
if(!res || !sizeof(res))
return 0;
string latest_state = res[0]->export;
int ts = (int)(res[0]->latest_build);
if( res[0]->commit_id )
return Sha1CommitId(res[0]->commit_id, ts, latest_state);
else
return TimeStampCommitId(ts, latest_state);
}
// Return true on success, false on error.
int(0..1) transform_source(string module, string name, string buildid) {
if(source_transformer) {
if(Process.create_process( ({ source_transformer, module, name, buildid }),
([]) )->wait() ) {
write(source_transformer+" failed\n");
return 0;
}
}
else {
string stamp = module+"/buildid.txt";
if(file_stat(stamp)) {
write(stamp+" exists!\n");
exit(1);
}
Stdio.write_file(stamp, buildid+"\n");
if(Process.create_process( ({ "tar", "cf", name+".tar", module }),
([]) )->wait() ) {
write("Failed to create %s.tar\n", name);
rm(stamp);
return 0;
}
if(Process.create_process( ({ "gzip", "-9", name+".tar" }), ([]) )->wait() ) {
write("Failed to compress %s.tar\n", name);
rm(stamp);
return 0;
}
rm(stamp);
}
return 1;
}
string make_build_low(CommitId latest_checkin)
{
int buildid = latest_checkin->create_build_id();
string name = latest_checkin->dist_name();
if (tag_format && client->tag_source) {
// FIXME: Consider formatting the tag label here
// instead of in the tag_source() function.
set_status("Tagging the source code.");
client->tag_source(buildid);
}
set_status("Creating source code dist.");
if (!transform_source(client->module(), name, (string)buildid)) {
persistent_query("UPDATE build SET export='FAIL' WHERE id=%d", buildid);
return 0;
}
return name+".tar.gz";
}
void make_build(CommitId timestamp)
{
debug("Making new build.\n");
set_status("Updating the source tree.");
client->update_source(timestamp);
string build_name = make_build_low(timestamp);
if(!build_name) {