-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclean_requirements.py
executable file
·70 lines (48 loc) · 1.62 KB
/
clean_requirements.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
#!/usr/bin/env python3
"""
This script reads a requirements.txt file and removes the version numbers from
each line
"""
import re
import sys
def main() -> None:
"""
The main function to clean a requirements.txt file.
"""
if len(sys.argv) != 2:
print("Usage: ./script.py <path_to_requirements.txt>")
sys.exit(1)
file_path = sys.argv[1]
cleaned_text = clean_requirements_file(file_path)
save_cleaned_file(file_path, cleaned_text)
def clean_requirements_file(file_path: str) -> str:
"""
Clean the 'requirements.txt' file by removing the version numbers from each
line.
Arguments:
file_path: The path to the 'requirements.txt' file.
Returns:
str
The cleaned text from the 'requirements.txt' file.
"""
with open(file_path, 'r') as file:
contents = file.readlines()
# Clean each line using regex
cleaned_contents = [re.sub(r"==.*", "", line) for line in contents]
cleaned_text = ''.join(cleaned_contents)
return cleaned_text
def save_cleaned_file(file_path: str, cleaned_text: str) -> None:
"""
Clean the 'requirements.txt' file and save the cleaned text to the same
file.
Arguments:
file_path: The 'requirements.txt' file to clean.
cleaned_text: The cleaned text from the 'requirements.txt' file.
"""
# Overwrite the original file with the cleaned text
replaced_file = file_path.replace('.txt', '.txt')
with open(replaced_file, 'w') as file:
file.write(cleaned_text)
print(f"Cleaned file saved as: {replaced_file}")
if __name__ == "__main__":
main()