Skip to content

Commit 9585941

Browse files
authored
Merge pull request #40 from nexB/fix-json-output-memory-usage
Fix json output memory usage
2 parents 9a963f4 + ad852ec commit 9585941

3 files changed

Lines changed: 44 additions & 34 deletions

File tree

src/deltacode/cli.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
import json
3232

3333
import click
34+
import simplejson
3435

3536
from deltacode import DeltaCode
3637
from deltacode import __version__
38+
from deltacode.utils import deltas
3739

3840

39-
def generate_csv(delta, result_file):
41+
# FIXME: update the function argument delta to deltacode
42+
def write_csv(delta, result_file):
4043
"""
4144
Using the DeltaCode object, create a .csv file
4245
containing the primary information from the Delta objects.
@@ -54,27 +57,28 @@ def generate_csv(delta, result_file):
5457
csv_out.writerow(row)
5558

5659

57-
def generate_json(delta, result_file):
60+
def write_json(deltacode, outfile):
5861
"""
5962
Using the DeltaCode object, create a .json file
6063
containing the primary information from the Delta objects.
6164
"""
62-
output = OrderedDict([
65+
results = OrderedDict([
6366
('deltacode_version', __version__),
64-
('deltacode_stats', delta.get_stats()),
65-
('deltas', delta.to_dict())
67+
('deltacode_stats', deltacode.get_stats()),
68+
('deltas', deltas(deltacode)),
6669
])
6770

68-
with open(result_file, 'w') as outfile:
69-
json.dump(output, outfile, indent=4)
71+
# TODO: add toggle for pretty printing
72+
simplejson.dump(results, outfile, iterable_as_array=True, indent=2)
73+
outfile.write('\n')
7074

7175

7276
@click.command()
7377
@click.help_option('-h', '--help')
7478
@click.option('-n', '--new', required=True, prompt=False, type=click.Path(exists=True, readable=True), help='Identify the path to the "new" scan file')
7579
@click.option('-o', '--old', required=True, prompt=False, type=click.Path(exists=True, readable=True), help='Identify the path to the "old" scan file')
7680
@click.option('-c', '--csv-file', prompt=False, type=click.Path(exists=False), help='Identify the path to the .csv output file')
77-
@click.option('-j', '--json-file', prompt=False, type=click.Path(exists=False), help='Identify the path to the .json output file')
81+
@click.option('-j', '--json-file', prompt=False, default='-', type=click.File(mode='wb', lazy=False), help='Identify the path to the .json output file')
7882
def cli(new, old, csv_file, json_file):
7983
"""
8084
Identify the changes that need to be made to the 'old'
@@ -84,14 +88,11 @@ def cli(new, old, csv_file, json_file):
8488
option is selected, print the JSON results to the console.
8589
"""
8690
# do the delta
87-
delta = DeltaCode(new, old)
91+
deltacode = DeltaCode(new, old)
8892

8993
# output to csv
9094
if csv_file:
91-
generate_csv(delta, csv_file)
95+
write_csv(deltacode, csv_file)
9296
# generate JSON output
93-
elif json_file:
94-
generate_json(delta, json_file)
95-
# print to stdout
9697
else:
97-
print(json.dumps(delta.to_dict()))
98+
write_json(deltacode, json_file)

src/deltacode/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
from commoncode import paths
3232

3333

34+
def deltas(deltacode):
35+
"""
36+
Return a generator of Delta dictionaries for JSON serialized ouput.
37+
"""
38+
for category, deltas in deltacode.deltas.iteritems():
39+
for delta in deltas:
40+
yield delta.to_dict()
41+
42+
3443
class AlignmentException(Exception):
3544
"""
3645
Named exception for alignment errors.

tests/test_cli.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,102 +75,102 @@ class TestCLI(FileBasedTesting):
7575

7676
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
7777

78-
def test_generate_csv_added(self):
78+
def test_write_csv_added(self):
7979
new_scan = self.get_test_loc('cli/new_added1.json')
8080
old_scan = self.get_test_loc('cli/old_added1.json')
8181

8282
delta = DeltaCode(new_scan, old_scan)
8383
result_file = self.get_temp_file('.csv')
84-
cli.generate_csv(delta, result_file)
84+
cli.write_csv(delta, result_file)
8585
expected_file = self.get_test_loc('cli/added1.csv')
8686
check_csvs(result_file, expected_file)
8787

88-
def test_generate_csv_modified(self):
88+
def test_write_csv_modified(self):
8989
new_scan = self.get_test_loc('cli/new_modified1.json')
9090
old_scan = self.get_test_loc('cli/old_modified1.json')
9191

9292
delta = DeltaCode(new_scan, old_scan)
9393
result_file = self.get_temp_file('.csv')
94-
cli.generate_csv(delta, result_file)
94+
cli.write_csv(delta, result_file)
9595
expected_file = self.get_test_loc('cli/modified1.csv')
9696
check_csvs(result_file, expected_file)
9797

98-
def test_generate_csv_removed(self):
98+
def test_write_csv_removed(self):
9999
new_scan = self.get_test_loc('cli/new_removed1.json')
100100
old_scan = self.get_test_loc('cli/old_removed1.json')
101101

