Skip to content

Commit 4428eae

Browse files
authored
Issue 422 support jpg format (#426)
* run graphviz on dot string Signed-off-by: haim-kermany <[email protected]> * call CmdlineRunner.run_and_get_output insted of graphviz Signed-off-by: haim-kermany <[email protected]> * call CmdlineRunner.run_and_get_output insted of graphviz Signed-off-by: haim-kermany <[email protected]> * update from master Signed-off-by: haim-kermany <[email protected]> * cleanup on error Signed-off-by: haim-kermany <[email protected]> * update readme Signed-off-by: haim-kermany <[email protected]> --------- Signed-off-by: haim-kermany <[email protected]>
1 parent f9bd6d2 commit 4428eae

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ The arguments to `--resource_list` and to `--base_resource_list` should be one o
8383
- `--period <minutes>`\
8484
Run NCA with given arguments every specified number of minutes
8585
- `--output_format <format>`\
86-
Output format specification (txt/yaml/csv/md/dot).\
86+
Output format specification (txt/yaml/csv/md/dot/jpg).\
87+
For jpg format, Graphviz executables must be installed and on user systems' PATH.\
8788
*default:* txt\
8889
*shorthand:* `-o`
8990
- `--file_out <file name>`\

docs/SchemeFileFormat.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ For example: `my_set/prod_ns/deny_all_policy`. If there are multiple policies na
7878
#### <a name="outputconfig"></a>Output Configuration object
7979
The supported entries in the outputConfiguration object are as follows:
8080

81-
| Field | Description | Value |
82-
|------------------|-----------------------------------------------------------------------------------------------------------------|----------------------------------------|
83-
| outputFormat | Output format specification. | string [ txt / yaml / csv / md / dot ] |
84-
| outputPath | A file path to redirect output into. | string |
85-
| outputEndpoints | Choose endpoints type in output. | string [ pods / deployments ] |
86-
| subset | A dict object with the defined subset elements to display in the output | [subset](#subset) object |
87-
| fullExplanation | Choose if to print all counterexamples causing the query result in the output | bool |
88-
| excludeIPv6Range | If the policies of the config do not contain any IPv6 addresses, do not include IPv6 range in the query results | bool [default: True] |
81+
| Field | Description | Value |
82+
|------------------|-----------------------------------------------------------------------------------------------------------------|---------------------------------------------|
83+
| outputFormat | Output format specification. | string [ txt / yaml / csv / md / dot / jpg] |
84+
| outputPath | A file path to redirect output into. | string |
85+
| outputEndpoints | Choose endpoints type in output. | string [ pods / deployments ] |
86+
| subset | A dict object with the defined subset elements to display in the output | [subset](#subset) object |
87+
| fullExplanation | Choose if to print all counterexamples causing the query result in the output | bool |
88+
| excludeIPv6Range | If the policies of the config do not contain any IPv6 addresses, do not include IPv6 range in the query results | bool [default: True] |
8989

9090

9191
#### <a name="subset"></a>Subset object

nca/NetworkConfig/NetworkConfigQuery.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class ConnectivityMapQuery(NetworkConfigQuery):
621621

622622
@staticmethod
623623
def get_supported_output_formats():
624-
return {'txt', 'yaml', 'csv', 'md', 'dot', 'json'}
624+
return {'txt', 'yaml', 'csv', 'md', 'dot', 'json', 'jpg'}
625625

626626
def is_in_subset(self, peer):
627627
"""
@@ -725,7 +725,7 @@ def get_connectivity_output_full(self, connections, peers, peers_to_compare):
725725
:param PeerSet peers_to_compare: the peers to consider for fw-rules output
726726
:rtype Union[str,dict]
727727
"""
728-
if self.output_config.outputFormat == 'dot':
728+
if self.output_config.outputFormat in ['dot', 'jpg']:
729729
dot_full = self.dot_format_from_connections_dict(connections, peers)
730730
return dot_full
731731
# handle formats other than dot
@@ -743,7 +743,7 @@ def get_connectivity_output_split_by_tcp(self, connections, peers, peers_to_comp
743743
connectivity_tcp_str = 'TCP'
744744
connectivity_non_tcp_str = 'non-TCP'
745745
connections_tcp, connections_non_tcp = self.convert_connections_to_split_by_tcp(connections)
746-
if self.output_config.outputFormat == 'dot':
746+
if self.output_config.outputFormat in ['dot', 'jpg']:
747747
dot_tcp = self.dot_format_from_connections_dict(connections_tcp, peers, connectivity_tcp_str)
748748
dot_non_tcp = self.dot_format_from_connections_dict(connections_non_tcp, peers, connectivity_non_tcp_str)
749749
# concatenate the two graphs into one dot file

nca/Utils/OutputConfiguration.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import os
88
from urllib import request
9+
from nca.Utils.CmdlineRunner import CmdlineRunner
910

1011

1112
class OutputConfiguration(dict):
@@ -46,12 +47,23 @@ def print_query_output(self, output, supported_output_formats=None):
4647
path = self.outputPath
4748
if path is not None:
4849
# print output to a file
49-
try:
50-
with open(path, "a") as f:
51-
f.write(output)
52-
print(f'wrote query output to: {path}')
53-
except FileNotFoundError:
54-
print(f"FileNotFoundError: configured outputPath is: {path}")
50+
if self.outputFormat == 'jpg':
51+
tmp_dot_file = f'{path}.nca_tmp.dot'
52+
try:
53+
with open(tmp_dot_file, "w") as f:
54+
f.write(output)
55+
CmdlineRunner.run_and_get_output(['dot', tmp_dot_file, '-Tjpg', f'-o{path}'])
56+
except Exception as e:
57+
print(f'Failed to create a jpg file: {path}\n{e}')
58+
if os.path.isfile(tmp_dot_file):
59+
os.remove(tmp_dot_file)
60+
else:
61+
try:
62+
with open(path, "a") as f:
63+
f.write(output)
64+
print(f'wrote query output to: {path}')
65+
except FileNotFoundError:
66+
print(f'FileNotFoundError: configured outputPath is: {path}')
5567
elif self.prURL is not None:
5668
self.write_git_comment(output)
5769
else:

nca/nca_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def nca_main(argv=None):
301301
help='A list of labels to subset the query by')
302302
parser.add_argument('--ghe_token', '--gh_token', type=str, help='A valid token to access a GitHub repository')
303303
parser.add_argument('--output_format', '-o', type=str,
304-
help='Output format specification (txt, csv, md, dot or yaml). The default is txt.')
304+
help='Output format specification (txt, csv, md, dot, jpg or yaml). The default is txt.')
305305
parser.add_argument('--file_out', '-f', type=str, help='A file path to which output is redirected')
306306
parser.add_argument('--expected_output', type=str, help='A file path of the expected query output,'
307307
'relevant only with --connectivity and --semantic_diff')

0 commit comments

Comments
 (0)