summaryrefslogtreecommitdiff
path: root/setup.py
blob: 7f7a1a819f39de7cd9da3bf4ccd217bb06a31b57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from distutils.core import setup
from sys import exit
from os import path
from zipfile import ZipFile, ZIP_DEFLATED
from glob import glob
from shutil import rmtree
from subprocess import call
import py2exe
import git

OUT_DIR = 'ck'
PROJECT_FULLNAME = 'Controller Keyboard'
PROJECT_MACHINENAME = 'controller-keyboard'

def hex_print(binary_string):
    return ''.join( [ "%02X" % ord(chr(x)) for x in binary_string ] ).strip().lower()

repo = git.Repo('.')
if repo.is_dirty():
    print('Git repo is dirty!\nAborting.')
    exit(1)

print('Git repo is clean.  Proceeding.')

# Get the first 8 characters of the commit hash
full_hash = hex_print(repo.commit(rev="HEAD").binsha)
short_hash = full_hash[:8]

try:
    rmtree(OUT_DIR)
except FileNotFoundError:
    pass

setup(
    name=PROJECT_FULLNAME,
    version='0.1',
    url='http://zorchenhimer.com',
    console=['main.py'],
    author='Nick Scheibenpflug',
    author_email='zorchenhimer@gmail.com',
    # Setting this to 'None' puts everything in the .exe.  Makes it bloated as
    # fuck and stores data twice (once per .exe).
    #zipfile=None,
    data_files=[
        ('./pics/', ['pics/bg_castle.png']),
        ('./data/', ['data/profont.ttf'])
        ],
    options={
        'py2exe': {
            'dist_dir': OUT_DIR,
            # Exclude modules that aren't found.
            #'excludes': [
            #    'readline',
            #    'elementtree',
            #    'PIL',
            #    'Image',
            #    'cElementTree',
            #    '_yaml'],
            # General optimizations
            'optimize': 1,
            # Don't bundle anything (outside of what's bundled in library.zip.
            # Shit crashes and burns otherwise.  Idk why.
            'bundle_files': 3,
            'compressed': 1
        }
    }
)

tag_txt = ''
if len(repo.tags) > 1:
    last_tag = repo.tags[-1]
    if hex_print(last_tag.commit.binsha) == full_hash:
        ## Commit is tagged.  TODO: search all tags, not just the most recent.
        tag_txt = '_{!s}_'.format(last_tag)

with open(path.join(OUT_DIR, 'version.txt'), 'w') as f:
    f.write(full_hash)
    if len(tag_txt) > 0:
        f.write('\n{!s}'.format(last_tag))

filename_versioning = short_hash
if len(tag_txt) > 0:
    filename_versioning = '{!s}_{}'.format(last_tag, short_hash)

zip_filename = '{}_{}.zip'.format(PROJECT_MACHINENAME, filename_versioning)
print('Compressing "{}" folder to {}.'.format(OUT_DIR, zip_filename))
dist_files = glob('{}/*'.format(OUT_DIR))
with ZipFile(zip_filename, mode='w') as zf:
    for f in dist_files:
        zf.write(f, arcname=f)

if path.isfile('installer.nsi'):
    call(r"C:\Program Files (x86)\NSIS\makensis.exe /DOF=Setup_{mn}_{tag}.exe .\installer.nsi".format(
        tag = filename_versioning,
        mn  = PROJECT_MACHINENAME
        ))
else:
    print('No NSIS installer script.')

print('Done.')