<commit_before>from getpass import getuser
from json import dumps
from socket import getfqdn, socket, AF_INET, SOCK_STREAM
from argparse import ArgumentDefaultsHelpFormatter
from os import name as operating_system_name, umask, makedirs, chdir
from os.path import expanduser, exists, join, abspath, dirname
from sys import exit as system_exit, argv, platform
from time import sleep
from http.server import SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn, TCPServer
from shutil import copyfile, copyfileobj, rmtree, which
from threading import Thread
from urllib.request import urlopen
from tarfile import open as open_tar_file
from sqlite3 import connect as sequel_lite_connect
from string import ascii_uppercase, ascii_lowercase, digits
from random import choice

import virtual_box_tools
from virtual_box_tools.scan_code import ScanCode
from virtual_box_tools.command_process import CommandProcess, CommandFailed
from virtual_box_tools.custom_argument_parser import CustomArgumentParser
from virtual_box_tools.yaml_config import YamlConfig

if 'nt' == operating_system_name:
    import virtual_box_tools.windows_password_database as pwd
else:
    import pwd


class ThreadedTCPServer(ThreadingMixIn, TCPServer):
    logging = False


class CustomLoggingHandler(SimpleHTTPRequestHandler):
    def log_message(self, log_format, *args) -> None:
        if self.server.logging:
            SimpleHTTPRequestHandler.log_message(
                self, log_format,
                *args
            )


