File size: 1,434 Bytes
fc0f7bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Script for changing a regular requirements.txt into a pinned one

This searches through the specified file, replacing all occurences of
a lower bound '>=' with a pinned '=='.
"""
import argparse
import logging
import sys

logger = logging.getLogger(__name__)


def build_argument_parser():
    desc = "Pin Requirements file by converting '>=' to '=='"

    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("--input", help="Path to the file to be processed", required=True)
    parser.add_argument("--output", help="Path to store the processed file", required=True)
    parser.add_argument("--loglevel",
                        help="Set log level",
                        choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])

    return parser


def process_line(src_line):
    return src_line.replace('>=', '==')


def main(argv):
    parser = build_argument_parser()
    args = parser.parse_args(argv)

    if args.loglevel:
        logging.basicConfig(level=getattr(logging, args.loglevel))

    logger.debug("Reading file %s", args.input)
    text_lines = []
    with open(args.input, 'r') as f:
        text_lines = f.readlines()

    result_lines = [process_line(l) for l in text_lines]

    logger.debug("Writing file %s", args.output)
    with open(args.output, 'w') as f:
        f.writelines(result_lines)
    logger.debug("Completed")


if __name__ == "__main__":
    main(sys.argv[1:])