Import sys and modify exit invocation to be able to create single file executable of iottb

This commit is contained in:
Sebastian Lenzlinger
2024-07-17 12:10:57 +02:00
parent 78c155208e
commit c6c8cfb223
6 changed files with 14 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
import json
import sys
import click
from pathlib import Path
@@ -165,7 +166,7 @@ def add_device(device, db, guided):
click.echo(
f'To initialize the testbed in the default location run "iottb init-db"')
click.echo('Exiting...')
exit()
sys.exit()
# Ensure a device name was passed as argument
if device == "":
click.echo("Device name cannot be an empty string. Exiting...", lvl='w')
@@ -181,19 +182,18 @@ def add_device(device, db, guided):
logger.warning(f'Device directory {device_dir} already exists.')
click.echo(f'Device {device} already exists in the database.')
click.echo('Exiting...')
exit()
sys.exit()
try:
device_dir.mkdir()
except OSError as e:
logger.error(f'Error trying to create device {e}')
click.echo('Exiting...')
exit()
sys.exit()
# Step 4: Save metadata into device_dir
metadata_path = device_dir / definitions.DEVICE_METADATA_FILE_NAME
with metadata_path.open('w') as metadata_file:
json.dump(device_metadata.__dict__, metadata_file, indent=4)
click.echo(f'Successfully added device {device} to database')
logger.debug(f'Added device {device} to database {
database}. Full path of metadata {metadata_path}')
logger.debug(f'Added device {device} to database {database}. Full path of metadata {metadata_path}')
logger.info(f'Metadata for {device} {device_metadata.print_attributes()}')

View File

@@ -3,6 +3,7 @@ import logging
import os
import re
import subprocess
import sys
import uuid
from datetime import datetime
from pathlib import Path
@@ -280,7 +281,7 @@ def sniff(ctx, device, interface, print_pacno, ff, count, monitor_mode, print_ll
click.prompt('Create metadata anyway?')
else:
click.echo('Aborting capture...')
exit()
sys.exit()
end_time = datetime.now().strftime("%H:%M:%S")
end = time()
delta = end - start

View File

@@ -30,7 +30,7 @@ def init_db(ctx, dest, name, update_default):
click.echo(f'A database {name} already exists.')
logger.debug(f'DB {name} exists in {dest}')
click.echo(f'Exiting...')
exit()
sys.exit()
logger.debug(f'DB name {name} registered but does not exist.')
if not dest:
logger.info('No dest set, choosing default destination.')