<commit_before>#
# (c) <NAME> 2007
# $Id: DeployDatabaseModule.py 299 2008-01-19 08:42:05Z andrei $
#

import clr
import sys
import os
import shutil

clr.AddReference("System.Data")
clr.AddReference("System.Xml")

import System.Data
import System.Data.SqlClient
import System.Diagnostics
import System.Text
import System.Xml

from Pi.Deploy import DeployUtilities
from Pi.Deploy.DeployConfiguration import Configuration
from Pi.Deploy.DeployAction import Action
from Pi.Deploy.DeployModule import DeployModule

from DeployDatabaseConfiguration import DatabaseConfiguration



SourceFilesAttributeName             = 'SourceFiles'
DirectoriesAttributeName             = 'Directories'

DatabaseElementName                  = 'Database'
NameAttributeName                    = 'Name'
ServerAttributeName                  = 'Server'
UserNameAttributeName                = 'UserName'
PasswordAttributeName                = 'Password'
CreateOnceAttributeName              = 'CreateOnce'
DriverAttributeName                  = 'Driver'
ApplicationNameAttributeName         = 'ApplicationName'
IntegratedSecurityAttributeName      = 'IntegratedSecurity'
TrustedConnectionAttributeName       = 'TrustedConnection'
ConnectionNameAttributeName          = 'ConnectionName'
ProviderAttributeName                = 'Provider'

ScriptsElementName                   = 'Scripts'

HooksElementName                     = 'Hooks'
HookElementName                      = 'Hook'
ExecutableAttributeName              = 'Executable'
ArgumentsAttributeName               = 'Arguments'
BeforeDropAttributeName              = 'BeforeDrop'


class DeployDatabaseModule(DeployModule):

    def __init__(self):
        pass


    def CreateWebConfigSections(self, document, configuration):

        if hasattr(configuration, 'Databases'):

            for database in configuration.Databases:

                for module in database.Modules.values():
                    module.CreateWebConfigSections(document, configuration)


    def ReadConfiguration(self, reader, configuration, database = None):
        
        if not hasattr(configuration, 'Databases'):
            configuration.Databases = []

        if reader.NodeType == System.Xml.XmlNodeType.Element: 

            if reader.LocalName == DatabaseElementName and reader.NamespaceURI == self.NamespaceUri:

                if database == None:
                    database = DatabaseConfiguration()
                    configuration.Databases.append(database)

                self.__ReadDatabaseConfiguration(reader, database)

                database.ConnectionString = self.CreateConnectionString(database)


    def __ReadDatabaseConfiguration(self, reader, database):

        if reader.MoveToAttribute(NameAttributeName):
            database.Name = reader.ReadContentAsString()

        if reader.MoveToAttribute(ServerAttributeName):
            database.Server = reader.ReadContentAsString()

        if reader.MoveToAttribute(ApplicationNameAttributeName):
            database.ApplicationName = reader.ReadContentAsString()

        if reader.MoveToAttribute(IntegratedSecurityAttributeName):
            database.IntegratedSecurity = reader.ReadContentAsString()

        if reader.MoveToAttribute(TrustedConnectionAttributeName):
            database.TrustedConnection = reader.ReadContentAsString()

        if reader.MoveToAttribute(ConnectionNameAttributeName):
            database.ConnectionName = reader.ReadContentAsString()

        if reader.MoveToAttribute(ProviderAttributeName):
            database.Provider = reader.ReadContentAsString()

        if reader.MoveToAttribute(UserNameAttributeName):
            database.UserName = reader.ReadContentAsString()

        if reader.MoveToAttribute(PasswordAttributeName):
            database.Password = reader.ReadContentAsString()

        else:
            database.Password = System.Convert.ToBase64String(System.Guid.NewGuid().ToByteArray())

        if reader.MoveToAttribute(CreateOnceAttributeName):

            if reader.ReadContentAsString() == 'true':
                database.CreateOnce = True
                    
            else:
                database.CreateOnce = False

        if reader.MoveToAttribute(DriverAttributeName):
            database.Driver = reader.ReadContentAsString()

        if reader.MoveToAttribute(SourceFilesAttributeName):

            for file in reader.ReadContentAsString().split():
                database.Scripts.append(file)

        reader.MoveToElement()

        depth = reader.Depth

        while reader.Read() and reader.Depth > depth:

            if reader.NodeType == System.Xml.XmlNodeType.Element:

                if reader.LocalName == HooksElementName and reader.NamespaceURI == self.NamespaceUri:
                    self.__ReadHookConfiguration(reader, database)

                if reader.NamespaceURI != self.NamespaceUri and reader.NamespaceURI in Configuration.Modules: 
                    namespaceURI = reader.NamespaceURI
                    handler = Configuration.Modules[namespaceURI]
                    handler.ReadConfiguration(reader, database)

                    database.Modules[namespaceURI] = handler


    def __ReadHookConfiguration(self, reader, database):
        reader.MoveToElement()

        depth = reader.Depth

        while reader.Read() and reader.Depth > depth:

            if reader.NodeType == System.Xml.XmlNodeType.Element:

                if reader.LocalName == HookElementName and reader.NamespaceURI == self.NamespaceUri:

                    hook = DatabaseConfiguration.HookConfiguration()

                    if reader.MoveToAttribute(ExecutableAttributeName):
                        hook.Executable = reader.ReadContentAsString()

                    if reader.MoveToAttribute(ArgumentsAttributeName):
                        hook.Arguments = reader.ReadContentAsString()

                    if reader.MoveToAttribute(BeforeDropAttributeName):
                        hook.BeforeDrop = reader.ReadContentAsBoolean()

                    database.Hooks.append(hook)

    def CreateConnectionString(self, configuration):
        print 'Creating connection string (not implemented) ...'

    def PopulateDatabase(self, configuration):
        print 'Populating database (not implemented) ...'

    def DropDatabase(self, configuration):<commit_msg>Replaced append() with Add() on a dotnet List<> (for DB hooks).<commit_after>#
