ListPackagesAction   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 17

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 37 17
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional pkg_information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import requests
17
import six.moves.http_client as http_client
18
19
from st2actions.runners import pythonrunner
20
21
__all__ = [
22
    'ListPackagesAction'
23
]
24
25
BASE_URL = 'https://%(api_token)s:@packagecloud.io/api/v1/repos/%(repo)s/packages.json'
26
MAX_PAGE_NUMBER = 100
27
28
29
class ListPackagesAction(pythonrunner.Action):
30
    def run(self, repo, package, distro_version, version, release, api_token, per_page=200):
31
        params = {'per_page': per_page}
32
        values = {'repo': repo, 'api_token': api_token}
33
        url = BASE_URL % values
34
35
        page = 1
36
        packages = []
37
38
        while page < MAX_PAGE_NUMBER:
39
            page_url = url + '?page=' + str(page)
40
            response = requests.get(url=page_url, params=params)
41
42
            if response.status_code != http_client.OK:
43
                raise Exception(response.text)
44
45
            packages += response.json()
46
47
            if len(packages) >= int(response.headers.get('Total', 0)):
48
                break
49
50
            page += 1
51
52
        if package:
53
            packages = [pkg_info for pkg_info in packages if pkg_info['name'] == package]
54
55
        if distro_version:
56
            packages = [
57
                pkg_info for pkg_info in packages if pkg_info['distro_version'] == distro_version
58
            ]
59
60
        if version:
61
            packages = [pkg_info for pkg_info in packages if pkg_info['version'] == version]
62
63
        if release:
64
            packages = [pkg_info for pkg_info in packages if pkg_info['release'] == release]
65
66
        return sorted(packages, key=lambda x: (x['version'], x['release']), reverse=True)
67