Skip to content

Commit e6661d4

Browse files
committed
give the opportunity to use a custom output directory in output-filename/of option
1 parent 9b26fab commit e6661d4

File tree

3 files changed

+76
-35
lines changed

3 files changed

+76
-35
lines changed

data_source_mapping.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -249,20 +249,26 @@ def plot_data_sources_graph(filename, output_filename, output_overwrite):
249249
output_filename = 'graph_data_sources'
250250
elif output_filename.endswith('.html'):
251251
output_filename = output_filename.replace('.html', '')
252+
253+
if os.sep not in output_filename:
254+
output_filename = 'output/%s' % output_filename
252255

253256
if not output_overwrite:
254-
output_filename = get_non_existing_filename('output/' + output_filename, 'html')
257+
output_filename = get_non_existing_filename(output_filename, 'html')
255258
else:
256-
output_filename = use_existing_filename('output/' + output_filename, 'html')
259+
output_filename = use_existing_filename(output_filename, 'html')
257260

258-
import plotly.graph_objs as go
259-
import plotly.offline as offline
260-
offline.plot(
261-
{'data': [go.Scatter(x=df['date'], y=df['cumcount'])],
262-
'layout': go.Layout(title="# of data sources for " + name)},
263-
filename=output_filename, auto_open=False
264-
)
265-
print("File written: " + output_filename)
261+
try:
262+
import plotly.graph_objs as go
263+
import plotly.offline as offline
264+
offline.plot(
265+
{'data': [go.Scatter(x=df['date'], y=df['cumcount'])],
266+
'layout': go.Layout(title="# of data sources for " + name)},
267+
filename=output_filename, auto_open=False
268+
)
269+
print("File written: " + output_filename)
270+
except Exception as e:
271+
print('[!] Error while writing graph file: %s' % str(e))
266272

267273

