Advertisement
egor230

python script build in exe file for linux

Aug 16th, 2023 (edited)
1,561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | Source Code | 0 0
  1. import os, time, shutil, subprocess, glob
  2. from PIL import Image
  3.  
  4. def find_image():
  5.  t=['*.png', '*.jpg','*.jpeg','*.bmp']
  6.  number=0
  7.  for i in t:
  8.    i2=str(i)
  9.    for file in glob.glob(i2):
  10.     adress=os.path.join(os.getcwd(), file).lstrip('\u202a')
  11.     return adress
  12.  return
  13. def convert_to_ico(input_path):
  14.   if input_path != '':
  15.     image = Image.open(input_path)
  16.     # output_path=adress[:-3]+'ico'
  17.     # image.save(output_path, format='ico')
  18. def find_file(type):
  19.   adress_to_file=""
  20.   adress=str(os.getcwd())
  21.   for root, dirs, files in os.walk(adress):
  22.     for file in files:
  23.       if file.endswith(type) and file !='con_in_bin.py':
  24.         adress_to_file=os.path.join(root, file)
  25.         adress_to_file=adress_to_file.replace(" ", "\ ")
  26.         break
  27.   return adress_to_file
  28.  
  29. # adress = find_image()
  30. # convert_to_ico(adress)
  31. py_scrypt= find_file("py")# нахождения расположения скрипта.
  32. # adress_icon= find_file("ico")#нахождения расположения иконки.
  33.  
  34. home=str(os.getcwd()) #
  35. os.system(str('pyinstaller --onefile '+ py_scrypt))# сборка в exe файл.
  36. py_scrypt= os.path.basename(py_scrypt)# получить имя скрипта.
  37. name_file=py_scrypt[:-3]#.replace("\\","")
  38. adress=str(os.getcwd()+"/dist/"+name_file).replace('\\','') # расположение директории с исполяемым файлом.
  39.  
  40. script = f'''#!/bin/bash
  41. name="{name_file}"
  42. gnome-terminal -- bash -c 'chmod +x "$name"; done; exit'
  43. '''
  44. subprocess.call(['bash', '-c', script])
  45. parent_dir = str(os.path.dirname(home))
  46. # print(adress)
  47. # print(parent_dir)
  48. # print(name_file)
  49. script = ("#!/bin/bash\n"
  50.           "gnome-terminal -- bash -c \'cd \"{0}\";\n"
  51.           "./{1};\n"
  52.           "exit;\n"
  53.           "exec bash\'".format(parent_dir,name_file))
  54.  
  55. # print(parent_dir)
  56. # print(script)
  57. parent_dir=str(parent_dir+"/"+name_file).replace('\\','') # расположения exe файла.
  58. with open(str(parent_dir)+".sh",'w') as f:
  59.   f.write(script)
  60.  
  61. shutil.move(adress, parent_dir)# переместить файл в новую директорию.
  62. shutil.rmtree(home+'/dist')
  63. shutil.rmtree(home+'/build')
  64. name_file1=name_file.replace("\\","")
  65. os.remove(os.getcwd()+'/'+name_file1+'.spec')
  66. name_file=name_file1.replace(" ", "_")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement