diff --git a/pyminifier/__init__.py b/pyminifier/__init__.py index 0e61063..d1544be 100644 --- a/pyminifier/__init__.py +++ b/pyminifier/__init__.py @@ -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 @@ -67,6 +68,7 @@ # Import built-in modules import os, sys, re, io + from optparse import OptionParser from collections import Iterable @@ -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') @@ -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) @@ -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)) @@ -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) @@ -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 + + + + diff --git a/pyminifier/__main__.py b/pyminifier/__main__.py index 5af142a..d9a213c 100644 --- a/pyminifier/__main__.py +++ b/pyminifier/__main__.py @@ -1,6 +1,5 @@ from optparse import OptionParser import sys - from . import pyminify from . import __version__ @@ -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 @@ -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()