# Path: dgitcore/config.py
# def get_config():
#     if config is None:
#         init()
#     return config
# 
# Path: dgitcore/plugins/common.py
# def plugins_get_mgr():
#     """
#     Get the global plugin manager
#     """
#     global pluginmgr
#     return pluginmgr
# 
# Path: dgitcore/helper.py
# class bcolors:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     BOLD = '\033[1m'
#     UNDERLINE = '\033[4m'
# 
# def clean_str(s):
# 
#     for c in ['/', '\\','.',' ']:
#         s = s.replace(c,'_')
#     return s
# 
# class cd:
#     """Context manager for changing the current working directory"""
#     def __init__(self, newPath):
#         self.newPath = os.path.expanduser(newPath)
# 
#     def __enter__(self):
#         self.savedPath = os.getcwd()
#         os.chdir(self.newPath)
# 
#     def __exit__(self, etype, value, traceback):
#         os.chdir(self.savedPath)
# 
# def compute_sha256(filename):
#     """
#     Try the library. If it doesnt work, use the command line..
#     """
#     try:
#         h = sha256()
#         fd = open(filename, 'rb')
#         while True:
#             buf = fd.read(0x1000000)
#             if buf in [None, ""]:
#                 break
#             h.update(buf.encode('utf-8'))
#         fd.close()
#         return h.hexdigest()
#     except:
#         output = run(["sha256sum", "-b", filename])
#         return output.split(" ")[0]
# 
# def run(cmd):
#     """
#     Run a shell command
#     """
#     cmd = [pipes.quote(c) for c in cmd]
#     cmd = " ".join(cmd)
#     cmd += "; exit 0"
#     # print("Running {} in {}".format(cmd, os.getcwd()))
#     try:
#         output = subprocess.check_output(cmd,
#                                          stderr=subprocess.STDOUT,
#                                          shell=True)
#     except subprocess.CalledProcessError as e:
#             output = e.output
# 
#     output = output.decode('utf-8')
#     output = output.strip()
#     return output
# 
# def clean_name(n):
#     n = "".join([x if (x.isalnum() or x == "-") else "_" for x in n])
#     return n


import os, sys, copy, fnmatch, re, shutil
import yaml, json, tempfile
import webbrowser
import subprocess, string, random, pipes
import getpass
import mimetypes
import platform
import uuid, shutil
from collections import OrderedDict
from datetime import datetime
from hashlib import sha256
from dateutil import parser
from ..config import get_config
from ..plugins.common import plugins_get_mgr
from ..helper import bcolors, clean_str, cd, compute_sha256, run, clean_name
    print(json.dumps(result, indent=4))
    return result

def find_executable_commitpath(repo, args):

    print("Finding executable commit path", args)
    # Find the first argument that is a file and is part of a repo
    for f in args:
        if os.path.exists(f):

            # Get full path (to get username)
            f = os.path.realpath(f)

            # Try getting the permalink
            (relpath, permalink) = repo.manager.permalink(repo, f)
            if permalink is not None:
                return (relpath, permalink)

            # Check if this part of system bin directories
            if os.environ['HOME'] in f:
                return (f, None)

    return (None, None)

def run_executable(repo, args, includes):
    """
    Run the executable and capture the input and output...
    """

    # Get platform information

# Path: dgitcore/config.py
# def get_config():
#     if config is None:
#         init()
#     return config
# 
# Path: dgitcore/plugins/common.py
# def plugins_get_mgr():
#     """
#     Get the global plugin manager
#     """
#     global pluginmgr
#     return pluginmgr
# 
# Path: dgitcore/helper.py
# class bcolors:
#     HEADER = '\033[95m'
#     OKBLUE = '\033[94m'
#     OKGREEN = '\033[92m'
#     WARNING = '\033[93m'
#     FAIL = '\033[91m'
#     ENDC = '\033[0m'
#     BOLD = '\033[1m'
#     UNDERLINE = '\033[4m'
# 
# def clean_str(s):
# 
#     for c in ['/', '\\','.',' ']:
#         s = s.replace(c,'_')
#     return s
# 
# class cd:
#     """Context manager for changing the current working directory"""
#     def __init__(self, newPath):
#         self.newPath = os.path.expanduser(newPath)
# 
#     def __enter__(self):
#         self.savedPath = os.getcwd()
#         os.chdir(self.newPath)
# 
#     def __exit__(self, etype, value, traceback):
#         os.chdir(self.savedPath)
# 
# def compute_sha256(filename):
#     """
#     Try the library. If it doesnt work, use the command line..
#     """
#     try:
#         h = sha256()
#         fd = open(filename, 'rb')
#         while True:
#             buf = fd.read(0x1000000)
#             if buf in [None, ""]:
#                 break
#             h.update(buf.encode('utf-8'))
#         fd.close()
#         return h.hexdigest()
#     except:
#         output = run(["sha256sum", "-b", filename])
#         return output.split(" ")[0]
# 
# def run(cmd):
#     """
#     Run a shell command
#     """
#     cmd = [pipes.quote(c) for c in cmd]
#     cmd = " ".join(cmd)
#     cmd += "; exit 0"
#     # print("Running {} in {}".format(cmd, os.getcwd()))
#     try:
#         output = subprocess.check_output(cmd,
#                                          stderr=subprocess.STDOUT,
#                                          shell=True)
#     except subprocess.CalledProcessError as e:
#             output = e.output
# 
#     output = output.decode('utf-8')
#     output = output.strip()
#     return output
# 
# def clean_name(n):
#     n = "".join([x if (x.isalnum() or x == "-") else "_" for x in n])
#     return n


# Path: dgitcore/datasets/files.py
import os, sys, copy, fnmatch, re, shutil
import yaml, json, tempfile
import webbrowser
import subprocess, string, random, pipes
import getpass
import mimetypes
import platform
import uuid, shutil
from collections import OrderedDict
from datetime import datetime
from hashlib import sha256
from dateutil import parser
from ..config import get_config
from ..plugins.common import plugins_get_mgr
from ..helper import bcolors, clean_str, cd, compute_sha256, run, clean_name

    print(json.dumps(result, indent=4))
    return result

def find_executable_commitpath(repo, args):

    print("Finding executable commit path", args)
    # Find the first argument that is a file and is part of a repo
    for f in args:
        if os.path.exists(f):

            # Get full path (to get username)
            f = os.path.realpath(f)

            # Try getting the permalink
            (relpath, permalink) = repo.manager.permalink(repo, f)
            if permalink is not None:
                return (relpath, permalink)

            # Check if this part of system bin directories
            if os.environ['HOME'] in f:
                return (f, None)

    return (None, None)

def run_executable(repo, args, includes):
    """
    Run the executable and capture the input and output...
    """

    # Get platform information

    mgr = plugins_get_mgr()