102102
delta = DeltaCode(new_scan, old_scan)
103103
result_file = self.get_temp_file('.csv')
104-
cli.generate_csv(delta, result_file)
104+
cli.write_csv(delta, result_file)
105105
expected_file = self.get_test_loc('cli/removed1.csv')
106106
check_csvs(result_file, expected_file)
107107

108-
def test_generate_csv_renamed(self):
108+
def test_write_csv_renamed(self):
109109
new_scan = self.get_test_loc('cli/new_renamed1.json')
110110
old_scan = self.get_test_loc('cli/old_renamed1.json')
111111

112112
delta = DeltaCode(new_scan, old_scan)
113113
result_file = self.get_temp_file('.csv')
114-
cli.generate_csv(delta, result_file)
114+
cli.write_csv(delta, result_file)
115115
expected_file = self.get_test_loc('cli/renamed1.csv')
116116
check_csvs(result_file, expected_file)
117117

118-
def test_generate_csv_modified_new_license_added(self):
118+
def test_write_csv_modified_new_license_added(self):
119119
new_scan = self.get_test_loc('cli/scan_modified_new_license_added.json')
120120
old_scan = self.get_test_loc('cli/scan_modified_old_license_added.json')
121121

122122
delta = DeltaCode(new_scan, old_scan)
123123
result_file = self.get_temp_file('.csv')
124-
cli.generate_csv(delta, result_file)
124+
cli.write_csv(delta, result_file)
125125
expected_file = self.get_test_loc('cli/modified_new_license_added.csv')
126126
check_csvs(result_file, expected_file)
127127

128-
def test_generate_csv_modified_new_license_added_low_score(self):
128+
def test_write_csv_modified_new_license_added_low_score(self):
129129
new_scan = self.get_test_loc('cli/scan_modified_new_license_added_low_score.json')
130130
old_scan = self.get_test_loc('cli/scan_modified_old_license_added_low_score.json')
131131

132132
delta = DeltaCode(new_scan, old_scan)
133133
result_file = self.get_temp_file('.csv')
134-
cli.generate_csv(delta, result_file)
134+
cli.write_csv(delta, result_file)
135135
expected_file = self.get_test_loc('cli/modified_new_license_added_low_score.csv')
136136
check_csvs(result_file, expected_file)
137137

138-
def test_generate_csv_license_info_removed(self):
138+
def test_write_csv_license_info_removed(self):
139139
new_scan = self.get_test_loc('cli/scan_new_license_info_removed.json')
140140
old_scan = self.get_test_loc('cli/scan_old_license_info_removed.json')
141141

142142
delta = DeltaCode(new_scan, old_scan)
143143
result_file = self.get_temp_file('.csv')
144-
cli.generate_csv(delta, result_file)
144+
cli.write_csv(delta, result_file)
145145
expected_file = self.get_test_loc('cli/license_info_removed.csv')
146146
check_csvs(result_file, expected_file)
147147

148-
def test_generate_csv_license_info_added(self):
148+
def test_write_csv_license_info_added(self):
149149
new_scan = self.get_test_loc('cli/scan_new_license_info_added.json')
150150
old_scan = self.get_test_loc('cli/scan_old_license_info_added.json')
151151

152152
delta = DeltaCode(new_scan, old_scan)
153153
result_file = self.get_temp_file('.csv')
154-
cli.generate_csv(delta, result_file)
154+
cli.write_csv(delta, result_file)
155155
expected_file = self.get_test_loc('cli/license_info_added.csv')
156156
check_csvs(result_file, expected_file)
157157

158-
def test_generate_csv_license_info_removed_below_cutoff_score(self):
158+
def test_write_csv_license_info_removed_below_cutoff_score(self):
159159
new_scan = self.get_test_loc('cli/scan_new_license_info_removed_below_cutoff_score.json')
160160
old_scan = self.get_test_loc('cli/scan_old_license_info_removed_below_cutoff_score.json')
161161

162162
delta = DeltaCode(new_scan, old_scan)
163163
result_file = self.get_temp_file('.csv')
164-
cli.generate_csv(delta, result_file)
164+
cli.write_csv(delta, result_file)
165165
expected_file = self.get_test_loc('cli/license_info_removed_below_cutoff_score.csv')
166166
check_csvs(result_file, expected_file)
167167

168-
def test_generate_csv_license_info_added_below_cutoff_score(self):
168+
def test_write_csv_license_info_added_below_cutoff_score(self):
169169
new_scan = self.get_test_loc('cli/scan_new_license_info_added_below_cutoff_score.json')
170170
old_scan = self.get_test_loc('cli/scan_old_license_info_added_below_cutoff_score.json')
171171

172172
delta = DeltaCode(new_scan, old_scan)
173173
result_file = self.get_temp_file('.csv')
174-
cli.generate_csv(delta, result_file)
174+
cli.write_csv(delta, result_file)
175175
expected_file = self.get_test_loc('cli/license_info_added_below_cutoff_score.csv')
176176
check_csvs(result_file, expected_file)

0 commit comments

Comments
 (0)