From 06a6cad9804be226631a147ff728c564c670e3c8 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 27 Apr 2017 19:53:23 -0400 Subject: [PATCH] First stab at packaging with pyinstaller --- .gitignore | 3 +++ SotMDeckBuilder.py | 55 +++++++++++++++++++++++++++++++------------- SotMDeckBuilder.spec | 36 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 16 deletions(-) mode change 100644 => 100755 SotMDeckBuilder.py create mode 100644 SotMDeckBuilder.spec diff --git a/.gitignore b/.gitignore index e77ef3d..77cba7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ /*.png /*.json +/__pycache__/ +/build/ +/dist/ diff --git a/SotMDeckBuilder.py b/SotMDeckBuilder.py old mode 100644 new mode 100755 index cdb43c9..8355613 --- a/SotMDeckBuilder.py +++ b/SotMDeckBuilder.py @@ -10,17 +10,23 @@ from PIL import Image, ImageDraw, ImageFont import textwrap import math import os +import sys -IMG_HERO_CHAR_FRONT = Image.open("images/HeroCharFront.png") -IMG_HERO_CHAR_BACK = Image.open("images/HeroCharBack.png") -IMG_HERO_DECK = Image.open("images/HeroCard.png") -IMG_TARGET_HP = Image.open("images/targetHP.png") +if getattr(sys, 'frozen', False): # we are running in a bundle + bundle_dir = sys._MEIPASS +else: # we are running in a normal Python environment + bundle_dir = os.path.dirname(os.path.abspath(__file__)) -FONT_KEYWORD = ImageFont.truetype("fonts/RedStateBlueStateBB_reg.otf", size=40) -FONT_TITLE = ImageFont.truetype("fonts/CrashLandingBB.otf", size=60) -FONT_DESCRIPTION = ImageFont.truetype("fonts/RedStateBlueStateBB_reg.otf", size=30) -FONT_TARGET_HP = ImageFont.truetype("fonts/CrashLandingBB.otf", size=120) -FONT_CHAR_HP = ImageFont.truetype("fonts/ap.ttf", size=120) +IMG_HERO_CHAR_FRONT = Image.open(bundle_dir + "/images/HeroCharFront.png") +IMG_HERO_CHAR_BACK = Image.open(bundle_dir + "/images/HeroCharBack.png") +IMG_HERO_DECK = Image.open(bundle_dir + "/images/HeroCard.png") +IMG_TARGET_HP = Image.open(bundle_dir + "/images/targetHP.png") + +FONT_KEYWORD = ImageFont.truetype(bundle_dir + "/fonts/RedStateBlueStateBB_reg.otf", size=40) +FONT_TITLE = ImageFont.truetype(bundle_dir + "/fonts/CrashLandingBB.otf", size=60) +FONT_DESCRIPTION = ImageFont.truetype(bundle_dir + "/fonts/RedStateBlueStateBB_reg.otf", size=30) +FONT_TARGET_HP = ImageFont.truetype(bundle_dir + "/fonts/CrashLandingBB.otf", size=120) +FONT_CHAR_HP = ImageFont.truetype(bundle_dir + "/fonts/ap.ttf", size=120) def drawTextIf(draw, position, font, card, key, wrap=False): if key in card: @@ -84,9 +90,9 @@ def makeFaces(deckJson, outfile): return (baseX, baseX) def makeJson(deckJson, imgWidth, imgHeight, outfile): - with open("templates/deck.json") as f: + with open(bundle_dir + "/templates/deck.json") as f: outJson = json.load(f) - with open("templates/card.json") as f: + with open(bundle_dir + "/templates/card.json") as f: cardTemplate = json.load(f) # number of cards in x and y direction @@ -130,10 +136,27 @@ def makeJson(deckJson, imgWidth, imgHeight, outfile): with open(outfile + ".json", "w") as f: json.dump(outJson, f) -with open("hero_3.json") as f: - deckJson = json.load(f) +if __name__ == '__main__': + if len(sys.argv) < 3: + print("not enough arguments!") + inputJson = input("Input file: ") + outfile = input("Output file (no suffix): ") + else: + inputJson = sys.argv[1] + outfile = sys.argv[2] -outfile = "out" + with open(inputJson) as f: + deckJson = json.load(f) -imgWidth, imgHeight = makeFaces(deckJson, outfile) -makeJson(deckJson, imgWidth, imgHeight, outfile) + imgWidth, imgHeight = makeFaces(deckJson, outfile) + makeJson(deckJson, imgWidth, imgHeight, outfile) + + +else: + with open("hero_3.json") as f: + deckJson = json.load(f) + + outfile = "out" + + imgWidth, imgHeight = makeFaces(deckJson, outfile) + makeJson(deckJson, imgWidth, imgHeight, outfile) diff --git a/SotMDeckBuilder.spec b/SotMDeckBuilder.spec new file mode 100644 index 0000000..a58519a --- /dev/null +++ b/SotMDeckBuilder.spec @@ -0,0 +1,36 @@ +# -*- mode: python -*- + +block_cipher = None + + +a = Analysis(['SotMDeckBuilder.py'], + pathex=['Z:\\home\\adam\\scratch\\TTS_SotM_Deck'], + binaries=[], + datas=[('images/*.png', 'images'), + ('fonts/*.ttf', 'fonts'), + ('fonts/*.otf', 'fonts'), + ('templates/*.json', 'templates')], + hiddenimports=['pillow'], + hookspath=[], + runtime_hooks=[], + excludes=[], + win_no_prefer_redirects=False, + win_private_assemblies=False, + cipher=block_cipher) +pyz = PYZ(a.pure, a.zipped_data, + cipher=block_cipher) +exe = EXE(pyz, + a.scripts, + exclude_binaries=True, + name='SotMDeckBuilder', + debug=False, + strip=False, + upx=True, + console=True ) +coll = COLLECT(exe, + a.binaries, + a.zipfiles, + a.datas, + strip=False, + upx=True, + name='SotMDeckBuilder')