# (c) Peralta Informatics 2007
# $Id: DeployDatabaseModule.py 299 2008-01-19 08:42:05Z andrei $
#

import clr
import sys
import os
import shutil

clr.AddReference("System.Data")
clr.AddReference("System.Xml")

import System.Data
import System.Data.SqlClient
import System.Diagnostics
import System.Text
import System.Xml

from Pi.Deploy import DeployUtilities
from Pi.Deploy.DeployConfiguration import Configuration
from Pi.Deploy.DeployAction import Action
from Pi.Deploy.DeployModule import DeployModule

from DeployDatabaseConfiguration import DatabaseConfiguration



SourceFilesAttributeName             = 'SourceFiles'
DirectoriesAttributeName             = 'Directories'

DatabaseElementName                  = 'Database'
NameAttributeName                    = 'Name'
ServerAttributeName                  = 'Server'
UserNameAttributeName                = 'UserName'
PasswordAttributeName                = 'Password'
CreateOnceAttributeName              = 'CreateOnce'
DriverAttributeName                  = 'Driver'
ApplicationNameAttributeName         = 'ApplicationName'
IntegratedSecurityAttributeName      = 'IntegratedSecurity'
TrustedConnectionAttributeName       = 'TrustedConnection'
ConnectionNameAttributeName          = 'ConnectionName'
ProviderAttributeName                = 'Provider'

ScriptsElementName                   = 'Scripts'

HooksElementName                     = 'Hooks'
HookElementName                      = 'Hook'
ExecutableAttributeName              = 'Executable'
ArgumentsAttributeName               = 'Arguments'
BeforeDropAttributeName              = 'BeforeDrop'


