import pysftp
import subprocess

hostname = " "
sourceDir = "public/"
sourceTar = "hugo_personal.tar.gz"
gator_user = " "

password = input("Enter UF Password: ")


def runBash(bashCommand):
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()
    return output, error
# chmod -R 755 public/ directory
out, _ = runBash(f"chmod -R 755 {sourceDir}")
print(out)
# Zip public/ directory
out, _ = runBash(f"tar -zcvf {sourceTar} {sourceDir}")
print(out)

# sftp put file into uf server
with pysftp.Connection(
        host=hostname,
        username=gator_user,
        password=password) as sftp:
    sftp.put(sourceTar)

# ssh into uf server
    # unzip archive
    # delete old public_html/
    # rename unzipped public/ to public_html/
commands = [f"tar -zxvf {sourceTar} {sourceDir}",
            "rm -R public_html/",
            "mv public/ public_html"]
out, _ = subprocess.Popen(f"ssh {gator_user}@{hostname} \"{' && '.join(commands)}\"",
                          shell=True, stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE).communicate()
print(out)
