update-search.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/perl -w
  2. # Run this from cron to update the glimpse database that lxr uses
  3. # to do full-text searches.
  4. # Created 12-Jun-98 by jwz.
  5. # Updated 2-27-99 by endico. Added multiple tree support.
  6. use Cwd;
  7. use File::Basename;
  8. use lib 'lib';
  9. use LXR::Common;
  10. use LXR::Config;
  11. use LXR::Shell;
  12. my @paths=qw(
  13. /usr/local/bin
  14. /opt/local/bin
  15. /opt/cvs-tools/bin
  16. /usr/ucb
  17. /usr/local/apache/html/mxr/glimpse
  18. /usr/local/glimpse-4.18.1p/bin
  19. /usr/local/glimpse-3.6/bin
  20. /home/build/glimpse-3.6.src/bin
  21. );
  22. my $STDERRTOSTDOUT = '2>&1';
  23. my $TREE;
  24. my %defaults = qw(
  25. TIME time
  26. UPTIME uptime
  27. DATE date
  28. );
  29. sub do_log {
  30. my $msg = shift;
  31. open LOG, '>>', $log;
  32. print LOG "$msg
  33. ";
  34. close LOG;
  35. }
  36. sub do_and_log {
  37. my $cmd = shift;
  38. do_log($cmd);
  39. system($cmd);
  40. }
  41. sub process_args {
  42. my $was_arg;
  43. do {
  44. $was_arg = 0;
  45. $TREE = shift;
  46. if ($TREE) {
  47. if ($TREE eq '-cron') {
  48. $was_arg = 1;
  49. $defaults{TIME} = $defaults{UPTIME} = '';
  50. }
  51. $TREE =~ s{/$}{};
  52. }
  53. } while ($TREE && $was_arg);
  54. }
  55. process_args(@ARGV);
  56. check_defaults(\%defaults);
  57. my $DATE = $defaults{DATE};
  58. my $TIME = $defaults{TIME};
  59. my $UPTIME = $defaults{UPTIME};
  60. $ENV{'LANG'} = 'C';
  61. # need to consider lxr.conf
  62. $lxr_dir = '.';
  63. die "can't find $lxr_dir" unless -d $lxr_dir;
  64. my $lxr_conf = "$lxr_dir/lxr.conf";
  65. unless (-f $lxr_conf) {
  66. die "could not find $lxr_conf";
  67. }
  68. my $src_dir;
  69. my $script_prefix = './';
  70. if (defined $TREE) {
  71. $script_prefix = "/$TREE/";
  72. } else {
  73. # need to sniff lxr.conf
  74. open LXRCONF, "< $lxr_conf" || die "Could not open $lxr_conf";
  75. while ($line = <LXRCONF>) {
  76. warn "trailing whitespace on line $. {$line}" if $line =~ /^\w+:.*\w.*\s+\n$/;
  77. #since no tree is defined, assume sourceroot is defined the old way
  78. #grab sourceroot from config file indexing only a single tree where
  79. #format is "sourceroot: dirname"
  80. next unless $line =~ /^sourceroot:\s*(\S+)(\s+\S+|)$/;
  81. if ($2 ne '') {
  82. $TREE = $1;
  83. $ENV{'TREE'} = $TREE;
  84. $script_prefix = "/$TREE/";
  85. } else {
  86. $src_dir = $1;
  87. }
  88. last;
  89. }
  90. close LXRCONF;
  91. }
  92. # let LXR:: handle lxr.conf
  93. $ENV{'SCRIPT_NAME'} = $script_prefix . basename($0);
  94. my ($Conf, $HTTP, $Path, $head) = &init($0);
  95. {
  96. my @trees = @{$Conf->{'trees'}};
  97. die "Could not find tree $TREE" if scalar @trees > 1 && !(grep /^\Q$TREE\E$/, @trees);
  98. }
  99. die "dbdir not set" unless defined $Conf->dbdir;
  100. $db_dir = $Conf->dbdir;
  101. $src_dir = $Conf->sourceroot;
  102. if (defined $Conf->glimpsebin) {
  103. push @paths, $1 if ($Conf->glimpsebin =~ m{(.*)/([^/]*)$});
  104. }
  105. unless (defined $src_dir) {
  106. die "could not find sourceroot for tree $TREE";
  107. }
  108. my %pathmap=();
  109. for my $mapitem (@paths) {
  110. $pathmap{$mapitem} = 1;
  111. }
  112. for my $possible_path (keys %pathmap) {
  113. $ENV{'PATH'} = "$possible_path:$ENV{'PATH'}" if -d $possible_path;
  114. }
  115. mkdir $db_dir unless -d $db_dir;
  116. my $pid_lock = get_lock($db_dir, 'xref');
  117. $log = "$db_dir/glimpseindex.log";
  118. #exec > $log 2>&1
  119. #XXX what does |set -x| mean?
  120. #system ("set -x > $log");
  121. =pod
  122. -e Exit immediately if a simple command (see
  123. SHELL GRAMMAR above) exits with a non-zero
  124. status.
  125. -x After expanding each simple command, for
  126. command, case command, select command, or
  127. arithmetic for command, display the
  128. expanded value of PS4, followed by the com�
  129. mand and its expanded arguments or associ�
  130. ated word list.
  131. =cut
  132. =pod
  133. for my $envvar (keys %ENV) {
  134. print LOG "$envvar=$ENV{$envvar}
  135. ";
  136. }
  137. =cut
  138. #system ("date >> $log");
  139. do_log ('date
  140. '.localtime().'
  141. ');
  142. unless (-d $src_dir) {
  143. do_log("$TREE src_dir $src_dir does not exist.");
  144. exit 4;
  145. }
  146. my $db_dir_tmp = "$db_dir/tmp";
  147. unless (-d $db_dir_tmp) {
  148. do_log("mkdir $db_dir_tmp");
  149. unless (mkdir $db_dir_tmp) {
  150. do_log("mkdir $db_dir_tmp failed");
  151. exit 5;
  152. }
  153. }
  154. do_log("chdir $db_dir_tmp");
  155. unless (chdir $db_dir_tmp) {
  156. do_log("chdir $db_dir_tmp failed");
  157. exit 6;
  158. }
  159. # do index everything in lxrroot
  160. my @include_paths = qw (
  161. );
  162. unshift @include_paths, $db_dir;
  163. push @include_paths, '';
  164. open GLIMPSEINCLUDE, '>.glimpse_include';
  165. print GLIMPSEINCLUDE join("\n", @include_paths);
  166. close GLIMPSEINCLUDE;
  167. # don't index VCS files
  168. open GLIMPSEEXCLUDE, '>.glimpse_exclude';
  169. my @exclude_paths = qw (
  170. /CVS/
  171. /.hg/
  172. /.git/
  173. /.svn/
  174. /.bzr/
  175. /_MTN/
  176. );
  177. push @exclude_paths, '';
  178. print GLIMPSEEXCLUDE join("\n", @exclude_paths);
  179. close GLIMPSEEXCLUDE;
  180. #XXX what does |set -e| mean?
  181. #system ("set -e >> $log");
  182. #system("time", "glimpseindex", "-H", ".", "$src_dir");
  183. my $cmd = "($TIME glimpseindex -o -n -f -B -M 128 -H . $src_dir $STDERRTOSTDOUT) >> $log";
  184. do_and_log($cmd);
  185. my $mxr_dir_tmp = "$db_dir_tmp/.mxr";
  186. -d $mxr_dir_tmp || mkdir $mxr_dir_tmp;
  187. $cmd = "ls -al >> $log
  188. (cp .glimpse_filenames $mxr_dir_tmp/files $STDERRTOSTDOUT || echo failed to copy .glimpse_filenames) >> $log
  189. ls .mxr -al >> $log
  190. ($TIME glimpseindex -H $mxr_dir_tmp $mxr_dir_tmp $STDERRTOSTDOUT) >> $log
  191. ";
  192. do_and_log($cmd);
  193. if (-f "$mxr_dir_tmp/.glimpse_filenames") {
  194. $cmd = "
  195. perl -pi -e 's{tmp/\.mxr}{\.mxr}' $mxr_dir_tmp/.glimpse_filenames
  196. glimpseindex -H $mxr_dir_tmp -R $STDERRTOSTDOUT >> $log";
  197. do_and_log($cmd);
  198. } else {
  199. do_log("could not find .mxr/.glimpse_filenames");
  200. }
  201. # build filename index
  202. # shared w/ update-root.pl
  203. do_log('chmod -R a+r .');
  204. system("chmod", "-R", "a+r", ".");
  205. $cmd = "mv .glimpse* ../";
  206. if (-d $mxr_dir_tmp) {
  207. $cmd = 'mv .glimpse* .mxr ../';
  208. if (-d '../.mxr') {
  209. $cmd = "
  210. mv ../.mxr ../.mxr-old
  211. $cmd
  212. rm -rf ../.mxr-old
  213. ";
  214. }
  215. }
  216. do_and_log($cmd);
  217. do_log('cd ../..');
  218. chdir '../..';
  219. do_log(
  220. 'date
  221. '.localtime()."
  222. $UPTIME
  223. ");
  224. system ("$UPTIME >> $log") if $UPTIME =~ /\w/;
  225. unlink $pid_lock;
  226. exit 0;