import os, shutil, time, sys, threading, pickle
## SHOW ##
# 0 None
# 1 Errors
# 2 Prebuild stuff
# 3 Command stuff
# 4 Py2exe stuff
# 5 ALL
class stdout:
def __init__(self, textwidget):
self.textwidget = textwidget
def write(self, text):
self.textwidget.insert('end', text)
self.textwidget.see(self.last_line())
def last_line(self):
return '%s.%s' % (len(self.textwidget.get('0.0', 'end').split('\n'))-1, 0)
def find_allfiles(startloc,orginal_dir,path='',dir=''):
if not(dir):
dir = orginal_dir
if not(path):
ls = os.listdir(startloc)
else:
ls = os.listdir(startloc+'\\'+path)
if dir:
dir += '\\'+path
else:
dir = path
folders = {dir:[]}
for item in ls:
if path:
new_path = path+'\\'+item
else:
new_path = item
if not(os.path.splitext(new_path)[1]):
folders.update(find_allfiles(startloc,orginal_dir,new_path,dir))
else:
folders[dir].append(orginal_dir+'\\'+new_path)
return folders
class build:
def __init__(self, pymakefile_path, args, textwidget=None):
self.pymakefile_path = pymakefile_path
self.pymakefile_data = {}
self.location = os.path.dirname(self.pymakefile_path)
self.textwidget = textwidget
if self.textwidget:
self.show = 5
sys.stdout = stdout(self.textwidget)
else:
self.show = 3
def load(self):
file = open(self.pymakefile_path, 'r')
self.pymakefile_data = pickle.load(file)
file.close()
#Name
self.pymakefile_data['name'] = '\\'.join(self.pymakefile_data['name'].split('/'))
#Script
self.pymakefile_data['script'] = '\\'.join(self.pymakefile_data['script'].split('/'))
#Icon
self.pymakefile_data['icon'] = '\\'.join(self.pymakefile_data['icon'].split('/'))
#Files
new_list = []
for file in self.pymakefile_data['files']:
new_list.append('\\'.join(file.split('/')))
self.pymakefile_data['files'] = new_list
#Data
new_list = []
for file in self.pymakefile_data['data']:
new_list.append('\\'.join(file.split('/')))
self.pymakefile_data['data'] = new_list
def command(self,command):
if self.show >= 3:
print command
pipe = os.popen(command, "r")
output = pipe.read().strip()
status = pipe.close()
if self.show >= 3:
print output
return output
def make_setupfile(self,location2save):
current_dir = os.path.realpath('')
os.chdir(self.location)
#Data Files
data = list(self.pymakefile_data['data'])
data_dict = {}
#print 'loc', os.path.realpath('')
for line in self.pymakefile_data['data']:
line = '\\'.join(line.split('/'))
entry = line.strip().split('=')
#print '|'+entry[1]+'|', os.path.isdir(entry[1]), os.path.isfile(entry[1])
if os.path.isdir(entry[1]):
folder = entry[1] #JUMP2
folders = find_allfiles(self.location+'\\'+entry[0],folder)
for folder in folders.keys():
if not(folder in data_dict.keys()):
data_dict[folder] = folders[folder]
else:
data_dict[folder].extend(folders[folder])
else:
folder = os.path.dirname(entry[1])
if not(folder in data_dict.keys()):
data_dict[folder] = [entry[1]]
else:
data_dict[folder].append(entry[1])
data_list = []
for key in data_dict.keys():
data_list.append((key, data_dict[key]))
#Files
files = list(self.pymakefile_data['files'])
files.append(self.pymakefile_data['icon'])
#files.append(self.data['script'].get())
for file in self.pymakefile_data['files']:
files[files.index(file)] = '\\'.join(file.split('/'))
file = open(location2save+'\\setup.pyw', 'w')
file.write("""
# setup.py
from distutils.core import setup
import py2exe
import sys
sys.stdout = open('screen.txt','w',0)
sys.stderr = open('errors.txt','w',0)
setup(name='%s',
version='%s',
author='%s',
data_files=%s,
%s=[{'script':'%s',
'icon_resources':[(1,'%s')],
}])
print "---Done---"
""" % (self.pymakefile_data['name'],
self.pymakefile_data['version'],
self.pymakefile_data['author'],
str(data_list),
self.pymakefile_data['displaymode'],
self.pymakefile_data['name']+'.py',#self.data['script'].get(),
self.pymakefile_data['icon']))
file.close()
## #Location
## loc = self.data['loc'].get()
##
## #Data
## for file in data:
## data[data.index(file)] = '\\'.join(file.split('/'))
os.chdir(current_dir)
def run(self):#loc, files, data, setup_file, dist, flags, text=False):
## #Flags ----> REDO <-----
## for flag in flags:
## if '--file_debug=' in flag:
## f = open(flag.strip().split('=')[1], 'w')
## f.write('loc = %s\n\n' % loc)
## f.write('files = %s\n\n' % files)
## f.write('data = %s\n\n' % data)
## f.write('setup_file = %s\n\n' % setup_file)
## f.write('dist = %s\n\n' % dist)
## f.write('compiler_flags = %s\n\n' % compiler_flags)
## f.close()
## break
##
## if '--no_show' in flags:
## show = False
## else:
## show = True
self.load()
dist = 'Binary' #Temp
if self.show >= 2:
print 'cd', self.location
os.chdir(self.location)
if self.show >= 2:
print "Checking files ..."
error = []
names = []
## if not(os.path.exists(setup_file)):
## if self.show >= 2:
## print "Setup File Not Found (%s)" % setup_file
## error.append("Setup File Not Found (%s)" % setup_file)
## else:
## names.append(setup_file)
for file in self.pymakefile_data['files']:
if file != '':
if not(os.path.exists(file)):
if show:print "Could not find: %s" % file
error.append("Could not find: %s" % file)
else:
if os.path.basename(file) in names:
if show:print "Dupicate Names: %s" % file
error.append("Dupicate Names: %s" % file)
names.append(os.path.basename(file))
for file in self.pymakefile_data['data']:
file = file.split('=')
if not(os.path.exists(file[0])):
if show:print "Could not find: %s" % file[0]
error.append("Could not find: %s" % file[0])
else:
if os.path.basename(file[1]) in names:
if show:print "Dupicate Names: %s" % file[1]
error.append("Dupicate Names: %s" % file[1])
names.append(os.path.basename(file[1]))
if error and self.show >= 1:
print "ERRORS:\n\n" + "\n".join(error)
return 0
if self.show >= 2:
print "Copying Files to build\\..."
if not(os.path.exists('build')):
os.mkdir('build')
else:
shutil.rmtree('build')
os.mkdir('build')
for file in self.pymakefile_data['files']:
name = os.path.basename(file)
if os.path.isdir(file):
shutil.copytree(file, 'build\\'+file)
if self.show >= 2:
print "Shutil copy tree %s to build\\%s" % (file, file)
else:
self.command('copy "%s" "build\\%s"' % (file, name))
for file in self.pymakefile_data['data']:
file = file.split('=')
if not(os.path.isdir(file[1])):
name = os.path.basename(file[1])
else:
name = file[1].split('\\')[-1].split('/')[-1]
dirs = []
for dir in file[1].split('\\'):
if dir != name:
dirs.append(dir)
if not(os.path.exists('build\\'+'\\'.join(dirs))):
if self.show >= 2:
print 'Making', dir, '...'
os.mkdir('build\\'+'\\'.join(dirs))
if os.path.isdir(file[1]):
shutil.copytree(file[0], 'build\\'+file[1])
if self.show >= 2:
print "Shutil copy tree %s to build\\%s" % (file[0], file[1])
else:
self.command('copy "%s" "build\\%s"' % (file[0], file[1]))
if os.path.exists(os.path.realpath(dist)):
shutil.rmtree(os.path.realpath(dist))
if self.show >= 2:
print "Making Setup.pyw ..."
self.make_setupfile('build')
#command('copy "%s" "build\\setup.pyw"' % (os.path.realpath(setup_file))) #pyw so that the window wont appear
if self.show >= 2:
print "Copying script ..."
self.command('copy "%s" "build\\%s.py"' % (self.pymakefile_data['script'], self.pymakefile_data['name']))
args = (os.path.realpath(dist))
os.chdir('build')
result = self.command('start setup.pyw py2exe -d "%s"' % args)
time.sleep(1)
file = open('screen.txt', 'r',0)
line = True
success = False
while line != '---Done---':
line = file.readline().strip()
if '*** binary dependencies ***' in line:
success = True
if line and line != '---Done---' and self.show >= 4:
print line
else:
time.sleep(0.1)
file.close()
os.chdir('..')
shutil.rmtree('build')
if success and self.show >= 1:
print "\nBuild appears to be successful.\n\n ! Always test your exe's to make sure they work !\n PyBuilder by David Mack"
if __name__ == '__main__':
pass
|