-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_pkg.py
96 lines (94 loc) · 3.01 KB
/
py_pkg.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
import os
import sys
import re
import codecs
from tam import info_struct, checkArg, get_arg_in_cmd
import subprocess as sp
import random
def cpy_file(old: str, new: str):
cmd = f"cp -af {old} {new}"
os.system(cmd)
if not os.path.exists(new):
print(f"Failed to copy {old} to {new}")
sys.exit(-5)
def correct_ver_in_toml():
tomlFile = get_arg_in_cmd("-toml", sys.argv)
if tomlFile is None:
print("You didn't set toml file.")
sys.exit(-7)
if not os.path.exists(tomlFile):
print(f"{tomlFile} ain't existed")
sys.exit(-8)
bkp_toml = f"cp -af {tomlFile} {tomlFile}_bkp"
os.system(bkp_toml)
if not os.path.exists(f"{tomlFile}_bkp"):
print(f"Failed to backup {tomlFile}")
sys.exit(-3)
ver = re.compile('\d+', re.UNICODE)
try:
openToml = open(tomlFile, "r+")
except FileExistsError:
print(f"toml = {os.path.exists(tomlFile)}")
sys.exit(-1)
if openToml is None or openToml == -1: sys.exit(-2)
readToml = f"{openToml.read()}"
openToml.flush()
openToml.seek(0)
print(f"{readToml =}")
""""""
New_Ver = f"{info_struct.ver}.{info_struct.rev}"
OldVer = re.findall("\d+\.\d+\-\d+", readToml, re.IGNORECASE|re.UNICODE)[0]
print(OldVer)
readToml = readToml.replace(OldVer, New_Ver)
if openToml.write(readToml) == -1:
print("write to toml been failed")
sys.exit(-4)
def mass_cpy():
SRC = []
for s in sys.argv:
if s[:4] == "-src":
SRC.append(s.removeprefix("-src"))
src_set = set(SRC)
if len(src_set) != len(SRC):
print("List of files to copy has duplicates.")
sys.exit(-6)
for i in SRC:
src0 = get_arg_in_cmd(f"-src{i}", sys.argv)
dst0 = get_arg_in_cmd(f"-dst{i}", sys.argv)
if dst0 is None:
print(f"You didn't set -dst{i}")
continue
if src0 is None:
print(f"You didn't set -src{i}")
continue
cpy_file(src0, dst0)
def run_process_w_output(cmd):
stop_code = "∇\n"
cmd = [f"{str(cmd)};echo '\n{stop_code}'", ]
stderr0_name = f"/tmp/build_tam_err{str(random.random())}"
stderr0 = open(stderr0_name, "w+")
stdout0_name = f"/tmp/build_tam_out{str(random.random())}"
stdout0 = open(stdout0_name, "w+")
p = sp.Popen(cmd, shell=True, stderr=stderr0, stdout=stdout0)
read_stdout0 = open(stdout0_name)
read_stdout0.flush()
read_stdout0.seek(0)
for line in iter(read_stdout0.readline, b''):
if line == stop_code:
break
if line !="":
print(line)
print(stderr0.read())
def build_pkg():
build_dir = get_arg_in_cmd("-build-dir", sys.argv)
if build_dir is None:
print("You didn't set -build-dir")
build_dir = str(input("Please, set build directory: "))
os.system(f"rm -f {build_dir}/dist/*")
cmd = f"python3 -m build {build_dir}/"
run_process_w_output(cmd)
def make_all():
mass_cpy()
correct_ver_in_toml()
build_pkg()
make_all()