GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( aa7ca1...d88e79 )
by dup
02:21
created

byproject.check_versions_feeds_by_projects()   A

Complexity

Conditions 5

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 13
nop 7
dl 0
loc 29
ccs 13
cts 13
cp 1
crap 5
rs 9.2833
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf8 -*-
3
#
4
#  byproject.py : related to sites that gives an RSS/Atom feed for
5
#                 each project (such as github)
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 os
25 1
import operator
26 1
import re
27 1
import caches
28 1
import common
29
30
31 1
def format_project_feed_filename(feed_filename, name):
32
    """
33
    Returns a valid filename formatted based on feed_filename (the site name)
34
    and name (the name of the project).
35
    """
36
37 1
    (root, ext) = os.path.splitext(feed_filename)
38 1
    norm_name = name.replace('/', '_')
39
40 1
    filename = "{}_{}{}".format(root, norm_name, ext)
41
42 1
    return filename
43
44
# End of format_project_feed_filename() function
45
46
47 1
def is_entry_last_checked(entry):
48
    """
49
    Returns true if entry is equal to last checked and
50
    false otherwise.
51
    >>> is_entry_last_checked('last checked')
52
    True
53
    >>> is_entry_last_checked('')
54
    False
55
    >>> is_entry_last_checked('latest')
56
    False
57
    """
58
59 1
    return entry == 'last checked'
60
61
# End of is_entry_last_checked() function
62
63
64 1
def get_values_from_project(project):
65
    """
66
    Gets the values of 'regex' and 'name' keys if found and
67
    returns a tuple (valued, name, regex, entry)
68
    >>> project = {'name': 'version', 'regex': 'v([\d\.]+)\s*:.*', 'entry': 'entry'}
69
    >>> get_values_from_project(project)
70
    (True, 'version', 'v([\\\\d\\\\.]+)\\\\s*:.*', 'entry')
71
    >>> project = {'name': 'version'}
72
    >>> get_values_from_project(project)
73
    (False, 'version', '', '')
74
    """
75
76 1
    regex = ''
77 1
    entry = ''
78 1
    name = project
79 1
    valued = False
80
81 1
    if type(project) is dict:
82 1
        if 'name' in project:
83 1
            name = project['name']
84
85 1
        if 'regex' in project:
86 1
            regex = project['regex']
87 1
            valued = True
88
89 1
        if 'entry' in project:
90 1
            entry = project['entry']
91 1
            valued = True
92
93 1
    return (valued, name, regex, entry)
94
95
# End of get_values_from_project() function
96
97
98 1
def sort_feed_list(feed_list, feed):
99
    """
100
    Sorts the feed list with the right attribute which depends on the feed.
101
    sort is reversed because feed_list is build by inserting ahead when
102
    parsing the feed from the most recent to the oldest entry.
103
    Returns a sorted list (by date) the first entry is the newest one.
104
    """
105
106 1
    if feed.entries[0]:
107 1
        (published_date, field_name) = common.get_entry_published_date(feed.entries[0])
108 1
        if field_name != '':
109 1
            feed_list = sorted(feed_list, key=operator.attrgetter(field_name), reverse=True)
110
111 1
    return feed_list
112
113
# End of sort_feed_list() function
114
115
116 1
def get_releases_filtering_feed(debug, local_dir, filename, feed, entry):
117
    """
118
    Filters the feed and returns a list of releases with one
119
    or more elements
120
    """
121
122 1
    feed_list = []
123
124 1
    if is_entry_last_checked(entry):
125 1
        feed_info = caches.FeedCache(local_dir, filename)
126 1
        feed_info.read_cache_feed()
127 1
        feed_list = common.make_list_of_newer_feeds(feed, feed_info, debug)
128 1
        feed_list = sort_feed_list(feed_list, feed)
129
130
        # Updating feed_info with the latest parsed feed entry date
131 1
        if len(feed_list) >= 1:
132 1
            (published_date, field_name) = common.get_entry_published_date(feed_list[0])
133 1
            feed_info.update_cache_feed(published_date)
