-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtw_facter.py
687 lines (547 loc) · 23.5 KB
/
tw_facter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#!/usr/bin/env python3
# coding: utf-8
__version__ = '0.0.2'
__author__ = 'Mateusz Jaromi, mateusz.jaromi@gmail.com'
__description__ = 'tw_facter is a tool which provides facts about BMC Discovery Appliance'
import os
import re
import csv
import sys
import json
import uuid
import socket
import hashlib
import argparse
import subprocess
from datetime import datetime
sys.path.append('/usr/tideway/python/common/')
import product
if os.getenv('TW_FACTER_DRY_RUN'):
try:
TW_FACTER_DRY_RUN = int(os.getenv('TW_FACTER_DRY_RUN'))
except:
print(json.dumps({'status': 'error', 'message': 'TW_FACTER_DRY_RUN environment variable must be an integer!'}))
else:
TW_FACTER_DRY_RUN = 60 # time in seconds
TW_TIDEWAY = '/usr/tideway'
TW_FACTER_USERNAME = 'tw_facter'
TW_FACTER_PASSWORD_FILE = '{}/.tw_facter_passwd'.format(TW_TIDEWAY)
TW_FACTER_CUSTOM_FACTS = '{}/.tw_facter_custom'.format(TW_TIDEWAY)
TW_FACTER_TIMESTAMP = '{}/.tw_facter_timestamp'.format(TW_TIDEWAY)
TW_FACTER_FACTS = '{}/.tw_facter_facts'.format(TW_TIDEWAY)
TW_CPU = '/proc/loadavg'
TW_MEM = '/proc/meminfo'
# docs: https://docs.bmc.com/docs/discovery/113/tw_service_control-800561100.html
TW_PERMISSIBLE_SERVICES = {'Application Server service': 'appserver',
'CMDB Sync (Exporter) service': 'cmdb_sync_exporter',
'CMDB Sync (Transformer) service': 'cmdb_sync_transformer',
'Discovery service': 'discovery',
'External API service': 'external_api',
'Mainframe Provider service': 'mainframe_provider',
'Model service': 'model',
'Reasoning service': 'reasoning',
'Reports service': 'reports',
'Security service': 'security',
'SQL Provider service': 'sql_provider',
'Tomcat service': 'tomcat',
'Vault service': 'vault'}
TW_POOL_HEADER = ['Pool', 'Type', 'Domains', 'IP Ranges']
TW_PROXIES_HEADER = ['Pool', 'Proxy', 'Address', 'Port', 'Type', 'Enabled', 'Active', 'Version']
""" BMC Discovery Queries """
TW_EXCLUDE_RANGES_QUERY = "search in '_System' ExcludeRange " \
"show " \
"name as 'name', " \
"range_strings as 'range_strings', " \
"description as 'description', " \
"recurrenceDescription(schedule) as 'schedule', " \
"enabled as 'enabled', " \
"fullFoundationName(created_by) as 'created_by', " \
"exrange_id as 'exrange_id', " \
"range_prefix as 'range_prefix'"
TW_SCHEDULED_RUNS = "search ScanRange where scan_type = 'Scheduled' " \
"show " \
"provider as 'cloud_provider', " \
"created_by as 'created_by', " \
"created_time as 'created_time', " \
"deleting as 'deleting', " \
"enabled as 'enabled', " \
"range_id as 'range_id', " \
"label as 'label', " \
"company as 'company', " \
"allow_tpl_scan as 'allow_tpl_scan', " \
"range_strings as 'range_strings', " \
"range_prefix as 'range_prefix', " \
"scan_kind as 'scan_kind', " \
"scan_level as 'scan_level', " \
"scan_type as 'scan_type', " \
"schedule as 'schedule', " \
"recurrenceDescription(schedule) as 'date_rules'"
TW_CURRENTLY_PROCESSING_RUNS = "search DiscoveryRun " \
"where __inprogress defined and not _consolidation_source defined " \
"order by starttime desc " \
"show " \
"blocked as blocked, " \
"cancelled as cancelled, " \
"_consolidation_source_name as _consolidation_source_name, " \
"_consolidation_source_system as _consolidation_source_system, " \
"_consolidation as _consolidation, " \
"consolidation_cancelled as consolidation_cancelled, " \
"_consolidation_last_received as _consolidation_last_received, " \
"_consolidation_done as _consolidation_done, " \
"_consolidation_source as _consolidation_source, " \
"discovery_endtime as discovery_endtime, " \
"discovery_starttime as discovery_starttime, " \
"has_eca_error as has_eca_error, " \
"endtime as endtime, " \
"valid_ranges as valid_ranges, " \
"failure_reason as failure_reason, " \
"hard_failure as hard_failure, " \
"key as key, " \
"label as label, " \
"cdm_company as cdm_company, " \
"consolidation_done_count as consolidation_done_count, " \
"done as done, " \
"scanning as scanning, " \
"explicit_ip_count as explicit_ip_count, " \
"pre_scanning as pre_scanning, " \
"waiting as waiting, " \
"implicit_ip_count as implicit_ip_count, " \
"range_prefix as range_prefix, " \
"range_summary as range_summary, " \
"scan_kind as scan_kind, " \
"scan_level as scan_level, " \
"scan_type as scan_type, " \
"starttime as starttime, " \
"total as total, " \
"user as user, " \
"done * 100 / total as percent_complete"
""" BMC Discovery Commands """
TW_USERNAME_PASSWORD = '--username={} --passwordfile={}'.format(TW_FACTER_USERNAME, TW_FACTER_PASSWORD_FILE)
# docs: https://docs.bmc.com/docs/discovery/113/tw_query-788110376.html
TW_QUERY_COMMAND = 'tw_query {}'.format(TW_USERNAME_PASSWORD)
# no vendor docs ; provide list of options set in BMC Discovery appliance
TW_OPTIONS_COMMAND = 'tw_options {}'.format(TW_USERNAME_PASSWORD)
TW_OPTIONS_COMMAND_DETAILS = '{} | ' \
'egrep -e \'\s+=\s+\' | ' \
'sed \'s/ = /=/g;s/ \+//g\''.format(TW_OPTIONS_COMMAND)
# docs: https://docs.bmc.com/docs/discovery/113/tw_listusers-788110363.html
TW_LISTUSERS_COMMAND = "tw_listusers | egrep -o '\\b^[a-zA-Z0-9@_+=-]+'"
TW_LISTUSERS_COMMAND_DETAILS = "tw_listusers --filter={}$ | " \
"egrep -v '{}:|^$' | " \
"sed 's/ \+/ /g;s/^ /#/g;s/: /%/g' | " \
"tr '\\n' ',' | " \
"sed 's/\(.*\),/\\1/;s/,#/#/g;s/:#/%/g;s/^#//g' | " \
"tr '#' '\\n' | " \
"sed -r ':r;/^user state/N;s/\\n/;/;tr' | " \
"sed 's/;password/\\npassword/g;s/;groups/\\ngroups/g'"
# docs: https://docs.bmc.com/docs/discovery/113/tw_vault_control-788110389.html
TW_VAULT_CONTROL_COMMAND = "tw_vault_control {} --show --json".format(TW_USERNAME_PASSWORD)
# docs: https://docs.bmc.com/docs/discovery/113/tw_service_control-800561100.html
TW_SERVICE_CONTROL_COMMAND = 'tw_service_control | ' \
'egrep -e \'\[\s+\w+\s+\]\' | ' \
'sed \'s/\[\s*/;/g;s/\s*\]//g;s/ \+//g;s/^ //g;s/:/;/g\''
# no vendor docs ; provide configuration related to the Administration/Security Policy (/ui/SetupSecurityOptions)
TW_SECOPTS_COMMAND = 'tw_secopts | ' \
'egrep -e \'\s+=\s+\' | ' \
'sed \'s/ = /=/g;s/ \+//g\''
# no vendor docs ; among other things provide configuration related Windows Pools and Proxies
TW_RESTRICTED_WINPROXY_COMMAND = 'tw_restricted_winproxy_ctrl {} {} > {}'
# provide information about tideway rpm's
TW_RPM = 'rpm -qa | grep tideway'
TW_RPM_DETAILS = "rpm -qi {} | sed '/^Description/N;s/\\n/ /'"
# provide information about cluster and omniNames services
TW_SYSTEMCTL_COMMAND = 'systemctl show -p LoadState,ActiveState,SubState,MainPID {} | ' \
'tr \'[:upper:]\' \'[:lower:]\' | ' \
'sed \'s/p/_p/g;s/st/_st/g\''
# provide information about taxonomy files, installed and custom
TW_TAXONOMY_COMMAND = 'find /usr/tideway/data/*/taxonomy/ -type f -iname "*.xsl" -o -iname "*.xml"'
# provide information about jdbc drivers, installed and custom
TW_JDBC_COMMAND = 'find /usr/tideway/data/*/jdbcdrivers/ -type f -iname "*.jar" -o -iname "*.properties"'
# provide information about file
TW_STAT_COMMAND = 'stat -c \'%A;%U;%G\' {}'
# system
# network information
TW_SYSTEM_NETWORK_GET_PRIMARY_INTERFACE = "route | awk '/^default/{print $NF}'"
TW_SYSTEM_NETWORK_GET_IP_ADDRESS = "ip route show | grep -Po '(?<=(src )).*(?= metric)'"
TW_SYSTEM_NETWORK_GET_GATEWAY = "ip route show | grep -Po '(?<=(default via )).*(?= dev)'"
# uptime
TW_SYSTEM_UPTIME = "awk '{print $1}' /proc/uptime"
# uname
TW_SYSTEM_UNAME = "uname -a"
# cores
TW_CORES_COUNT = "find /usr/tideway/cores/ -iname 'core.*' | wc -l"
TW_CORES_SIZE = "du -h /usr/tideway/cores | awk '{print $1}'"
""" tw_facter functions """
def main(facts=None):
parser = argparse.ArgumentParser(description=__description__, epilog='tw_facter v{}'.format(__version__))
parser.add_argument('-d', '--dry', action='store_true', help='Dry run')
parser.add_argument('-k', '--kind', default=['all'], choices=['appliance', 'cluster', 'custom', 'cores', 'discovery',
'env', 'facter', 'jdbc', 'metadata', 'omninames',
'options', 'rpms', 'security', 'services', 'system',
'taxonomy', 'users', 'vault', 'windows'],
help='Shows selected kinds', nargs='+')
parser.add_argument('-v', '--version', action='store_true', help='Shows tw_facter version')
args = parser.parse_args()
if args.version:
print('tw_facter v{}'.format(__version__))
elif args.dry or dry_run():
facts = tw_facts(args.kind)
tw_facts_write(facts)
else:
facts = tw_facts_read()
if facts:
print(json.dumps(facts, sort_keys=True))
def dry_run():
prev = 0
now = int(datetime.now().timestamp())
if os.path.isfile(TW_FACTER_TIMESTAMP):
prev = int(open(TW_FACTER_TIMESTAMP, mode='r').read())
with open(TW_FACTER_TIMESTAMP, mode='w') as file:
file.write(str(now))
if now - prev > TW_FACTER_DRY_RUN:
return True
return False
def appliance():
_dict = {}
for item in product.__dict__:
if item.isupper() and not re.search('COPYRIGHT', item):
_dict[item.lower()] = product.__dict__[item]
hostname = socket.gethostname()
ip = socket.gethostbyname_ex(hostname)[-1]
fqdn = socket.getfqdn()
return {'ip': ip,
'hostname': hostname,
'fqdn': fqdn,
'product': _dict}
def cluster():
return exec_systemctl('cluster')
def custom():
_dict = {}
if os.path.isfile(TW_FACTER_CUSTOM_FACTS):
with open(TW_FACTER_CUSTOM_FACTS, 'r') as f:
array = f.read().splitlines()
for item in array:
k, v = item.split('=')
_dict[k] = v
return _dict
def cores():
def count():
return exec(TW_CORES_COUNT)[0]
def size():
return exec(TW_CORES_SIZE)[0]
return {'count': count(),
'size': size()}
def discovery():
def exclude_ranges():
return convert_to_list_of_dicts(exec_tw_query(TW_EXCLUDE_RANGES_QUERY))
def scheduled_runs():
return convert_to_list_of_dicts(exec_tw_query(TW_SCHEDULED_RUNS))
def currently_processing_runs():
return convert_to_list_of_dicts(exec_tw_query(TW_CURRENTLY_PROCESSING_RUNS))
return {'exclude_ranges': exclude_ranges(),
'scheduled_runs': scheduled_runs(),
'currently_processing_runs': currently_processing_runs()}
def env():
_dict = {}
for env in os.environ:
_dict[env] = os.getenv(env)
return _dict
def facter():
return {'version': __version__,
'id': os.getegid(),
'gid': os.getuid()}
def jdbc():
_dict = {}
custom = []
installed = []
for item in exec(TW_JDBC_COMMAND):
stat = file_stat_details(item)
if re.search('properties', item):
content = {}
for property in convert_to_list(item, False):
property = property.split('=')
k = property[0]
v = ''.join(property[1:])
content[k] = v
stat['content'] = content
custom.append(stat) if re.search('custom', item) else installed.append(stat)
_dict['custom'] = custom
_dict['installed'] = installed
return _dict
def metadata():
def timestamp_strftime(time):
return time.strftime('%Y-%m-%d %H:%M:%S')
def timestamp():
now = datetime.now()
now_utc = now.utcnow()
return {'now': timestamp_strftime(now),
'now_utc': timestamp_strftime(now_utc)}
return {'timestamp': timestamp(),
'id': message_id()}
def omninames():
return exec_systemctl('omniNames')
def options():
_dict = {}
for item in exec(TW_OPTIONS_COMMAND_DETAILS):
item = item.split('=')
k = item[0]
v = ''.join(item[1:])
_dict[k] = v
return _dict
def rpms():
def rpms_list():
return exec(TW_RPM)
_array = []
for rpm in rpms_list():
rpm_dict = {}
item = exec(TW_RPM_DETAILS.format(rpm))
for detail in item:
detail = detail.split(':')
k = detail[0].strip().replace(' ', '_').lower()
v = ''.join(detail[1:]).strip()
rpm_dict[k] = v
_array.append(rpm_dict)
return _array
def security():
_dict = {}
for item in exec(TW_SECOPTS_COMMAND):
k = item.split('=')[0]
v = ''.join(item.split('=')[1:])
_dict[k] = v
return _dict
def services():
_dict = {}
for service in exec(TW_SERVICE_CONTROL_COMMAND):
name, pid, status = service.split(';')
_dict[TW_PERMISSIBLE_SERVICES[name]] = {'name': name,
'pid': pid,
'status': status}
return _dict
def system():
def network():
def get_primary_interface():
return exec(TW_SYSTEM_NETWORK_GET_PRIMARY_INTERFACE)[0]
def get_ip_address():
return exec(TW_SYSTEM_NETWORK_GET_IP_ADDRESS)[0]
def get_gateway():
return exec(TW_SYSTEM_NETWORK_GET_GATEWAY)[0]
return {'interface': get_primary_interface(),
'ip_address': get_ip_address(),
'gateway': get_gateway()}
def uptime():
return exec(TW_SYSTEM_UPTIME)[0]
def uname():
return exec(TW_SYSTEM_UNAME)[0]
def cpu():
with open(TW_CPU, 'r') as f:
return f.read().strip().split()[0:3]
def getmem(meminfo, item):
return re.findall('[0-9]+', ' '.join(meminfo[item].strip().split()))[0]
def mem():
with open(TW_MEM, 'r') as f:
meminfo = f.readlines()
return {'total_kb': getmem(meminfo, 0),
'free_kb': getmem(meminfo, 1),
'available_kb': getmem(meminfo, 2)}
def rh():
return exec('cat /etc/redhat-release')[0]
return {'uptime': uptime(),
'cpu': cpu(),
'mem': mem(),
'network': network(),
'os': rh(),
'uname': uname()}
def taxonomy():
_dict = {}
custom = []
installed = []
for item in exec(TW_TAXONOMY_COMMAND):
stat = file_stat_details(item)
custom.append(stat) if re.search('custom', item) else installed.append(stat)
_dict['custom'] = custom
_dict['installed'] = installed
return _dict
def users():
def users_list():
return exec(TW_LISTUSERS_COMMAND)
_dict = {}
for user in users_list():
user_dict = {}
item = exec(TW_LISTUSERS_COMMAND_DETAILS.format(user, user))
for detail in item:
if len(detail.split('%')) == 2:
k, v = detail.split('%')
user_dict[k.replace(' ', '_')] = v
else:
item_dict = {}
detail = detail.split(';')
for item in detail:
k, v = item.split('%')
k = k.lower().replace(' ', '_').replace('password', 'value').replace('user_state', 'state')
v = v.replace('\"', '')
item_dict[k] = v
if any('user state' in i for i in detail):
user_dict['user'] = item_dict
if any('password' in i for i in detail):
user_dict['password'] = item_dict
_dict[user] = user_dict
return _dict
def vault():
_array = []
for credential in exec(TW_VAULT_CONTROL_COMMAND):
if 'types' in credential:
_array.append(json.loads(credential))
return _array
def windows():
def win_details(file, headers, type=None):
if type == 'pool':
global number_of_pools
number_of_pools = 0
if type == 'proxy':
global number_of_proxies, number_of_proxies_enabled, number_of_proxies_active, number_of_proxies_disabled, number_of_proxies_inactive
number_of_proxies = 0
number_of_proxies_enabled = 0
number_of_proxies_disabled = 0
number_of_proxies_active = 0
number_of_proxies_inactive = 0
tw_header_position = []
results = []
array = convert_to_list(file)
for header in headers:
try:
tw_header_position.append(array[0].find(header))
except:
pass
for item in array[1:]:
_dict = {}
for i in range(len(tw_header_position)):
start = tw_header_position[i]
try:
end = tw_header_position[i + 1]
except:
end = None
k = headers[i].lower().replace(' ', '_')
v = item[start:end].strip()
_dict[k] = v
if type == 'proxy':
if headers[i] == 'Enabled' and _dict[headers[i].lower()] == 'True':
number_of_proxies_enabled += 1
if headers[i] == 'Active' and _dict[headers[i].lower()] == 'True':
number_of_proxies_active += 1
if headers[i] == 'Enabled' and _dict[headers[i].lower()] == 'False':
number_of_proxies_disabled += 1
if headers[i] == 'Active' and _dict[headers[i].lower()] == 'False':
number_of_proxies_inactive += 1
results.append(_dict)
if type == 'pool':
number_of_pools = len(array[1:])
if type == 'proxy':
number_of_proxies = len(array[1:])
return results
def win(type):
command_results = '/tmp/tw_facter_{}'.format(message_id())
if type == 'pool':
switch = '--list-all-pools'
header = TW_POOL_HEADER
elif type == 'proxy':
switch = '--list-all-proxies'
header = TW_PROXIES_HEADER
exec(TW_RESTRICTED_WINPROXY_COMMAND.format(TW_USERNAME_PASSWORD, switch, command_results))
return win_details(command_results, header, type)
def windows_pools():
return win('pool')
def windows_proxies():
return win('proxy')
return {'pools': windows_pools(),
'proxies': windows_proxies(),
'number_of_pools': number_of_pools,
'number_of_proxies': number_of_proxies,
'number_of_proxies_enabled': number_of_proxies_enabled,
'number_of_proxies_disabled': number_of_proxies_disabled,
'number_of_proxies_active': number_of_proxies_active,
'number_of_proxies_inactive': number_of_proxies_inactive}
def tw_facts(kind):
if 'all' in kind:
return {'appliance': appliance(),
'cluster': cluster(),
'custom': custom(),
'cores': cores(),
'discovery': discovery(),
'env': env(),
'facter': facter(),
'jdbc': jdbc(),
'metadata': metadata(),
'omninames': omninames(),
'options': options(),
'rpms': rpms(),
'security': security(),
'services': services(),
'system': system(),
'taxonomy': taxonomy(),
'users': users(),
'vault': vault(),
'windows': windows()}
else:
_return = {}
for _ in kind:
_return[_] = globals()[_]()
return _return
""" tw_facter helpers """
def tw_facts_read():
with open(TW_FACTER_FACTS, mode='r') as file:
return json.loads(file.read())
def tw_facts_write(facts):
with open(TW_FACTER_FACTS, mode='w') as file:
file.write(json.dumps(facts, sort_keys=True))
def file_stat_details(file):
def file_stat(file):
return exec(TW_STAT_COMMAND.format(file))
_dict = {}
permissions, user, group = file_stat(file)[0].split(';')
_dict['file'] = os.path.basename(file)
_dict['md5sum'] = file_md5sum(file)
_dict['permissions'] = permissions
_dict['user'] = user
_dict['group'] = group
return _dict
def file_md5sum(file):
return hashlib.md5(open(file, 'rb').read()).hexdigest()
def message_id():
return uuid.uuid4().hex
def exec(command):
result = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
universal_newlines=True,
bufsize=0)
_array = []
for line in result.stdout:
_array.append(line.strip())
return _array
def exec_tw_query(query):
command_results = '/tmp/tw_facter_{}'.format(message_id())
command = '{} --csv "{}" > {}'.format(TW_QUERY_COMMAND, query, command_results)
exec(command)
return command_results
def exec_systemctl(service):
_dict = {}
for item in exec(TW_SYSTEMCTL_COMMAND.format(service)):
k, v = item.split('=')
_dict[k.lower()] = v
return _dict
def convert_to_list_of_dicts(file):
_array = []
with open(file, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for item in csv_reader:
_array.append(item)
os.remove(file)
return _array
def convert_to_list(file, remove=True):
with open(file, mode='r') as tab_file:
array = tab_file.read().strip().splitlines()
os.remove(file) if remove else None
return array
if __name__ == '__main__':
sys.exit(main())