from datetime import datetime
from shutil import copyfile
import shutil
import subprocess
import os
import sys
import os
from distutils.dir_util import copy_tree
from datetime import datetime
# Getting the current date and time
dt = datetime.now()
# getting the timestamp
ts = datetime.timestamp(dt)
folders = os.listdir("../..")
os.mkdir("../../backup/"+str(ts))
for folder in folders:
if folder.lower() != "backup":
copy_tree("../../"+folder, "../../backup/"+str(ts)+"/"+folder)
#shutil.make_archive("../../backup/"+str(ts), 'zip', "../../backup/"+str(ts))
#os.remove("../../backup/"+str(ts))
ui_main_dir = os.path.abspath("../../ui")
py_from_ui_dir = os.path.abspath("../compiled_ui")
#2. Compile all ui files and qrc file with pyuic5 command.
def parse_directory_2(path):
path_contents = os.listdir(path)
if os.path.exists(path.replace(ui_main_dir,py_from_ui_dir))==False:
os.mkdir(path.replace(ui_main_dir,py_from_ui_dir))
for path_content in path_contents:
if os.path.isdir(path+"/"+path_content):
parse_directory_2(path+"/"+path_content)
else:
original_path = path+"/"+path_content
extension = original_path.split(".")[-1].lower()
if extension == "ui":
saved_path = original_path.replace(".ui",".py").replace(ui_main_dir,py_from_ui_dir)
process = subprocess.Popen("python -m PyQt5.uic.pyuic -x \""+original_path+"\" -o \""+saved_path+"\"", shell=False)
process.wait()
folder_depth = saved_path.count("/")-1
folders_back = ""
for i in range(0,folder_depth):
folders_back +="../"
py_file = open(saved_path,"r+",encoding="utf-8")
contents = py_file.read()
contents = contents.replace("import Εικόνες προγράμματος_rc","import sys\nimport importlib\nsys.path.append(\""+folders_back+"\")\nicons = importlib.import_module(\"Κεντρικό παράθυρο προγράμματος και αρχείο qrc (Main window and qrc).Εικόνες προγράμματος\")")
py_file.seek(0)
py_file.write(contents)
#py_file.flush()
py_file.close()
elif extension=="qrc":
saved_path = original_path.replace(".qrc",".py").replace(ui_main_dir,py_from_ui_dir)
subprocess.Popen("python -m PyQt5.pyrcc_main \""+original_path+"\" -o \""+saved_path+"\"", shell=False)
parse_directory_2(ui_main_dir)
上記のスクリプト (関数: parse_directory_2) の 2 番目の部分 (#2) で、pyuic5 を呼び出して、PyQt5 ui ファイル (Qt Designer で作成) を .py ファイルにコンパイルします。
エラーは次のとおりです。
chris@chris-Inspiron-3847:~/Documents/Projects/papinhio-player/src/python$ python compile-and-backup.py
Traceback (most recent call last):
File "/home/chris/Documents/Projects/papinhio-player/src/python/compile-and-backup.py", line 64, in <module>
parse_directory_2(ui_main_dir)
File "/home/chris/Documents/Projects/papinhio-player/src/python/compile-and-backup.py", line 40, in parse_directory_2
parse_directory_2(path+"/"+path_content)
File "/home/chris/Documents/Projects/papinhio-player/src/python/compile-and-backup.py", line 46, in parse_directory_2
process = subprocess.Popen("/usr/bin/python3.9 -m PyQt5.uic.pyuic -x \""+original_path+"\" -o \""+saved_path+"\"", shell=False)
File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'python -m PyQt5.uic.pyuic -x "/home/chris/Documents/Projects/papinhio-player/ui/main-window/wrong-url-address.ui" -o "/home/chris/Documents/Projects/papinhio-player/src/compiled_ui/main-window/wrong-url-address.py"'
しかし、その後手動で実行してpython -m PyQt5.uic.pyuic -x "/home/chris/Documents/Projects/papinhio-player/ui/main-window/wrong-url-address.ui" -o "/home/chris/Documents/Projects/papinhio-player/src/compiled_ui/main-window/wrong-url-address.py"
もエラーは発生せず、py ファイルが作成されます。
だから、私はpythonパスエラーだと思います。
/usr/bin/python3.9 (エイリアス python) で試しましたが、同じエラーが表示されます。
何かご意見は?