dupgit /
versions
| 1 | #!/usr/bin/env python |
||
| 2 | # -*- coding: utf8 -*- |
||
| 3 | # |
||
| 4 | # bylist.py : related to sites that gives one RSS/Atom feed for |
||
| 5 | # all the projects (such as freshcode.club) |
||
| 6 | # |
||
| 7 | # (C) Copyright 2016 - 2018 Olivier Delhomme |
||
| 8 | # e-mail : [email protected] |
||
| 9 | # |
||
| 10 | # This program is free software; you can redistribute it and/or modify |
||
| 11 | # it under the terms of the GNU General Public License as published by |
||
| 12 | # the Free Software Foundation; either version 3, or (at your option) |
||
| 13 | # any later version. |
||
| 14 | # |
||
| 15 | # This program is distributed in the hope that it will be useful, |
||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 18 | # GNU General Public License for more details. |
||
| 19 | # |
||
| 20 | # You should have received a copy of the GNU General Public License |
||
| 21 | # along with this program; if not, write to the Free Software Foundation, |
||
| 22 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
| 23 | # |
||
| 24 | 1 | import re |
|
| 25 | 1 | import caches |
|
| 26 | 1 | import common |
|
| 27 | |||
| 28 | |||
| 29 | 1 | def cut_title_with_default_method(title): |
|
| 30 | """ |
||
| 31 | Cuts title with a default method and a fallback |
||
| 32 | >>> cut_title_with_default_method('versions 1.3.2') |
||
| 33 | ('versions', '1.3.2') |
||
| 34 | >>> cut_title_with_default_method('no_version_project') |
||
| 35 | ('no_version_project', '') |
||
| 36 | """ |
||
| 37 | |||
| 38 | 1 | try: |
|
| 39 | 1 | (project, version) = title.strip().split(' ', 1) |
|
| 40 | |||
| 41 | 1 | except ValueError: |
|
| 42 | 1 | project = title.strip() |
|
| 43 | 1 | version = '' |
|
| 44 | |||
| 45 | 1 | return (project, version) |
|
| 46 | |||
| 47 | # End of cut_title_with_default_method() function |
||
| 48 | |||
| 49 | |||
| 50 | 1 | def cut_title_with_regex_method(title, regex): |
|
| 51 | """ |
||
| 52 | Cuts title using a regex. If it does not success |
||
| 53 | fallback to default. |
||
| 54 | >>> cut_title_with_regex_method('versions 1.3.2', '([\w]+)\s([\d\.]+)') |
||
| 55 | ('versions', '1.3.2', False) |
||
| 56 | >>> cut_title_with_regex_method('versions 1.3.2', '([\w]+)notgood\s([\d\.]+)') |
||
| 57 | ('', '', True) |
||
| 58 | """ |
||
| 59 | |||
| 60 | 1 | default = False |
|
| 61 | 1 | project = '' |
|
| 62 | 1 | version = '' |
|
| 63 | |||
| 64 | 1 | res = re.match(regex, title) |
|
| 65 | 1 | if res: |
|
| 66 | 1 | project = res.group(1) |
|
| 67 | 1 | version = res.group(2) |
|
| 68 | else: |
||
| 69 | 1 | default = True |
|
| 70 | |||
| 71 | 1 | return (project, version, default) |
|
| 72 | |||
| 73 | # End of cut_title_with_regex_method() function |
||
| 74 | |||
| 75 | |||
| 76 | 1 | def cut_title_in_project_version(title, regex): |
|
| 77 | """ |
||
| 78 | Cuts the title into a tuple (project, version) where possible with a regex |
||
| 79 | or if there is no regex or the regex did not match cuts the title with a |
||
| 80 | default method |
||
| 81 | """ |
||
| 82 | 1 | default = False |
|
| 83 | |||
| 84 | 1 | if regex is not None: |
|
| 85 | 1 | (project, version, default) = cut_title_with_regex_method(title, regex) |
|
| 86 | else: |
||
| 87 | 1 | default = True |
|
| 88 | |||
| 89 | 1 | if default: |
|
| 90 | 1 | (project, version) = cut_title_with_default_method(title) |
|
| 91 | |||
| 92 | 1 | return (project, version) |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 93 | |||
| 94 | # End of cut_title_in_project_version() function |
||
| 95 | |||
| 96 | |||
| 97 | 1 | def lower_list_of_strings(project_list): |
|
| 98 | """ |
||
| 99 | Lowers every string in the list to ease sorting and comparisons |
||
| 100 | """ |
||
| 101 | |||
| 102 | 1 | project_list_low = [project.lower() for project in project_list] |
|
| 103 | |||
| 104 | 1 | return project_list_low |
|
| 105 | |||
| 106 | # End of lower_list_of_strings() function |
||
| 107 | |||
| 108 | |||
| 109 | 1 | def split_multiproject_title_into_list(title, multiproject): |
|
| 110 | """ |
||
| 111 | Splits title into a list of projects according to multiproject being |
||
| 112 | a list of separators |
||
| 113 | """ |
||
| 114 | |||
| 115 | 1 | if multiproject is not None: |
|
| 116 | 1 | titles = re.split(multiproject, title) |
|
| 117 | else: |
||
| 118 | 1 | titles = [title] |
|
| 119 | |||
| 120 | 1 | return titles |
|
| 121 | |||
| 122 | # End of split_multiproject_title_into_list() function |
||
| 123 | |||
| 124 | |||
| 125 | 1 | def check_and_update_feed(feed_list, project_list, cache, debug, regex, multiproject): |
|
| 126 | """ |
||
| 127 | Checks every feed entry in the list against project list cache and |
||
| 128 | then updates the dictionnary then writes the cache file to the disk. |
||
| 129 | - feed_list is a list of feed (from feedparser module) |
||
| 130 | - project_list is the list of project as read from the yaml |
||
| 131 | configuration file |
||
| 132 | - cache is an initialized instance of FileCache |
||
| 133 | """ |
||
| 134 | |||
| 135 | # Lowers the list before searching in it |
||
| 136 | 1 | project_list_low = lower_list_of_strings(project_list) |
|
| 137 | |||
| 138 | # Checking every feed entry that are newer than the last check |
||
| 139 | # and updates the dictionnary accordingly |
||
| 140 | 1 | for entry in feed_list: |
|
| 141 | |||
| 142 | 1 | titles = split_multiproject_title_into_list(entry.title, multiproject) |
|
| 143 | |||
| 144 | 1 | for title in titles: |
|
| 145 | 1 | (project, version) = cut_title_in_project_version(title, regex) |
|
| 146 | 1 | common.print_debug(debug, u'\tChecking {0:16}: {1}'.format(project, version)) |
|
| 147 | 1 | if project.lower() in project_list_low: |
|
| 148 | 1 | cache.print_if_newest_version(project, version, debug) |
|
| 149 | 1 | cache.update_cache_dict(project, version, debug) |
|
| 150 | |||
| 151 | 1 | cache.write_cache_file() |
|
| 152 | |||
| 153 | # End of check_and_update_feed() function |
||
| 154 | |||
| 155 | |||
| 156 | 1 | def check_versions_for_list_sites(feed_project_list, url, cache_filename, feed_filename, local_dir, debug, regex, multiproject): |
|
| 157 | """ |
||
| 158 | Checks projects of 'list' type sites such as freshcode's web site's RSS |
||
| 159 | """ |
||
| 160 | |||
| 161 | 1 | freshcode_cache = caches.FileCache(local_dir, cache_filename) |
|
| 162 | |||
| 163 | 1 | feed_info = caches.FeedCache(local_dir, feed_filename) |
|
| 164 | 1 | feed_info.read_cache_feed() |
|
| 165 | |||
| 166 | 1 | feed = common.get_feed_entries_from_url(url) |
|
| 167 | |||
| 168 | 1 | if feed is not None: |
|
| 169 | 1 | common.print_debug(debug, u'\tFound {} entries'.format(len(feed.entries))) |
|
| 170 | 1 | feed_list = common.make_list_of_newer_feeds(feed, feed_info, debug) |
|
| 171 | 1 | common.print_debug(debug, u'\tFound {} new entries (relative to {})'.format(len(feed_list), feed_info.date_minutes)) |
|
| 172 | |||
| 173 | 1 | check_and_update_feed(feed_list, feed_project_list, freshcode_cache, debug, regex, multiproject) |
|
| 174 | |||
| 175 | # Updating feed_info with the latest parsed feed entry date |
||
| 176 | 1 | feed_info.update_cache_feed(feed.entries[0].published_parsed) |
|
| 177 | |||
| 178 | 1 | feed_info.write_cache_feed() |
|
| 179 | |||
| 180 | # End of check_versions_for_list_sites() function |
||
| 181 | |||
| 182 | 1 | def check_versions(versions_conf, list_site_list): |
|
| 183 | """ |
||
| 184 | Checks version by checking each project's feed. |
||
| 185 | """ |
||
| 186 | |||
| 187 | 1 | for site_name in list_site_list: |
|
| 188 | 1 | common.print_debug(versions_conf.options.debug, u'Checking {} updates'.format(site_name)) |
|
| 189 | 1 | (project_list, project_url, cache_filename, project_entry) = versions_conf.get_infos_for_site(site_name) |
|
| 190 | 1 | regex = versions_conf.extract_regex_from_site(site_name) |
|
| 191 | 1 | multiproject = versions_conf.extract_multiproject_from_site(site_name) |
|
| 192 | 1 | feed_filename = u'{}.feed'.format(site_name) |
|
| 193 | 1 | check_versions_for_list_sites(project_list, project_url, cache_filename, feed_filename, versions_conf.local_dir, versions_conf.options.debug, regex, multiproject) |
|
| 194 | |||
| 195 | # End of check_versions() function |
||
| 196 |