<commit_before>########################################################################
# Group: Windows Package Manager A
# Name: <NAME>
# Group Members: <NAME> and <NAME>
# Date: October 15, 2011
# Query database, perform web only given a db object
########################################################################

# re for regex, urllib2 for downloading.
from db_wrapper import *
import re, urllib2, logging

class app:

    # Initialize the application using the database object.
    def __init__(self, appName, dbObject, logFileName):

        self.logger = logging.getLogger('Application')
        self.logger.setLevel(logging.DEBUG)

        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename=logFileName, filemode='a')
        self.name = appName
        self.db = dbObject
        self.href = ''
        self.version = ''

#logging.info("Application: Initialized application using database for " + appName + ".")

    # abritrary delete function.
    def __del__(self):
#logging.info("Application: Done using Application " + self.name + ".")
        pass


    # Queries the database for regex(s) and download site(s) then downloads
    # the webpage and checks the regex against it.
    #
    # Returns Boolean True means there is an newer version,
    #  false means no newer version found.
    def checkUpdates(self):
#logging.info("Checking for updates for " + self.name + ".")


        newVersion = ''
        if self.href == '':
            allHREFS = self.getExeURLs()

            if len(allHREFS) > 1:
                self.href = self.chooseHREF(allHREFS)
            else:
                self.href = allHREFS[0]


            newVersion = self.getVersionFromURL(self.href)

        validQuery, versionsInDB =  get_app_version(self.db, self.name)
        if validQuery:
            for currVersion in versionsInDB:

                # Found this version in the db.
                if str(currVersion) == newVersion:
                    return False

        # Did not find this version in the db.
        return True


    # Download the updates - check for updates first.
    def dlUpdates(self):
        if self.checkUpdates() or True:
            logging.info( "Application: Updating " + self.name)

            if self.href == '':
                allHREFS = self.getExeURLs()
                href = ''
                if allHREFS == []:
                    logging.info( 'Application: Found no hyper links using that regular expression.')

                # More than one hyperlink.  Prompt user.
                elif(len(allHREFS) > 1):
                    href = self.chooseHREF(allHREFS)

                # Only one hyperlink in html.
                elif(len(allHREFS) == 1):
                    href = allHREFS[0]

                self.href = href

            if(self.href != ''):
                # assume that there is no quotes are found in hyperlink.
                logging.info( "Application: Dowloading executable from:", self.href)
                if self.version != '':
                    self.version = self.getVersionFromURL(self.href)


                if self.version == '':
                    self.downloadFileName = self.name + '.' + self.version + '.exe'
                else:
                    self.downloadFileName = self.name + '.exe'


                add_update_file(self.db, self.name, self.version, self.downloadFileName)

                req = urllib2.urlopen(self.href)
                output = open(self.downloadFileName, 'wb')
                output.write(req.read())
                output.close()




    # Choose a hyperlink.
    def chooseHREF(self, allHREFS):
        print "Found more than one href.  All hrefs are:"
        count = 0

        for href in allHREFS:
            print str(count) + ': ', href
            count = count + 1

        href = ''
        hrefNum = raw_input('Choose href (number): ')
        if int(hrefNum) > 0 and int(hrefNum) < len(allHREFS):
            href = allHREFS[int(hrefNum)]

        href = href.replace('"', '')
        return href


    def getVersionFromURL(self, href):
        validVersionRegexQuery, versionRegexList = get_app_version_regex(self.db, self.name)

        # apply version regex to the (possibly chosen) url.
        if validVersionRegexQuery and versionRegexList != []:
            versions = []
            version = ''

            # Get the version from the regex list.
            for regex in versionRegexList:
                versions = versions + re.findall(regex, href, re.IGNORECASE)

            versions = sorted(set(versions)) # Remove Duplicates!

            # Found with more than one. Prompt the user.
            if (len(versions) > 1):
                print 'Found more than one version in with the version regular exprossion.'
                count = 0
                for version in versions:
                    print str(count) + ': ' + version
                    count = count + 1

                # Prompt.
                versionNum = raw_input('Please choose a number: ')
                if int(versionNum) > 0 and int(versionNum) < len(versions):<commit_msg>fixed some runtime bugs.<commit_after>########################################################################
# Group: Windows Package Manager A
# Name: <NAME>
# Group Members: <NAME> and <NAME>
# Date: October 15, 2011
# Query database, perform web only given a db object
########################################################################

