Exemple #1
0
def get_search_criteria(config, section_name):
    """Read in new search criteria from configuration file.

    Returns:
        `SearchCriteria`
    """
    label = section_name[7:]
    regex = ''
    git_project_location = ''
    exclude_these_file_extensions_list = ''
    case_sensitive = False
    github_link = ''

    name_value_pairs = config.items(section_name)
    for name, value in name_value_pairs:
        if name == 'regex':
            regex = value
        elif name == 'git_project_location':
            git_project_location = value
        elif name == 'exclude_these_file_extensions':
            exclude_these_file_extensions_list = value
        elif name == 'case_sensitive':
            case_sensitive = string2bool(value)
        elif name == 'github_link':
            github_link = value
        else:
            sys.exit(("Invalid configuration syntax '%s' under section '%s' in "
                      "'%s'. Please fix.") %
                     (name, section_name, const.config_filename))

    search_crit = SearchCriteria(label)
    search_crit.regex = regex
    search_crit.git_project_location = git_project_location
    if case_sensitive:
        search_crit.case_sensitive = True # False by default in class

    #split list as string into actual list
    if is_not_blank_or_whitespace(exclude_these_file_extensions_list):
        search_crit.exclude_these_file_extensions = exclude_these_file_extensions_list.split(',')

    search_crit.github_link = github_link

    return search_crit
Exemple #2
0
def main():
    """Get search config, perform searches, output results, get paid."""

    result = set_globals_from_config()
    searches, file_extension_exclusions, commit_hash_exclusions = result

    html_file = None
    if const.output_format == 'HTML':
        html_filename = 'git-monitor_%s.html' % get_timestamp_filename_friendly()
        html_file = open(html_filename, "a")
        html_file_printline(html_file,
                            ("<!DOCTYPE html><html><!--generated by "
                             "git-monitor.py--><head><style>table{"
                             "border-collapse:collapse;}table,td,th{border:1px "
                             "solid black;}</style></head><body>"))

    for search in searches:
        results = get_search_results(
            search, file_extension_exclusions, commit_hash_exclusions)

        if results != None and len(results) != 0:
            msg = ("Found %d matches for search criteria labeled '%s':" %
                   (len(results), search.label))
            if const.output_format == 'HTML':
                html_file_printline(html_file, '<p>' + msg + '</p>')
            elif const.output_format == 'plaintext':
                print msg

            if const.output_format == 'HTML':
                html_file_printline(html_file,
                                    ("<table><tr><td><u>#</u></td><td>"
                                     "<u>commit hash</u></<td><td><u>file path "
                                     "and name</u></td><td><u>match</u></td>"
                                     "<td><u>github link</u></td></tr>"))

            result_num = 0
            for result in results:
                result_num = result_num + 1
                if const.output_format == 'HTML':
                    search_result = (("<tr><td>%d</td><td>%s</td><td>%s</td>"
                                      "<td>%s</td><td>") %
                                     (result_num, result.commit_hash,
                                      result.file_path_and_name,
                                      result.match_string))
                    if is_not_blank_or_whitespace(search.github_link):
                        search_result += generate_github_link(
                            project_link=search.github_link,
                            commit_hash=result.commit_hash,
                            file_path_and_name=result.file_path_and_name)
                    search_result += '</td></tr>' #end HTML row
                    html_file_printline(html_file, search_result)
                elif const.output_format == 'plaintext':
                    print("\t%s:%s:%s" % (result.commit_hash,
                                          result.file_path_and_name,
                                          result.match_string))

            if const.output_format == 'HTML':
                html_file_printline(html_file, '</table>') #end table for this search
        else:
            msg = "No matches for search criteria labeled '%s'." % search.label
            if const.output_format == 'HTML':
                html_file_printline(html_file, "<p>%s</p>" % msg)
            elif const.output_format == 'plaintext':
                print msg

    if const.output_format == 'HTML':
        html_file_printline(html_file,
                            '<!-- done with search results --></body></html>')
        html_file.close()