# Path: servi/semantic.py
# class SemanticVersion(object):
#     mult = [10**8, 10**4, 10**0]
# 
#     def __init__(self, sv_string):
#         self.sv, self.sv_ar = self.sv_string_to_ver(sv_string)
# 
#     @staticmethod
#     def sv_string_to_ver(sv_string):
#         if sv_string is None:
#             return None
# 
#         if type(sv_string) is not str:
#             raise ValueError('Bad semantic version - must be string: |{0}|'
#                              .format(sv_string))
# 
#         sv_ar = sv_string.split('.')
#         if len(sv_ar) > 3 or len(sv_ar) == 0:
#             raise ValueError('Bad semantic version string: |{0}|'
#                              .format(sv_string))
#         try:
#             sv_ar = [int(s) for s in sv_ar]
#             sv_val = SemanticVersion.sv_ar_to_sv(sv_ar)
#         except ValueError:
#             raise ValueError('Bad semantic version string: |{0}|'
#                              .format(sv_string))
#         sv_ar = [a1+a2 for a1, a2 in
#                  zip_longest(sv_ar, [0, 0, 0], fillvalue=0)]
#         return sv_val, sv_ar
# 
#     @staticmethod
#     def sv_ar_to_sv(sv_ar):
#         sv_zip = zip_longest(SemanticVersion.mult, sv_ar, fillvalue=0)
#         sv_val = [m * v for m, v in sv_zip]
#         return sv_val
# 
#     def __lt__(self, other):
#         return self.sv < other.sv
# 
#     def __le__(self, other):
#         return self.sv <= other.sv
# 
#     def __eq__(self, other):
#         return self.sv == other.sv
# 
#     def __ge__(self, other):
#         return self.sv >= other.sv
# 
#     def __gt__(self, other):
#         return self.sv > other.sv
# 
#     def __str__(self):
#         return '.'.join([str(v) for v in self.sv_ar])
# 
#     def bump_ver(self, ver_type):
#         assert ver_type in SEMANTIC_VERSIONS
#         if ver_type == MAJOR:
#             pos = 0
#         elif ver_type == MINOR:
#             pos = 1
#         else:
#             pos = 2
#         self.sv_ar[pos] += 1
#         self.sv = self.sv_ar_to_sv(self.sv_ar)
#         return self
# 
# Path: servi/utils.py
# def hash_of_file(fname):
#     try:
#         with open(fname, 'rb') as fp:
#             content = fp.read()
#             hashv = hashlib.sha1(content).hexdigest()
#     except FileNotFoundError:
#         hashv = c.MISSING_HASH
#     return hashv
# 
# def templatepath_to_destpath(template_path):
#     return pathfor(normalize_path(template_path, c.TEMPLATE), c.MASTER)
# 
# def pathfor(fname, source):
#     if source is c.MASTER and fname == c.SERVIFILE_GLOBAL:
#         return c.SERVIFILE_GLOBAL_FULL
#     else:
#         return c.pathfor(fname, source, c.TEMPLATE, c.MASTER, c.TMPL_DIR_SITE,
#                          c.MASTER_DIR)
# 
# def normalize_path(path, source):
#     assert source in [c.TEMPLATE, c.MASTER]
#     prefix = c.TMPL_DIR_SITE if source is c.TEMPLATE else c.MASTER_DIR
#     prefix += '/'
# 
#     assert os.path.commonprefix([prefix, path]) == prefix
# 
#     return path.split(sep=prefix, maxsplit=1)[1]


import json
import os
import servi.config as c
from servi.semantic import SemanticVersion
from servi.utils import hash_of_file, templatepath_to_destpath, pathfor, \
    normalize_path
        if load:
            self._load()
        else:
            self._create()

    def _create(self):
        # create a new manifest
        # use the files in TMPL_DIR_SITE as the source list
        self.template_version = SemanticVersion(get_template_version())

        self.manifest = {
            "files": {},
            "template_version": str(self.template_version),
        }

        for (dirpath, dirnames, filenames) in os.walk(c.TMPL_DIR_SITE):
            for file in filenames:
                # Keep the version file and manifest files out of the manifest
                if file == c.VERSION_FILE or file == c.MANIFEST_FILE:
                    continue

                if file == c.SERVIFILE_GLOBAL and self.source is c.MASTER:
                    self.manifest["files"][file] = \
                        hash_of_file(c.SERVIFILE_GLOBAL_FULL)
                else:
                    template_file = os.path.join(dirpath, file)
                    if self.source is c.MASTER:
                        hashv = hash_of_file(
                            templatepath_to_destpath(template_file))
                    else:

