Skip to content

Commit

Permalink
setup.py: Add cx_Freeze support, as py2exe seems outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebual committed Apr 20, 2019
1 parent 7cc55ea commit 3312186
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,19 @@ For git:

For optimal performance, you can compile the code to C using Cython.

To do so, you have to install Cython and Python dev files
(`sudo apt-get install cython python-dev` under Ubuntu), then run
`python setup.py build_ext --inplace`. You will have
to run this last command each time you update the game.
```
pip install cython
python setup.py build_ext --inplace
```
This will generate `.pyd` files, which Python will prefer to load instead of your `.py` files,
so you will need to rebuild or delete the `.pyd` each time you make changes.

setup.py will also compile Pyglet using Cython, if you download
the pyglet source code and put the *pyglet* folder inside the game repository.

## Building Windows Executables
```
pip install cython cx_Freeze
python setup.py build
```
This builds Cython-optimized `.pyd`'s, and bundles together all libraries including Python itself, into `build/exe.win-amd64-3.7/`
91 changes: 79 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# Imports, sorted alphabetically.
import glob
import os
import sys

# Python packages
from Cython.Distutils import build_ext
from distutils.core import setup
from distutils.util import get_platform
from distutils.extension import Extension
import os

# Third-party packages
# Nothing for now...

# Modules from this project
import globals as G


Expand All @@ -18,7 +14,11 @@
'gui',
'views',
'controllers',
'setup-py2exe',
'mods',
'build',
'setup',
'setup-py2exe',
'setup-cx_Freeze',
'pyglet.gl.glext_arb',
'pyglet.gl.glext_nv',
'pyglet.image.codecs',
Expand All @@ -27,9 +27,39 @@
'pyglet.media.drivers.alsa.asound',
'pyglet.window',
'pyglet.window.xlib.xlib',
'venv',
)

excluded_includes = (
'main',
'manager',
)



def find_files(source, target, patterns):
"""Locates the specified data-files and returns the matches
in a data_files compatible format.
source is the root of the source data tree.
Use '' or '.' for current directory.
target is the root of the target data tree.
Use '' or '.' for the distribution directory.
patterns is a sequence of glob-patterns for the
files you want to copy.
"""
if glob.has_magic(source) or glob.has_magic(target):
raise ValueError("Magic not allowed in src, target")
ret = {}
for pattern in patterns:
pattern = os.path.join(source, pattern)
for filename in glob.glob(pattern):
if os.path.isfile(filename):
targetpath = os.path.join(target, os.path.relpath(filename, source))
path = os.path.dirname(targetpath)
ret.setdefault(path, []).append(filename)
return sorted(ret.items())


def get_modules(path=None):
first = False
if path is None:
Expand All @@ -38,7 +68,7 @@ def get_modules(path=None):
for f_or_d in os.listdir(path):
if not first:
f_or_d = os.path.join(path, f_or_d)
if os.path.isdir(f_or_d):
if os.path.isdir(f_or_d) and f_or_d not in excluded_modules:
d = f_or_d
for name, f in get_modules(d):
yield name, f
Expand All @@ -50,10 +80,47 @@ def get_modules(path=None):
if name and name not in excluded_modules:
yield name, f


ext_modules = [Extension(name, [f]) for name, f in get_modules()]
includes = [name for name, f in get_modules() if name not in excluded_includes]

packages = ["pyglet", "sqlite3"] # force cx_Freeze to bundle these

cython_output_dir = "build/lib.%s-%d.%d" % (get_platform(), *sys.version_info[:2])
options = {
'build_ext': {
# 'inplace': True,
'cython_c_in_temp': True,
'cython_directives': {
'language_level': '3',
}
},
}

if len(sys.argv) > 1 and sys.argv[1] in ("build", "build_exe"):
from cx_Freeze import setup, Executable

options['build_exe'] = {
'packages': packages,
'excludes': ['test', 'unittest'],
'includes': includes,
'include_files': ['resources/'],
"path": [cython_output_dir] + sys.path,
}
executables = [Executable("main.py", targetName=G.APP_NAME + (".exe" if sys.platform == 'win32' else '')), Executable("server.py")]

else:
from distutils.core import setup

executables = []

setup(
name=G.APP_NAME,
cmdclass={'build_ext': build_ext},
ext_modules=ext_modules, requires=['pyglet', 'Cython']
options=options,
requires=['pyglet', 'Cython'],
version="0.1",
description="A Minecraft demo clone in Python 3.6.x",
ext_modules=ext_modules,
executables=executables,
)

0 comments on commit 3312186

Please sign in to comment.