Exemple #1
0
    def test_search(self):
        path = os.path.join(
            os.path.dirname(__file__),
            'assets/projekt'
        )

        # Test: Search for '#include <stdio.h>' in source at ./assets/projekt
        expected = ['projekt.c']
        self.assertListEqual(
            expected,
            utilities.search('#include <stdio.h>', path)
        )

        # Test: Search for '#include <stdio.h>' in '*.c' files in source at
        #   ./assets/projekt
        expected = ['projekt.c']
        self.assertListEqual(
            expected,
            utilities.search(
                '#include <stdio.h>', path, include=['*.c']
            )
        )

        # Test: Search for '#include <stdio.h>' in '*.c' and '*.js' files in
        #   source at ./assets/projekt
        expected = ['projekt.c']
        self.assertListEqual(
            expected,
            utilities.search(
                '#include <stdio.h>', path, include=['*.c', '*.js']
            )
        )

        # Test: Search for '#include <stdio.h>' in '*.h' files in source at
        #   ./assets/projekt
        self.assertIsNone(
            utilities.search(
                '#include <stdio.h>', path, include=['*.h']
            )
        )

        # Test: Missing source directory
        self.assertRaises(
            Exception, utilities.search,
            pattern='#include <stdio.h>', path='/home/nocturnal',
        )

        # Test: Path is not a directory
        self.assertRaises(
            Exception, utilities.search,
            pattern='#include <stdio.h>', path='/bin/bash',
        )
def run(project_id, repo_path, cursor, **options):
    cursor.execute('''
        SELECT
            url
        FROM
            projects
        WHERE
            id = {0}
        '''.format(project_id))

    record = cursor.fetchone()

    full_url = record[0]
    json_response = url_to_json(
        full_url,
        headers={'Accept': 'application/vnd.github.drax-preview+json'})

    result = True if 'license' in json_response \
                     and json_response['license'] else False

    if not result:
        for pattern in LICENSE_PATTERNS:
            if utilities.search(pattern, repo_path, ignorecase=True):
                result = True
                break
    print("----- METRIC: LICENSE -----")
    print('result: ', result)
    return result, result
Exemple #3
0
def run(project_id, repo_path, cursor, **options):
    cursor.execute('''
        SELECT
            url
        FROM
            projects
        WHERE
            id = {0}
        '''.format(project_id))

    record = cursor.fetchone()

    full_url = utilities.TOKENIZER.tokenize(record[0].rstrip())
    json_response = url_to_json(full_url, headers={
            'Accept': 'application/vnd.github.drax-preview+json'
        }
    )

    result = True if 'license' in json_response \
                     and json_response['license'] else False

    if not result:
        for pattern in LICENSE_PATTERNS:
            if utilities.search(pattern, repo_path, ignorecase=True):
                result = True
                break

    return result, result
Exemple #4
0
    def test_search(self):
        path = os.path.join(os.path.dirname(__file__), 'assets/projekt')

        # Test: Search for '#include <stdio.h>' in source at ./assets/projekt
        expected = ['projekt.c']
        self.assertListEqual(expected,
                             utilities.search('#include <stdio.h>', path))

        # Test: Search for '#include <stdio.h>' in '*.c' files in source at
        #   ./assets/projekt
        expected = ['projekt.c']
        self.assertListEqual(
            expected,
            utilities.search('#include <stdio.h>', path, include=['*.c']))

        # Test: Search for '#include <stdio.h>' in '*.c' and '*.js' files in
        #   source at ./assets/projekt
        expected = ['projekt.c']
        self.assertListEqual(
            expected,
            utilities.search('#include <stdio.h>',
                             path,
                             include=['*.c', '*.js']))

        # Test: Search for '#include <stdio.h>' in '*.h' files in source at
        #   ./assets/projekt
        self.assertIsNone(
            utilities.search('#include <stdio.h>', path, include=['*.h']))

        # Test: Missing source directory
        self.assertRaises(
            Exception,
            utilities.search,
            pattern='#include <stdio.h>',
            path='/home/nocturnal',
        )

        # Test: Path is not a directory
        self.assertRaises(
            Exception,
            utilities.search,
            pattern='#include <stdio.h>',
            path='/bin/bash',
        )
