Forum Archive

Preparation for Xcode ? Delete files in PythonistaAppTemplate?

tlinnet

Hi Pythonistas.

Thanks for a super product, and thanks for all the good help. :)
This have made it possible to make a first UI app in iOS for me.

So, I am a little excited. ;)

I have also tried to compile via xCode.
Using the template at: https://github.com/omz/PythonistaAppTemplate
This worked!

So now I am playing around with icons.

I would like to try TestFlight Beta Testing.
https://developer.apple.com/testflight/

But I wonder if I could/should? make the Template smaller?

I really only use the modules

import platform, os, datetime, logging, sys, os, json, requests
from operator import itemgetter
from time import strftime
import ui, console, dialogs
from objc_util import *

Can I / Should I delete these folders:

PythonistaAppTemplate/PythonistaKit.framework/Media
PythonistaAppTemplate/PythonistaKit.framework/pylib_ext

Is it "worth" it, to start hassle with this?
Will the compilation be "faster" without the files?
Will the "App size" be smaller?

Or should I just leave it?

Thanks again!

NoBetterName

Go into each of your modules. Start a list, and add them to it. In each module, find the module s imported by it. Add them to the list and cross off the first module. For example, your list would start as:

platform
datetime
logging
sys
os
json
requests
operator
time
ui
console
dialogs
objc_util

Go down the list, starting with platform. The modules imported by platform.py are sys, string, os, re, tempfile, socket, subprocess, and struct. Sys and os are already in your list, so add the others to the list. Cross platform.py off on your list. Then go into os.py and repeat the process. Some internal modules do not have a python file, so beware.

Once you have crossed off all of the module names, delete everything else from Xcode. The crossed off names should be the only modules in Xcode. If there are any empty folders, you can delete them. If they are not empty, do not move anything out of them or into them. I hope this helps.

NoBetterName

Some modules are actually folders, consisting of many files. For example, I don't think that your program uses the sympy module. It takes up a huge amount of space. Apple has a maximum app size, so make sure to trim your app down as much as possible. As for the Media folder, it stores the built in sounds, images, and a few other things. Delete what you don't use, but not the folder.

tlinnet

@tlinnet said:

PythonistaAppTemplate/PythonistaKit.framework/Media
PythonistaAppTemplate/PythonistaKit.framework/pylib_ext

Hi!

I deleted:
PythonistaAppTemplate/PythonistaKit.framework/Media
PythonistaAppTemplate/PythonistaKit.framework/pylib_ext

And the app still work. :)
The compilation was a little more smooth, when the compiler didnt seem to copy over all the
media files.

For your suggestion, it almost calls for a small snippet of code to analyse what could be deleted. ;)
But for now I am happy, and I will come back to this issue later.

Thanks again!

NoBetterName

You must not have had any modules in the pylib_ext folder. But still, remember that you use more modules than your main program imports. Also, remember that apple has a maximum app size. It is in the documentation, I forget the actual number.

lukaskollmer

@NoBetterName maximum app size shouldn't be a problem. iOS apps can be up to 4GB in size, and the app executable can be up to 60MB.

Source: http://stackoverflow.com/a/4753253/2513803

tlinnet

I made it through to TestFlight. :)

Deleting:
PythonistaAppTemplate/PythonistaKit.framework/Media
PythonistaAppTemplate/PythonistaKit.framework/pylib_ext

And just a main.py file, gave an app size og 60 MB.
Which is within the 100 Mb, "over the air" download limit.

So I dont want to hassle around to get lower app size. :)
Cool!

NoBetterName

A lot of the stuff in the regular pylib folder is taking up space. A lot of those modules you don't need. Your main.py will barely take up any space. You might be able to get your app size under 10mB.

ltddev

May I ask the community and @omz in particular if there is yet a an equivalent template available for Pythonista v2.0? If there is, please provide a link to it. If there is not, is there an action plan to provide one?

It is my understanding that given there is no template for Pythonista v2.0 it is NOT currently possible to produce apps for the Apple App store. Is my understanding correct?

