<commit_before>#!/usr/bin/env python

import argparse
import datetime
import os
import socket
import subprocess
import sys

import yaml

def main():
    parser = argparse.ArgumentParser(
        description="Send and receive files through the sneakernet")
    subparsers = parser.add_subparsers(help="Sub-commands help")
    
    send_parser = subparsers.add_parser(
        "send", help="Send data on the sneakernet",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    send_parser.add_argument("destination")
    send_parser.set_defaults(function=send)
    
    receive_parser = subparsers.add_parser(
        "receive", help="Receive data from the sneakernet",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    receive_parser.add_argument("source")
    receive_parser.set_defaults(function=receive)
    
    arguments = vars(parser.parse_args())
    function = arguments.pop("function")
    function(**arguments)
    
    return 0

def send(destination):
    sync_data = read_sync_data(os.path.join(destination, "sneakersync.dat"))
    if sync_data["previous_direction"] == "send":
        confirmed = confirm(
            "WARNING: "
            "do you want to re-send the current files "
            "(sent from {} on {})?".format(
                sync_data["previous_host"], 
                sync_data["previous_date"].strftime("%c")))
        if not confirmed:
            return 0
    
    configuration = read_configuration(
        os.path.join(destination, "sneakersync.cfg"))
    
    for module in configuration["modules"]:
        # WARNING: make sure there is no "/" at the end of the module
        module["root"] = module["root"].rstrip("/")
        
        command = [
            "rsync",
            "--archive", "--xattrs", "--delete", "--relative",
            "--progress", "--stats"
        ]
        command += get_filters(configuration["filters"])
        command += get_filters(module["filters"])
        command += [
            os.path.join("/." + module["root"], ""), 
            os.path.join(destination, "")
        ]
        subprocess.check_call(command)
    
    sync_data["previous_direction"] = "send"
    sync_data["previous_date"] = datetime.datetime.now()
    sync_data["previous_host"] = socket.gethostname()
    write_sync_data(sync_data, os.path.join(destination, "sneakersync.dat"))

def receive(source, configuration_path, sync_data_path):
    sync_data = read_sync_data(sync_data_path)
    if sync_data["previous_direction"] == "receive":
        confirmed = confirm(
            "WARNING: "
            "do you want to receive the current files again "
            "(sent from {} on {})?".format(
                sync_data["previous_host"], 
                sync_data["previous_date"].strftime("%c")))
        if not confirmed:
            return 0
        confirmed = confirm(
            "This will overwrite your data. Are you really sure?")
        if not confirmed:
            return 0
    
    configuration = read_configuration(configuration_path)
    
    for module in configuration["modules"]:
        # WARNING: make sure there is no "/" at the end of the module
        module = module.rstrip("/")
        
        command = [
            "rsync",
            "--archive", "--xattrs", "--delete", "--progress", "--stats",
            os.path.join(source, module.lstrip("/"), ""),
            os.path.join(module, "")
        ]
        print " ".join(command)
        
    sync_data["previous_direction"] = "receive"
    write_sync_data(sync_data, sync_data_path)

def read_configuration(path):
    configuration = {
        "modules": [],<commit_msg>Adapt receive to send API<commit_after>#!/usr/bin/env python

import argparse
import datetime
import os
import socket
import subprocess
import sys

import yaml

def main():
    parser = argparse.ArgumentParser(
        description="Send and receive files through the sneakernet")
    subparsers = parser.add_subparsers(help="Sub-commands help")
    
    send_parser = subparsers.add_parser(
        "send", help="Send data on the sneakernet",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    send_parser.add_argument("destination")
    send_parser.set_defaults(function=send)
    
    receive_parser = subparsers.add_parser(
        "receive", help="Receive data from the sneakernet",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    receive_parser.add_argument("source")
    receive_parser.set_defaults(function=receive)
    
    arguments = vars(parser.parse_args())
    function = arguments.pop("function")
    function(**arguments)
    
    return 0

def send(destination):
    sync_data = read_sync_data(os.path.join(destination, "sneakersync.dat"))
    if sync_data["previous_direction"] == "send":
        confirmed = confirm(
            "WARNING: "
            "do you want to re-send the current files "
            "(sent from {} on {})?".format(
                sync_data["previous_host"], 
                sync_data["previous_date"].strftime("%c")))
        if not confirmed:
            return 0
    
    configuration = read_configuration(
        os.path.join(destination, "sneakersync.cfg"))
    
    for module in configuration["modules"]:
        # WARNING: make sure there is no "/" at the end of the module
        module["root"] = module["root"].rstrip("/")
        
        command = [
            "rsync",
            "--archive", "--xattrs", "--delete", "--relative",
            "--progress", "--stats"
        ]
        command += get_filters(configuration["filters"])
        command += get_filters(module["filters"])
        command += [
            os.path.join("/." + module["root"], ""), 
            os.path.join(destination, "")
        ]
        subprocess.check_call(command)
    
    sync_data["previous_direction"] = "send"
    sync_data["previous_date"] = datetime.datetime.now()
    sync_data["previous_host"] = socket.gethostname()
    write_sync_data(sync_data, os.path.join(destination, "sneakersync.dat"))

def receive(source):
    sync_data = read_sync_data(os.path.join(source, "sneakersync.dat"))
    if sync_data["previous_direction"] == "receive":
        confirmed = confirm(
            "WARNING: "
            "do you want to receive the current files again "
            "(sent from {} on {})?".format(
                sync_data["previous_host"], 
                sync_data["previous_date"].strftime("%c")))
        if not confirmed:
            return 0
        confirmed = confirm(
            "This will overwrite your data. Are you really sure?")
        if not confirmed:
            return 0
    
    configuration = read_configuration(os.path.join(source, "sneakersync.cfg"))
    
    for module in configuration["modules"]:
        # WARNING: make sure there is no "/" at the end of the module
        module["root"] = module["root"].rstrip("/")
        
        command = [
            "rsync",
            "--archive", "--xattrs", "--delete", 
            "--progress", "--stats"
        ]
        command += get_filters(configuration["filters"])
        command += get_filters(module["filters"])
        command += [
            os.path.join(source, module["root"].lstrip("/"), ""),
            os.path.join(module["root"], "")
        ]
        subprocess.check_call(command)
        
    sync_data["previous_direction"] = "receive"
    write_sync_data(sync_data, os.path.join(source, "sneakersync.dat"))

def read_configuration(path):
    configuration = {
        "modules": [],
{'fraction_non_alphanumeric': 0.12921646746347942, 'fraction_numerical': 0.0010624169986719787, 'mean_word_length': 2.4801293900184844, 'pattern_counts': {'":': 6, '<': 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': '5736009', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2056, 'n_words': 459}