import os
from subprocess import Popen, PIPE

from son_editor.app.exceptions import PackException, ExtNotReachable, NameConflict
from son_editor.models.project import Project
from son_editor.models.repository import Platform


def pack_project(project: Project) -> str:
    ws_path = project.workspace.path
    pj_path = os.path.join(ws_path, 'projects', project.rel_path)
    proc = Popen(['son-package', '--workspace', ws_path, '--project', pj_path], stdout=PIPE, stderr=PIPE)

    out, err = proc.communicate()
    out = out.decode()
    err = err.decode()

    exitcode = proc.returncode
    if exitcode == 0:
        for line in err.splitlines():
            if line.find("File") >= 0:
                file_name = line[line.lower().find("file"):].split(" ", 1)[1]
                return file_name
    else:
        for line in err.splitlines():
            if line.find("ERROR") >= 0:
                error_message = line[line.find("ERROR"):]
                raise PackException(error_message)
    raise PackException(err)


def push_to_platform(package_path: str, platform: Platform) -> str:
    proc = Popen(['son-push', platform.url, '-U', package_path], stdout=PIPE, stderr=PIPE)

    out, err = proc.communicate()
    out = out.decode()
    err = err.decode()

    print(out)
    print(err)

    exitcode = proc.returncode  # as of now exitcode is 0 even if there is an error
    if "ConnectionError" in out:
        raise ExtNotReachable("Could not connect to platform.")
    elif "error" in out.lower():
        raise NameConflict(out)
    elif "201" or "200" in out:
        message = out.split(":", 1)[1]
        return message
    else:
        return out

@@ -1,12 +1,27 @@
+import logging%0A
 import os%0Afr
@@ -234,16 +234,54 @@
 atform%0A%0A
+logger = logging.getLogger(__name__)%0A%0A
 %0Adef pac
@@ -1325,29 +1325,61 @@
     
-print(out)%0A    print(
+logger.info(%22Out:%22 + out)%0A    logger.info(%22Error:%22 + 
 err)
@@ -1487,32 +1487,39 @@
 ionError%22 in out
+ or err
 :%0A        raise 
@@ -1599,16 +1599,31 @@
 .lower()
+ or err.lower()
 :%0A      
@@ -1643,19 +1643,47 @@
 onflict(
-out
+%22Out: %22 + out + %22Error: %22 + err
 )%0A    el

fix output parsing