# re for regex, urllib2 for downloading.
from db_wrapper import *
import re, urllib2, logging

class app:

    # Initialize the application using the database object.
    def __init__(self, appName, dbObject, logFileName):

        self.logger = logging.getLogger('Application')
        self.logger.setLevel(logging.DEBUG)

        #logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename=logFileName, filemode='a')
        self.name = appName
        self.db = dbObject
        self.href = ''
        self.version = ''

#logging.info("Application: Initialized application using database for " + appName + ".")

    # abritrary delete function.
    def __del__(self):
#logging.info("Application: Done using Application " + self.name + ".")
        pass


    # Queries the database for regex(s) and download site(s) then downloads
    # the webpage and checks the regex against it.
    #
    # Returns Boolean True means there is an newer version,
    #  false means no newer version found.
    def checkUpdates(self):
#logging.info("Checking for updates for " + self.name + ".")

        newVersion = ''
        if self.href == '':
            allHREFS = self.getExeURLs()

            if len(allHREFS) > 1:
                self.href = self.chooseHREF(allHREFS)
            else:
                self.href = allHREFS[0]


            newVersion = self.getVersionFromURL(self.href)

        validQuery, versionsInDB =  get_app_version(self.db, self.name)
        if validQuery:
            for currVersion in versionsInDB:

                # Found this version in the db.
                if str(currVersion) == newVersion:
                    return False
        # Did not find this version in the db.
        return True


    # Download the updates - check for updates first.
    def dlUpdates(self):
        if self.checkUpdates() or True:
            logging.info( "Application: Updating " + self.name)

            if self.href == '':
                allHREFS = self.getExeURLs()
                href = ''
                if allHREFS == []:
                    logging.info( 'Application: Found no hyper links using that regular expression.')

                # More than one hyperlink.  Prompt user.
                elif(len(allHREFS) > 1):
                    href = self.chooseHREF(allHREFS)

                # Only one hyperlink in html.
                elif(len(allHREFS) == 1):
                    href = allHREFS[0].replace('"', '')

                self.href = href.replace('"', '')

            if(self.href != ''):
                # assume that there is no quotes are found in hyperlink.
                if self.version != '':
                    self.version = self.getVersionFromURL(self.href)


                #if self.version == '':
                #    self.downloadFileName = self.name + self.version + '.exe'
                #else:
                #    self.downloadFileName = self.name + '.exe'

                self.downloadFileName = self.name + self.version + '.exe'
                print "Application: Dowloading executable from: %s to %s" % (self.href, self.downloadFileName)
                add_update_file(self.db, self.name, self.version, self.downloadFileName)
                self.href = self.href.replace('"', '')
                req = urllib2.urlopen(self.href)
                output = open(self.downloadFileName, 'wb')
                output.write(req.read())
                output.close()




    # Choose a hyperlink.
    def chooseHREF(self, allHREFS):
        print "Found more than one href.  All hrefs are:"
        count = 0

        for href in allHREFS:
            print str(count) + ': ', href
            count = count + 1

        href = ''
        hrefNum = raw_input('Choose href (number): ')
        if int(hrefNum) > 0 and int(hrefNum) < len(allHREFS):
            href = allHREFS[int(hrefNum)]

        href = href.replace('"', '')

        return href


    def getVersionFromURL(self, href):
        validVersionRegexQuery, versionRegexList = get_app_version_regex(self.db, self.name)

        # apply version regex to the (possibly chosen) url.
        if validVersionRegexQuery and versionRegexList != []:
            versions = []
            version = ''

            # Get the version from the regex list.
            for regex in versionRegexList:
                versions = versions + re.findall(regex, href, re.IGNORECASE)

            versions = sorted(set(versions)) # Remove Duplicates!

            # Found with more than one. Prompt the user.
            if (len(versions) > 1):
                print 'Found more than one version in with the version regular exprossion.'
                count = 0
                for version in versions:
                    print str(count) + ': ' + version
                    count = count + 1

                # Prompt.
                versionNum = raw_input('Please choose a number: ')
                if int(versionNum) > 0 and int(versionNum) < len(versions):
{'fraction_non_alphanumeric': 0.12625023589356482, 'fraction_numerical': 0.004151726740894509, 'mean_word_length': 1.9054276315789473, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1713825', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2753, 'n_words': 824}