lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Release script
"""
 
import glob
import os
import shutil
import subprocess
import sys
 
import click
 
@click.group()
def cli():
    pass
 
@cli.command()
def build():
    DIST_PATH = 'dist'
    if os.path.exists(DIST_PATH) and os.listdir(DIST_PATH):
        if click.confirm('{} is not empty - delete contents?'.format(DIST_PATH)):
            shutil.rmtree(DIST_PATH)
            os.makedirs(DIST_PATH)
        else:
            click.echo('Aborting')
            sys.exit(1)
 
    subprocess.check_call(['python', 'setup.py', 'bdist_wheel'])
    subprocess.check_call(['python', 'setup.py', 'sdist',
                           '--formats=gztar'])
 
@cli.command()
def sign():
    # Sign all the distribution files
    for fpath in glob.glob('dist/*'):
        subprocess.check_call(['gpg', '--armor', '--output', fpath + '.asc',
                               '--detach-sig', fpath])
 
    # Verify the distribution files
    for fpath in glob.glob('dist/*'):
        if fpath.endswith('.asc'):
            continue
 
        subprocess.check_call(['gpg', '--verify', fpath + '.asc', fpath])
 
 
@cli.command()
@click.option('--passfile', default=None)
@click.option('--release/--no-release', default=False)
def upload(passfile, release):
    if release:
        repository='pypi'
    else:
        repository='pypitest'
 
    env = os.environ.copy()
    if passfile is not None:
        gpg_call = subprocess.run(['gpg', '-d', passfile],
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
 
        username, password = gpg_call.stdout.decode('utf-8').split('\n')
        env['TWINE_USERNAME'] = username
        env['TWINE_PASSWORD'] = password
 
    dist_files = glob.glob('dist/*')
    for dist_file in dist_files:
        if dist_file.endswith('.asc'):
            continue
        if dist_file + '.asc' not in dist_files:
            raise ValueError('Missing signature file for: {}'.format(dist_file))
 
    args = ['twine', 'upload', '-r', repository] + dist_files
    
    p = subprocess.Popen(args, env=env)
    p.wait()
 
if __name__ == "__main__":
    cli()