forked from v3io/v3io-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpypi_upload.py
124 lines (97 loc) · 3.75 KB
/
pypi_upload.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# Copyright 2018 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Upload packages to PyPI"""
from argparse import ArgumentParser
from glob import glob
from os import environ, path
from shutil import rmtree
from subprocess import run
from sys import executable
def should_upload():
repo = environ.get('TRAVIS_REPO_SLUG')
tag = environ.get('TRAVIS_TAG')
return repo == 'v3io/v3io-py' and tag
def git_sha():
return environ.get('TRAVIS_COMMIT', '')[:7]
def set_version():
version = environ.get('TRAVIS_TAG')
assert version, 'no tag'
if version[0] == 'v':
version = version[1:]
if version.endswith('.py'):
version = version[:-3]
lines = []
init_py = 'v3io/__init__.py'
with open(init_py) as fp:
for line in fp:
if '__version__' in line:
line = "__version__ = '{}'\n".format(version)
lines.append(line)
with open(init_py, 'w') as out:
out.write(''.join(lines))
if __name__ == '__main__':
parser = ArgumentParser(description=__doc__)
parser.add_argument(
'--force', '-f', help='force upload', action='store_true')
parser.add_argument(
'--skip-artifactory', help='disable uploading to Artifactory', action='store_true')
parser.add_argument(
'--test', '-t', help='upload to testpypi', action='store_true')
parser.add_argument(
'--user', '-u', help='pypi user (or V3IO_PYPI_USER)', default='')
parser.add_argument(
'--password', '-p', help='pypi password (or V3IO_PYPI_PASSWORD)',
default='')
args = parser.parse_args()
ok = args.force or should_upload()
if not ok:
raise SystemExit('error: wrong branch or repo (try with --force)')
if path.exists('dist'):
rmtree('dist')
set_version()
for dist in ('sdist', 'bdist_wheel'):
out = run([executable, 'setup.py', dist])
if out.returncode != 0:
raise SystemExit('error: cannot build {}'.format(dist))
user = args.user or environ.get('V3IO_PYPI_USER')
passwd = args.password or environ.get('V3IO_PYPI_PASSWORD')
if not (user and passwd):
print('warning: missing login information - skipping upload')
raise SystemExit()
cmd = [
'twine', 'upload',
'--user', user,
'--password', passwd,
] + glob('dist/v3io-*')
cmd = cmd + ['-r', 'testpypi'] if args.test else cmd
out = run(cmd)
if out.returncode != 0:
raise SystemExit('error: cannot upload to pypi')
artifactory_user = environ.get('V3IO_ARTIFACTORY_USER')
artifactory_passwd = environ.get('V3IO_ARTIFACTORY_PASSWORD')
artifactory_repo = environ.get('ARTIFACTORY_PYPI_URL')
if not args.skip_artifactory:
if not (artifactory_user and artifactory_passwd or artifactory_repo):
print('warning: missing artifactory information - skipping upload')
raise SystemExit()
cmd = [
'twine', 'upload',
'--user', artifactory_user,
'--password', artifactory_passwd,
'--repository-url', artifactory_repo,
] + glob('dist/v3io-*')
out = run(cmd)
if out.returncode != 0:
raise SystemExit('error: cannot upload to Artifactory')