class DeployDatabaseModule(DeployModule):

    def __init__(self):
        pass


    def CreateWebConfigSections(self, document, configuration):

        if hasattr(configuration, 'Databases'):

            for database in configuration.Databases:

                for module in database.Modules.values():
                    module.CreateWebConfigSections(document, configuration)


    def ReadConfiguration(self, reader, configuration, database = None):
        
        if not hasattr(configuration, 'Databases'):
            configuration.Databases = []

        if reader.NodeType == System.Xml.XmlNodeType.Element: 

            if reader.LocalName == DatabaseElementName and reader.NamespaceURI == self.NamespaceUri:

                if database == None:
                    database = DatabaseConfiguration()
                    configuration.Databases.append(database)

                self.__ReadDatabaseConfiguration(reader, database)

                database.ConnectionString = self.CreateConnectionString(database)


    def __ReadDatabaseConfiguration(self, reader, database):

        if reader.MoveToAttribute(NameAttributeName):
            database.Name = reader.ReadContentAsString()

        if reader.MoveToAttribute(ServerAttributeName):
            database.Server = reader.ReadContentAsString()

        if reader.MoveToAttribute(ApplicationNameAttributeName):
            database.ApplicationName = reader.ReadContentAsString()

        if reader.MoveToAttribute(IntegratedSecurityAttributeName):
            database.IntegratedSecurity = reader.ReadContentAsString()

        if reader.MoveToAttribute(TrustedConnectionAttributeName):
            database.TrustedConnection = reader.ReadContentAsString()

        if reader.MoveToAttribute(ConnectionNameAttributeName):
            database.ConnectionName = reader.ReadContentAsString()

        if reader.MoveToAttribute(ProviderAttributeName):
            database.Provider = reader.ReadContentAsString()

        if reader.MoveToAttribute(UserNameAttributeName):
            database.UserName = reader.ReadContentAsString()

        if reader.MoveToAttribute(PasswordAttributeName):
            database.Password = reader.ReadContentAsString()

        else:
            database.Password = System.Convert.ToBase64String(System.Guid.NewGuid().ToByteArray())

        if reader.MoveToAttribute(CreateOnceAttributeName):

            if reader.ReadContentAsString() == 'true':
                database.CreateOnce = True
                    
            else:
                database.CreateOnce = False

        if reader.MoveToAttribute(DriverAttributeName):
            database.Driver = reader.ReadContentAsString()

        if reader.MoveToAttribute(SourceFilesAttributeName):

            for file in reader.ReadContentAsString().split():
                database.Scripts.append(file)

        reader.MoveToElement()

        depth = reader.Depth

        while reader.Read() and reader.Depth > depth:

            if reader.NodeType == System.Xml.XmlNodeType.Element:

                if reader.LocalName == HooksElementName and reader.NamespaceURI == self.NamespaceUri:
                    self.__ReadHookConfiguration(reader, database)

                if reader.NamespaceURI != self.NamespaceUri and reader.NamespaceURI in Configuration.Modules: 
                    namespaceURI = reader.NamespaceURI
                    handler = Configuration.Modules[namespaceURI]
                    handler.ReadConfiguration(reader, database)

                    database.Modules[namespaceURI] = handler


    def __ReadHookConfiguration(self, reader, database):
        reader.MoveToElement()

        depth = reader.Depth

        while reader.Read() and reader.Depth > depth:

            if reader.NodeType == System.Xml.XmlNodeType.Element:

                if reader.LocalName == HookElementName and reader.NamespaceURI == self.NamespaceUri:

                    hook = DatabaseConfiguration.HookConfiguration()

                    if reader.MoveToAttribute(ExecutableAttributeName):
                        hook.Executable = reader.ReadContentAsString()

                    if reader.MoveToAttribute(ArgumentsAttributeName):
                        hook.Arguments = reader.ReadContentAsString()

                    if reader.MoveToAttribute(BeforeDropAttributeName):
                        hook.BeforeDrop = reader.ReadContentAsBoolean()

                    database.Hooks.Add(hook)

    def CreateConnectionString(self, configuration):
        print 'Creating connection string (not implemented) ...'

    def PopulateDatabase(self, configuration):
        print 'Populating database (not implemented) ...'

    def DropDatabase(self, configuration):
{'fraction_non_alphanumeric': 0.07355010161012975, 'fraction_numerical': 0.0035954353603251522, 'mean_word_length': 2.5026006022447302, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5313361', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2825, 'n_words': 602}