# Path: servi/semantic.py
# class SemanticVersion(object):
#     mult = [10**8, 10**4, 10**0]
# 
#     def __init__(self, sv_string):
#         self.sv, self.sv_ar = self.sv_string_to_ver(sv_string)
# 
#     @staticmethod
#     def sv_string_to_ver(sv_string):
#         if sv_string is None:
#             return None
# 
#         if type(sv_string) is not str:
#             raise ValueError('Bad semantic version - must be string: |{0}|'
#                              .format(sv_string))
# 
#         sv_ar = sv_string.split('.')
#         if len(sv_ar) > 3 or len(sv_ar) == 0:
#             raise ValueError('Bad semantic version string: |{0}|'
#                              .format(sv_string))
#         try:
#             sv_ar = [int(s) for s in sv_ar]
#             sv_val = SemanticVersion.sv_ar_to_sv(sv_ar)
#         except ValueError:
#             raise ValueError('Bad semantic version string: |{0}|'
#                              .format(sv_string))
#         sv_ar = [a1+a2 for a1, a2 in
#                  zip_longest(sv_ar, [0, 0, 0], fillvalue=0)]
#         return sv_val, sv_ar
# 
#     @staticmethod
#     def sv_ar_to_sv(sv_ar):
#         sv_zip = zip_longest(SemanticVersion.mult, sv_ar, fillvalue=0)
#         sv_val = [m * v for m, v in sv_zip]
#         return sv_val
# 
#     def __lt__(self, other):
#         return self.sv < other.sv
# 
#     def __le__(self, other):
#         return self.sv <= other.sv
# 
#     def __eq__(self, other):
#         return self.sv == other.sv
# 
#     def __ge__(self, other):
#         return self.sv >= other.sv
# 
#     def __gt__(self, other):
#         return self.sv > other.sv
# 
#     def __str__(self):
#         return '.'.join([str(v) for v in self.sv_ar])
# 
#     def bump_ver(self, ver_type):
#         assert ver_type in SEMANTIC_VERSIONS
#         if ver_type == MAJOR:
#             pos = 0
#         elif ver_type == MINOR:
#             pos = 1
#         else:
#             pos = 2
#         self.sv_ar[pos] += 1
#         self.sv = self.sv_ar_to_sv(self.sv_ar)
#         return self
# 
# Path: servi/utils.py
# def hash_of_file(fname):
#     try:
#         with open(fname, 'rb') as fp:
#             content = fp.read()
#             hashv = hashlib.sha1(content).hexdigest()
#     except FileNotFoundError:
#         hashv = c.MISSING_HASH
#     return hashv
# 
# def templatepath_to_destpath(template_path):
#     return pathfor(normalize_path(template_path, c.TEMPLATE), c.MASTER)
# 
# def pathfor(fname, source):
#     if source is c.MASTER and fname == c.SERVIFILE_GLOBAL:
#         return c.SERVIFILE_GLOBAL_FULL
#     else:
#         return c.pathfor(fname, source, c.TEMPLATE, c.MASTER, c.TMPL_DIR_SITE,
#                          c.MASTER_DIR)
# 
# def normalize_path(path, source):
#     assert source in [c.TEMPLATE, c.MASTER]
#     prefix = c.TMPL_DIR_SITE if source is c.TEMPLATE else c.MASTER_DIR
#     prefix += '/'
# 
#     assert os.path.commonprefix([prefix, path]) == prefix
# 
#     return path.split(sep=prefix, maxsplit=1)[1]


# Path: servi/manifest.py
import json
import os
import servi.config as c
from servi.semantic import SemanticVersion
from servi.utils import hash_of_file, templatepath_to_destpath, pathfor, \
    normalize_path

        if load:
            self._load()
        else:
            self._create()

    def _create(self):
        # create a new manifest
        # use the files in TMPL_DIR_SITE as the source list
        self.template_version = SemanticVersion(get_template_version())

        self.manifest = {
            "files": {},
            "template_version": str(self.template_version),
        }

        for (dirpath, dirnames, filenames) in os.walk(c.TMPL_DIR_SITE):
            for file in filenames:
                # Keep the version file and manifest files out of the manifest
                if file == c.VERSION_FILE or file == c.MANIFEST_FILE:
                    continue

                if file == c.SERVIFILE_GLOBAL and self.source is c.MASTER:
                    self.manifest["files"][file] = \
                        hash_of_file(c.SERVIFILE_GLOBAL_FULL)
                else:
                    template_file = os.path.join(dirpath, file)
                    if self.source is c.MASTER:
                        hashv = hash_of_file(
                            templatepath_to_destpath(template_file))
                    else:

                        hashv = hash_of_file(template_file)