# Path: alibuild_helpers/log.py
# def dieOnError(err, msg):
#   def __init__(self, fmtstr):
#   def format(self, record):
#   def __init__(self, begin_msg=""):
#   def __call__(self, txt, *args):
#   def erase(self):
#   def end(self, msg="", error=False):
# def log_banner(self, message, *args, **kws):
# def log_success(self, message, *args, **kws):
# class LogFormatter(logging.Formatter):
# class ProgressPrint:
# 
# Path: alibuild_helpers/cmd.py
# def execute(command, printer=debug):
#   kwargs = {} if sys.version_info.major < 3 else {"encoding": "utf-8"}
#   popen = Popen(command, shell=is_string(command), stdout=PIPE,
#                 universal_newlines=True, **kwargs)
#   lines_iterator = iter(popen.stdout.readline, "")
#   for line in lines_iterator:
#     if not line: break
#     printer("%s", line.strip("\n"))
#   out = popen.communicate()[0].strip("\n")
#   if out:
#     printer(out)
#   return popen.returncode
# 
# Path: alibuild_helpers/git.py
# def __partialCloneFilter():
# 
# Path: alibuild_helpers/utilities.py
# def format(s, **kwds):
#   if sys.version_info[0] >= 3:
#     if isinstance(s, bytes):
#       try:
#         s = s.decode("utf-8")  # to get newlines as such and not as escaped \n
#       except:
#         s = s.decode("latin-1")  # Workaround issue with some special characters of latin-1 which are not understood by unicode
#     else:
#       s = str(s)
#   elif isinstance(s, str):
#     s = unicode(s, "utf-8")  # utf-8 is a safe assumption
#   elif not isinstance(s, unicode):
#     s = unicode(str(s))
#   return s % kwds


from alibuild_helpers.log import dieOnError, debug
from alibuild_helpers.cmd import execute
from alibuild_helpers.git import partialCloneFilter
from os.path import dirname, abspath
from alibuild_helpers.utilities import format
  from collections import OrderedDict
  from ordereddict import OrderedDict
import os
import os.path as path
import tempfile
  referenceRepo = os.path.join(abspath(referenceSources), p.lower())

  try:
    os.makedirs(abspath(referenceSources))
  except:
    pass

  if not is_writeable(referenceSources):
    if path.exists(referenceRepo):
      debug("Using %s as reference for %s", referenceRepo, p)
      return referenceRepo  # reference is read-only
    else:
      debug("Cannot create reference for %s in %s", p, referenceSources)
      return None  # no reference can be found and created (not fatal)

  err = False
  logPath = os.path.join(dirname(referenceRepo), "fetch-log.txt")
  if not path.exists(referenceRepo):
    cmd = ["git", "clone"] + (usePartialClone and [partialCloneFilter] or []) + ["--bare", spec["source"], referenceRepo]
    cmd = [x for x in cmd if x]
    debug("Cloning reference repository: %s", " ".join(cmd))
    err = execute(cmd)
  elif fetch:
    cmd = format("cd %(referenceRepo)s && "
                 "git fetch -f --tags %(source)s >%(logPath)s 2>&1 && "
                 "git fetch -f %(source)s '+refs/heads/*:refs/heads/*' >>%(logPath)s 2>&1",
                 referenceRepo=referenceRepo,
                 source=spec["source"],
                 logPath=logPath)
    debug("Updating reference repository: %s", cmd)

# Path: alibuild_helpers/log.py
# def dieOnError(err, msg):
#   def __init__(self, fmtstr):
#   def format(self, record):
#   def __init__(self, begin_msg=""):
#   def __call__(self, txt, *args):
#   def erase(self):
#   def end(self, msg="", error=False):
# def log_banner(self, message, *args, **kws):
# def log_success(self, message, *args, **kws):
# class LogFormatter(logging.Formatter):
# class ProgressPrint:
# 
# Path: alibuild_helpers/cmd.py
# def execute(command, printer=debug):
#   kwargs = {} if sys.version_info.major < 3 else {"encoding": "utf-8"}
#   popen = Popen(command, shell=is_string(command), stdout=PIPE,
#                 universal_newlines=True, **kwargs)
#   lines_iterator = iter(popen.stdout.readline, "")
#   for line in lines_iterator:
#     if not line: break
#     printer("%s", line.strip("\n"))
#   out = popen.communicate()[0].strip("\n")
#   if out:
#     printer(out)
#   return popen.returncode
# 
# Path: alibuild_helpers/git.py
# def __partialCloneFilter():
# 
# Path: alibuild_helpers/utilities.py
# def format(s, **kwds):
#   if sys.version_info[0] >= 3:
#     if isinstance(s, bytes):
#       try:
#         s = s.decode("utf-8")  # to get newlines as such and not as escaped \n
#       except:
#         s = s.decode("latin-1")  # Workaround issue with some special characters of latin-1 which are not understood by unicode
#     else:
#       s = str(s)
#   elif isinstance(s, str):
#     s = unicode(s, "utf-8")  # utf-8 is a safe assumption
#   elif not isinstance(s, unicode):
#     s = unicode(str(s))
#   return s % kwds


# Path: alibuild_helpers/workarea.py
from alibuild_helpers.log import dieOnError, debug
from alibuild_helpers.cmd import execute
from alibuild_helpers.git import partialCloneFilter
from os.path import dirname, abspath
from alibuild_helpers.utilities import format
  from collections import OrderedDict
  from ordereddict import OrderedDict
import os
import os.path as path
import tempfile

  referenceRepo = os.path.join(abspath(referenceSources), p.lower())

  try:
    os.makedirs(abspath(referenceSources))
  except:
    pass

  if not is_writeable(referenceSources):
    if path.exists(referenceRepo):
      debug("Using %s as reference for %s", referenceRepo, p)
      return referenceRepo  # reference is read-only
    else:
      debug("Cannot create reference for %s in %s", p, referenceSources)
      return None  # no reference can be found and created (not fatal)

  err = False
  logPath = os.path.join(dirname(referenceRepo), "fetch-log.txt")
  if not path.exists(referenceRepo):
    cmd = ["git", "clone"] + (usePartialClone and [partialCloneFilter] or []) + ["--bare", spec["source"], referenceRepo]
    cmd = [x for x in cmd if x]
    debug("Cloning reference repository: %s", " ".join(cmd))
    err = execute(cmd)
  elif fetch:
    cmd = format("cd %(referenceRepo)s && "
                 "git fetch -f --tags %(source)s >%(logPath)s 2>&1 && "
                 "git fetch -f %(source)s '+refs/heads/*:refs/heads/*' >>%(logPath)s 2>&1",
                 referenceRepo=referenceRepo,
                 source=spec["source"],
                 logPath=logPath)
    debug("Updating reference repository: %s", cmd)

    err = execute(cmd)