268274
def export_data_source_list_to_excel(filename, output_filename, output_overwrite, eql_search=False):
@@ -283,11 +289,14 @@ def export_data_source_list_to_excel(filename, output_filename, output_overwrite
283289
output_filename = 'data_sources'
284290
elif output_filename.endswith('.xlsx'):
285291
output_filename = output_filename.replace('.xlsx', '')
292+
293+
if os.sep not in output_filename:
294+
output_filename = 'output/%s' % output_filename
286295

287296
if not output_overwrite:
288-
excel_filename = get_non_existing_filename('output/' + output_filename, 'xlsx')
297+
excel_filename = get_non_existing_filename(output_filename, 'xlsx')
289298
else:
290-
excel_filename = use_existing_filename('output/' + output_filename, 'xlsx')
299+
excel_filename = use_existing_filename(output_filename, 'xlsx')
291300

292301
workbook = xlsxwriter.Workbook(excel_filename)
293302
worksheet = workbook.add_worksheet('Data sources')
@@ -999,14 +1008,20 @@ def generate_technique_administration_file(filename, output_filename, output_ove
9991008
output_filename = 'techniques-administration-' + normalize_name_to_filename(name)
10001009
elif output_filename.endswith('.yaml'):
10011010
output_filename = output_filename.replace('.yaml', '')
1011+
1012+
if os.sep not in output_filename:
1013+
output_filename = 'output/%s' % output_filename
10021014

10031015
if not output_overwrite:
1004-
output_filename = get_non_existing_filename(f'output/{output_filename}', 'yaml')
1016+
output_filename = get_non_existing_filename(output_filename, 'yaml')
10051017
else:
1006-
output_filename = use_existing_filename(f'output/{output_filename}', 'yaml')
1007-
1008-
with open(output_filename, 'w') as f:
1009-
f.writelines(yaml_file_lines)
1010-
print("File written: " + output_filename)
1018+
output_filename = use_existing_filename(output_filename, 'yaml')
1019+
1020+
try:
1021+
with open(output_filename, 'w') as f:
1022+
f.writelines(yaml_file_lines)
1023+
print("File written: " + output_filename)
1024+
except Exception as e:
1025+
print('[!] Error while writing yaml file: %s' % str(e))
10111026
else:
10121027
return yaml_file

file_output.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ def _clean_filename(filename):
1010
"""
1111
return filename.replace('/', '').replace('\\', '').replace(':', '')[:200]
1212

13+
def _clean_filepath(filename):
14+
"""
15+
Remove invalid characters from filename and maximize it to 200 characters
16+
:param filename: Input filename including path
17+
:return: sanitized filename
18+
"""
19+
fixed_filename = filename.replace(':', '')
20+
if os.sep == '/':
21+
fixed_filename = fixed_filename.replace('\\', '')
22+
elif os.sep == '\\':
23+
fixed_filename = fixed_filename.replace('/', '')
24+
return fixed_filename[:200]
1325

1426
def write_file(filename, overwrite_mode, content):
1527
"""
@@ -20,17 +32,22 @@ def write_file(filename, overwrite_mode, content):
2032
:param content: the content of the file that needs to be written to the file
2133
:return:
2234
"""
23-
output_filename = 'output/%s' % _clean_filename(filename)
35+
if os.sep in filename:
36+
output_filename = _clean_filepath(filename)
37+
else:
38+
output_filename = 'output/%s' % _clean_filename(filename)
2439

2540
if not overwrite_mode:
2641
output_filename = get_non_existing_filename(output_filename, 'json')
2742
else:
2843
output_filename = use_existing_filename(output_filename, 'json')
2944

30-
with open(output_filename, 'w') as f:
31-
f.write(content)
32-
33-
print('File written: ' + output_filename)
45+
try:
46+
with open(output_filename, 'w') as f:
47+
f.write(content)
48+
print('File written: ' + output_filename)
49+
except Exception as e:
50+
print('[!] Error while writing layer file: %s' % str(e))
3451

3552

3653
def backup_file(filename):

technique_mapping.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,25 @@ def plot_graph(filename, type_graph, output_filename, output_overwrite):
417417
elif output_filename.endswith('.html'):
418418
output_filename = output_filename.replace('.html', '')
419419

420+
if os.sep not in output_filename:
421+
output_filename = 'output/%s' % output_filename
422+
420423
if not output_overwrite:
421-
output_filename = get_non_existing_filename('output/' + output_filename, 'html')
424+
output_filename = get_non_existing_filename(output_filename, 'html')
422425
else:
423-
output_filename = use_existing_filename('output/' + output_filename, 'html')
426+
output_filename = use_existing_filename(output_filename, 'html')
424427

425-
import plotly
426-
import plotly.graph_objs as go
427-
plotly.offline.plot(
428-
{'data': [go.Scatter(x=df['date'], y=df['cumcount'])],
429-
'layout': go.Layout(title="# of %s items for %s" % (type_graph, name))},
430-
filename=output_filename, auto_open=False
431-
)
432-
print("File written: " + output_filename)
428+
try:
429+
import plotly
430+
import plotly.graph_objs as go
431+
plotly.offline.plot(
432+
{'data': [go.Scatter(x=df['date'], y=df['cumcount'])],
433+
'layout': go.Layout(title="# of %s items for %s" % (type_graph, name))},
434+
filename=output_filename, auto_open=False
435+
)
436+
print("File written: " + output_filename)
437+
except Exception as e:
438+
print('[!] Error while writing graph file: %s' % str(e))
433439

434440

435441
def export_techniques_list_to_excel(filename, output_filename, output_overwrite):
@@ -450,11 +456,14 @@ def export_techniques_list_to_excel(filename, output_filename, output_overwrite)
450456
output_filename = 'techniques'
451457
elif output_filename.endswith('.xlsx'):
452458
output_filename = output_filename.replace('.xlsx', '')
459+
460+
if os.sep not in output_filename:
461+
output_filename = 'output/%s' % output_filename
453462

454463
if not output_overwrite:
455-
excel_filename = get_non_existing_filename('output/' + output_filename, 'xlsx')
464+
excel_filename = get_non_existing_filename(output_filename, 'xlsx')
456465
else:
457-
excel_filename = use_existing_filename('output/' + output_filename, 'xlsx')
466+
excel_filename = use_existing_filename(output_filename, 'xlsx')
458467

459468
workbook = xlsxwriter.Workbook(excel_filename)
460469
worksheet_detections = workbook.add_worksheet('Detections')

0 commit comments

Comments
 (0)