forked from seven6306/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPytoExe.py
82 lines (81 loc) · 2.49 KB
/
PytoExe.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
#!/usr/bin/python3
UsageInfo = '''
Usage: ./PytoExe [Script] [Option]
This tool support Windows and Linux system, easily convert Python
script to executable program.
-ico, --icon Convert script with icon logo, please put
icon image: "image.ico" under this directory
-nwd, --nowindow Convert script to exe without window
-res, --resource Keep resource files alive
e.g.,
./PytoExe example.py --resource
'''
from sys import argv
from re import search
from shutil import move, rmtree
from os.path import isfile
from os import getcwd, remove, system, listdir
def fileList(extension=''):
templist = []
for each_ico in listdir():
if search('\w+\.{}$'.format(extension), each_ico):
templist.append(each_ico)
return(templist)
def Check_Package():
if not isfile('/usr/bin/pip3'):
system('sudo apt-get install python3-pip -y')
if not isfile('/usr/local/bin/pyinstaller'):
system('sudo pip3 install pyinstaller')
if isfile('/usr/bin/pip3') and isfile('/usr/local/bin/pyinstaller'):
return(True)
else:
return(False)
try:
script = argv[1]
if script in ['-h', '--help', '?']:
raise ImportWarning
if not search('\w+\.py$', script):
errtext = 'Invalid python script name'
raise ImportError
try:
if len(argv) > 2:
option = argv
for rm_argv in [argv[0], argv[1]]:
option.remove(rm_argv)
for each_argv in option:
if each_argv not in ['-res', '--resource', '-nwd', '--nowindow', '-ico', '--icon']:
raise Exception
else:
option = []
except:
pass
raise Exception
option_ = ''
if '-nwd' in option or '--nowindow' in option:
option_ = '-w'
if '-ico' in option or '--icon' in option:
if len(fileList('ico')) == 0:
errtext = 'Icon image is not found'
raise ImportError
option_ = '{0} -i {1}'.format(option_, fileList('ico')[0])
if not Check_Package():
errtext = 'Requirements install failed.'
raise ImportError
system('pyinstaller -F {0} {1}'.format(option_, script))
try:
move('dist/{}'.format(script.replace('.py', '')), getcwd())
if '-res' not in option and '--resource' not in option:
for rm_f in ['__pycache__', 'build', 'dist']:
rmtree(rm_f)
remove(script.replace('.py', '.spec'))
except Exception as err:
print(str(err))
except ImportWarning:
pass
print(UsageInfo)
except ImportError:
pass
print('{}'.format(errtext))
except:
pass
print('Invalid argument: Try \'PytoExe --help\' for more information')