# 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/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/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
# 
# def star():
#   return re.sub("build.*$", "", basename(sys.argv[0]).lower())
# 
# def resolve_store_path(architecture, spec_hash):
#   """Return the path where a tarball with the given hash is to be stored.
# 
#   The returned path is relative to the working directory (normally sw/) or the
#   root of the remote store.
#   """
#   return "/".join(("TARS", architecture, "store", spec_hash[:2], spec_hash))
# 
# def resolve_links_path(architecture, package):
#   """Return the path where symlinks for the given package are to be stored.
# 
#   The returned path is relative to the working directory (normally sw/) or the
#   root of the remote store.
#   """
#   return "/".join(("TARS", architecture, package))


import glob
import os
import os.path
import re
import sys
import time
import requests
      import boto3
from requests.exceptions import RequestException
from alibuild_helpers.cmd import execute
from alibuild_helpers.log import debug, warning, error, dieOnError
from alibuild_helpers.utilities import format, star, resolve_store_path, resolve_links_path
    from botocore.exceptions import ClientError
    from botocore.exceptions import ClientError
         # repacked the tarball from the remote, so the file differs, but
         # there's no point in downloading the one from the remote again.
         rsync -vW --ignore-existing "{remoteStore}/$storePath/$tar" "{workDir}/$storePath/"
      then
        break
      fi
    done
    """.format(pkg=p, ver=spec["version"], arch=self.architecture,
               remoteStore=self.remoteStore,
               workDir=self.workdir,
               linksPath=resolve_links_path(self.architecture, p),
               storePaths=" ".join(resolve_store_path(self.architecture, pkg_hash)
                                   for pkg_hash in spec["remote_hashes"])))
    dieOnError(err, "Unable to update from specified store.")

  def syncToRemote(self, p, spec):
    if not self.writeStore:
      return
    tarballNameWithRev = format("%(package)s-%(version)s-%(revision)s.%(architecture)s.tar.gz",
                                architecture=self.architecture,
                                **spec)
    cmd = format("cd %(workdir)s && "
                 "rsync -avR --ignore-existing %(storePath)s/%(tarballNameWithRev)s  %(remoteStore)s/ &&"
                 "rsync -avR --ignore-existing %(linksPath)s/%(tarballNameWithRev)s  %(remoteStore)s/",
                 workdir=self.workdir,
                 remoteStore=self.remoteStore,
                 storePath=resolve_store_path(self.architecture, spec["hash"]),
                 linksPath=resolve_links_path(self.architecture, p),
                 tarballNameWithRev=tarballNameWithRev)
    err = execute(cmd)

# 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/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/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
# 
# def star():
#   return re.sub("build.*$", "", basename(sys.argv[0]).lower())
# 
# def resolve_store_path(architecture, spec_hash):
#   """Return the path where a tarball with the given hash is to be stored.
# 
#   The returned path is relative to the working directory (normally sw/) or the
#   root of the remote store.
#   """
#   return "/".join(("TARS", architecture, "store", spec_hash[:2], spec_hash))
# 
# def resolve_links_path(architecture, package):
#   """Return the path where symlinks for the given package are to be stored.
# 
#   The returned path is relative to the working directory (normally sw/) or the
#   root of the remote store.
#   """
#   return "/".join(("TARS", architecture, package))


# Path: alibuild_helpers/sync.py
import glob
import os
import os.path
import re
import sys
import time
import requests
      import boto3
from requests.exceptions import RequestException
from alibuild_helpers.cmd import execute
from alibuild_helpers.log import debug, warning, error, dieOnError
from alibuild_helpers.utilities import format, star, resolve_store_path, resolve_links_path
    from botocore.exceptions import ClientError
    from botocore.exceptions import ClientError

         # repacked the tarball from the remote, so the file differs, but
         # there's no point in downloading the one from the remote again.
         rsync -vW --ignore-existing "{remoteStore}/$storePath/$tar" "{workDir}/$storePath/"
      then
        break
      fi
    done
    """.format(pkg=p, ver=spec["version"], arch=self.architecture,
               remoteStore=self.remoteStore,
               workDir=self.workdir,
               linksPath=resolve_links_path(self.architecture, p),
               storePaths=" ".join(resolve_store_path(self.architecture, pkg_hash)
                                   for pkg_hash in spec["remote_hashes"])))
    dieOnError(err, "Unable to update from specified store.")

  def syncToRemote(self, p, spec):
    if not self.writeStore:
      return
    tarballNameWithRev = format("%(package)s-%(version)s-%(revision)s.%(architecture)s.tar.gz",
                                architecture=self.architecture,
                                **spec)
    cmd = format("cd %(workdir)s && "
                 "rsync -avR --ignore-existing %(storePath)s/%(tarballNameWithRev)s  %(remoteStore)s/ &&"
                 "rsync -avR --ignore-existing %(linksPath)s/%(tarballNameWithRev)s  %(remoteStore)s/",
                 workdir=self.workdir,
                 remoteStore=self.remoteStore,
                 storePath=resolve_store_path(self.architecture, spec["hash"]),
                 linksPath=resolve_links_path(self.architecture, p),
                 tarballNameWithRev=tarballNameWithRev)
    err = execute(cmd)

    dieOnError(err, "Unable to upload tarball.")