Add files via upload
[PyCatFile.git] / mkcatfilebundle.py
blob256a8f877a2391e25baa730f3b5ec0a197340373
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 '''
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
14 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
15 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
17 $FileInfo: mkbundle.py - Last Update: 5/15/2024 Ver. 0.11.4 RC 1 - Author: cooldude2k $
18 '''
20 import os, sys, shutil, subprocess, tempfile, subprocess, platform;
22 tempdir = tempfile.gettempdir();
23 if(os.sep=="\\"):
24 tempdir = tempdir.replace(os.sep, "/");
25 elif(os.path.sep=="\\"):
26 tempdir = tempdir.replace(os.path.sep, "/");
28 pyimplementation = platform.python_implementation();
29 pylist = ['CPython', 'IronPython', 'PyPy', 'CPython2', 'CPython3', 'Python2', 'Python3', 'Python', 'PyPy2', 'PyPy3', 'IronPython2', 'IronPython3'];
31 if(len(sys.argv) > 1):
32 if(sys.argv[1] in pylist):
33 if(sys.argv[1]=="CPython"):
34 pystring = "python3";
35 elif(sys.argv[1]=="IronPython"):
36 pystring = "ipy2";
37 elif(sys.argv[1]=="PyPy"):
38 pystring = "pypy3";
39 elif(sys.argv[1]=="CPython2"):
40 pystring = "python2";
41 elif(sys.argv[1]=="CPython3"):
42 pystring = "python3";
43 elif(sys.argv[1]=="Python2"):
44 pystring = "python2";
45 elif(sys.argv[1]=="Python3"):
46 pystring = "python3";
47 elif(sys.argv[1]=="PyPy2"):
48 pystring = "pypy";
49 elif(sys.argv[1]=="PyPy3"):
50 pystring = "pypy3";
51 elif(sys.argv[1]=="IronPython2"):
52 pystring = "ipy2";
53 elif(sys.argv[1]=="IronPython3"):
54 pystring = "ipy3";
55 else:
56 sys.exit();
57 else:
58 sys.exit();
60 if(len(sys.argv) < 1):
61 if(pyimplementation=="CPython"):
62 pystring = "python"+str(sys.version_info[0]);
63 elif(pyimplementation=="IronPython"):
64 pystring = "ipy"+str(sys.version_info[0]);
65 elif(pyimplementation=="PyPy"):
66 if(sys.version_info[0]==2):
67 pystring = "pypy";
68 elif(sys.version_info[0]==3):
69 pystring = "pypy"+str(sys.version_info[0]);
70 else:
71 sys.exit(1);
72 else:
73 sys.exit(1);
75 if(len(sys.argv) > 2):
76 if(os.path.exists("./"+sys.argv[2]) and os.path.isfile("./"+sys.argv[2])):
77 infile = sys.argv[2];
78 outfilebin = os.path.splitext(infile)[0];
79 outfilezip = outfilebin+".zip";
80 elif(os.path.exists("./"+sys.argv[2]) and os.path.isdir("./"+sys.argv[2])):
81 infile = "catfile.py";
82 outfilebin = os.path.splitext(infile)[0];
83 outfilezip = outfilebin+".zip";
84 else:
85 infile = "catfile.py";
86 outfilebin = os.path.splitext(infile)[0];
87 outfilezip = outfilebin+".zip";
88 else:
89 infile = "catfile.py";
90 outfilebin = os.path.splitext(infile)[0];
91 outfilezip = outfilebin+".zip";
92 if(os.path.exists(tempdir+"/pybundle") and os.path.isfile(tempdir+"/pybundle")):
93 os.unlink(tempdir+"/pybundle");
94 if(os.path.exists(tempdir+"/pybundle") and os.path.isdir(tempdir+"/pybundle")):
95 shutil.rmtree(tempdir+"/pybundle");
96 os.mkdir(tempdir+"/pybundle");
97 if(os.path.exists(tempdir+"/pybundle/__main__.py") and os.path.isfile(tempdir+"/pybundle/__main__.py")):
98 os.unlink(tempdir+"/pybundle/__main__.py");
99 if(os.path.exists(tempdir+"/pybundle/__main__.py") and os.path.isdir(tempdir+"/pybundle/__main__.py")):
100 shutil.rmtree(tempdir+"/pybundle/__main__.py");
101 shutil.copy2("./"+infile, tempdir+"/pybundle/__main__.py");
102 if(os.path.exists(tempdir+"/pybundle/pycatfile.py") and os.path.isfile(tempdir+"/pybundle/pycatfile.py")):
103 os.unlink(tempdir+"/pybundle/pycatfile.py");
104 if(os.path.exists(tempdir+"/pybundle/pycatfile.py") and os.path.isdir(tempdir+"/pybundle/pycatfile.py")):
105 shutil.rmtree(tempdir+"/pybundle/pycatfile.py");
106 shutil.copy2("./pycatfile.py", tempdir+"/pybundle/pycatfile.py");
107 if(os.path.exists(tempdir+"/"+outfilezip) and os.path.isfile(tempdir+"/"+outfilezip)):
108 os.unlink(tempdir+"/"+outfilezip);
109 if(os.path.exists(tempdir+"/"+outfilezip) and os.path.isdir(tempdir+"/"+outfilezip)):
110 shutil.rmtree(tempdir+"/"+outfilezip);
111 shutil.make_archive(tempdir+"/"+outfilebin, "zip", tempdir+"/pybundle");
112 if(os.path.exists(tempdir+"/pybundle/"+outfilezip) and os.path.isfile(tempdir+"/pybundle/"+outfilezip)):
113 os.unlink(tempdir+"/pybundle/"+outfilezip);
114 if(os.path.exists(tempdir+"/pybundle/"+outfilezip) and os.path.isdir(tempdir+"/pybundle/"+outfilezip)):
115 shutil.rmtree(tempdir+"/pybundle/"+outfilezip);
116 shutil.move(tempdir+"/"+outfilezip, tempdir+"/pybundle/"+outfilezip);
117 if(os.path.exists(tempdir+"/pybundle/"+outfilebin) and os.path.isfile(tempdir+"/pybundle/"+outfilebin)):
118 os.unlink(tempdir+"/pybundle/"+outfilebin);
119 if(os.path.exists(tempdir+"/pybundle/"+outfilebin) and os.path.isdir(tempdir+"/pybundle/"+outfilebin)):
120 shutil.rmtree(tempdir+"/pybundle/"+outfilebin);
121 mkbstring = "#!/usr/bin/env "+pystring+"\n\n";
122 mkbfp = open(tempdir+"/pybundle/"+outfilebin, "wb+");
123 mkbfp.write(mkbstring.encode());
124 zipfp = open(tempdir+"/pybundle/"+outfilezip, "rb");
125 mkbfp.write(zipfp.read());
126 mkbfp.close();
127 zipfp.close();
128 shutil.rmtree("./bundle/"+pystring);
129 os.mkdir("./bundle/"+pystring);
130 shutil.move(tempdir+"/pybundle/"+outfilebin, "./bundle/"+pystring+"/"+outfilebin);
131 os.chmod("./bundle/"+pystring+"/"+outfilebin, 0o755);
132 shutil.rmtree(tempdir+"/pybundle");
133 oldpath = os.getcwd();
134 os.chdir("./bundle/"+pystring);
135 curscrpath = os.path.dirname("./"+outfilebin);
136 if(curscrpath==""):
137 curscrpath = ".";
138 if(os.sep=="\\"):
139 curscrpath = curscrpath.replace(os.sep, "/");
140 elif(os.path.sep=="\\"):
141 curscrpath = curscrpath.replace(os.path.sep, "/");
142 curscrpath = curscrpath+os.path.sep;
143 scrfile = curscrpath+outfilebin;
145 if(os.path.exists(scrfile) and os.path.isfile(scrfile) and infile=="catfile.py"):
146 scrcmd = subprocess.Popen([sys.executable, scrfile, "mksymlinks"]);
147 scrcmd.wait();
149 os.chdir(oldpath);