Skip to content

Instantly share code, notes, and snippets.

@pdxjohnny
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdxjohnny/a4a86bcf09a99a24afc7 to your computer and use it in GitHub Desktop.
Save pdxjohnny/a4a86bcf09a99a24afc7 to your computer and use it in GitHub Desktop.
Script to change version and upload to PyPi
#!/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