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.
Passed
Push — master ( 2e6b2b...d81657 )
by dup
02:17
created

byproject.check_versions_feeds_by_projects()   B

Complexity

Conditions 6

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 14
nop 7
dl 0
loc 31
ccs 14
cts 14
cp 1
crap 6
rs 8.6666
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
    """
69
70 1
    regex = ''
71 1
    entry = ''
72 1
    name = project
73 1
    valued = False
74
75 1
    if type(project) is dict:
76 1
        if 'name' in project:
77 1
            name = project['name']
78
79 1
        if 'regex' in project:
80 1
            regex = project['regex']
81 1
            valued = True
82
83 1
        if 'entry' in project:
84 1
            entry = project['entry']
85 1
            valued = True
86
87 1
    return (valued, name, regex, entry)
88
89
# End of get_values_from_project() function
90
91
92 1
def sort_feed_list(feed_list, feed):
93
    """
94
    Sorts the feed list with the right attribute which depends on the feed.
95
    sort is reversed because feed_list is build by inserting ahead when
96
    parsing the feed from the most recent to the oldest entry.
97
    Returns a sorted list (by date) the first entry is the newest one.
98
    """
99
100 1
    if feed.entries[0]:
101 1
        if 'published_parsed' in feed.entries[0]:
102
            feed_list = sorted(feed_list, key=operator.attrgetter('published_parsed'), reverse=True)
103 1
        elif 'updated_parsed' in feed.entries[0]:
104 1
            feed_list = sorted(feed_list, key=operator.attrgetter('updated_parsed'), reverse=True)
105
106 1
    return feed_list
107
108
# End of sort_feed_list() function
109
110
111 1
def get_releases_filtering_feed(debug, local_dir, filename, feed, entry):
112
    """
113
    Filters the feed and returns a list of releases with one
114
    or more elements
115
    """
116
117 1
    feed_list = []
118
119 1
    if is_entry_last_checked(entry):
120 1
        feed_info = caches.FeedCache(local_dir, filename)
121 1
        feed_info.read_cache_feed()
122 1
        feed_list = common.make_list_of_newer_feeds(feed, feed_info, debug)
123 1
        feed_list = sort_feed_list(feed_list, feed)
124
125
        # Updating feed_info with the latest parsed feed entry date
126 1
        if len(feed_list) >= 1:
127 1
            published_date = common.get_entry_published_date(feed_list[0])
128 1
            feed_info.update_cache_feed(published_date)
129
130 1
        feed_info.write_cache_feed()
131
132
    else:
133 1
        feed_list.insert(0, feed.entries[0])
134
135 1
    return feed_list
136
137
138 1
def get_latest_release_by_title(project, debug, feed_url, local_dir, feed_filename, project_entry):
139
    """
140
    Gets the latest release or the releases between the last checked time of
141
    a program on a site of type 'byproject'.
142
    project must be a string that represents the project (user/repository in
143
    github for instance).
144
    Returns a tuple which contains the name of the project, a list of versions
145
    and a boolean that indicates if we checked by last checked time (True) or
146
    by release (False).
147
    """
148
149 1
    feed_list = []
150
151 1
    (valued, name, regex, entry) = get_values_from_project(project)
152
    
153 1
    if is_entry_last_checked(project_entry):
154 1
        last_checked = True
155 1
        entry = project_entry
156
    else:
157 1
        last_checked = is_entry_last_checked(entry)
158 1
    filename = format_project_feed_filename(feed_filename, name)
159
160 1
    url = feed_url.format(name)
161 1
    feed = common.get_feed_entries_from_url(url)
162
163 1
    if feed is not None and len(feed.entries) > 0:
164 1
        feed_list = get_releases_filtering_feed(debug, local_dir, filename, feed, entry)
165
166 1
        if valued and regex != '':
167
            # Here we match the whole list against the regex and replace the
168
            # title's entry of the result of that match upon success.
169 1
            for entry in feed_list:
170 1
                res = re.match(regex, entry.title)
171
                # Here we should make a new list with the matched entries and leave tho other ones
172 1
                if res:
173 1
                    entry.title = res.group(1)
174 1
                common.print_debug(debug, u'\tname: {}\n\tversion: {}\n\tregex: {} : {}'.format(name, entry.title, regex, res))
175
176 1
        common.print_debug(debug, u'\tProject {}: {}'.format(name, entry.title))
177
178 1
    return (name, feed_list, last_checked)
179
180
# End of get_latest_release_by_title() function
181
182
183 1
def check_versions_feeds_by_projects(project_list, local_dir, debug, feed_url, cache_filename, feed_filename, project_entry):
184
    """
185
    Checks project's versions on feed_url if any are defined in the yaml
186
    file under the specified tag that got the project_list passed as an argument.
187
    """
188
189 1
    site_cache = caches.FileCache(local_dir, cache_filename)
190
191 1
    for project in project_list:
192 1
        (name, feed_list, last_checked) = get_latest_release_by_title(project, debug, feed_url, local_dir, feed_filename, project_entry)
193
194
195 1
        if len(feed_list) >= 1:
196
            # Updating the cache with the latest version (the first entry)
197 1
            version = feed_list[0].title
198
199 1
            if not last_checked:
200
                # printing only for latest release as last checked is
201
                # already filtered and to be printed entirely
202 1
                site_cache.print_if_newest_version(name, version, debug)
203
204 1
            site_cache.update_cache_dict(name, version, debug)
205
206 1
            if not last_checked:
207
                # we already printed this.
208 1
                del feed_list[0]
209
210 1
        for entry in feed_list:
211 1
            common.print_project_version(name, entry.title)
212
213 1
    site_cache.write_cache_file()
214
215
# End of check_versions_feeds_by_projects() function
216
217
218 1
def check_versions(versions_conf, byproject_site_list):
219
    """
220
    Checks version by checking each project's feed.
221
    """
222
223 1
    for site_name in byproject_site_list:
224 1
        common.print_debug(versions_conf.options.debug, u'Checking {} projects'.format(site_name))
225 1
        (project_list, project_url, cache_filename, project_entry) = versions_conf.get_infos_for_site(site_name)
226 1
        feed_filename = u'{}.feed'.format(site_name)
227 1
        check_versions_feeds_by_projects(project_list, versions_conf.local_dir, versions_conf.options.debug, project_url, cache_filename, feed_filename, project_entry)
228
229
# End of check_versions() function.
230