# AUTOGENERATED! DO NOT EDIT! File to edit: 10_requirements.ipynb (unless otherwise specified).

__all__ = ['lr', 'out', 'out', 'table', 'table', 'upgrades', 'outputs']

# Cell

import subprocess
from os import name

# Windows handles line-returns differently.
lr = "\n"
if name == "nt":
    lr = "\r\n"

# List currenly installed pip packages.
out = subprocess.Popen(
    ["pip", "freeze"],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
)
stdout, stderr = out.communicate()

# Write a requirements.txt file as a "before" upgrades.
with open("requirements.txt", "w") as fh:
    decoded_out = stdout.decode("utf8")
    if name == "nt":
        decoded_out.replace("\r\n", "\n")
    fh.write(decoded_out)

# Cell

# Upgrade all pip packages

out = subprocess.Popen(
    ["pip", "list", "-o"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
stdout, stderr = out.communicate()
table = []

for item in stdout.decode("utf8").split(lr):
    table.append(item.split())
table = table[2:-1]
upgrades = [x[0] for x in table]

outputs = []
for item in upgrades:
    out = subprocess.Popen(
        ["pip", "install", "-U", item], stdout=subprocess.PIPE, stderr=subprocess.STDOUT
    )
    stdout, stderr = out.communicate()
    row = (item, stdout, stderr)
    outputs.append(row)
    print(item)
    print()
    print("stdout: ", stdout)
    print()
    print("stderr: ", stderr)
    print()