File: pymailgui-products/unzipped/build/build-source/build.py

#!/usr/bin/env python3
"""
=============================================================================
Make a PyMailGUI source-code package (a zipfile scrubbed of private content).
This is just a few moves and a zip; there are no frozen builds here.
Unlike the app/exe scripts, this may be run on Windows, Mac, or Linux.

*NOTE*: remove any unzipped app/exe folders in build/ before running this.

June-2022: new /Users/me and ~/MY-STUFF paths for new macos build machine,
delete new _private_ folder (spawned by shrinkpix project, sometime > 2017).
=============================================================================
"""

import os, sys, shutil
join, sep = os.path.join, os.path.sep
startdir = os.getcwd()                    # this build script's dir

# 'python3' fails in both IDLE and PyEdit RunCode (env's PATH not inherited?)
python = sys.executable

#----------------------------------------------------------------------------
# make app's icon if one doesn't already exist
#----------------------------------------------------------------------------

pass  # handle this manually in build-app-exe, as icons differ per platform

#----------------------------------------------------------------------------
# copy PyMailGUI to avoid accidents and recursive-copy loop
#----------------------------------------------------------------------------

# automated setup - run in this file's dir

if sys.platform.startswith('darwin'):
    temp = '/Users/me/Desktop/tempsrc'             # cp can't include self! (June22)
elif sys.platform.startswith('win'):
    temp = r'C:\Users\mark\Desktop\tempsrc'        # or use $HOME, etc.
elif sys.platform.startswith('linux'):
    temp = '/home/name/Desktop/tempsrc'

if os.path.exists(temp):
    shutil.rmtree(temp)
os.mkdir(temp)

# copy all to temp
print('Building tree')
shutil.copytree(join('..', '..', '..', 'pymailgui'),
                join(temp, 'pymailgui'), symlinks=True) 

#----------------------------------------------------------------------------
# make the source zipfile
#----------------------------------------------------------------------------

# zip command: use portable ziptools (vs: 'zip -r %s %s' % (thezip, thedir))
thedir = 'PyMailGUI-source'
thezip = thedir + '.zip'

if sys.platform.startswith('darwin'):
    code = '~/MY-STUFF/Code/ziptools/link'    # (June22)
elif sys.platform.startswith('win'):
    code = r'C:\MY-STUFF\Code\mergeall\test\ziptools'
elif sys.platform.startswith('linux'):
    code = '/media/name/End_of_the_drive/MARKS-STUFF/Code/mergeall/test/ziptools'

zipit = '%s %s/zip-create.py %s %s -skipcruft' % (python, code, thezip, thedir)
zipit = zipit.replace('/', os.sep)

# rename and move source product folder here
shutil.move(join(temp, 'pymailgui'), join(temp, thedir))
if os.path.exists(thedir):
    shutil.rmtree(thedir)                      # nuke bogus retained temp?
shutil.move(join(temp, thedir), '.')
shutil.rmtree(temp)                            # rm temp build tree 

# remove own zipped app, exes, src in the build tree for space
for (root, subs, files) in os.walk(join(thedir, 'build')):
    for file in files:
        if file.startswith(('PyMailGUI', 'prev-PyMailGUI')) and file.endswith('.zip'):
            filepath = join(root, file)
            print('Removing', filepath)
            os.remove(filepath)
            dummy = open(filepath + '.stripped', 'w')
            dummy.write('**REMOVED**')
            dummy.close()

# strip the 400M+ nested PyEdit build dirs
print('Removing PyEdit/Build')
pyeditbuild = 'PyMailGui-PP4E/PP4E/Gui/TextEditor/build'
shutil.rmtree(join(thedir, pyeditbuild))
os.mkdir(join(thedir, pyeditbuild))
dummy = open(join(thedir, pyeditbuild, 'README.txt'), 'w')
dummy.write('Content removed: see PyEdit for its build scripts.\n')
dummy.close()  # else in-use on rmtree

# clean pyedit auto-saves; pass on reinstating zipped examples in docetc/examples
autosave_pmg = 'PyMailGui-PP4E/__pyedit-autosaves__'
autosave_pe  = 'PyMailGui-PP4E/PP4E/Gui/TextEditor/__pyedit-autosaves__'

for asave in (autosave_pmg, autosave_pe):
    for item in os.listdir(join(thedir, asave)):
        if item != 'README-autosaves.txt':
            itempath = join(thedir, asave, item)
            print('Removing', itempath)
            os.remove(itempath)

# clean TempParts; pass on reinstating zipped example in docetc/examples
tempparts = 'PyMailGui-PP4E/TempParts'
for item in os.listdir(join(thedir, tempparts)):
    if item not in ['README-TempParts.txt', 'DSC00565.JPG', 'DSC03890.JPG']:
        itempath = join(thedir, tempparts, item)
        print('Removing', itempath)
        os.remove(itempath)       

# relace sent mail with example stub; very dangerous...
print('Replacing sentmail')
sentmail = 'PyMailGui-PP4E/sentmail.txt'
os.remove(join(thedir, sentmail))
os.rename(join(thedir, 'PyMailGui-PP4E/sentmail-save.txt'), join(thedir, sentmail))

# drop other non-public stuff (but not __sloc__.py)
print('Removing __private__, etc.')
shutil.rmtree(join(thedir, '_private_'))      # (June22)
shutil.rmtree(join(thedir, '__private__'))
shutil.rmtree(join(thedir, '__pycache__'))
shutil.rmtree(join(thedir, '_old-screenshots'))
shutil.rmtree(join(thedir, 'PyMailGui-PP4E', 'PP4E', 'Gui', 'TextEditor', '__private__'))
shutil.rmtree(join(thedir, 'PyMailGui-PP4E', 'PP4E', 'Gui', 'TextEditor', '_private_'))      # (June22)

# zip the reorganized source folder
if os.path.exists(thezip):
    shutil.move(thezip, 'prev-'+thezip)        # save previous version
os.system(zipit)                               # run zip in build-source
shutil.rmtree(thedir)                          # rm temp folder copy 

print('Done: see', thezip)

# +unzip and copy elsewhere for easy access



[Home page] Books Code Blog Python Author Train Find ©M.Lutz