<commit_before>from computer import login_server
import os.path

credentials = {'username': 'jongkai', 'domain': 'dmas.berkeley.edu'}
roots = {'data': '~/data', 'src': '~/src'}
codes = {'climate-commander': 'https://github.com/GitOnion/climate-commander.git'}

server = login_server.LoginServer(('berkeley', 'shackleton'), 20, roots, credentials)
server.connect()


def get_directory_content(target, flag=""):
    if flag:
        flag = "-" + flag + " "
    stdout, stderr = server.run_command("ls " + flag + target)
    if stderr:
        raise SystemExit("Cannot get content from directory %s:\n %s" % (target, stderr))
    return stdout


def change_directory(target):
    stdout, stderr = server.run_command("cd " + target)
    if stderr:
        raise SystemExit("Cannot change to directory %s:\n %s" % (target, stderr))
    print("Current Directory: " + server.run_command('pwd')[0])


def clone_codebase(codebase):
    stdout, stderr = server.run_command("git clone " + codes[codebase])
    if stderr:
        raise SystemExit("Cannot clone %s:\n %s" % (codebase, stderr))
    print(stdout)


def clean_codebase(codebase):
    stdout, stderr = server.run_command("rm -rf " + codebase)
    if stderr:
        raise SystemExit("Cannot remove %s:\n %s" % (codebase, stderr))
    print('removed directory: %s' % codebase)
    clone_codebase(codebase)


def update_codebase(codebase):
    stdout, stderr = server.run_command("git pull")
    if stderr:
        raise SystemExit("Cannot update %s by git pull: \n %s" % (codebase, stderr))
    print(stdout)

# Mission of this code:
# Check if target codebase is in the '~/src' directory
# if the codebase isn't there, clone it,
# if it is, git pull (update it).
# if git pull errs: remove the whole directory then clone a new one.

content_src = get_directory_content(roots['src'], "al")
for codebase in codes.keys():
    if codebase not in content_src:
        change_directory(roots['src'])
        clone_codebase(codebase)
        change_directory('-')
    else:
        change_directory(os.path.join(roots['src'], codebase))
        update_codebase(codebase)
        change_directory('-')

# TODO: Test on Data
# TODO:
# What are the expected workflows using Github?
# What are some potential failure of Github?
<commit_msg>intermediary test<commit_after>from computer import login_server
import os.path

credentials = {'username': 'jongkai', 'domain': 'dmas.berkeley.edu'}
roots = {'data': '~/data', 'src': '~/src'}
codes = {'climate-commander': 'https://github.com/GitOnion/climate-commander.git'}

server = login_server.LoginServer(('berkeley', 'shackleton'), 20, roots, credentials)
server.connect()


def get_directory_content(target, flag=""):
    if flag:
        flag = "-" + flag + " "
    stdout, stderr = server.run_command("ls " + flag + target)
    if stderr:
        raise SystemExit("Cannot get content from directory %s:\n %s" % (target, stderr))
    return stdout


def change_directory(target):
    stdout, stderr = server.run_command("cd " + target)
    if stderr:
        raise SystemExit("Cannot change to directory %s:\n %s" % (target, stderr))
    print("Current Directory: " + server.run_command('pwd')[0])


def clone_codebase(codebase):
    stdout, stderr = server.run_command("git clone " + codes[codebase])
    if stderr:
        raise SystemExit("Cannot clone %s:\n %s" % (codebase, stderr))
    print(stdout)


def clean_codebase(codebase):
    stdout, stderr = server.run_command("rm -rf " + codebase)
    if stderr:
        raise SystemExit("Cannot remove %s:\n %s" % (codebase, stderr))
    print('removed directory: %s' % codebase)


def update_codebase(codebase):
    stdout, stderr = server.run_command("git pull")
    if stderr:
        raise SystemExit("Cannot update %s by git pull: \n %s" % (codebase, stderr))
    if stdout:
        clean_codebase(codebase)
        clone_codebase(codebase)


# Mission of this code:
# Check if target codebase is in the '~/src' directory
# if the codebase isn't there, clone it,
# if it is, git pull (update it).
# if git pull errs: remove the whole directory then clone a new one.

content_src = get_directory_content(roots['src'], "al")
for codebase in codes.keys():
    if codebase not in content_src:
        change_directory(roots['src'])
        clone_codebase(codebase)
        change_directory('-')
    else:
        change_directory(os.path.join(roots['src'], codebase))
        update_codebase(codebase)
        change_directory('-')

# TODO: Test on Data
# TODO:
# What are the expected workflows using Github?
# What are some potential failure of Github?

{'fraction_non_alphanumeric': 0.13421796824015664, 'fraction_numerical': 0.0013051990428540352, 'mean_word_length': 3.7647668393782383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1236801', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1445, 'n_words': 421}