update readme and add gitignore
[client-tools.git] / tools / removeCustomizationDeclarations.pl
blob0202ddc63d7ce5c9acdbd7b2f9ac14b3f10d28ca
1 # Purpose: remove customization declarations from shared object templates.
2 # Handle p4 checkouts of TPFs requiring changes and do a templatecompile on them.
4 # Strategy:
6 # * Make deletions at the line level: either a whole line stays or gets deleted.
7 # * Identify lines for removal.
8 # * Line removal starts when we see "paletteColorCustomizationVariables = [" or "rangedIntCustomizationVariables = [".
9 # * Line remove ends when we hit a line that doesn't match "[variableName=".
11 # * P4 handling.
13 # * Two passes over the file. First one is read-only pass where we scan for "paletteColorCustomizationVariables"
14 # or "rangedIntCustomizationVariables".
15 # * If first pass find a match on any line, start an edit pass. Edit pass does this:
16 # * Does a p4 edit on the tpf and iff. Die if unsuccessful.
17 # * Generates new file contents to a temporary file.
18 # * Closes the temporary file.
19 # * Renames the temporary file to the p4 file.
20 # * Does a templateCompiler -compile on the new file.
22 use strict;
24 use Cwd;
25 use File::Find;
26 use File::Spec;
27 use File::Temp;
29 # =====================================================================
31 (@ARGV > 0) or die "Usage: perl removeCustomizationDeclarations.pl <TPF directory> [<TPF directory> ...]\n";
33 # =====================================================================
35 my $debug = 0;
37 my $checkedFileCount = 0;
38 my $changedFileCount = 0;
40 my $newTotalBytes = 0;
41 my $savedBytes = 0;
43 my $p4EditRetryCount = 10;
45 # =====================================================================
47 sub doesFileNeedModification
49 my $tpfFileName = shift;
51 # Looks like the base must have this.
52 if ($tpfFileName =~ m/shared_tangible_base\.tpf$/)
54 return 0;
57 my $inputFile;
58 open($inputFile, "< " . $tpfFileName) or die "Failed to open [$tpfFileName] for reading: $!";
60 my $foundMatch = 0;
62 while (<$inputFile>)
64 ++$foundMatch if (m/(paletteColorCustomizationVariables|rangedIntCustomizationVariables)\s*=/);
67 close($inputFile) or die "Failed to close file [$tpfFileName]: $!";
69 ++$checkedFileCount;
71 return ($foundMatch > 0);
74 # ----------------------------------------------------------------------
76 sub getIffFileName
78 my $iffFileName = shift;
79 $iffFileName =~ s!/dsrc/!/data/!;
80 $iffFileName =~ s!\.tpf$!.iff!;
82 return $iffFileName;
85 # ----------------------------------------------------------------------
87 sub p4EditFile
89 my $fileName = shift;
90 my $attemptCount = 0;
92 # Do a p4 edit on the file.
95 ++$attemptCount;
96 my $commandResult = `p4 edit $fileName`;
97 print "p4 edit $fileName: $commandResult\n" if $debug;
98 } while (($? != 0) && ($attemptCount < $p4EditRetryCount));
100 die "Failed to run p4 edit on $fileName, tried $attemptCount times: [$?]" if ($? != 0);
103 # ----------------------------------------------------------------------
105 sub removeCustomizationDeclarations
107 # Get args.
108 my $tpfFileName = shift;
109 my $iffFileName = shift;
111 # Find directory name of tpf.
112 my ($tpfVolumeName, $tpfDirName, $unusedFileName) = File::Spec->splitpath($tpfFileName);
113 my $tempFileDir = File::Spec->catpath($tpfVolumeName, $tpfDirName, "");
114 print "tempFileDir=$tempFileDir\n" if $debug;
116 # Create the tempfile.
117 my ($outputFile, $outputFileName) = File::Temp::tempfile(DIR => $tempFileDir);
118 die "failed to create tempfile: $!" if !defined($outputFile);
120 # Open the tpf file for reading.
121 my $inputFile;
122 open($inputFile, "< " . $tpfFileName) or die "failed to open tpf file [$tpfFileName] for reading: $!";
124 my $skipLines = 0;
126 # Process the TPF
127 while (<$inputFile>)
129 chomp();
131 # Determine if we should be skipping lines.
132 if (!$skipLines)
134 # Check if we should start skipping lines.
135 $skipLines = m/(?:paletteColorCustomizationVariables|rangedIntCustomizationVariables)\s*=/;
137 else
139 # Check if should keep skipping lines.
140 $skipLines = m/\[variableName\s*=/;
143 print $outputFile "$_\n" if !$skipLines;
144 print "DEL: $_\n" if $skipLines && $debug;
147 # Close up.
148 close($inputFile) or die "failed to close tpf file [$tpfFileName]: $!";
149 close($outputFile) or die "failed to close temp output file [$outputFileName]: $!";
151 # Rename modified file to tpf file.
152 rename($outputFileName, $tpfFileName) or die "failed to rename modified tpf file [$outputFileName] to [$tpfFileName]: $!";
154 ++$changedFileCount;
157 # ----------------------------------------------------------------------
159 sub compileTpf
161 my $tpfFileName = shift;
162 my $output = `TemplateCompiler -compile $tpfFileName`;
163 die "failed to compile $tpfFileName: $output" if ($? != 0);
164 print "TemplateCompiler -compile $tpfFileName: $output\n" if $debug;
167 # ----------------------------------------------------------------------
169 sub filenameProcessor
171 # Check if target is a file, is readable and is a TPF.
172 if (-f and -r and m/\.tpf$/i)
174 # Process the file.
175 my $tpfFileName = $File::Find::name;
176 print "Processing file [$tpfFileName].\n" if $debug;
178 # Check if the file needs to be modified.
179 if (doesFileNeedModification($tpfFileName))
181 print "Modifying [$tpfFileName].\n";
183 # Get the iff filename for the given tpf filename.
184 my $iffFileName = getIffFileName($tpfFileName);
186 # Get stats for the original iff file.
187 my @originalStats = stat $iffFileName;
189 # Open files for edit.
190 p4EditFile($tpfFileName);
191 p4EditFile($iffFileName);
193 # Remove the customization-related entries.
194 removeCustomizationDeclarations($tpfFileName, $iffFileName);
196 # Compile the tpf into the iff.
197 compileTpf($tpfFileName);
199 # Get stats for the modified iff file.
200 my @modifiedStats = stat $iffFileName;
202 # Update statistics.
203 $newTotalBytes += $modifiedStats[7];
204 $savedBytes += ($originalStats[7] - $modifiedStats[7]);
209 # ----------------------------------------------------------------------
211 my @dirs;
213 foreach my $dir (@ARGV)
215 push @dirs, Cwd::abs_path($dir);
218 File::Find::find(\&filenameProcessor, @dirs);
220 print "Statistics:\n";
221 print "\tfiles checked: $checkedFileCount\n";
222 print "\tfiles modified: $changedFileCount\n";
223 printf "\tmodified size: %.2f KB\n", $newTotalBytes / (1024);
224 printf "\treduced size by: %.2f KB\n", $savedBytes / (1024);
226 # =====================================================================