NoBetterName

Depends on which Python you are using. Pythonista 2.0 carries 2 Python levels: Python 2.7 and Python 3.5. The current template supports Python 2.7. To my knowledge, there is no template for Xcode and Python 3.5.

ltddev

@NoBetterName that was my understanding as well, and you help to format my request more clearly: I mean where is there a template for Pythonista 2.0, Python level 3.x code?

tlinnet

I got this started.

Running this snippet on the phone, give me the file paths.

```
import inspect
import importlib

pyt_mods = [
"os",
"datetime",
"json",
"requests",
"operator",
"time",
]

pythonista_mods = [
"ui",
"console",
"dialogs",
"objc_util"
]

all_mods = pyt_mods + pythonista_mods

keep_list = []
for imods in all_mods:
try:
module_obj = importlib.import_module(imods)
filepath = inspect.getfile(module_obj).split("PythonistaKit.framework")[1]
keep_list.append(filepath)

except TypeError as e:
    print(e)

for filep in keep_list:
print(filep)```

tlinnet

So I made an initial script "strip_pylib.py".

This script should be runned in pythonista at the phone.
It will make a "pylib_clean.sh" file, which should be copied to:

"PATH/PythonistaAppTemplate/PythonistaKit.framework"
at the Xcode project.

And then issue:

source pylib_clean.sh 
rm pylib_clean.sh 

(Note: pylib_clean.sh should be deleted in the PythonistaKit.framework folder afterwards,
or there will be problems with signing and sending to App store.)

pylib_clean.sh will delete a lot of files.

After compilation in Xcode, the program did not work.
I had to manually copy back the directory: "/pylib/encodings" from another copy of PythonistaAppTemplate, and then it worked.

The compilation in Xcode is a lot faster. :)

And the app size went from 60 MB to 30 MB.
There are still some files left, but I am not sure why they survived.

strip_pylib.py

# Clear all
# To get pythonista to work again, restart the app
import sys
sys.modules[__name__].__dict__.clear()

# Now import
import sys
import inspect
import importlib

splitstr = "PythonistaKit.framework"

pyt_mods = [
        "os",
        "datetime",
        "json",
        "requests",
        "operator",
        "time",
        ]

pythonista_mods = [
        "ui",
        "console",
        "dialogs",
        "objc_util"
      ]  

all_mods = pyt_mods + pythonista_mods

keep_list = []
for imods in all_mods:
    try:
        m  = importlib.import_module(imods)
        dirpath, filepath = inspect.getfile(m).split(splitstr)
        keep_list.append(filepath)

    except TypeError as e:
        #print(e)
        pass

# Get the imported modules
dict = sys.modules
for key in dict:
    val = dict[key]
    if val == None:
        continue
    else:
        try:
            filepath = inspect.getfile(val)
            if splitstr in filepath:
                filepath_append = filepath.split(splitstr)[1]
                keep_list.append(filepath_append)
            else:
                pass

        except TypeError as e:
            #print(e)
            pass

# Make uniq and sort
keep_list = sorted(set(keep_list))

# Now find all files
import os

fp = dirpath+splitstr
extensions = [".py", ".pyo"]
all_files = []
for path, dirs, files in os.walk(fp):
    for f in files:
        filename, file_extension = os.path.splitext(f)
        if "/pylib/" in path or "/pylib_ext/" in path:
            if file_extension in extensions:
                stringfp = os.path.join(path, f)
                dirpath, filepath = stringfp.split(splitstr)
                all_files.append(filepath)

# Make uniq and sort
all_files = sorted(set(all_files))

# Make a delete list
dellist = [x for x in all_files if x not in keep_list]

# Write delete file
fname = 'pylib_clean.sh'
f = open(fname,'w')
f.write("#!/usr/bin/env bash\n")

for idel in dellist:
    f.write("rm ."+idel+"\n")
f.close()

f = open(fname, "r")
for line in f:
    print(line),
f.close()