forked from jazzband/pip-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip-dump
executable file
·167 lines (131 loc) · 4.97 KB
/
pip-dump
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
from __future__ import absolute_import
import os.path
import glob
import argparse
import logging
import sys
import tempfile
from itertools import dropwhile, takewhile, chain
from functools import partial
from subprocess import check_call as _check_call
try:
from subprocess import check_output as _check_output
except ImportError:
import subprocess
def _check_output(*args, **kwargs):
process = subprocess.Popen(stdout=subprocess.PIPE, *args, **kwargs)
output, _ = process.communicate()
retcode = process.poll()
if retcode:
error = subprocess.CalledProcessError(retcode, args[0])
error.output = output
raise error
return output
check_call = partial(_check_call, shell=True)
check_output = partial(_check_output, shell=True)
# Constants
DEFAULT_REQUIREMENTS_FILE = u'requirements.txt'
GLOB_PATTERNS = (u'*requirements.txt', u'requirements[_-]*.txt', u'requirements/*.txt')
PIP_IGNORE_FILE = u'.pipignore'
SPLIT_PATTERN = u'## The following requirements were added by pip --freeze:'
class StdOutFilter(logging.Filter):
def filter(self, record):
return record.levelno in [logging.DEBUG, logging.INFO]
def setup_logging(verbose):
if verbose:
level = logging.DEBUG
else:
level = logging.INFO
format = '%(message)s'
logger = logging.getLogger(u'pip-dump')
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.addFilter(StdOutFilter())
stdout_handler.setFormatter(logging.Formatter(format))
stdout_handler.setLevel(logging.DEBUG)
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setFormatter(logging.Formatter(format))
stderr_handler.setLevel(logging.WARNING)
logger.setLevel(level)
logger.addHandler(stderr_handler)
logger.addHandler(stdout_handler)
return logger
def parse_args():
parser = argparse.ArgumentParser(
description='Rewrites requirements.txt to match your virtualenv.'
)
parser.add_argument(
'--verbose', '-v', action='store_true', default=False, help='Show more output'
)
parser.add_argument(
'--no-sort', action='store_false', default=True, help="Don't sort package names (default is to sort)", dest="should_sort"
)
parser.add_argument('files', nargs='*')
return parser.parse_args()
def pip_partition(lines):
no_split_match = lambda line: line != SPLIT_PATTERN
split_match = lambda line: line == SPLIT_PATTERN
first = takewhile(no_split_match, lines)
second = dropwhile(split_match, dropwhile(no_split_match, lines))
return list(first), list(second)
def pip_info(filename):
if not os.path.exists(filename):
check_call('touch {0}'.format(filename))
cmd = 'pip freeze -lr {0}'.format(filename,)
raw = check_output(cmd)
lines = raw.decode('utf-8').split('\n')
return pip_partition(lines)
def append_lines(lines, filename):
with open(filename, 'a') as f:
for line in lines:
f.write('{0}\n'.format(line))
def rewrite(filename, lines, should_sort=True):
if should_sort:
lines.sort(key=lambda s: s.lower())
with open(filename, 'w') as f:
for line in lines:
line = line.strip()
if line:
f.write('{0}\n'.format(line))
def dump_requirements(files, should_sort=True):
if not files:
raise ValueError(u'Expected a list of at least one file name.')
_, tmpfile = tempfile.mkstemp()
existing_files = list(filter(os.path.exists, files))
if existing_files:
if should_sort:
check_call(u'cat {0} | sort -fu > {1}'.format(' '.join(existing_files), tmpfile))
else:
check_call(u'cat {0} > {1}'.format(' '.join(existing_files), tmpfile))
_, new = pip_info(tmpfile)
check_call(u'rm {0}'.format(tmpfile))
append_lines(new, files[0])
for filename in files:
if os.path.basename(filename) == PIP_IGNORE_FILE: # never rewrite the pip ignore file
continue
pkgs, _ = pip_info(filename)
rewrite(filename, pkgs, should_sort=should_sort)
def find_default_files():
req_files = list(chain.from_iterable(glob.glob(pattern) for pattern in GLOB_PATTERNS))
try:
req_files.remove(DEFAULT_REQUIREMENTS_FILE)
except ValueError:
pass
else:
# Move the "default" requirements file to the front, so any new
# packages will be added there
req_files = [DEFAULT_REQUIREMENTS_FILE] + req_files
# In case of no found files, use the default file
if not req_files:
req_files = [DEFAULT_REQUIREMENTS_FILE]
if os.path.exists(PIP_IGNORE_FILE) and not PIP_IGNORE_FILE in req_files:
req_files.append(PIP_IGNORE_FILE)
return req_files
def main():
args = parse_args()
logger = setup_logging(args.verbose)
if not args.files:
args.files = find_default_files()
dump_requirements(args.files, should_sort=args.should_sort)
if __name__ == '__main__':
main()