-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathpypi.py
130 lines (80 loc) · 2.75 KB
/
pypi.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
# pylint: disable=C0321,C0103,C0301,E1305,E1121,C0302,C0330,C0111,W0613,W0611,R1705
# -*- coding: utf-8 -*-
"""
Pypi Uploader
python pypi.py
1) Create the package in one folder
2)
A file called .pypirc that has to contain login credentials.
$ ~ YOURTEXTEDITOR ~/.pypirc
Open a file and paste this to in it:
[pypi]
username = token
password = pypi-AgEI
"""
import subprocess
import re, os, sys
import os.path as op
curdir = op.abspath(op.curdir)
setup_file = op.join(curdir, 'setup.py')
class Version(object):
pattern = re.compile(r"(version\s*=\s*['\"]\s*(\d+)\s*\.\s*(\d+)\s*\.\s*(\d+)\s*['\"])")
def __init__(self, major, minor, patch):
self.major = int(major)
self.minor = int(minor)
self.patch = int(patch)
def __str__(self):
return f'Version({self.stringify()})'
def __repr__(self):
return self.__str__()
def stringify(self):
return f'\'{self.major}.{self.minor}.{self.patch}\''
def new_version(self, orig):
return '='.join([orig.split('=')[0], self.stringify()])
@classmethod
def parse(cls, string):
re_result = re.findall(cls.pattern, string)
if len(re_result) == 0:
return Exception('Program was not able to parse version string, please check your setup.py file.')
return re_result[0][0], cls(*re_result[0][1:])
def ask(question, ans='yes'):
return input(question).lower() == ans.lower()
def pypi_upload():
"""
It requires credential in .pypirc files
__token__
"""
os.system('python setup.py sdist bdist_wheel')
os.system('twine upload dist/*')
print("Upload files")
print("Deleting build files")
for item in os.listdir(op.abspath(op.join(setup_file, '..'))):
if item.endswith('.egg-info') or item in ['dist', 'build']:
os.system(f'rm -rf {item}')
def update_version(path, n):
content = open(path, 'r').read()
orig, version = Version.parse(content)
print (f'Current version: {version}')
version.minor += int(n)
print (f'New Version: {version}')
with open(path, 'w') as file:
file.write(content.replace(orig, version.new_version(orig)))
def git_commit(message):
if not ask(f'About to git commit {message}, are you sure: '):
exit()
os.system(f'git commit -am "{message}"')
if not ask('About to git push, are you sure: '):
exit()
os.system('git push')
def main(*args):
print ('Program Started')
update_version(setup_file, 1)
# git_commit(*sys.argv[1:3])
pypi_upload()
print ('Upload success')
if __name__ == '__main__':
main()
#if len(sys.argv) == 1:
# print (f'Usage: python {sys.argv[0]} "commmit message"'); sys.exit()
#
#main(*sys.argv[1:3])