<commit_before>import datetime
import os
import stat
import subprocess
import time

from .conf import settings


def get_hdinfo(path,value):
    return subprocess.check_output(["lsblk", path, "--nodeps", "-no", value]).strip()


def erase_disk(dev, erase_mode="0"):
    if erase_mode == "0":
        standard = "EraseBasic"
        zeros = settings.getboolean('eraser', 'ZEROS')
        if zeros == False:
            options = "-vn"
        else:
            options = "-zvn"
    elif erase_mode == "1":
        standard = "EraseBySectors"
        raise NotImplementedError

    time_start = datetime.datetime.now()
    try:
        steps = settings.getint('eraser', 'STEPS')
        subprocess.check_call(["shred", options, str(steps), dev])
        state = "Succeed"
    except subprocess.CalledProcessError:
        state = "Fail"
        print "Cannot erase the hard drive '{0}'".format(dev)
    time_end = datetime.datetime.now()
    
    return {
        '@type': standard,
        'secureAleatorySteps': steps,
        'cleanWithZeros': zeros,
        'state': state,
        'startTime': time_start.isoformat(),
        'endTime': time_end.isoformat()
    }


def do_erasure(sdx):
    if not os.path.exists(sdx):
        raise ValueError("Device '{0}' does not exist.".format(sdx))

    # check if file is a block special device
    if not stat.S_ISBLK(os.stat(sdx).st_mode):
        raise ValueError("File '{0}' is not a block special device.".format(sdx))
    
    print(
        "Selected '{disk}' (model: {model}, size: {size}, type: {connector})".format(
            disk=sdx,
            model=get_hdinfo(sdx, "model"),
            size=get_hdinfo(sdx, "size"),
            connector=get_hdinfo(sdx, "tran")
        )
    )

    erase = settings.get('eraser', 'ERASE')
    if erase == "yes":
        print("Eraser will start in 10 seconds, ALL DATA WILL BE LOST! Press "
              "Ctrl+C to cancel.")
        try:
            time.sleep(10)
        except KeyboardInterrupt:
            print("Eraser on disk '{0}' cancelled by user!".format(sdx))
            return
        return erase_disk(sdx)
    
    elif erase == "ask":
        confirm = raw_input("Do you want to erase \"{0}\"? [y/N] ".format(sdx))
        if confirm.lower().strip() == "y" or confirm.lower().strip() == "yes":
            return erase_disk(sdx)
    
    print("No disk erased.")
<commit_msg>Xavier Review. Data and structure change with more info for every step.<commit_after>import datetime
import os
import stat
import subprocess
import time
import json

from .conf import settings


def get_hdinfo(path,value):
    return subprocess.check_output(["lsblk", path, "--nodeps", "-no", value]).strip()

def erase_process(dev, options, steps):
    # Erasing
    try:
        subprocess.check_call(["shred", options, str(steps), dev])
        state = "Succeed"
    except subprocess.CalledProcessError:
        state = "Fail"
        print "Cannot erase the hard drive '{0}'".format(dev)
    return state

def erase_disk(dev, erase_mode="0"):
    time_start = get_datetime()
    zeros = settings.getboolean('eraser', 'ZEROS')
    steps = settings.getint('eraser', 'STEPS')
    count = steps
    step = []

    if erase_mode == "0":
        standard = "EraseBasic"
        
        # Random
        while count != 0:
            step.append({
                '@type': 'Random',
                'startingTime': get_datetime(),
                'state': erase_process(dev, '-vn', 1),
                'endingTime': get_datetime(),
            })
            count -= 1
            
        # Zeros
        if zeros == True:
            step.append({
                '@type': 'Zeros',
                'startingTime': get_datetime(),
                'state': erase_process(dev, '-zvn', 0),
                'endingTime': get_datetime(),
            })
            
    elif erase_mode == "1":
        standard = "EraseBySectors"
        raise NotImplementedError

    time_end = get_datetime()
    return {
        '@type': standard,
        'secureRandomSteps': steps,
        'cleanWithZeros': zeros,
        'startingTime': time_start,
        'endingTime': time_end,
        'steps': step
    }


def do_erasure(sdx):
    if not os.path.exists(sdx):
        raise ValueError("Device '{0}' does not exist.".format(sdx))

    # check if file is a block special device
    if not stat.S_ISBLK(os.stat(sdx).st_mode):
        raise ValueError("File '{0}' is not a block special device.".format(sdx))
    
    print(
        "Selected '{disk}' (model: {model}, size: {size}, type: {connector})".format(
            disk=sdx,
            model=get_hdinfo(sdx, "model"),
            size=get_hdinfo(sdx, "size"),
            connector=get_hdinfo(sdx, "tran")
        )
    )

    erase = settings.get('eraser', 'ERASE')
    if erase == "yes":
        print("Eraser will start in 10 seconds, ALL DATA WILL BE LOST! Press "
              "Ctrl+C to cancel.")
        try:
            time.sleep(10)
        except KeyboardInterrupt:
            print("Eraser on disk '{0}' cancelled by user!".format(sdx))
            return
        return erase_disk(sdx)
    
    elif erase == "ask":
        confirm = raw_input("Do you want to erase \"{0}\"? [y/N] ".format(sdx))
        if confirm.lower().strip() == "y" or confirm.lower().strip() == "yes":
            return erase_disk(sdx)
    
    print("No disk erased.")

def get_datetime():
    return datetime.datetime.utcnow().replace(microsecond=0).isoformat()

{'fraction_non_alphanumeric': 0.1374157098596683, 'fraction_numerical': 0.005285219609987243, 'mean_word_length': 2.2149970708845927, 'pattern_counts': {'":': 10, '<': 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': '2069782', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1679, 'n_words': 435}