-
Notifications
You must be signed in to change notification settings - Fork 16
/
updater.py
54 lines (45 loc) · 1.64 KB
/
updater.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
import asyncio
import difflib
import shlex
import sys
from typing import Tuple
async def lines_differnce(file1, file2):
with open(file1) as f1:
lines1 = f1.readlines()
lines1 = [line.rstrip("\n") for line in lines1]
with open(file2) as f2:
lines2 = f2.readlines()
lines2 = [line.rstrip("\n") for line in lines2]
diff = difflib.unified_diff(
lines1, lines2, fromfile=file1, tofile=file2, lineterm="", n=0
)
lines = list(diff)[2:]
added = [line[1:] for line in lines if line[0] == "+"]
removed = [line[1:] for line in lines if line[0] == "-"]
additions = [i for i in added if i not in removed]
removedt = [i for i in removed if i not in added]
return additions, removedt
async def runcmd(cmd: str) -> Tuple[str, str, int, int]:
args = shlex.split(cmd)
process = await asyncio.create_subprocess_exec(
*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
return (
stdout.decode("utf-8", "replace").strip(),
stderr.decode("utf-8", "replace").strip(),
process.returncode,
process.pid,
)
async def update_requirements(main , test):
a, r = await lines_differnce(main, test)
try:
for i in a:
await runcmd(f"pip install {i}")
print(f">> Installed Requirement: {i}")
except Exception as e:
print(f"Error installing requirments {str(e)}")
# loop = asyncio.get_running_loop()
# loop.run_until_complete(update_requirements(sys.argv[1] , sys.argv[2]))
# loop.close()
asyncio.run(update_requirements(sys.argv[1] , sys.argv[2]))