psutil

Форк
0
/
download_wheels_github.py 
108 строк · 3.0 Кб
1
#!/usr/bin/env python3
2

3
# Copyright (c) 2009 Giampaolo Rodola'. All rights reserved.
4
# Use of this source code is governed by a BSD-style license that can be
5
# found in the LICENSE file.
6

7
"""Script which downloads wheel files hosted on GitHub:
8
https://github.com/giampaolo/psutil/actions
9
It needs an access token string generated from personal GitHub profile:
10
https://github.com/settings/tokens
11
The token must be created with at least "public_repo" scope/rights.
12
If you lose it, just generate a new token.
13
REST API doc:
14
https://developer.github.com/v3/actions/artifacts/.
15
"""
16

17
import argparse
18
import json
19
import os
20
import sys
21
import zipfile
22

23
import requests
24

25
from psutil import __version__
26
from psutil._common import bytes2human
27
from psutil.tests import safe_rmpath
28

29

30
USER = "giampaolo"
31
PROJECT = "psutil"
32
PROJECT_VERSION = __version__
33
OUTFILE = "wheels-github.zip"
34
TOKEN = ""
35
TIMEOUT = 30
36

37

38
def get_artifacts():
39
    base_url = "https://api.github.com/repos/%s/%s" % (USER, PROJECT)
40
    url = base_url + "/actions/artifacts"
41
    res = requests.get(
42
        url=url, headers={"Authorization": "token %s" % TOKEN}, timeout=TIMEOUT
43
    )
44
    res.raise_for_status()
45
    data = json.loads(res.content)
46
    return data
47

48

49
def download_zip(url):
50
    print("downloading: " + url)
51
    res = requests.get(
52
        url=url, headers={"Authorization": "token %s" % TOKEN}, timeout=TIMEOUT
53
    )
54
    res.raise_for_status()
55
    totbytes = 0
56
    with open(OUTFILE, 'wb') as f:
57
        for chunk in res.iter_content(chunk_size=16384):
58
            f.write(chunk)
59
            totbytes += len(chunk)
60
    print("got %s, size %s)" % (OUTFILE, bytes2human(totbytes)))
61

62

63
def rename_win27_wheels():
64
    # See: https://github.com/giampaolo/psutil/issues/810
65
    src = 'dist/psutil-%s-cp27-cp27m-win32.whl' % PROJECT_VERSION
66
    dst = 'dist/psutil-%s-cp27-none-win32.whl' % PROJECT_VERSION
67
    if os.path.exists(src):
68
        print("rename: %s\n        %s" % (src, dst))
69
        os.rename(src, dst)
70
    src = 'dist/psutil-%s-cp27-cp27m-win_amd64.whl' % PROJECT_VERSION
71
    dst = 'dist/psutil-%s-cp27-none-win_amd64.whl' % PROJECT_VERSION
72
    if os.path.exists(src):
73
        print("rename: %s\n        %s" % (src, dst))
74
        os.rename(src, dst)
75

76

77
def run():
78
    data = get_artifacts()
79
    download_zip(data['artifacts'][0]['archive_download_url'])
80
    os.makedirs('dist', exist_ok=True)
81
    with zipfile.ZipFile(OUTFILE, 'r') as zf:
82
        zf.extractall('dist')
83
    rename_win27_wheels()
84

85

86
def main():
87
    global TOKEN
88
    parser = argparse.ArgumentParser(description='GitHub wheels downloader')
89
    parser.add_argument('--token')
90
    parser.add_argument('--tokenfile')
91
    args = parser.parse_args()
92

93
    if args.tokenfile:
94
        with open(os.path.expanduser(args.tokenfile)) as f:
95
            TOKEN = f.read().strip()
96
    elif args.token:
97
        TOKEN = args.token
98
    else:
99
        return sys.exit('specify --token or --tokenfile args')
100

101
    try:
102
        run()
103
    finally:
104
        safe_rmpath(OUTFILE)
105

106

107
if __name__ == '__main__':
108
    main()
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.