Last active
December 22, 2015 10:47
-
-
Save k0nserv/d034a31d191284cf89eb to your computer and use it in GitHub Desktop.
Better versoning in python
This file contains hidden or 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
from subprocess import call | |
import sys | |
class Package: | |
def __init__(self, name, min=None, max=None): | |
self.name = name | |
self.min = min | |
self.max = max | |
def spec(self): | |
result = "{}".format(self.name) | |
if self.max and self.min: | |
result += ">={},<{}".format(self.min, self.max) | |
return result | |
packages = [ | |
Package("requests", "2.9.1", "2.10.0"), | |
Package("scrapy", "1.0.3", "1.1.0"), | |
Package("sqlalchemy", "1.0.10", "1.1.0") | |
] | |
def install(upgrade=False): | |
for package in packages: | |
command = ['pip', 'install'] | |
if upgrade: | |
command.append('--upgrade') | |
command.append(package.spec()) | |
call(command) | |
def freeze(): | |
with open('requirements.txt', 'w') as outfile: | |
call(['pip', 'freeze'], stdout=outfile) | |
def print_usage_and_exit(): | |
print('Usage `python {} (install|upgrade)`'.format(sys.argv[0])) | |
exit(1) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print_usage_and_exit() | |
command = sys.argv[1] | |
if command != "install" and command != "upgrade": | |
print_usage_and_exit() | |
install(command == "upgrade") | |
freeze() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment