Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions pyminifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# TODO: Add the ability to use a config file instead of just command line args.
# TODO: Add the ability to save a file that allows for de-obfuscation later (or at least the ability to debug).
# TODO: Separate out the individual functions of minification so that they can be chosen selectively like the obfuscation functions.
# TODO: A conflict file entry in the windows operating system

__doc__ = """\
**Python Minifier:** Reduces the size of (minifies) Python code for use on
Expand Down Expand Up @@ -67,6 +68,7 @@

# Import built-in modules
import os, sys, re, io

from optparse import OptionParser
from collections import Iterable

Expand All @@ -85,6 +87,9 @@
import lzma
except ImportError:
pass

# define the name of the operating system 'nt'- windows
os_name = os.name

# Regexes
multiline_indicator = re.compile('\\\\(\s*#.*)?\n')
Expand Down Expand Up @@ -222,7 +227,10 @@ def pyminify(options, files):
# Get the module name from the path
module = os.path.split(sourcefile)[1]
module = ".".join(module.split('.')[:-1])
source = open(sourcefile).read()
if os_name in ('nt',):
source = open(sourcefile, encoding="utf8").read()
else:
source = open(sourcefile).read()
tokens = token_utils.listified_tokenizer(source)
if not options.nominify: # Perform minification
source = minification.minify(tokens, options)
Expand Down Expand Up @@ -258,15 +266,18 @@ def pyminify(options, files):
# Need the path where the script lives for the next steps:
filepath = os.path.split(sourcefile)[1]
path = options.destdir + '/' + filepath # Put everything in destdir
f = open(path, 'w')
if os_name in ('nt',):
f = open(path, 'w', encoding='utf-8')
else:
f = open(path, 'w')
f.write(result)
f.close()
new_filesize = os.path.getsize(path)
cumulative_new += new_filesize
percent_saved = round((float(new_filesize) / float(filesize)) * 100, 2) if float(filesize)!=0 else 0
print((
print(((
"{sourcefile} ({filesize}) reduced to {new_filesize} bytes "
"({percent_saved}% of original size)").format(**locals()))
"({percent_saved}% of original size)").format(**locals())))
p_saved = round(
(float(cumulative_new) / float(cumulative_size) * 100), 2)
print("Overall size reduction: {0}% of original size".format(p_saved))
Expand All @@ -276,7 +287,10 @@ def pyminify(options, files):
module = os.path.split(_file)[1]
module = ".".join(module.split('.')[:-1])
filesize = os.path.getsize(_file)
source = open(_file).read()
if os_name in ('nt',):
source = open(_file, encoding='utf-8').read()
else:
source = open(_file).read()
# Convert the tokens from a tuple of tuples to a list of lists so we can
# update in-place.
tokens = token_utils.listified_tokenizer(source)
Expand Down Expand Up @@ -318,4 +332,13 @@ def pyminify(options, files):
"{_file} ({filesize}) reduced to {new_filesize} bytes "
"({percent_saved}% of original size)".format(**locals())))
else:
print(result)
try:
import pprint
pprint.pprint(result)
except Exception as inst:
print(inst)
pass




43 changes: 42 additions & 1 deletion pyminifier/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from optparse import OptionParser
import sys

from . import pyminify
from . import __version__

Expand All @@ -14,6 +13,31 @@
except ImportError:
pass

import os, fnmatch, re

#------------------------------------------------------------------------------
def getsubs(dir):
'''Returns lists of files and folders located in the folder dir'''
# get all
dirs = []
files = []
for dirname, dirnames, filenames in os.walk(dir):
dirs.append(dirname)
for subdirname in dirnames:
dirs.append(os.path.join(dirname, subdirname))
for filename in filenames:
files.append(os.path.join(dirname, filename))
return dirs, files
#------------------------------------------------------------------------------
def getfilemask(f,mask):
'''Returns a file list from a list of files mask satisfying f'''
tt=[]
for i in f:
if fnmatch.fnmatch(i,mask):
tt.append(i)
return tt
#------------------------------------------------------------------------------

def main():
"""
Sets up our command line options, prints the usage/help (if warranted), and
Expand Down Expand Up @@ -168,8 +192,25 @@ def main():
if not files:
parser.print_help()
sys.exit(2)

tfiles = []
for f in files:
splf = os.path.split(f)
if splf[1].count('*'):
add_list_files(splf, f, tfiles)
else:
tfiles.append(f)
files = tfiles

pyminify(options, files)

def add_list_files(splf, f, tfiles):
fls = getfilemask(getsubs(splf[0])[1], f)
if len(fls) > 0:
for tf in fls:
if os.path.split(tf)[0] == splf[0]:
tfiles.append(tf)


if __name__ == "__main__":
main()