|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 ##################################### |
|
17 # Run with -h to get help |
|
18 ###################################### |
|
19 use strict; |
|
20 use lib "."; |
|
21 use Win32::OLE qw(in with); |
|
22 use Win32::OLE::Const 'Microsoft Excel'; |
|
23 use Cwd; |
|
24 use Getopt::Long; |
|
25 use File::Basename; |
|
26 use excel_support; # Own perl module that contains the reading of memory-sheets |
|
27 use cenrep_keys; # Own perl module that contains the reading of memory-sheets |
|
28 |
|
29 ########################################################################### |
|
30 # Function that gets a dir as parameter and reads the list of excel files from the |
|
31 # given directory. |
|
32 # |
|
33 # Params: a directory name |
|
34 # |
|
35 # Return value: a reference to a list of filenames. |
|
36 ########################################################################### |
|
37 sub getExcelFilesFromDir |
|
38 { |
|
39 my $dir = shift; |
|
40 |
|
41 # the given dir should be relative to the dir, where the script is executed. |
|
42 if($dir !~ /^[\\\/]/ && $dir !~ /^[A-Za-z]:/) |
|
43 { |
|
44 $dir = cwd . "\\$dir"; |
|
45 } |
|
46 my @files; |
|
47 @files = <$dir\\*.xls >; |
|
48 |
|
49 return \@files; |
|
50 } |
|
51 |
|
52 |
|
53 ################################################### |
|
54 # Function that reads the file names from the given file. |
|
55 # THe information is read from Column A and 1st empty row indicates |
|
56 # end of input. |
|
57 # |
|
58 # Params: fileNameRead, refToExcelApp |
|
59 ################################################### |
|
60 sub getExcelFilesFromFile |
|
61 { |
|
62 my $file = shift; |
|
63 my $refToExcel = shift; |
|
64 |
|
65 my @fileList; |
|
66 # ---------------------------------------------------------------- |
|
67 # Open the file and get the worksheet |
|
68 # ---------------------------------------------------------------- |
|
69 my $refToWb = openWorkbook( $file,$refToExcel); |
|
70 my $refToWorkSheet = getWorkSheet( $refToWb,"Info"); |
|
71 |
|
72 #------------------------------------------------------------ |
|
73 # Read the needed rows from the row, until empty row found |
|
74 #------------------------------------------------------------ |
|
75 my $row; |
|
76 for( $row = 1;; $row++ ) |
|
77 { |
|
78 my $fileName = $$refToWorkSheet->Cells( $row, 1 )->{ 'Value' }; |
|
79 |
|
80 if(!defined($fileName) || $fileName =~ /^\s*$/) |
|
81 { |
|
82 last; |
|
83 } |
|
84 |
|
85 push(@fileList,$fileName); |
|
86 } |
|
87 |
|
88 return \@fileList; |
|
89 } |
|
90 |
|
91 ########################################################################### |
|
92 # Main function, |
|
93 ########################################################################### |
|
94 sub main |
|
95 { |
|
96 my $refToExcelFiles = shift; |
|
97 my $refToExcel = shift; |
|
98 my $ignoreWarnings = shift; |
|
99 |
|
100 my %reshHash; |
|
101 my $refToWorkSheet; |
|
102 |
|
103 my $errorsFound = 0; |
|
104 |
|
105 # Loop through each found excel sheet. This will open one file after each other |
|
106 # and parse information into the hashes defined above. |
|
107 foreach my $file ( @{$refToExcelFiles} ) |
|
108 { |
|
109 $file =~ /[\\\/]([a-zA-Z0-9\-_\.\s\&]+\.xls)/; |
|
110 print "\n--------------------------------------------\nReading: $1\n"; |
|
111 |
|
112 my ($name,$path,$suffix) = fileparse($file,qr{\.xls}); |
|
113 checkCentrepKeySheetName("$name$suffix"); |
|
114 # ---------------------------------------------------------------- |
|
115 # Open the file |
|
116 # ---------------------------------------------------------------- |
|
117 my $refToWb = openWorkbook( $file,$refToExcel); |
|
118 |
|
119 # ---------------------------------------------------------------- |
|
120 # Read the information from the worksheets |
|
121 # ---------------------------------------------------------------- |
|
122 for( my $i = 0; $i < $$refToWb->Worksheets->Count; $i++) |
|
123 { |
|
124 my $workSheet = $$refToWb->WorkSheets($i + 1); |
|
125 $refToWorkSheet = getWorkSheet( $refToWb,$workSheet->Name); |
|
126 if(checkCentrepKeyInfo( $refToWorkSheet,$ignoreWarnings)) |
|
127 { |
|
128 $errorsFound = 1; |
|
129 } |
|
130 } |
|
131 # "delete" the workbooks, this closes the excel application |
|
132 $$refToWb->Close(0); |
|
133 undef $$refToWb; |
|
134 undef $refToWb; |
|
135 } |
|
136 |
|
137 return $errorsFound; |
|
138 } |
|
139 |
|
140 ########################################################################### |
|
141 # Shows help. |
|
142 # |
|
143 # Params: - |
|
144 # |
|
145 # Return: - |
|
146 # |
|
147 ########################################################################### |
|
148 sub usage |
|
149 { |
|
150 print "\n"; |
|
151 print "\n"; |
|
152 print "This script reads all the cenrep keys sheets from the given\n"; |
|
153 print "directory and makes a syntax check for them.\n"; |
|
154 print "\n"; |
|
155 print "Usage:\n"; |
|
156 print " $0 [-h|-help] (-d <cenrepSheetDir> | -f <excelSheetName>) \n"; |
|
157 print "\n"; |
|
158 print "Options:\n"; |
|
159 print " -h : Show this help\n"; |
|
160 print " -help : Show this help\n"; |
|
161 print " -d <sheetDir> : relative path from the dir where this script is run\n"; |
|
162 print " to the directory where the cenrep-sheets exist.\n"; |
|
163 print " -e <excelSheetName> : name of the sheet, that specifies the read excel sheet names in the column A.\n"; |
|
164 print " No empty rows allowed, reading is stopped, when the 1st empty row is found.\n"; |
|
165 print " The sheet name defining the files inside Workbook has to be \"Info\".\n"; |
|
166 print " Also no header expected for the column (reading starts from row 1)\n"; |
|
167 print " -w : Ignores warning messages\n"; |
|
168 print " -h : Show this help\n"; |
|
169 print "\n"; |
|
170 print "You have specify either -d or -e, but not both at the same time.\n"; |
|
171 print "\n"; |
|
172 print "Example usage:\n"; |
|
173 print " perl $0 -d cenrep\n"; |
|
174 print "\n"; |
|
175 print "Known issues:\n"; |
|
176 print " - The script will give an error, if directory names in the path contain spaces\n"; |
|
177 print "\n"; |
|
178 print "\n"; |
|
179 |
|
180 exit(0); |
|
181 } |
|
182 ########################################################################### |
|
183 # Parses the command line parameters from ARGV |
|
184 # |
|
185 # Params: - |
|
186 # |
|
187 # Return: - |
|
188 # |
|
189 ########################################################################### |
|
190 sub parseCmdLine |
|
191 { |
|
192 my $dir = ""; |
|
193 my $file = ""; |
|
194 my $sheetFile = ""; |
|
195 my $ignoreWarnings = 0; |
|
196 |
|
197 if( ! GetOptions('d:s' => \$dir, |
|
198 's:s' => \$sheetFile, |
|
199 'f:s' => \$file, |
|
200 'h' => \&usage, |
|
201 'w' => \$ignoreWarnings, |
|
202 'help' => \&usage, |
|
203 '<>' => \&usage)) |
|
204 { |
|
205 exit(1); |
|
206 } |
|
207 |
|
208 if($dir ne "" and $file ne "" ) |
|
209 { |
|
210 print "\nError: The file and directory arguments can't be specified at the same time.\n"; |
|
211 exit(1); |
|
212 } |
|
213 elsif($dir eq "" and $file eq "" ) |
|
214 { |
|
215 usage; |
|
216 print "\n\nError: You have to specify either the -d or -f argument.\n"; |
|
217 exit(1); |
|
218 } |
|
219 |
|
220 |
|
221 return ($dir,$file,$sheetFile,$ignoreWarnings); |
|
222 } |
|
223 |
|
224 |
|
225 ################################################### |
|
226 # Actual program |
|
227 ################################################### |
|
228 my $sheetDir; |
|
229 my $file; |
|
230 my $sheetFile; |
|
231 my $ignoreWarnings; |
|
232 |
|
233 ($sheetDir,$file,$sheetFile,$ignoreWarnings) = parseCmdLine; |
|
234 |
|
235 # ---------------------------------------------------------------- |
|
236 # Get excel application |
|
237 # ---------------------------------------------------------------- |
|
238 my $refToExcel = getExcelApp(); |
|
239 |
|
240 #----------------------------------------------------------------- |
|
241 # There are 2 options how the user can give the if-sheet names handled: |
|
242 # 1) He gives the directory where all files are located |
|
243 # 2) He gives an excel sheet, which contains all the files in column A. |
|
244 #----------------------------------------------------------------- |
|
245 my $refToExcelFilesList; |
|
246 |
|
247 if($sheetFile !~ /^\s*$/) |
|
248 { |
|
249 my @list; |
|
250 push(@list,$sheetFile); |
|
251 $refToExcelFilesList = \@list; |
|
252 } |
|
253 if($sheetDir !~ /^\s*$/) |
|
254 { |
|
255 # Read the filenames from the directory given |
|
256 $refToExcelFilesList = getExcelFilesFromDir $sheetDir; |
|
257 } |
|
258 else |
|
259 { |
|
260 # Read the filenames from the excel file given |
|
261 $refToExcelFilesList = getExcelFilesFromFile $file, $refToExcel; |
|
262 } |
|
263 |
|
264 #----------------------------------------------------------------- |
|
265 # The main function that parses the information |
|
266 #----------------------------------------------------------------- |
|
267 |
|
268 my $errors = main $refToExcelFilesList,$refToExcel,$ignoreWarnings; |
|
269 |
|
270 #----------------------------------------------------------------- |
|
271 # quit excel and "delete" the instances. |
|
272 #----------------------------------------------------------------- |
|
273 quitExcelApp($refToExcel); |
|
274 |
|
275 exit $errors; |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |