# Path: dgitcore/plugins/validator.py
# class ValidatorBase(object):
#     """
#     This is the base class for all backends including
#     """
#     def __init__(self, name, version, description, supported=[]):
#         """
#         Parameters:
#         -----------
#         name: Name of the backend service e.g., s3
#         version: Version of this implementation
#         description: Text description of this service
#         supported: supported services with including name
# 
#         For example, there may be multiple s3 implementations that
#         support different kinds of services.
# 
#         """
#         self.enable = 'y'
#         self.name = name
#         self.version = version
#         self.description = description
#         self.support = supported + [name]
#         self.initialize()
# 
#     def __str__(self): 
#         return self.name 
# 
#     def initialize(self):
#         """
#         Called to initialize sessions, internal objects etc.
#         """
#         return
# 
#     def autooptions(self):
#         """
#         Get default options
#         """
#         return None
# 
#     def evaluate(self, repo, files, rules):
#         """
#         Evaluate the repo
# 
#         returns: A list of dictionaries with:
#            target: relative path of the file
#            rules: rules file used
#            validator: name of the validator
#            status: OK/Success/Error
#            Message: Any additional information
#         """
#         return []
# 
# Path: dgitcore/config.py
# def get_config():
#     if config is None:
#         init()
#     return config
# 
# Path: dgitcore/helper.py
# 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]
# 
# 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)


import os, sys, glob2
from collections import OrderedDict
from dgitcore.plugins.validator import ValidatorBase
from dgitcore.config import get_config
from dgitcore.helper import compute_sha256, cd
            if (('metadata-validator' in params) and
                'enable' in params['metadata-validator']):
                self.enable = params['metadata-validator']['enable']
            else:
                self.enable = 'y'

    def autooptions(self):
        return OrderedDict([
            ("files",["*"])
        ])

    def evaluate(self, repo, spec, args):
        """
        Check the integrity of the datapackage.json
        """

        status = []
        with cd(repo.rootdir):
            files = spec.get('files', ['*'])
            resource_files = repo.find_matching_files(files)
            files = glob2.glob("**/*")
            disk_files = [f for f in files if os.path.isfile(f) and f != "datapackage.json"]

            allfiles = list(set(resource_files + disk_files))
            allfiles.sort()

            for f in allfiles:
                if f in resource_files and f in disk_files:
                    r = repo.get_resource(f)
                    coded_sha256 = r['sha256']

# Path: dgitcore/plugins/validator.py
# class ValidatorBase(object):
#     """
#     This is the base class for all backends including
#     """
#     def __init__(self, name, version, description, supported=[]):
#         """
#         Parameters:
#         -----------
#         name: Name of the backend service e.g., s3
#         version: Version of this implementation
#         description: Text description of this service
#         supported: supported services with including name
# 
#         For example, there may be multiple s3 implementations that
#         support different kinds of services.
# 
#         """
#         self.enable = 'y'
#         self.name = name
#         self.version = version
#         self.description = description
#         self.support = supported + [name]
#         self.initialize()
# 
#     def __str__(self): 
#         return self.name 
# 
#     def initialize(self):
#         """
#         Called to initialize sessions, internal objects etc.
#         """
#         return
# 
#     def autooptions(self):
#         """
#         Get default options
#         """
#         return None
# 
#     def evaluate(self, repo, files, rules):
#         """
#         Evaluate the repo
# 
#         returns: A list of dictionaries with:
#            target: relative path of the file
#            rules: rules file used
#            validator: name of the validator
#            status: OK/Success/Error
#            Message: Any additional information
#         """
#         return []
# 
# Path: dgitcore/config.py
# def get_config():
#     if config is None:
#         init()
#     return config
# 
# Path: dgitcore/helper.py
# 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]
# 
# 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)


# Path: dgitcore/contrib/validators/metadata_validator.py
import os, sys, glob2
from collections import OrderedDict
from dgitcore.plugins.validator import ValidatorBase
from dgitcore.config import get_config
from dgitcore.helper import compute_sha256, cd

            if (('metadata-validator' in params) and
                'enable' in params['metadata-validator']):
                self.enable = params['metadata-validator']['enable']
            else:
                self.enable = 'y'

    def autooptions(self):
        return OrderedDict([
            ("files",["*"])
        ])

    def evaluate(self, repo, spec, args):
        """
        Check the integrity of the datapackage.json
        """

        status = []
        with cd(repo.rootdir):
            files = spec.get('files', ['*'])
            resource_files = repo.find_matching_files(files)
            files = glob2.glob("**/*")
            disk_files = [f for f in files if os.path.isfile(f) and f != "datapackage.json"]

            allfiles = list(set(resource_files + disk_files))
            allfiles.sort()

            for f in allfiles:
                if f in resource_files and f in disk_files:
                    r = repo.get_resource(f)
                    coded_sha256 = r['sha256']

                    computed_sha256 = compute_sha256(f)