├── .gitignore
├── test
├── file3.conf
├── file1.conf
└── file2.conf
├── NOTICE
├── Dockerfile
├── CONTRIBUTING
├── README.md
├── CODE_OF_CONDUCT.md
├── LICENSE
└── compare-ini-files
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | #
3 | # Vim
4 | #
5 | *~
6 |
7 |
--------------------------------------------------------------------------------
/test/file3.conf:
--------------------------------------------------------------------------------
1 |
2 | [common in all files]
3 | key1 = value1
4 | key2=value2
5 | specific to file3= somevalue
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 |
2 | Compare .ini Files
3 | Copyright 2016 Comcast Cable Communications Management, LLC
4 |
5 | This product includes software developed at Comcast (http://www.comcast.com/).
6 |
7 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 |
2 | FROM alpine
3 |
4 | RUN apk add --no-cache php7
5 |
6 | COPY compare-ini-files /mnt
7 |
8 | WORKDIR /mnt
9 |
10 | ENTRYPOINT ["./compare-ini-files"]
11 |
12 |
13 |
--------------------------------------------------------------------------------
/test/file1.conf:
--------------------------------------------------------------------------------
1 |
2 | [common in all files]
3 | key1 = value1
4 | key2=value2
5 |
6 | #
7 | # Yes, we can have spaces in stanza names. And even key names
8 | #
9 | [this stanza is found in file 1 and 2]
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/test/file2.conf:
--------------------------------------------------------------------------------
1 |
2 | [common in all files]
3 | key1 = value1
4 | key2=value2
5 |
6 | #
7 | # Yes, we can have spaces in stanza names. And even key names
8 | #
9 | [this stanza is found in file 1 and 2]
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/CONTRIBUTING:
--------------------------------------------------------------------------------
1 |
2 | If you would like to contribute code to this project you can do so through GitHub by forking the repository and sending a pull request.
3 |
4 | Before Comcast accepts your code into the project you must sign the Comcast Contributor License Agreement (CLA).
5 |
6 | If you haven’t previously signed a Comcast CLA, we can e-mail you a PDF that you can sign and scan back to us. Please send us an e-mail or create a new GiHhub issue to request a PDF version of the CLA.
7 |
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Compare Ini Files
2 |
3 | The problem: You have multiple copies of an "ini" file (https://en.wikipedia.org/wiki/INI_file)
4 | in use by an app (possibly an app deployed in multiple datacenters...), and you want to compare them/diff them. Or worse yet: the stanzas and key-value pairs are in different orders in each file, and sorting through them would be a manual process that takes hours and is prone to error.
5 |
6 | Solution: This script. The syntax is simple:
7 | ```
8 | compare-ini-files filename1 filename2 [filename3 [filename4 [... ] ] ]
9 | ```
10 |
11 | After loading the contents of the specified files into memory, this script
12 | then checks to make sure all stanzas exist in each file, then does a key-by-key
13 | comparison on each stanza, and reports any differences between the files.
14 |
15 |
16 | ## Installation
17 |
18 | ```
19 | brew tap comcast/opensource https://github.com/Comcast/homebrew-opensource.git
20 | brew install compare-ini-files
21 | ```
22 |
23 |
24 | ## Testing out the script
25 |
26 | Clone this repo, and run `./compare-ini-files test/*` on the command line.
27 | The output should resemble this:
28 |
29 | ```
30 | #
31 | # Starting config file audit of the following files:
32 | #
33 | # test/file1.conf
34 | # test/file2.conf
35 | # test/file3.conf
36 | #
37 | #
38 | # Loading files...
39 | #
40 | #
41 | # Comparing contents...
42 | #
43 | File: 'test/file3.conf' is missing the following stanzas:
44 | [this stanza is found in file 1 and 2]
45 |
46 | Stanza: [common in all files]
47 |
48 | Key: 'specific to file3' found with multiple values:
49 |
50 | Value 'somevalue' found in files:
51 |
52 | test/file3.conf
53 |
54 | These files did NOT contain the key:
55 |
56 | test/file1.conf
57 | test/file2.conf
58 |
59 |
60 | ```
61 |
62 |
63 | ## Requirements
64 |
65 | - A relatively recent version of PHP should be installed.
66 | - Read access to the files in question
67 |
68 |
69 | ## Development
70 |
71 | Development can be done in Docker:
72 |
73 | `docker build -t compare-ini-files . && docker run -it -v $(pwd):/mnt compare-ini-files test/*`
74 |
75 | That command builds an image locally and runs the `compare-ini-files` script inside of a
76 | Docker container.
77 |
78 |
79 | ## Author
80 |
81 |
82 | Douglas Muth
83 |
84 | Bugs can be filed here or emailed directly to me.
85 |
86 |
87 |
88 |
89 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at Comcast_Open_Source_Services@comcast.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/compare-ini-files:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env php
2 | $value) {
57 |
58 | if (!is_readable($value)) {
59 | $error = "File '$value' does not exist or is not readable!";
60 | throw new Exception($error);
61 | }
62 |
63 | $retval["files"][] = $value;
64 |
65 | }
66 |
67 | return($retval);
68 |
69 | } // End of parseArgs()
70 |
71 |
72 | /**
73 | * Print up our report header, telling what files we're auditing.
74 | *
75 | * @param array $files Array of filenames we're going to audit.
76 | */
77 | function printHeader($files) {
78 |
79 | print "#\n";
80 | print "# Starting config file audit of the following files:\n";
81 | print "#\n";
82 |
83 | foreach ($files as $key => $value) {
84 | printf("#\t%s\n", $value);
85 | }
86 |
87 | print "#\n";
88 |
89 | } // End of printHeader()
90 |
91 |
92 | /**
93 | * Parse our conf file. Input is expected to be in "ini file" format.
94 | *
95 | * @param string $file The filename
96 | *
97 | * @returna array An array of the stanza names and key/value pairs under them.
98 | */
99 | function parseConf($file) {
100 |
101 | //
102 | // Read the contents of the file
103 | //
104 | $lines = array();
105 | $fp = fopen($file, "r");
106 | while ($line = fgets($fp)) {
107 | $lines[] = rtrim($line);
108 | }
109 | fclose($fp);
110 |
111 | //
112 | // Parse the contents
113 | //
114 | $retval = parseConfLines($lines);
115 |
116 | return($retval);
117 |
118 | } // End of parseConf()
119 |
120 |
121 | /**
122 | * Is this line a stanza?
123 | *
124 | * @return boolean True if the line is, false otherwise.
125 | */
126 | function parseConfLinesIsStanza($line) {
127 |
128 | $len = strlen($line);
129 |
130 | if ($len == 0) {
131 | return(false);
132 | }
133 |
134 | $index = $len - 1;
135 | $first = $line[0];
136 | $last = $line[$index];
137 |
138 | if ($first == "[" && $last == "]") {
139 | return(true);
140 | }
141 |
142 | return(false);
143 |
144 | } // End of parseConfLinesIsStanza()
145 |
146 |
147 | /**
148 | * Extract the name of a stanza from a line.
149 | *
150 | * @param string $line The line containing the stanza.
151 | *
152 | * @return string The name of the stanza, or an empty string if no stanza was found.
153 | */
154 | function parseConfLinesGetStanzaName($line) {
155 |
156 | $retval = "";
157 |
158 | if (!parseConfLinesIsStanza($line)) {
159 | return($retval);
160 | }
161 |
162 | $retval = substr($line, 1, -1);
163 |
164 | return($retval);
165 |
166 | } // End of parseConfLinesGetStanzaName()
167 |
168 |
169 | /**
170 | * Extract the key and value from the line, so long as there is an
171 | * equals sign present.
172 | *
173 | * @return array An array where the first value is the key and the
174 | * second value is the value.
175 | */
176 | function parseConfLinesGetKeyValue($line) {
177 |
178 | $retval = array("", "");
179 |
180 | //
181 | // If we don't fine an equals sign, this line is invalid, ignore it!
182 | //
183 | $index = strpos($line, "=");
184 |
185 | if (!$index) {
186 | return($retval);
187 | }
188 |
189 | $key = substr($line, 0, $index);
190 | $value = substr($line, $index + 1);
191 |
192 | $key = rtrim($key);
193 | $value = ltrim($value);
194 | //print "DEBUG: $line, '$key', '$value'\n";
195 |
196 | $retval = array($key, $value);
197 |
198 | return($retval);
199 |
200 | } // End of parseConfLinesGetKeyValue()
201 |
202 |
203 | /**
204 | * Parse the lines in our conf file.
205 | *
206 | * @param array $lines An array of lines we read from the file.
207 | *
208 | * @return array An arary of the full parsed file.
209 | */
210 | function parseConfLines($lines) {
211 |
212 | $retval = array();
213 |
214 | $stanza = "";
215 | foreach ($lines as $key => $value) {
216 | $line = $value;
217 |
218 | if (parseConfLinesIsStanza($line)) {
219 | $stanza = parseConfLinesGetStanzaName($line);
220 | $retval[$stanza] = array();
221 | continue;
222 | }
223 |
224 | if (!$stanza) {
225 | //print "DEBUG: Emtpy stanza, skipping line: $line\n";
226 | continue;
227 | }
228 |
229 | list($line_key, $line_value) = parseConfLinesGetKeyValue($line);
230 |
231 | if ($line_key) {
232 | //print "DEBUG: $stanza: $line_key, $line_value\n";
233 | $retval[$stanza][$line_key] = $line_value;
234 | }
235 |
236 | }
237 |
238 | return($retval);
239 |
240 | } // End of parseConfLines()
241 |
242 |
243 | /**
244 | * Load our specified files.
245 | *
246 | * @param array $files An array of file names to load. These files should be readable.
247 | *
248 | * @return array An array where the key is the base filename and the
249 | * value is the array of parsed data.
250 | *
251 | */
252 | function loadFiles($files) {
253 |
254 | $retval = array();
255 |
256 | foreach ($files as $key => $value) {
257 |
258 | $file = $value;
259 | //
260 | // The reason we are using our own ini file parser instead
261 | // of parse_ini_file() here is because parse_ini_file()
262 | // actually choked(!!) on transforms.conf.
263 | //
264 | $retval["$file"] = parseConf($file);
265 | //print_r($retval["$file"]); // Debugging
266 | ksort($retval[$file]);
267 |
268 | }
269 |
270 | return($retval);
271 |
272 | } // End of loadFiles()
273 |
274 |
275 | /**
276 | * This funciton gets a unique list of stanzas across all of the parsed files.
277 | *
278 | * @return array An array of unique stanzas where the key is the stanza.
279 | */
280 | function getStanzas($data) {
281 |
282 | $retval = array();
283 |
284 | foreach ($data as $key => $value) {
285 | foreach ($value as $key2 => $value2) {
286 | $retval[$key2] = true;
287 | }
288 | }
289 |
290 | ksort($retval);
291 |
292 | return($retval);
293 |
294 | } // End of getStanzas()
295 |
296 |
297 | /**
298 | * Get all keys from a stanza across multiple files.
299 | *
300 | * @param array $data An array where the key is the filename
301 | * and the value an array of stanza data
302 | *
303 | * @return array An array of keys where the key is the name.
304 | */
305 | function getStanzaKeys($data) {
306 |
307 | $retval = array();
308 |
309 | foreach ($data as $key => $value) {
310 | foreach ($value as $key2 => $value2) {
311 | $retval[$key2] = true;
312 | }
313 | }
314 |
315 | return($retval);
316 |
317 | } // End of getStanzaKeys()
318 |
319 |
320 | /**
321 | * Custom callback for sorting values.
322 | */
323 | function stanzaKeySortCallback($a, $b) {
324 |
325 | //print "A/B: $a, $b\n"; // Debugging
326 |
327 | //
328 | // If one of the values is "notfound", it gets
329 | // pushed to the end of array.
330 | //
331 | if ($a == "notfound") {
332 | return(1);
333 | } else if ($b == "notfound") {
334 | return(-1);
335 | }
336 |
337 | if ($a < $b) {
338 | return(-1);
339 | } else if ($a > $b) {
340 | return(1);
341 | } else {
342 | return(0);
343 | }
344 |
345 | } // End of stanzaKeySortCallback()
346 |
347 |
348 | /**
349 | * Compare a key from a particular stanza across all files.
350 | *
351 | * @param array $data An array where the key is the file and
352 | * the value is the stanza contents.
353 | *
354 | * @param array $keys An array where the key is the name of a stanza key.
355 | *
356 | * @param string $key_name The name of the stanza key we are comparing.
357 | *
358 | * @return array An array where the keys are the values
359 | * (or "notfound" if a key wasn't found in a specific file)
360 | * and the values are the filenames where that value is found.
361 | *
362 | */
363 | function compareStanazaKey($data, $keys, $key_name) {
364 |
365 | $retval = array();
366 |
367 | //
368 | // Loop through the files, and store the value for each key in an array
369 | // where the value (if present) is the key. That way we can then go through
370 | // the array and see all files match or note.
371 | //
372 | foreach ($data as $key2 => $value2) {
373 |
374 | $filename = $key2;
375 |
376 | if (!isset($value2[$key_name])) {
377 | if (!isset($retval["notfound"])) {
378 | $retval["notfound"] = array();
379 | }
380 |
381 | //print "DEBUG: Key '$key' not found in file '$key2'!\n";
382 | $retval["notfound"][] = $filename;
383 |
384 | } else {
385 | $stanza_value = $value2[$key_name];
386 | if (!isset($retval[$stanza_value])) {
387 | $retval[$stanza_value] = array();
388 | }
389 |
390 | $retval[$stanza_value][] = $filename;
391 |
392 | }
393 |
394 | }
395 |
396 | uksort($retval, "stanzaKeySortCallback");
397 |
398 | return($retval);
399 |
400 | } // End of compareStanzakey()
401 |
402 |
403 | /**
404 | * Comapre a stanza across different files.
405 | *
406 | * @param string $stanza The name of the current stanza
407 | *
408 | * @param array $data An array where the key is the file and
409 | * the value is the stanza contents.
410 | *
411 | * @return array An array of keys, with each key containing an array of distinct
412 | * values for that key and the files those values are found in.
413 | */
414 | function compareStanza($stanza, $data) {
415 |
416 | $retval = array();
417 |
418 | $keys = getStanzakeys($data);
419 |
420 | //
421 | // Loop through our keys in the stanza.
422 | //
423 | foreach ($keys as $key => $value) {
424 |
425 | $results = compareStanazaKey($data, $keys, $key);
426 | $retval[$key] = $results;
427 |
428 | }
429 |
430 | return($retval);
431 |
432 | } // End of comparseStanza()
433 |
434 |
435 | /**
436 | * Compare our parsed data and find differences.
437 | *
438 | * @param array $data An array of data where the key is the
439 | * filename and the value is the parsed data.
440 | *
441 | * @return array An array of differences.
442 | */
443 | function compareFiles($data) {
444 |
445 | $retval = array();
446 | $retval["missing"] = array();
447 | $retval["diff"] = array();
448 |
449 | $stanzas = getStanzas($data);
450 |
451 | //
452 | // Loop through our files and look for missing stanzas.
453 | //
454 | foreach ($data as $key => $value) {
455 | $filename = $key;
456 |
457 | foreach ($stanzas as $key2 => $value2) {
458 |
459 | $stanza = $key2;
460 |
461 | if (!isset($value[$stanza])) {
462 | if (!isset($retval["missing"][$filename])) {
463 | $retval["missing"][$filename] = array();
464 | }
465 | $retval["missing"][$filename][] = $stanza;
466 |
467 | }
468 |
469 | }
470 |
471 | }
472 |
473 | //
474 | // Now loop through our stanzas and compare them across all files
475 | //
476 | foreach ($stanzas as $key => $value) {
477 | $stanza = $key;
478 |
479 | $results = array();
480 |
481 | foreach ($data as $key2 => $value2) {
482 | $filename = $key2;
483 |
484 | if (isset($data[$filename][$stanza])) {
485 | $results[$filename] = $value2[$stanza];
486 | }
487 |
488 | }
489 |
490 | $retval["diff"][$stanza] = compareStanza($stanza, $results);
491 |
492 | }
493 |
494 | return($retval);
495 |
496 | } // End of compareFiles()
497 |
498 |
499 | /**
500 | * Print up info on stanzas which are missing from files.
501 | *
502 | * @param array $data Array of filenames and the stanzas they are missing.
503 | */
504 | function printMissingStanzas($data) {
505 |
506 | foreach ($data as $key => $value) {
507 |
508 | print "File: '$key' is missing the following stanzas:\n";
509 |
510 | foreach ($value as $key2 => $value2) {
511 | print "\t[$value2]\n";
512 | }
513 |
514 | print "\n";
515 |
516 | }
517 |
518 | } // End of printMissingStanzas()
519 |
520 |
521 | /**
522 | * Print up the differences in a particular stanza.
523 | *
524 | * @param string $name The name of a key in the stanza.
525 | *
526 | * @param array $data An array of the key values and the files those keys are in.
527 | *
528 | */
529 | function printDiffStanza($name, $data) {
530 |
531 | print "\tKey: '$name' found with multiple values:\n\n";
532 |
533 | foreach ($data as $key2 => $value2) {
534 |
535 | if ($key2 != "notfound") {
536 | print "\t\tValue '$key2' found in files:\n\n";
537 | } else {
538 | print "\t\tThese files did NOT contain the key:\n\n";
539 | }
540 |
541 | foreach ($value2 as $key3 => $value3) {
542 | print "\t\t\t$value3\n";
543 | }
544 |
545 | print("\n");
546 |
547 | }
548 |
549 | } // End of printDiffStanza()
550 |
551 |
552 | /**
553 | * Go through our stanzas and print up stanzas which are different between files.
554 | */
555 | function printDiffStanzas($data) {
556 |
557 | $any_stanza_printed = false;
558 |
559 | foreach ($data as $key => $value) {
560 | $stanza = $key;
561 | $stanza_printed = false;
562 |
563 | foreach ($value as $key2 => $value2) {
564 |
565 | $num_values = count($value2);
566 |
567 | if ($num_values > 1) {
568 |
569 | if (!$stanza_printed) {
570 | print "Stanza: [$stanza]\n\n";
571 | $stanza_printed = true;
572 | }
573 |
574 | printDiffStanza($key2, $value2);
575 |
576 | }
577 |
578 | }
579 |
580 | if ($stanza_printed) {
581 | $any_stanza_printed = true;
582 | }
583 | }
584 |
585 | if (!$any_stanza_printed) {
586 | print("# All files match!\n");
587 | }
588 |
589 | } // End of printDiffStanzas()
590 |
591 |
592 | /**
593 | * Our main entry point.
594 | */
595 | function main($argv) {
596 |
597 | $params = parseArgs($argv);
598 | printHeader($params["files"]);
599 |
600 | print "#\n";
601 | print "# Loading files...\n";
602 | print "#\n";
603 | $data = loadFiles($params["files"]);
604 |
605 | print "#\n";
606 | print "# Comparing contents...\n";
607 | print "#\n";
608 | $results = compareFiles($data);
609 | printMissingStanzas($results["missing"]);
610 | printDiffStanzas($results["diff"]);
611 |
612 | }
613 |
614 |
615 | try {
616 | main($argv);
617 |
618 | } catch (Exception $e) {
619 | print $e;
620 |
621 | }
622 |
623 |
--------------------------------------------------------------------------------