Skip to content

Commit

Permalink
macOS GUI v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissimpkins committed May 19, 2018
1 parent 3136afc commit eff9b7e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 36 deletions.
Binary file modified bin/Crunch.app/Contents/Info.plist
Binary file not shown.
7 changes: 6 additions & 1 deletion bin/Crunch.app/Contents/Resources/Credits.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ <h3 style="color:#000;">Upgrade</h3>
<p>The latest release can be found
<a href="https://github.com/chrissimpkins/Crunch/releases/latest" style="text-decoration: none;">
<b>here</b>
</a>. To upgrade with Homebrew, enter the following command in your terminal:</p>
</a>. A
<a href="https://github.com/chrissimpkins/Crunch/blob/master/CHANGELOG.md">
<b>changelog</b>
</a> is available for review of changes to the application since your installed version was released.
</p>
<p>To upgrade with Homebrew, enter the following command in your terminal:</p>
<p>
<code style="font-family:Monaco,monospace;padding-left:10px;">$ brew cask uninstall crunch && brew cask install crunch</code>
</p>
Expand Down
90 changes: 55 additions & 35 deletions bin/Crunch.app/Contents/Resources/crunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import sys
import os
import shutil
import struct
import subprocess
from subprocess import CalledProcessError

Expand All @@ -22,11 +23,21 @@
# Locks
lock = Lock()

# Processor Constants
PROCESSES = 0 # detected automatically in source if this is defined as zero
# Processor Constant
# - Modify this to an integer value if you want to fix the number of
# processes spawned during execution. The process number is
# automatically defined during source execution when this is defined
# as a value of 0
PROCESSES = 0

# Dependency Path Constants for Command Line Executable
# - Redefine these path strings to use system-installed versions of
# pngquant and zopflipng (e.g. to "/usr/local/bin/[executable]")
PNGQUANT_CLI_PATH = os.path.join(os.path.expanduser("~"), "pngquant", "pngquant")
ZOPFLIPNG_CLI_PATH = os.path.join(os.path.expanduser("~"), "zopfli", "zopflipng")

# Application Constants
VERSION = "2.0.2"
VERSION = "2.1.0"
VERSION_STRING = "crunch v" + VERSION

HELP_STRING = """
Expand Down Expand Up @@ -98,9 +109,8 @@ def main(argv):
# COMMAND LINE ERROR HANDLING
# //////////////////////////////////

# PNG file path error handling

for png_path in png_path_list:
# Not a file test
if not os.path.isfile(png_path): # is not an existing file
sys.stderr.write(
"[ERROR] '"
Expand All @@ -109,13 +119,9 @@ def main(argv):
+ os.linesep
)
sys.exit(1)
elif not png_path[-4:] == ".png": # does not end with .png extension
sys.stderr.write(
"[ERROR] '"
+ png_path
+ "' does not appear to be a PNG image file"
+ os.linesep
)
# PNG validity test
if not is_valid_png(png_path):
sys.stderr.write("[ERROR] '" + png_path + "' is not a valid PNG file." + os.linesep)
sys.exit(1)

# Dependency error handling
Expand Down Expand Up @@ -162,7 +168,7 @@ def main(argv):
except Exception as e:
lock.acquire()
sys.stderr.write("-----" + os.linesep)
sys.stderr.write("[ERROR] Error detected during execution of request:" + os.linesep)
sys.stderr.write("[ERROR] Error detected during execution of the request." + os.linesep)
sys.stderr.write(str(e) + os.linesep)
lock.release()
sys.exit(1)
Expand Down Expand Up @@ -240,13 +246,36 @@ def optimize_png(png_path):
lock.release()


def fix_filepath_args(args):
arg_list = []
parsed_filepath = ""
for arg in args:
if arg[0] == "-":
# add command line options
arg_list.append(arg)
elif len(arg) > 4 and arg[-4:] == ".png":
# this is the end of a filepath string that may have had
# spaces in directories prior to this level. Let's recreate
# the entire original path
filepath = parsed_filepath + arg
arg_list.append(filepath)
# reset the temp string that is used to reconstruct the filepaths
parsed_filepath = ""
else:
# if the argument does not end with a .png, then there must have
# been a space in the directory paths, let's add it back
parsed_filepath = parsed_filepath + arg + " "
# return new argument list with fixed filepaths to calling code
return arg_list


def get_pngquant_path():
if sys.argv[1] == "--gui":
return "./pngquant"
elif sys.argv[1] == "--service":
return "/Applications/Crunch.app/Contents/Resources/pngquant"
else:
return os.path.join(os.path.expanduser("~"), "pngquant", "pngquant")
return PNGQUANT_CLI_PATH


def get_zopflipng_path():
Expand All @@ -255,7 +284,17 @@ def get_zopflipng_path():
elif sys.argv[1] == "--service":
return "/Applications/Crunch.app/Contents/Resources/zopflipng"
else:
return os.path.join(os.path.expanduser("~"), "zopfli", "zopflipng")
return ZOPFLIPNG_CLI_PATH


def is_valid_png(filepath):
# The PNG byte signature (https://www.w3.org/TR/PNG/#5PNG-file-signature)
expected_signature = struct.pack('8B', 137, 80, 78, 71, 13, 10, 26, 10)
# open the file and read first 8 bytes
with open(filepath, 'rb') as filer:
signature = filer.read(8)
# return boolean test result for first eight bytes == expected PNG byte signature
return signature == expected_signature


def shellquote(filepath):
Expand Down Expand Up @@ -298,26 +337,7 @@ def get_compression_percent(self):
# that are split by the shell script into separate arguments
# when there are spaces in the macOS file path
if sys.argv[1] == "--gui" or sys.argv[1] == "--service":
arg_list = []
parsed_filepath = ""
for arg in sys.argv[1:]:
if arg[0] == "-":
# add command line options
arg_list.append(arg)
elif arg[-4:] == ".png":
# this is the end of a filepath string that may have had
# spaces in directories prior to this level. Let's recreate
# the entire original path
filepath = parsed_filepath + arg
arg_list.append(filepath)
# reset the temp string that is used to reconstruct the filepaths
parsed_filepath = ""
else:
# if the argument does not end with a .png, then there must have
# been a space in the directory paths, let's add it back
parsed_filepath = arg + " "
# now that any space characters are appropriately escaped in the
# original filepaths, call main function with the new arg list
arg_list = fix_filepath_args(sys.argv[1:])
main(arg_list)
else:
# the command line executable assumes that users will appropriately quote
Expand Down

0 comments on commit eff9b7e

Please sign in to comment.