mirror of
https://github.com/Kilian/Trimage.git
synced 2026-01-26 01:58:41 -05:00
Move two functions in the tools.py file
This commit is contained in:
parent
b829b6ac5f
commit
c040b63595
2 changed files with 43 additions and 41 deletions
|
|
@ -1,9 +1,46 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import errno
|
||||
from subprocess import call, PIPE
|
||||
|
||||
def human_readable_size(num, suffix='B'):
|
||||
for unit in ['','K','M','G','T','P','E','Z']:
|
||||
|
||||
def check_dependencies():
|
||||
"""Check if the required command line apps exist."""
|
||||
exe = ".exe" if (sys.platform == "win32") else ""
|
||||
status = True
|
||||
dependencies = {
|
||||
"jpegoptim": "--version",
|
||||
"optipng": "-v",
|
||||
"advpng": "--version",
|
||||
"pngcrush": "-version"
|
||||
}
|
||||
|
||||
for elt in dependencies:
|
||||
retcode = safe_call(elt + exe + " " + dependencies[elt])
|
||||
if retcode != 0:
|
||||
status = False
|
||||
print("[error] please install {}".format(elt), file=sys.stderr)
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def safe_call(command):
|
||||
"""Cross-platform command-line check."""
|
||||
while True:
|
||||
try:
|
||||
return call(command, shell=True, stdout=PIPE)
|
||||
except OSError as e:
|
||||
if e.errno == errno.EINTR:
|
||||
continue
|
||||
else:
|
||||
raise
|
||||
|
||||
|
||||
def human_readable_size(num, suffix="B"):
|
||||
"""Bytes to a readable size format"""
|
||||
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
|
||||
if abs(num) < 1024.0:
|
||||
return "%3.1f%s%s" % (num, unit, suffix)
|
||||
num /= 1024.0
|
||||
return "%.1f%s%s" % (num, 'Y', suffix)
|
||||
return "%.1f%s%s" % (num, "Y", suffix)
|
||||
Loading…
Add table
Add a link
Reference in a new issue