Exemple #5
0
def license():
    os.chdir(root_directory)
    json_response = {'license': False}
    try:
        flag = False
        for token in git_tokens:
            if(flag == True):
                break
            else:
                try:
                    json_response = url_to_json(URL, headers={
                            'Accept': 'application/vnd.github.drax-preview+json'
                        }
                    )
                    flag = True
                    break
                except:
                    continue
        if(flag == False):
            try:
                print("[Reg: License]Tokens didn't work! Trying out without token...")
                json_response = url_to_json(URL, headers={
                                'Accept': 'application/vnd.github.drax-preview+json'
                            }, authentication=[]
                        )
                print('Fetch ok')
            except:
                print("[Reg: License]Couldn't fetch data from API! Trying out search for patterns in the license files..")
        result = True if 'license' in json_response \
                         and json_response['license'] else False
        if not result:
            for pattern in LICENSE_PATTERNS:
                if utilities.search(pattern, repo_path, ignorecase=True):
                    result = True
                    break
        if(result == True):
            val = (str(int(result)),repo_id)
            QUERY = '''UPDATE `repoquester_results` SET `license`=? WHERE `repo_id`=?'''
            try:
                os.chdir(root_directory)
                conn = sqlite3.connect('repo_quester.db',timeout=180)
                conn.execute(QUERY,val)
                conn.commit()
                conn.close()
                print('LICENSE: ',result)
            except Exception as ex:
                print(ex)
                os.chdir(root_directory)
                conn = sqlite3.connect('repo_quester.db',timeout=180)
                conn.execute(QUERY,val)
                conn.commit()
                conn.close()
                print('LICENSE: ',result,' trying again.. ok')
    except Exception as ex:
        print(ex)
def run(project_id, repo_path, cursor, **options):
    print("----- METRIC: LICENSE -----")
    cursor.execute('''
        SELECT
            url
        FROM
            projects
        WHERE
            id = {0}
        '''.format(project_id))
    git_tokens = options['tokens']
    record = cursor.fetchone()
    full_url = record[0]
    token_avail = False

    # Making a github api request with the tokens provided
    for token in git_tokens:
        if (token_avail == True):
            break
        else:
            try:
                json_response = url_to_json(
                    full_url,
                    headers={
                        'Accept': 'application/vnd.github.drax-preview+json'
                    },
                    authentication=[git_tokens[token], token])
                token_avail = True
            except:
                continue
    # Making api request without token, in the case all OAuth tokens got expired or incorrect tokens provided
    if (token_avail == False):
        try:
            print(
                "[Reg: License]Tokens didn't work! Trying out without token..."
            )
            json_response = url_to_json(
                full_url,
                headers={'Accept': 'application/vnd.github.drax-preview+json'},
                authentication=[])
            print('Fetch Successful')
        except Exception as ex:
            print(ex)
            json_response = None
    result = True if 'license' in json_response \
        and json_response['license'] else False
    if not result:
        for pattern in LICENSE_PATTERNS:
            if utilities.search(pattern, repo_path, ignorecase=True):
                result = True
                break
    print('License: ', result)
    return result, result
Exemple #7
0
    def measure(self, path, sloc, pattern, whole=False):
        proportion = 0
        files = utilities.search(pattern, path, whole=whole, include=self.extensions)
        if files:
            # SLOC of test code
            _slotc = utilities.get_loc(path, files=files)
            slotc = 0
            for language in self.languages:
                if language in _slotc:
                    slotc += _slotc[language]["sloc"]
            proportion = slotc / sloc

        return proportion
Exemple #8
0
def run(project_id, repo_path, cursor, **options):
    print("----- METRIC: LICENSE -----")
    cursor.execute('''
        SELECT
            url
        FROM
            projects
        WHERE
            id = {0}
        '''.format(project_id))
    git_tokens = options['tokens']
    record = cursor.fetchone()
    full_url = record[0]
    token_avail = False
    for user_name in git_tokens:
        if (token_avail == True):
            break
        else:
            try:
                json_response = url_to_json(
                    full_url,
                    headers={
                        'Accept': 'application/vnd.github.drax-preview+json'
                    },
                    authentication=[user_name, git_tokens[user_name]])
                token_avail = True
            except:
                continue
    if (token_avail == False):
        try:
            print(
                "[Reg: License]Tokens didn't work! Trying out without token..."
            )
            json_response = url_to_json(
                full_url,
                headers={'Accept': 'application/vnd.github.drax-preview+json'},
                authentication=[])
            print('Fetch Successful')
        except:
            print(
                "[Reg: License]Couldn't fetch data from API! Trying out search for patterns in the license files.."
            )
    result = True if 'license' in json_response \
        and json_response['license'] else False
    if not result:
        for pattern in LICENSE_PATTERNS:
            if utilities.search(pattern, repo_path, ignorecase=True):
                result = True
                break
    print('License: ', result)
    return result, result
Exemple #9
0
    def measure(self, path, sloc, pattern, whole=False):
        proportion = 0
        files = utilities.search(
            pattern, path, whole=whole, include=self.extensions
        )
        if files:
            # SLOC of test code
            _slotc = utilities.get_loc(path, files=files)
            slotc = 0
            for language in self.languages:
                if language in _slotc:
                    slotc += _slotc[language]['sloc']
            proportion = slotc / sloc

        return proportion