import os
import shlex
import logging
import subprocess
import contextlib

@contextlib.contextmanager
def cd(newdir):
    prevdir = os.getcwd()
    os.chdir(os.path.expanduser(newdir))
    try:
        yield
    finally:
        os.chdir(prevdir)

def execute(cmd):
    args = shlex.split(cmd)
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    out = p.communicate()

    for line in out:
        if not line: continue
        logging.info(line.rstrip())
    if p.returncode != 0:
        logging.error("{} failed with {}".format(cmd, p.returncode))

def git_update(dotmodule, dst, plugins):
    for plugin in plugins:
        if "\.git" in plugin:
            name = os.path.basename(plugin).rpartition(".git")[0] # replace trailing ".git"
        else:
            name = os.path.basename(plugin)
        path = "{}{}".format(dst, name)

        if os.path.exists(path):
            logging.debug("updating {} plugin {}".format(dotmodule, name))
            with cd(path):
                execute("git pull")
        else:
            logging.debug("cloning {} plugin {}".format(dotmodule, name))
            os.makedirs(dst)
            with cd(dst):
                execute("git clone --recursive {}".format(plugin))

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

import os
import shlex
import logging
import subprocess
import contextlib

@contextlib.contextmanager
def cd(newdir):
    prevdir = os.getcwd()
    os.chdir(os.path.expanduser(newdir))
    try:
        yield
    finally:
        os.chdir(prevdir)

def execute(cmd):
    args = shlex.split(cmd)
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    out = p.communicate()

    for line in out:
        if not line: continue
        logging.info(line.rstrip())
    if p.returncode != 0:
        logging.error("{} failed with {}".format(cmd, p.returncode))

def git_update(dotmodule, dst, plugins):
    for plugin in plugins:
        if ".git" in plugin:
            name = os.path.basename(plugin).rpartition(".git")[0] # replace trailing ".git"
        else:
            name = os.path.basename(plugin)
        path = "{}{}".format(dst, name)

        print("path: {} ({})".format(path, name))
        if os.path.exists(path):
            logging.debug("updating {} plugin {}".format(dotmodule, name))
            with cd(path):
                execute("git pull")
        else:
            logging.debug("cloning {} plugin {}".format(dotmodule, name))
            try:
                os.makedirs(os.path.split(dst)[0])
            except FileExistsError:
                pass
            with cd(dst):
                execute("git clone --recursive {}".format(plugin))

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

Fix path creation/git detection bugs
[libdotify] Fix path creation/git detection bugs
