Last active
August 29, 2015 14:20
-
-
Save pdxjohnny/a4a86bcf09a99a24afc7 to your computer and use it in GitHub Desktop.
Script to change version and upload to PyPi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
import shutil | |
PACKAGE_NAME = "some_package" | |
def id_generator(size=6, chars=string.ascii_uppercase + string.digits): | |
return ''.join(random.choice(chars) for _ in range(size)) | |
def update_version_files(version): | |
files = { | |
"setup.py": (" version = ", " version = \'%s\',\n") | |
} | |
for name in files: | |
tmp = name + id_generator() | |
read_file = open(name, "rb") | |
write_file = open(tmp, "wb") | |
for line in read_file: | |
if line.startswith(files[name][0]): | |
line = files[name][1] % (version, ) | |
write_file.write( line ) | |
read_file.close() | |
write_file.close() | |
shutil.copyfile(tmp, name) | |
if os.path.exists(tmp): | |
os.remove(tmp) | |
return version | |
def git_commit(message): | |
command = "git commit -am \"%s\"" % (message, ) | |
return os.system(command) | |
def git_tag(version, message): | |
command = "git tag \"%s\" -m \"%s\"" % (version, message, ) | |
return os.system(command) | |
def git_push(): | |
command = "git push origin master" | |
os.system(command) | |
command = "git push --tags origin master" | |
return os.system(command) | |
def upload(): | |
command = "python setup.py sdist upload -r pypi" | |
os.system(command) | |
if os.name == "nt": | |
command = "python -m pip install --upgrade %s" % (PACKAGE_NAME, ) | |
else: | |
command = "sudo -HE pip install --upgrade %s" % (PACKAGE_NAME, ) | |
return os.system(command) | |
def main(): | |
if len(sys.argv) < 3: | |
print "Usage %s version \"Commit message\"" % ( sys.argv[0], ) | |
sys.exit(0) | |
version = sys.argv[1] | |
message = sys.argv[2] | |
update_version_files( version ) | |
git_commit( message ) | |
git_tag(version, message) | |
git_push() | |
upload() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment