<commit_before>import argparse
import contextlib
import inspect
import os
import sys
import re

import shutil
import tempfile
import threading

from os.path import abspath, basename, dirname, exists, join, splitext
from pathlib import PurePosixPath

from robotpy_installer import sshcontroller

from ..util import print_err, yesno

import wpilib

import logging

logger = logging.getLogger("wipe")


class PyFrcUndeploy:
    def run(self, options, robot_class, **static_options):
        from .. import config

        config.mode = "undeploy"

        robot_file = abspath(inspect.getfile(robot_class))
        robot_path = dirname(robot_file)
        robot_filename = basename(robot_file)
        cfg_filename = join(robot_path, ".deploy_cfg")

        if not options.nonstandard and robot_filename != "robot.py":
            print_err(
                "ERROR: Your robot code must be in a file called robot.py (launched from %s)!"
                % robot_filename
            )
            print_err()
            print_err(
                "If you really want to do this, then specify the --nonstandard argument"
            )
            return 1

        hostname_or_team = options.robot
        if not hostname_or_team and options.team:
            hostname_or_team = options.team

        try:
            with sshcontroller.ssh_from_cfg(
                cfg_filename,
                username="lvuser",
                password="",
                hostname=hostname_or_team,
                no_resolve=options.no_resolve,
            ) as ssh:

                self._delete_dir(ssh)

        except sshcontroller.SshExecError as e:
            print_err("ERROR:", str(e))
            return 1

        print("SUCCESS: Files have been successfully wiped!")
        return 0

    def _delete_dir(
        self, ssh: sshcontroller.SshController, robot_path: str, robot_filename: str
    ):
        wipe_dir = PurePosixPath("/home/lvuser")
        py_wipe_subdir = "py"
        py_wipe_dir = wipe_dir / py_wipe_subdir

        sshcmd = f"rm -rf {py_wipe_dir}"

        logger.debug("SSH: %s", sshcmd)

        with wrap_ssh_error("erasing robot code"):
            ssh.exec_cmd(sshcmd, check=True, print_output=True)


@contextlib.contextmanager
def wrap_ssh_error(msg: str):
    try:
        yield
    except sshcontroller.SshExecError as e:
        raise sshcontroller.SshExecError(f"{msg}: {str(e)}") from e<commit_msg>Correct and simplify the undeploy command<commit_after>import argparse
import inspect

from os.path import abspath, dirname, join

from robotpy_installer import sshcontroller

from ..util import print_err, yesno


class PyFrcUndeploy:
    """
    Removes current robot code from a RoboRIO
    """

    def __init__(self, parser: argparse.ArgumentParser):

        robot_args = parser.add_mutually_exclusive_group()

        robot_args.add_argument(
            "--robot", default=None, help="Set hostname or IP address of robot"
        )

        robot_args.add_argument(
            "--team", default=None, type=int, help="Set team number to deploy robot for"
        )

        parser.add_argument(
            "--no-resolve",
            action="store_true",
            default=False,
            help="If specified, don't do a DNS lookup, allow ssh et al to do it instead",
        )

    def run(self, options, robot_class, **static_options):
        from .. import config

        config.mode = "undeploy"

        robot_file = abspath(inspect.getfile(robot_class))
        robot_path = dirname(robot_file)
        cfg_filename = join(robot_path, ".deploy_cfg")

            return 1

        hostname_or_team = options.robot
        if not hostname_or_team and options.team:
            hostname_or_team = options.team

        try:
            with sshcontroller.ssh_from_cfg(
                cfg_filename,
                username="lvuser",
                password="",
                hostname=hostname_or_team,
                no_resolve=options.no_resolve,
            ) as ssh:

                # delete the code
                ssh.exec_cmd("rm -rf /home/lvuser/py")

        except sshcontroller.SshExecError as e:
            print_err("ERROR:", str(e))
            return 1

        print("SUCCESS: Files have been successfully wiped!")
        return 0
{'fraction_non_alphanumeric': 0.08118167015585019, 'fraction_numerical': 0.0013956734124214933, 'mean_word_length': 2.1455742501828823, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, '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': '4909650', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1221, 'n_words': 324}