134
135 1
        feed_info.write_cache_feed()
136
137
    else:
138 1
        feed_list.insert(0, feed.entries[0])
139
140 1
    return feed_list
141
142
143 1
def get_latest_release_by_title(project, debug, feed_url, local_dir, feed_filename, project_entry):
144
    """
145
    Gets the latest release or the releases between the last checked time of
146
    a program on a site of type 'byproject'.
147
    project must be a string that represents the project (user/repository in
148
    github for instance).
149
    Returns a tuple which contains the name of the project, a list of versions
150
    and a boolean that indicates if we checked by last checked time (True) or
151
    by release (False).
152
    """
153
154 1
    feed_list = []
155
156 1
    (valued, name, regex, entry) = get_values_from_project(project)
157
158 1
    if is_entry_last_checked(project_entry):
159 1
        last_checked = True
160 1
        entry = project_entry
161
    else:
162 1
        last_checked = is_entry_last_checked(entry)
163 1
    filename = format_project_feed_filename(feed_filename, name)
164
165 1
    url = feed_url.format(name)
166 1
    feed = common.get_feed_entries_from_url(url)
167
168 1
    if feed is not None and len(feed.entries) > 0:
169 1
        feed_list = get_releases_filtering_feed(debug, local_dir, filename, feed, entry)
170
171 1
        if valued and regex != '':
172
            # Here we match the whole list against the regex and replace the
173
            # title's entry of the result of that match upon success.
174 1
            for feed_entry in feed_list:
175 1
                res = re.match(regex, feed_entry.title)
176
                # Here we should make a new list with the matched entries and leave the other ones
177 1
                if res:
178 1
                    feed_entry.title = res.group(1)
179 1
                common.print_debug(debug, u'\tname: {}\n\tversion: {}\n\tregex: {} : {}'.format(name, feed_entry.title, regex, res))
180
181 1
            common.print_debug(debug, u'\tProject {}: {}'.format(name, feed.entries[0].title))
182
183 1
    return (name, feed_list, last_checked)
184
185
# End of get_latest_release_by_title() function
186
187
188 1
def check_versions_feeds_by_projects(project_list, local_dir, debug, feed_url, cache_filename, feed_filename, project_entry):
189
    """
190
    Checks project's versions on feed_url if any are defined in the yaml
191
    file under the specified tag that got the project_list passed as an argument.
192
    """
193
194 1
    site_cache = caches.FileCache(local_dir, cache_filename)
195
196 1
    for project in project_list:
197 1
        (name, feed_list, last_checked) = get_latest_release_by_title(project, debug, feed_url, local_dir, feed_filename, project_entry)
198
199 1
        if len(feed_list) >= 1:
200
            # Updating the cache with the latest version (the first feed entry)
201 1
            version = feed_list[0].title
202
203 1
            if not last_checked:
204
                # printing only for latest release as last checked is
205
                # already filtered and to be printed entirely
206 1
                site_cache.print_if_newest_version(name, version, debug)
207
                # we already printed this.
208 1
                del feed_list[0]
209
210 1
            site_cache.update_cache_dict(name, version, debug)
211
212
        # Printing all entries in the list.
213 1
        for feed_entry in feed_list:
214 1
            common.print_project_version(name, feed_entry.title)
215
216 1
    site_cache.write_cache_file()
217
218
# End of check_versions_feeds_by_projects() function
219
220
221 1
def check_versions(versions_conf, byproject_site_list):
222
    """
223
    Checks version by checking each project's feed.
224
    """
225
226 1
    for site_name in byproject_site_list:
227 1
        common.print_debug(versions_conf.options.debug, u'Checking {} projects'.format(site_name))
228 1
        (project_list, project_url, cache_filename, project_entry) = versions_conf.get_infos_for_site(site_name)
229 1
        feed_filename = u'{}.feed'.format(site_name)
230 1
        check_versions_feeds_by_projects(project_list, versions_conf.local_dir, versions_conf.options.debug, project_url, cache_filename, feed_filename, project_entry)
231
232
# End of check_versions() function.
233