Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 19582af

Browse files
authored
Adding json processing example to ReadMe (#26)
* Adding json processing example to ReadMe * Adding json snippet
1 parent e498a14 commit 19582af

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,84 @@ Packages found only in gcr.io/google-appengine/python:2017-06-29-190410: None
241241
Version differences: None
242242
243243
```
244+
## Example Run with json post-processing
245+
The following example demonstrates how one might selectively display the output of their diff, such that version differences are ignored and only package absence/presence is displayed and the packages present in only one image are sorted by size in descending order. A small piece of the json being post-processed can be seen below:
246+
```
247+
[
248+
{
249+
"DiffType": "AptDiffer",
250+
"Diff": {
251+
"Image1": "gcr.io/gcp-runtimes/multi-base",
252+
"Packages1": {},
253+
"Image2": "gcr.io/gcp-runtimes/multi-modified",
254+
"Packages2": {
255+
"dh-python": {
256+
"Version": "1.20141111-2",
257+
"Size": "277"
258+
},
259+
"libmpdec2": {
260+
"Version": "2.4.1-1",
261+
"Size": "275"
262+
},
263+
...
264+
```
265+
The post-processing script used for this example is below:
266+
267+
```import sys, json
268+
269+
def main():
270+
data = json.loads(sys.stdin.read())
271+
img1packages = []
272+
img2packages = []
273+
for differ in data:
274+
diff = differ['Diff']
275+
276+
if len(diff['Packages1']) > 0:
277+
for package in diff['Packages1']:
278+
Size = diff['Packages1'][package]['Size']
279+
img1packages.append((str(package), int(str(Size))))
280+
281+
if len(diff['Packages2']) > 0:
282+
for package in diff['Packages2']:
283+
Size = diff['Packages2'][package]['Size']
284+
img2packages.append((str(package), int(str(Size))))
285+
286+
img1packages = reversed(sorted(img1packages, key=lambda x: x[1]))
287+
img2packages = reversed(sorted(img2packages, key=lambda x: x[1]))
288+
289+
290+
print "Only in image1\n"
291+
for pkg in img1packages:
292+
print pkg
293+
print "Only in image2\n"
294+
for pkg in img2packages:
295+
print pkg
296+
print
297+
298+
if __name__ == "__main__":
299+
main()
300+
```
301+
302+
Given the above python script to postprocess json output, you can produce the following behavior:
303+
```
304+
container-diff gcr.io/gcp-runtimes/multi-base gcr.io/gcp-runtimes/multi-modified -a -j | python pyscript.py
305+
306+
Only in image1
244307
308+
Only in image2
245309
246-
## Make your own analyzer
310+
('libpython3.4-stdlib', 9484)
311+
('python3.4-minimal', 4506)
312+
('libpython3.4-minimal', 3310)
313+
('python3.4', 336)
314+
('dh-python', 277)
315+
('libmpdec2', 275)
316+
('python3-minimal', 96)
317+
('python3', 36)
318+
('libpython3-stdlib', 28)
319+
320+
```
321+
## Make your own differ
247322

248323
Feel free to develop your own analyzer leveraging the utils currently available. PRs are welcome.
249324

0 commit comments

Comments
 (0)