Skip to content

Commit

Permalink
Initial commit of working project
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyespo committed Apr 15, 2017
0 parents commit 829400c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Deployment files
*.egg-info
dist

# Environment files
env
.cache
*.py[cod]

# OS-specific files
.DS_Store
Desktop.ini
Thumbs.db
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017 Joe Esposito <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include *.txt
include *.md
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
upgrade-requirements.py
=======================
[![Current version on PyPI](http:https://img.shields.io/pypi/v/upgrade-requirements.svg)](http:https://pypi.python.org/pypi/upgrade-requirements/)

Upgrade all your outdated `requirements.txt` in a single command.


Motivation
----------

Even though `pip list --outdated` exists, sometimes you just want to
run `pip install --upgrade` to upgrade a package, then persist it to
your `requirements.txt` in one big sweep.


Installation
------------

```bash
$ pip install upgrade-requirements
```


Usage
-----

```bash
$ upgrade-requirements
```

(Or use the shortcut command `upreq`.)

Now's a good time to grab a ☕ while it runs.

After it finishes, run your tests to make sure an individual upgrade didn't
break anything. Then move on to bigger things 🚀

#### Found a problem with an upgraded version?

No worries!

1. Revert individual entries in `requirements.txt` with the help of git
2. Run `pip install -r requirements.txt` to downgrade to working versions
3. Commit the upgraded-and-tweaked `requirements.txt` like normal and carry on 🎉


Room for improvement
--------------------

- This only work with pinned (`==`) packages at the moment. The intention is to
get it to work more generally. This can be broken down into three sub-tasks:

- Get it to ignore non-pinned requirements instead of fail
- Get it to work with other specifiers, e.g. `>=` (how would this work?)
- Handle all types of requirement entries

Feel free to open an issue or PR with ideas.
27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from setuptools import setup


def read(filename):
with open(os.path.join(os.path.dirname(__file__), filename)) as f:
return f.read()


setup(
name='upgrade-requirements',
version='1.3.0',
description='Upgrade all your outdated requirements in a single command.',
long_description=read('README.md'),
author='Joe Esposito',
author_email='[email protected]',
url='http:https://github.com/joeyespo/upgrade-requirements.py',
license='MIT',
platforms='any',
py_modules=['upgrade_requirements'],
entry_points={
'console_scripts': [
'upgrade-requirements = upgrade_requirements:main',
'upreq = upgrade_requirements:main',
],
},
)
77 changes: 77 additions & 0 deletions upgrade_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from __future__ import print_function, unicode_literals

import io
import subprocess


def get_installed_requirement(entry):
installed_name, installed_version = None, None

name = entry.split('[', 1)[0]
info = subprocess.check_output(['pip', 'show', name]).decode('utf-8')
for line in info.split('\n'):
line = line.strip()
if 'Name: ' in line:
installed_name = line[len('Name: '):]
if 'Version: ' in line:
installed_version = line[len('Version: '):]

if not installed_name or not installed_version:
raise ValueError('Could not info for {!r}'.format(entry))

return entry.replace(name, installed_name, 1), installed_version


def main():
# Read pinned requirements
try:
with io.open('requirements.txt') as f:
print('Reading requirements...')
requirements = [r.strip() for r in f.readlines()]
except:
print('Error: No requirements.txt found')
return

# Get names of requirements to run 'pip install --upgrade' on
upgrades = []
for requirement in requirements:
if not requirement:
continue
# TODO: Handle other version instructions
if '==' not in requirement:
print('Error: Can only work with pinned requirements for now.')
name, version = requirement.split('==')
upgrades.append(name)

# Confirm
answer = raw_input('Upgrade {} requirements (y/N)? '.format(len(upgrades)))
if answer != 'y':
return
print()

# Run 'pip install --upgrade' on all requirements
for name in upgrades:
print('$ pip install --upgrade', name)
exit_code = subprocess.call(['pip', 'install', '--upgrade', name])
if exit_code != 0:
return
print()

# Show message
print('Collecting installed versions...')

# Generate resulting requirements.txt content
result = ''
for name in upgrades:
installed_name, installed_version = get_installed_requirement(name)
result = '{}{}=={}\n'.format(result, installed_name, installed_version)

# Save upgraded requirements
with io.open('requirements.txt', 'w') as f:
f.write(result)

print('Wrote requirements.txt')


if __name__ == '__main__':
main()

0 comments on commit 829400c

Please sign in to comment.