-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyreqs.py
executable file
·124 lines (104 loc) · 3.58 KB
/
pyreqs.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 rmad17 <souravbasu17@gmail.com>
#
# Distributed under terms of the MIT license.
"""
"""
import click
from sh import grep as sh_grep
from sh import mv as sh_mv
from sh import pip as sh_pip
from sh import rm as sh_rm
@click.group()
@click.version_option()
def pyreqs():
"""
Entry point for pyreqs
"""
pass
@pyreqs.command()
@click.argument('packagename')
@click.option('--save', is_flag=True, help='pin package to requirements')
@click.option('--save-dev', is_flag=True,
help='pin package to dev-requirements')
@click.option('--save-test', is_flag=True,
help='pin package to test-requirements')
@click.argument('filename', required=False)
def install(packagename, save, save_dev, save_test, filename):
"""
Install the package via pip, pin the package only to requirements file.
Use option to decide which file the package will be pinned to.
"""
print('Installing ', packagename)
print(sh_pip.install(packagename))
if not filename:
filename = get_filename(save, save_dev, save_test)
try:
add_requirements(packagename, filename)
except AssertionError:
print('Package already pinned in ', filename)
@pyreqs.command()
@click.argument('packagename')
@click.option('--save', is_flag=True, help='remove package from requirements')
@click.option('--save-dev', is_flag=True,
help='remove package from dev-requirements')
@click.option('--save-test', is_flag=True,
help='remove package from test-requirements')
@click.argument('filename', required=False)
def remove(packagename, save, save_dev, save_test, filename):
"""
Uninstall the package and remove it from requirements file.
"""
print(sh_pip.uninstall(packagename, "-y"))
if not filename:
filename = get_filename(save, save_dev, save_test)
remove_requirements(packagename, filename)
def add_requirements(packagename, filename):
output = sh_pip.freeze
packages = output.split('\n')
try:
with open(filename, 'rb+') as f0:
current_requirements = f0.readlines()
versioned_packagename = sh_grep(
sh_pip.freeze(_iter=True), packagename, _iter=True)
byted_packagename = str(versioned_packagename).encode('utf-8')
assert byted_packagename not in current_requirements
except FileNotFoundError:
pass
for req in packages:
if not req:
continue
if packagename in req:
with open(filename, 'ab+') as f:
f.write(req.encode('utf-8'))
order_requirements(filename)
print('Updated', str(filename) + '!')
def remove_requirements(packagename, filename):
with open(filename, 'rb+') as f0:
for line in f0.readlines():
if packagename not in str(line):
with open(filename + '.tmp', 'wb+') as f1:
f1.write(line)
sh_rm(filename, "-f")
sh_mv(filename + '.tmp', filename)
print('Updated', str(filename) + '!')
def get_filename(save=False, save_dev=False, save_test=False):
if save_dev:
return 'dev-requirements.txt'
if save_test:
return 'test-requirements.txt'
return 'requirements.txt'
def order_requirements(filename):
with open(filename, 'rb+') as f0:
packages = f0.readlines()
packages.sort()
with open(filename + '.tmp', 'ab+') as f1:
for package in packages:
f1.write(package)
sh_rm(filename, "-f")
sh_mv(filename + '.tmp', filename)
if __name__ == '__main__':
pyreqs()