class VirtualBoxTools:
    DEFAULT_CORES = 1
    DEFAULT_MEMORY = 2048
    DEFAULT_DISK_SIZE = 16
    STRETCH_RELEASE = 'stretch'
    BUSTER_RELEASE = 'buster'
    HOST_COMMAND = 'host'
    START_COMMAND = 'start'
    STOP_COMMAND = 'stop'
    SHOW_COMMAND = 'show'
    CREATE_COMMAND = 'create'
    LIST_COMMAND = 'list'
    POWER_OFF_STATE = 'poweroff'

    def __init__(self, arguments: list) -> None:
        config = YamlConfig('~/.virtual-box-tools.yaml')
        self.sudo_user = config.get('sudo_user')
        self.bridge_interface = config.get('bridge_interface')
        self.parser = self.create_parser()
        self.parsed_arguments = self.parser.parse_args(arguments)

    @staticmethod
    def main() -> int:
        return VirtualBoxTools(argv[1:]).run()

    def run(self) -> int:
        arguments = self.parsed_arguments

        if self.HOST_COMMAND in arguments:
            commands = Commands(self.sudo_user)

            if self.LIST_COMMAND in arguments:
                print(
                    dumps(
                        commands.list_hosts(list_all=arguments.all)
                    )
                )
            elif self.CREATE_COMMAND in arguments:
                try:
                    commands.create_host(
                        host_name=arguments.name,
                        cores=arguments.cores,
                        memory=arguments.memory,
                        disk_size=int(arguments.disk_size),
                        bridge_interface=arguments.bridge_interface,
                        graphical=arguments.graphical,
                        no_post_install=arguments.no_post_install,
                        proxy=arguments.proxy,
                        release=arguments.release,
                        user_name=arguments.user_name,
                        real_name=arguments.real_name,
                    )
                except CommandFailed as exception:
                    print(exception)
            elif 'destroy' in arguments:
                try:
                    commands.destroy_host(name=arguments.name)
                except CommandFailed as exception:
                    print(exception)
            elif self.START_COMMAND in arguments:
                try:
                    commands.start_host(
                        name=arguments.name,
                        graphical=arguments.graphical,
                        wait=arguments.wait,
                    )
                except CommandFailed as exception:
                    print(exception)
            elif self.STOP_COMMAND in arguments:
                try:
                    commands.stop_host(
                        name=arguments.name,
                        force=arguments.force,
                        wait=arguments.wait,
                    )
                except CommandFailed as exception:
                    print(exception)
            elif self.SHOW_COMMAND in arguments:
                try:
                    print(commands.show_host(arguments.name))
                except CommandFailed as exception:
                    if 'Could not find a registered machine named' \
                            in exception.get_standard_error():
                        print('Host not found.')
                    else:
                        print(exception)
            else:
                self.parser.print_help()
        else:
            self.parser.print_help()

        return 0

    def create_parser(self) -> CustomArgumentParser:
        parser = CustomArgumentParser(
            description='Wrapper for VirtualBox to simplify operations',
            formatter_class=ArgumentDefaultsHelpFormatter
        )
        subparsers = parser.add_subparsers()
        self.add_host_parser(subparsers)

        return parser

    def add_host_parser(self, subparsers) -> None:
        host_parent = CustomArgumentParser(add_help=False)
        host_parser = subparsers.add_parser(
            self.HOST_COMMAND,
            parents=[host_parent],
            help='manage hosts',
        )
        host_parser.add_argument(self.HOST_COMMAND, action='store_true')
        host_subparsers = host_parser.add_subparsers()

        create_parent = CustomArgumentParser(add_help=False)
        create_parent.add_argument('--name', required=True)
        create_parent.add_argument(
            '--bridge-interface',
            default=self.bridge_interface,
        )
        create_parent.add_argument(
            '--cores',
            default=VirtualBoxTools.DEFAULT_CORES,
        )
        create_parent.add_argument(
            '--memory',
            default=VirtualBoxTools.DEFAULT_MEMORY,
        )
        create_parent.add_argument(
            '--disk-size',
            default=VirtualBoxTools.DEFAULT_DISK_SIZE,
        )
        create_parent.add_argument('--graphical', action='store_true')
        create_parent.add_argument('--no-post-install', action='store_true')
        create_parent.add_argument('--proxy', default='')
        create_parent.add_argument(
            '--release',
            default=VirtualBoxTools.BUSTER_RELEASE,
            choices=[
                VirtualBoxTools.BUSTER_RELEASE,
                VirtualBoxTools.STRETCH_RELEASE,
            ],
        )
        create_parent.add_argument('--user-name', default='')
        create_parent.add_argument('--real-name', default='')
        create_parser = host_subparsers.add_parser(
            self.CREATE_COMMAND,
            parents=[create_parent],<commit_msg>add --show-after-install argument to start, show addresses and stop again after create<commit_after>from getpass import getuser
from json import dumps
from socket import getfqdn, socket, AF_INET, SOCK_STREAM
from argparse import ArgumentDefaultsHelpFormatter
from os import name as operating_system_name, umask, makedirs, chdir
from os.path import expanduser, exists, join, abspath, dirname
from sys import exit as system_exit, argv, platform
from time import sleep
from http.server import SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn, TCPServer
from shutil import copyfile, copyfileobj, rmtree, which
from threading import Thread
from urllib.request import urlopen
from tarfile import open as open_tar_file
from sqlite3 import connect as sequel_lite_connect
from string import ascii_uppercase, ascii_lowercase, digits
from random import choice

import virtual_box_tools
from virtual_box_tools.scan_code import ScanCode
from virtual_box_tools.command_process import CommandProcess, CommandFailed
from virtual_box_tools.custom_argument_parser import CustomArgumentParser
from virtual_box_tools.yaml_config import YamlConfig

if 'nt' == operating_system_name:
    import virtual_box_tools.windows_password_database as pwd
else:
    import pwd


class ThreadedTCPServer(ThreadingMixIn, TCPServer):
    logging = False


class CustomLoggingHandler(SimpleHTTPRequestHandler):
    def log_message(self, log_format, *args) -> None:
        if self.server.logging:
            SimpleHTTPRequestHandler.log_message(
                self, log_format,
                *args
            )


class VirtualBoxTools:
    DEFAULT_CORES = 1
    DEFAULT_MEMORY = 2048
    DEFAULT_DISK_SIZE = 16
    STRETCH_RELEASE = 'stretch'
    BUSTER_RELEASE = 'buster'
    HOST_COMMAND = 'host'
    START_COMMAND = 'start'
    STOP_COMMAND = 'stop'
    SHOW_COMMAND = 'show'
    CREATE_COMMAND = 'create'
    LIST_COMMAND = 'list'
    POWER_OFF_STATE = 'poweroff'

    def __init__(self, arguments: list) -> None:
        config = YamlConfig('~/.virtual-box-tools.yaml')
        self.sudo_user = config.get('sudo_user')
        self.bridge_interface = config.get('bridge_interface')
        self.parser = self.create_parser()
        self.parsed_arguments = self.parser.parse_args(arguments)

    @staticmethod
    def main() -> int:
        return VirtualBoxTools(argv[1:]).run()

    def run(self) -> int:
        arguments = self.parsed_arguments

        if self.HOST_COMMAND in arguments:
            commands = Commands(self.sudo_user)

            if self.LIST_COMMAND in arguments:
                print(
                    dumps(
                        commands.list_hosts(list_all=arguments.all)
                    )
                )
            elif self.CREATE_COMMAND in arguments:
                try:
                    commands.create_host(
                        host_name=arguments.name,
                        cores=arguments.cores,
                        memory=arguments.memory,
                        disk_size=int(arguments.disk_size),
                        bridge_interface=arguments.bridge_interface,
                        graphical=arguments.graphical,
                        no_post_install=arguments.no_post_install,
                        proxy=arguments.proxy,
                        release=arguments.release,
                        user_name=arguments.user_name,
                        real_name=arguments.real_name,
                    )

                    if arguments.show_after_install:
                        commands.start_host(
                            name=arguments.name,
                            graphical=arguments.graphical,
                            wait=True,
                        )
                        print(commands.show_host(arguments.name))
                        commands.stop_host(
                            name=arguments.name,
                            force=False,
                            wait=True,
                        )
                except CommandFailed as exception:
                    print(exception)
            elif 'destroy' in arguments:
                try:
                    commands.destroy_host(name=arguments.name)
                except CommandFailed as exception:
                    print(exception)
            elif self.START_COMMAND in arguments:
                try:
                    commands.start_host(
                        name=arguments.name,
                        graphical=arguments.graphical,
                        wait=arguments.wait,
                    )
                except CommandFailed as exception:
                    print(exception)
            elif self.STOP_COMMAND in arguments:
                try:
                    commands.stop_host(
                        name=arguments.name,
                        force=arguments.force,
                        wait=arguments.wait,
                    )
                except CommandFailed as exception:
                    print(exception)
            elif self.SHOW_COMMAND in arguments:
                try:
                    print(commands.show_host(arguments.name))
                except CommandFailed as exception:
                    if 'Could not find a registered machine named' \
                            in exception.get_standard_error():
                        print('Host not found.')
                    else:
                        print(exception)
            else:
                self.parser.print_help()
        else:
            self.parser.print_help()

        return 0

    def create_parser(self) -> CustomArgumentParser:
        parser = CustomArgumentParser(
            description='Wrapper for VirtualBox to simplify operations',
            formatter_class=ArgumentDefaultsHelpFormatter
        )
        subparsers = parser.add_subparsers()
        self.add_host_parser(subparsers)

        return parser

    def add_host_parser(self, subparsers) -> None:
        host_parent = CustomArgumentParser(add_help=False)
        host_parser = subparsers.add_parser(
            self.HOST_COMMAND,
            parents=[host_parent],
            help='manage hosts',
        )
        host_parser.add_argument(self.HOST_COMMAND, action='store_true')
        host_subparsers = host_parser.add_subparsers()

        create_parent = CustomArgumentParser(add_help=False)
        create_parent.add_argument('--name', required=True)
        create_parent.add_argument(
            '--bridge-interface',
            default=self.bridge_interface,
        )
        create_parent.add_argument(
            '--cores',
            default=VirtualBoxTools.DEFAULT_CORES,
        )
        create_parent.add_argument(
            '--memory',
            default=VirtualBoxTools.DEFAULT_MEMORY,
        )
        create_parent.add_argument(
            '--disk-size',
            default=VirtualBoxTools.DEFAULT_DISK_SIZE,
        )
        create_parent.add_argument('--graphical', action='store_true')
        create_parent.add_argument('--no-post-install', action='store_true')
        create_parent.add_argument('--show-after-install', action='store_true')
        create_parent.add_argument('--proxy', default='')
        create_parent.add_argument(
            '--release',
            default=VirtualBoxTools.BUSTER_RELEASE,
            choices=[
                VirtualBoxTools.BUSTER_RELEASE,
                VirtualBoxTools.STRETCH_RELEASE,
            ],
        )
        create_parent.add_argument('--user-name', default='')
        create_parent.add_argument('--real-name', default='')
        create_parser = host_subparsers.add_parser(
            self.CREATE_COMMAND,
            parents=[create_parent],
{'fraction_non_alphanumeric': 0.0710711387611577, 'fraction_numerical': 0.001352447930754666, 'mean_word_length': 1.8423986161829713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1199128', 'n_tokens_mistral': 3803, 'n_tokens_neox': 3631, 'n_words': 757}