Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
11 KiB

  1. #---------------------------------------------------------------------
  2. package LocalEnvEx;
  3. #
  4. # (c) 2000 Microsoft Corporation. All rights reserved.
  5. #
  6. # Version: 0.01 (4/7/2000) : Inital concept
  7. #---------------------------------------------------------------------
  8. $class='LocalEnvEx';
  9. $VERSION = '0.01';
  10. require 5.003;
  11. # Use section
  12. use lib $ENV{RAZZLETOOLPATH};
  13. use GetParams;
  14. use Logmsg;
  15. use cklang;
  16. use strict;
  17. no strict 'vars';
  18. # Require section
  19. require Exporter;
  20. # IsA (Inheritance) define section
  21. @ISA = qw(Exporter);
  22. #
  23. #Public Functions:
  24. #
  25. # Constructor function
  26. sub new {
  27. my ($class)=shift;
  28. my $self = {@_};
  29. $class = ref($class) || $class;
  30. # The keys of 'self' are
  31. #
  32. # -i <Initialize environment> : See usage
  33. # -e <End environment> : See usage
  34. # Initial Binding section
  35. $self->{-VariableSet}=sub {&PerlVarSet($self,@_)} if (!defined $self->{-VariableSet});
  36. $self->{-Initialize}=sub {Initialize($self,@_)} if (!defined $self->{-Initialize});
  37. $self->{-End}=sub {End($self,@_)} if (!defined $self->{-End});
  38. $self->{-Error}=sub {Error($self, @_)} if (!defined $self->{-Error});
  39. @EXPORT = qw();
  40. #Import razzle variables
  41. $self->{_NTPostBld} = $ENV{_NTPostBld};
  42. $self->{temp} = $ENV{temp};
  43. $self->{ntdebug} = $ENV{ntdebug};
  44. $self->{_BuildArch} = $ENV{_BuildArch};
  45. #Import script specific variables
  46. $self->{Script_Name} = $ENV{script_name};
  47. $self->{lang} = $ENV{lang};
  48. #Import local environment extensions
  49. $self->{logfile} = $ENV{logfile};
  50. $self->{logfile_bak} = $ENV{logfile_bak};
  51. $self->{errfile} = $ENV{errfile};
  52. $self->{errfile_bak} = $ENV{errfile_bak};
  53. $self->{tmpfile} = $ENV{tmpfile};
  54. $self->{temp_bak} = $ENV{temp_bak};
  55. $self->{_NTPostBld_Bak} = $ENV{_NTPostBld_Bak};
  56. $self->{errors} = $ENV{errors};
  57. return bless ($self, $class);
  58. }
  59. #Initalization object function
  60. sub Initialize {
  61. my $self=shift;
  62. $self->{-VariableSet} = sub {PerlVarSet($self, @_)} if (!defined $self->{-VariableSet});
  63. #Step 0: Set defaults for global variables
  64. if (!defined $self->{errors}) {
  65. $self->{errors} = 0;
  66. push @varlist, "errors";
  67. }
  68. #Step 1: Verify the language and build environment
  69. if (!defined $self->{lang}) {
  70. $self->{lang} = "usa";
  71. push @varlist, "lang";
  72. }
  73. if ( !&cklang::CkLang($self->{lang})) {
  74. print "echo Language $self->{lang} is invalid.$!";
  75. die "\n";
  76. }
  77. #Step 2: Redefine temp/tmp with a language subdir to abstract language
  78. $path = "$self->{temp}";
  79. $path_bak = "$self->{temp_bak}";
  80. if (!defined $self->{temp_bak}) {
  81. $self->{temp_bak} = $self->{temp};
  82. $path_bak = "$self->{temp_bak}";
  83. push @varlist, "temp_bak";
  84. }
  85. $self->{temp} = "$path_bak"."\\"."$self->{lang}";
  86. $self->{tmp} = "$path_bak"."\\"."$self->{lang}";
  87. push @varlist, "temp";
  88. push @varlist, "tmp";
  89. # Step 3: Define the logfile to be used by the logging scripts
  90. # Use the calling scripts logfile if it is defined
  91. if (!defined $self->{logfile}) {
  92. $path = "$self->{temp}";
  93. mkdir $path, -d;
  94. my $script_name = $self->{Script_Name};
  95. if ($self->{ntdebug} =~ /^ntsd$/i) {
  96. $ntdebug = "chk";
  97. } else {
  98. $ntdebug = "fre";
  99. }
  100. $self->{logfile} = "$path\\$script_name.$self->{_BuildArch}.$ntdebug.$self->{lang}.log";
  101. push @varlist, "logfile";
  102. # Delete the old logfile if it exists and create a new one
  103. if (-e $self->{logfile}) {
  104. unlink $self->{logfile};
  105. open(LOGFILE, "> $self->{logfile}") or die "Can not open $self->{logfile}: $!";
  106. close (LOGFILE);
  107. }
  108. } else {
  109. # Delete create a new logfile if it does not exist
  110. unless (-e $self->{logfile}) {
  111. open(LOGFILE, "> $self->{logfile}") or die "Can not open $self->{logfile}: $!";
  112. close (LOGFILE);
  113. }
  114. }
  115. #Step 4: Define the errfile to be used by the logging scripts
  116. if (!defined $self->{errfile}) {
  117. $path = "$self->{temp}";
  118. mkdir $path, -d;
  119. system "md $path 2>Nul";
  120. my $script_name = $self->{Script_Name};
  121. if ($self->{ntdebug} =~ /^ntsd$/i) {
  122. $ntdebug = "chk";
  123. } else {
  124. $ntdebug = "fre";
  125. }
  126. $self->{errfile} = "$path\\$script_name.$self->{_BuildArch}.$ntdebug.$self->{lang}.err";
  127. push @varlist, "errfile";
  128. # Delete the old errfile if it exists and create a new one
  129. if (-e $self->{errfile}) {
  130. unlink $self->{errfile};
  131. }
  132. }
  133. #Step 5: Define the tmpfile to be used by the logging scripts
  134. # Always create a local tmpfile
  135. $path = "$self->{temp}";
  136. mkdir $path, -d;
  137. system "md $path 2>Nul";
  138. my $script_name = $self->{Script_Name};
  139. if ($self->{ntdebug} =~ /^ntsd$/i) {
  140. $ntdebug = "chk";
  141. } else {
  142. $ntdebug = "fre";
  143. }
  144. $self->{tmpfile} = "$path\\$script_name.$self->{_BuildArch}.$ntdebug.$self->{lang}.tmp";
  145. push @varlist, "tmpfile";
  146. # Delete the old tmpfile if it exists and create a new one
  147. if (-e $self->{tmpfile}) {
  148. unlink $self->{tmpfile};
  149. open(TMPFILE, "> $self->{tmpfile}") or die "Can not open $self->{tmpfile}: $!";
  150. close (TMPFILE);
  151. }
  152. #Step x: Define the errtmpfile to be used by the logging scripts
  153. # Always create a local errtmpfile
  154. $path = "$self->{temp}";
  155. mkdir $path, -d;
  156. system "md $path 2>Nul";
  157. my $script_name = $self->{Script_Name};
  158. if ($self->{ntdebug} =~ /^ntsd$/i) {
  159. $ntdebug = "chk";
  160. } else {
  161. $ntdebug = "fre";
  162. }
  163. $self->{errtmpfile} = "$path\\$script_name.$self->{_BuildArch}.$ntdebug.$self->{lang}.err.tmp";
  164. # Delete the old errtmpfile if it exists and create a new one
  165. if (-e $self->{errtmpfile}) {
  166. unlink $self->{errtmpfile};
  167. }
  168. #Step 6: Define _NTPostBld which points to the _ntpostbld tree
  169. #to be worked on by the calling script.
  170. $path = "$self->{_NTPostBld}";
  171. $path_bak = "$self->{_NTPostBld_Bak}";
  172. if (!defined $self->{_NTPostBld_Bak}) {
  173. $self->{_NTPostBld_Bak} = $self->{_NTPostBld};
  174. $path_bak = "$self->{_NTPostBld_Bak}";
  175. push @varlist, "_NTPostBld_Bak";
  176. }
  177. if ($self->{lang} =~ /^usa$/i) {
  178. $self->{_NTPostBld} = "$path_bak";
  179. } else {
  180. $self->{_NTPostBld} = "$path_bak"."\\"."$self->{lang}";
  181. }
  182. push @varlist, "_NTPostBld";
  183. # #Step 7: Set build_data to point to postbuild's critical data files
  184. # $path = "$self->{_NTPostBld}\\build_data";
  185. # mkdir $path, -d;
  186. # system "md $path 2>Nul";
  187. # if ($self->{lang} =~ /^usa$/i) {
  188. # $self->{build_data} = "$path";
  189. # } else {
  190. # $self->{build_data} = "$path";
  191. # }
  192. # push @varlist, "build_data";
  193. #Step 8: Use tmpfile as log file during the scripts execution
  194. $self->{logfile_bak} = $self->{logfile};
  195. $self->{logfile} = $self->{tmpfile};
  196. push @varlist, "logfile";
  197. push @varlist, "logfile_bak";
  198. #Step x: Use tmperrfile as errfile during the scripts execution
  199. $self->{errfile_bak} = $self->{errfile};
  200. $self->{errfile} = $self->{errtmpfile};
  201. push @varlist, "errfile";
  202. push @varlist, "errfile_bak";
  203. #Step 9: Set the environment variables
  204. foreach $envvar (@varlist) {
  205. $name = $envvar;
  206. $value = $self->{$envvar};
  207. &{$self->{-VariableSet}}($name, $value);
  208. }
  209. #Step 10: Mark the beginning of the scripts execution
  210. $ENV{logfile}=$self->{logfile};
  211. open(LOGFILE, "> $self->{logfile}") or die "Can not open $self->{logfile}: $!";
  212. close (LOGFILE);
  213. timemsg ("****** START scripts execution. *****");
  214. }
  215. #End object function
  216. sub End {
  217. my $self=shift;
  218. #Step 1: Mark the end of the scripts execution
  219. timemsg ("END scripts execution with:($self->{errors} errors).");
  220. #Step 2: Reset the logfile
  221. $self->{logfile} = "$self->{logfile_bak}";
  222. $self->{logfile_bak} = "";
  223. push @varlist, "logfile";
  224. logmsg ("Script logged in:\n$self->{logfile}");
  225. #Step x: Append and reset the errfile
  226. #Step a: Append the errfile_bak to the errfile
  227. open(ERRFILE_BAK, ">>$self->{errfile_bak}");
  228. open(ERRFILE, "<$self->{errfile}");
  229. foreach $tmpline (<ERRFILE>) {
  230. print (ERRFILE_BAK "$tmpline");
  231. }
  232. close(ERRFILE);
  233. close(ERRFILE_BAK);
  234. #Delete the temp errfile
  235. unlink $self->{errfile};
  236. #Step b: Reset the errfile
  237. $self->{errfile} = "$self->{errfile_bak}";
  238. $self->{errfile_bak} = "";
  239. push @varlist, "errfile";
  240. #Step c: Remove errorfile if it is zero length
  241. if (-e $self->{errfile}) {
  242. if (-z $self->{errfile}) {
  243. unlink $self->{errfile};
  244. }
  245. }
  246. #Step 3: Append the tmpfile to the logfile
  247. open(LOGFILE, ">>$self->{logfile}");
  248. open(TMPFILE, "<$self->{tmpfile}");
  249. foreach $tmpline (<TMPFILE>) {
  250. print (LOGFILE "$tmpline");
  251. }
  252. close(LOGFILE);
  253. close(TMPFILE);
  254. # Step 4: Check for errors
  255. if (-e $self->{errfile}) {
  256. print "*** $self->{Script_Name} encountered errors, please see errfile: ***\n";
  257. print "$self->{errfile}\n";
  258. }
  259. #Step 5: Clean up environment
  260. $self->{logfile_bak} = "";
  261. push @varlist, "logfile_bak";
  262. $self->{errfile_bak} = "";
  263. push @varlist, "errfile_bak";
  264. if (-e $self->{tmpfile}) {
  265. unlink $self->{tmpfile};
  266. }
  267. $self->{tmpfile} = "";
  268. push @varlist, "tmpfile";
  269. $path = "$self->{_NTPostBld}";
  270. if ($self->{lang} =~ /^usa$/i) {
  271. $self->{_NTPostBld} = "$path";
  272. } else {
  273. $path =~ s/^\\$self->{lang}$//i;
  274. $self->{_NTPostBld} = "$path";
  275. }
  276. push @varlist, "_NTPostBld";
  277. #Step 6: Set the environment variables
  278. foreach $envvar (@varlist) {
  279. $name = $envvar;
  280. $value = $self->{$envvar};
  281. &{$self->{-VariableSet}}($name, $value);
  282. }
  283. # Step 7: Exit with the correct error code
  284. if ($self->{errors} > 0) {
  285. exit 1;
  286. } else {
  287. exit 0;
  288. }
  289. }
  290. #
  291. #Private functions
  292. #
  293. sub localenvex {
  294. my ($Option)=(@_);
  295. if ($Option=~/^initialize$/i) {
  296. my $LocalEnvEx=LocalEnvEx->new;
  297. &{$LocalEnvEx->{-Initialize}};
  298. }
  299. if ($Option=~/^end$/i) {
  300. my $LocalEnvEx=LocalEnvEx->new;
  301. &{$LocalEnvEx->{-End}};
  302. }
  303. }
  304. sub PerlVarSet {
  305. my ($self, $name, $value)=@_;
  306. $ENV{$name}=$value;
  307. }
  308. sub CmdVarSet {
  309. my($self, $name, $value)=@_;
  310. print "set $name=$value\n";
  311. }
  312. sub Error {
  313. my $self=shift;
  314. }
  315. sub Usage {
  316. print <<USAGE;
  317. $0 - V Program purpose
  318. ==================================================
  319. Syntax: $0 parameters
  320. ==================================================
  321. Parameters:
  322. ==================================================
  323. Example:
  324. 1. #<what you want to do>
  325. => #<real command>
  326. USAGE
  327. exit(1);
  328. }
  329. #
  330. # CMD handler
  331. #
  332. if ($0=~/${class}.pm$/i) {
  333. #Step 1: Parse the command line
  334. ($initflag, $endflag)=();
  335. @syntax=(
  336. -o => 'ie',
  337. -p => 'initflag endflag'
  338. );
  339. my $getparams=GetParams->new;
  340. # Parse the value
  341. &{$getparams->{-Process}}(
  342. @syntax,
  343. @ARGV
  344. );
  345. #Step 2: Set local environment extentions for the specified stage.
  346. # Create the object and call the correct method.
  347. if (defined $initflag) {
  348. my $LocalEnvEx=LocalEnvEx->new(-VariableSet => sub {&CmdVarSet($self,@_)});
  349. &{$LocalEnvEx->{-Initialize}}(@ARGV);
  350. }
  351. if (defined $endflag) {
  352. my $LocalEnvEx=LocalEnvEx->new(-VariableSet => sub {&CmdVarSet($self,@_)});
  353. &{$LocalEnvEx->{-End}}(@ARGV);
  354. }
  355. }
  356. =head1 NAME
  357. B<mypackage> - What this package for
  358. =head1 SYNOPSIS
  359. =head1 DESCRIPTION
  360. =head1 INSTANCES
  361. =head2
  362. =head1 METHODS
  363. =head2
  364. =head1 SEE ALSO
  365. =head1 AUTHOR
  366. =cut
  367. 1;