From eea861e4ac14116cb23597c567d556b30dbdfd81 Mon Sep 17 00:00:00 2001 From: Alex_Dv Date: Wed, 21 Dec 2016 18:27:18 +0300 Subject: [PATCH 1/4] A conflict file entry in the WINDOWS operating system --- pyminifier/__init__.py | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/pyminifier/__init__.py b/pyminifier/__init__.py index 0e61063..9f1384a 100644 --- a/pyminifier/__init__.py +++ b/pyminifier/__init__.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- # # Copyright 2013 Liftoff Software Corporation # @@ -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') @@ -184,7 +189,7 @@ def pyminify(options, files): try: prepend = open(options.prepend).read() except Exception as err: - print("Error reading %s:" % options.prepend) + print(("Error reading %s:" % options.prepend)) print(err) obfuscations = (options.obfuscate, options.obf_classes, @@ -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,25 +266,31 @@ 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)) + print(("Overall size reduction: {0}% of original size".format(p_saved))) else: # Get the module name from the path _file = files[0] 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 + + + + From f6c6f802168da95591904884f40d7721bde225cd Mon Sep 17 00:00:00 2001 From: alexandr_dragunkin_acer Date: Tue, 27 Dec 2016 19:33:17 +0300 Subject: [PATCH 2/4] Processing lists of files by mask *. py c:\temp\obf>pyminifier --destdir=c:\temp\obf\tt\ c:\temp\obf\*.py --- pyminifier/__main__.py | 43 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) 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() From 739dc0ee170998e63bdefe80c3643ee73ee9268d Mon Sep 17 00:00:00 2001 From: alexandr_dragunkin_acer Date: Tue, 27 Dec 2016 19:59:10 +0300 Subject: [PATCH 3/4] ((->) --- pyminifier/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyminifier/__init__.py b/pyminifier/__init__.py index 9f1384a..d1544be 100644 --- a/pyminifier/__init__.py +++ b/pyminifier/__init__.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- # # Copyright 2013 Liftoff Software Corporation # @@ -189,7 +189,7 @@ def pyminify(options, files): try: prepend = open(options.prepend).read() except Exception as err: - print(("Error reading %s:" % options.prepend)) + print("Error reading %s:" % options.prepend) print(err) obfuscations = (options.obfuscate, options.obf_classes, @@ -280,7 +280,7 @@ def pyminify(options, files): "({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))) + print("Overall size reduction: {0}% of original size".format(p_saved)) else: # Get the module name from the path _file = files[0] From 8765a75fd72559d09e3e05a8632064f9e444b12c Mon Sep 17 00:00:00 2001 From: Alexander Dragunkin Date: Thu, 25 Jul 2019 20:47:00 +0300 Subject: [PATCH 4/4] Aadd in the first line utf-8 --- pyminifier/__init__.py | 61 +++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/pyminifier/__init__.py b/pyminifier/__init__.py index d1544be..64d7325 100644 --- a/pyminifier/__init__.py +++ b/pyminifier/__init__.py @@ -5,9 +5,9 @@ # For license information see LICENSE.txt # Meta -__version__ = '2.2' -__version_info__ = (2, 2) -__license__ = "GPLv3" # See LICENSE.txt +__version__ = '2.2.1' +__version_info__ = (2, 2, 1) +__license__ = "GPLv3" # See LICENSE.txt __author__ = 'Dan McDougall ' # TODO: Add the ability to mark variables, functions, classes, and methods for non-obfuscation. @@ -67,7 +67,10 @@ """ # Import built-in modules -import os, sys, re, io +import os +import sys +import re +import io from optparse import OptionParser from collections import Iterable @@ -87,7 +90,7 @@ import lzma except ImportError: pass - + # define the name of the operating system 'nt'- windows os_name = os.name @@ -95,21 +98,26 @@ multiline_indicator = re.compile('\\\\(\s*#.*)?\n') # The test.+() functions below are for testing pyminifier... + + def test_decorator(f): """Decorator that does nothing""" return f + def test_reduce_operators(): """Test the case where an operator such as an open paren starts a line""" - (a, b) = 1, 2 # The indentation level should be preserved + (a, b) = 1, 2 # The indentation level should be preserved pass + def test_empty_functions(): """ This is a test function. This should be replaced with 'def test_empty_functions(): pass' """ + class test_class(object): "Testing indented decorators" @@ -117,6 +125,7 @@ class test_class(object): def test_function(self): pass + def test_function(): """ This function encapsulates the edge cases to prevent them from invading the @@ -125,8 +134,8 @@ def test_function(): # This tests method obfuscation: method_obfuscate = test_class() method_obfuscate.test_function() - foo = ("The # character in this string should " # This comment - "not result in a syntax error") # ...and this one should go away + foo = ("The # character in this string should " # This comment + "not result in a syntax error") # ...and this one should go away test_multi_line_list = [ 'item1', 'item2', @@ -140,13 +149,14 @@ def test_function(): # It may seem strange but the code below tests our docstring removal code. test_string_inside_operators = imaginary_function( "This string was indented but the tokenizer won't see it that way." - ) # To understand how this could mess up docstring removal code see the + ) # To understand how this could mess up docstring removal code see the # minification.minification.remove_comments_and_docstrings() function # starting at this line: # "elif token_type == tokenize.STRING:" # This tests remove_extraneous_spaces(): - this_line_has_leading_indentation = '''<--That extraneous space should be - removed''' # But not these spaces + this_line_has_leading_indentation = '''<--That extraneous space should be + removed''' # But not these spaces + def is_iterable(obj): """ @@ -157,6 +167,7 @@ def is_iterable(obj): return False return isinstance(obj, Iterable) + def pyminify(options, files): """ Given an *options* object (from `optparse.OptionParser` or similar), @@ -182,7 +193,7 @@ def pyminify(options, files): sys.exit(1) # Make our .pyz: compression.zip_pack(files, options) - return None # Make sure we don't do anything else + return None # Make sure we don't do anything else # Read in our prepend text (if any) prepend = None if options.prepend: @@ -200,8 +211,8 @@ def pyminify(options, files): # obfuscation is stated) if options.use_nonlatin and not any(obfuscations): options.obfuscate = True - if len(files) > 1: # We're dealing with more than one file - name_generator = None # So we can tell if we need to obfuscate + if len(files) > 1: # We're dealing with more than one file + name_generator = None # So we can tell if we need to obfuscate if any(obfuscations): # Put together that will be used for all obfuscation functions: identifier_length = int(options.replacement_length) @@ -217,9 +228,9 @@ def pyminify(options, files): else: name_generator = obfuscate.obfuscation_machine( identifier_length=identifier_length) - table =[{}] - cumulative_size = 0 # For size reduction stats - cumulative_new = 0 # Ditto + table = [{}] + cumulative_size = 0 # For size reduction stats + cumulative_new = 0 # Ditto for sourcefile in files: # Record how big the file is so we can compare afterwards filesize = os.path.getsize(sourcefile) @@ -232,7 +243,7 @@ def pyminify(options, files): else: source = open(sourcefile).read() tokens = token_utils.listified_tokenizer(source) - if not options.nominify: # Perform minification + if not options.nominify: # Perform minification source = minification.minify(tokens, options) # Have to re-tokenize for obfucation (it is quick): tokens = token_utils.listified_tokenizer(source) @@ -246,7 +257,7 @@ def pyminify(options, files): table=table ) # Convert back to text - result = '' + result = '# -*- coding: utf-8 -*-\n' if prepend: result += prepend result += token_utils.untokenize(tokens) @@ -265,7 +276,7 @@ def pyminify(options, files): os.mkdir(options.destdir) # 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 + path = options.destdir + '/' + filepath # Put everything in destdir if os_name in ('nt',): f = open(path, 'w', encoding='utf-8') else: @@ -274,7 +285,7 @@ def pyminify(options, files): 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 + percent_saved = round((float(new_filesize) / float(filesize)) * 100, 2) if float(filesize) != 0 else 0 print((( "{sourcefile} ({filesize}) reduced to {new_filesize} bytes " "({percent_saved}% of original size)").format(**locals()))) @@ -294,7 +305,7 @@ def pyminify(options, files): # 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) - if not options.nominify: # Perform minification + if not options.nominify: # Perform minification source = minification.minify(tokens, options) # Convert back to tokens in case we're obfuscating tokens = token_utils.listified_tokenizer(source) @@ -327,7 +338,7 @@ def pyminify(options, files): f.write(result) f.close() new_filesize = os.path.getsize(options.outfile) - percent_saved = round(float(new_filesize)/float(filesize) * 100, 2) + percent_saved = round(float(new_filesize) / float(filesize) * 100, 2) print(( "{_file} ({filesize}) reduced to {new_filesize} bytes " "({percent_saved}% of original size)".format(**locals()))) @@ -337,8 +348,8 @@ def pyminify(options, files): pprint.pprint(result) except Exception as inst: print(inst) - pass + pass + -