# Path: scm/models.py
# class PackageCheckerBaseImpl(object):
#     description = None
#     def check(self, fileinfo, logical_component, installation_method):
#         raise NotImplemented()


import os
from zipfile import ZipFile, BadZipfile
from django import forms
from scm.models import PackageCheckerBaseImpl
## MAGE imports


class DeliveryCheckerSql(PackageCheckerBaseImpl):
    description = 'Sample SQL delivery checker'

    def check(self, fileinfo, logical_component, installation_method):
        ## Must be a zip file
        try:
            zf = ZipFile(fileinfo)
            zf.testzip()
        except BadZipfile as e:
            raise forms.ValidationError(e)

        ## Must contain only SQL files either inside a 'scripts-sql' directory or at the root.
        for f in zf.namelist():
            s = f.strip('/').split('/') # zipfile read by Python's lib => always "/"
            if          (len(s) > 2) or \
                        (len(s) == 2 and s[0] != "scripts-sql") or \
                        (len(s) == 2 and os.path.splitext(s[1])[1] != '.sql') or \
                        (len(s) == 1 and not (s[0] == 'scripts-sql' or os.path.splitext(s[0])[1] == '.sql')):
                raise forms.ValidationError('Must only contain .sql files or a single "scripts-sql" directory containing .sql files')


        ## Encoding must be ISO-8859-1/ISO-8859-15/cp1252


        ## Done
        return


# Path: scm/models.py
# class PackageCheckerBaseImpl(object):
#     description = None
#     def check(self, fileinfo, logical_component, installation_method):
#         raise NotImplemented()


# Path: scm/checkers.py
import os
from zipfile import ZipFile, BadZipfile
from django import forms
from scm.models import PackageCheckerBaseImpl

## MAGE imports


class DeliveryCheckerSql(PackageCheckerBaseImpl):
    description = 'Sample SQL delivery checker'

    def check(self, fileinfo, logical_component, installation_method):
        ## Must be a zip file
        try:
            zf = ZipFile(fileinfo)
            zf.testzip()
        except BadZipfile as e:
            raise forms.ValidationError(e)

        ## Must contain only SQL files either inside a 'scripts-sql' directory or at the root.
        for f in zf.namelist():
            s = f.strip('/').split('/') # zipfile read by Python's lib => always "/"
            if          (len(s) > 2) or \
                        (len(s) == 2 and s[0] != "scripts-sql") or \
                        (len(s) == 2 and os.path.splitext(s[1])[1] != '.sql') or \
                        (len(s) == 1 and not (s[0] == 'scripts-sql' or os.path.splitext(s[0])[1] == '.sql')):
                raise forms.ValidationError('Must only contain .sql files or a single "scripts-sql" directory containing .sql files')


        ## Encoding must be ISO-8859-1/ISO-8859-15/cp1252


        ## Done
        return


class DeliveryCheckerEar(PackageCheckerBaseImpl):