Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dflook committed Apr 2, 2023
1 parent 955dc50 commit 596a01e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
47 changes: 34 additions & 13 deletions src/python_minifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ def __init__(self, exception, source, minified):
def __str__(self):
return 'Minification was unstable! Please create an issue at https://github.com/dflook/python-minifier/issues'

def validate_python_version_arg(python_version):
if not isinstance(python_version, tuple) or (not isinstance(python_version[0], int) or not isinstance(python_version[1], int)):
raise ValueError('minimum_python_version should be a tuple of major and minor version numbers, e.g. (3, 6)')

def version_constraints(module, minimum_python_version):
"""
Return the minimum and maximum python versions that the module should target.
The minimum version will be at least what is required by the module's source code.
The maximum version will always be the current python version, as we need to be able to parse it to verify the minification.
:param module: The module to check
:param minimum_python_version: The minimum python version that the module should target, or None
:return: A tuple of (minimum, maximum) python versions, where is version is a tuple of (major, minor)
"""

if minimum_python_version is not None:
minimum = max((minimum_python_version[0], minimum_python_version[1]), python_source_compat(module))
else:
minimum = python_source_compat(module)

maximum = sys.version_info[:2]

return minimum, maximum

def minify(
source,
Expand Down Expand Up @@ -109,13 +133,8 @@ def minify(
# This will raise if the source file can't be parsed
module = ast.parse(source, filename)

if minimum_python_version:
if not isinstance(minimum_python_version[0], int) or not isinstance(minimum_python_version[1], int):
raise ValueError('minimum_python_version should be a tuple of major and minor version numbers, e.g. (3, 6)')
minimum_python_version = max((minimum_python_version[0], minimum_python_version[1]), python_source_compat(module))
else:
minimum_python_version = python_source_compat(module)
sys.stderr.write('Minifying for python >=' + repr(minimum_python_version))
minimum_python_version, maximum_python_version = version_constraints(module, minimum_python_version)
sys.stderr.write('Targeting python >=' + repr(minimum_python_version) + ', <=' + repr(maximum_python_version))

add_namespace(module)

Expand Down Expand Up @@ -202,6 +221,7 @@ def unparse(module, minimum_python_version=None):
"""

assert isinstance(module, ast.Module)

if minimum_python_version is None:
minimum_python_version = 2, 7

Expand All @@ -213,15 +233,15 @@ def unparse(module, minimum_python_version=None):
except SyntaxError as syntax_error:
raise UnstableMinification(syntax_error, '', printer.code)

try:
compare_ast(module, minified_module)
except CompareError as compare_error:
raise UnstableMinification(compare_error, '', printer.code)
#try:
# compare_ast(module, minified_module)
#except CompareError as compare_error:
# raise UnstableMinification(compare_error, '', printer.code)

return printer.code


def awslambda(source, filename=None, entrypoint=None):
def awslambda(source, filename=None, entrypoint=None, python_version=None):
"""
Minify a python module for use as an AWS Lambda function
Expand All @@ -245,5 +265,6 @@ def awslambda(source, filename=None, entrypoint=None):
filename,
remove_literal_statements=True,
rename_globals=rename_globals,
preserve_globals=[entrypoint]
preserve_globals=[entrypoint],
minimum_python_version=python_version,
)
3 changes: 2 additions & 1 deletion src/python_minifier/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ def unparse(
def awslambda(
source: AnyStr,
filename: Optional[Text] = ...,
entrypoint: Optional[Text] = ...
entrypoint: Optional[Text] = ...,
python_version: Optional[Tuple[int, int]] = ...
) -> Text: ...
4 changes: 2 additions & 2 deletions src/python_minifier/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def parse_args():
dest='remove_debug',
)
minification_options.add_argument(
'--minimum-python',
'--minimum-python-version',
type=str,
action='store',
default='2.7',
Expand All @@ -212,7 +212,7 @@ def parse_args():
sys.stderr.write('error: path ' + args.path[0] + ' is a directory, --in-place required\n')
sys.exit(1)
if args.minimum_python_version and not re.match(r'^\d+\.\d+$', args.minimum_python_version):
sys.stderr.write('error: --minimum-python should specify a major and minor version, e.g. --minimum-python 3.8\n')
sys.stderr.write('error: --minimum-python-version should specify a major and minor version, e.g. --minimum-python 3.8\n')
sys.exit(1)

return args
Expand Down
3 changes: 3 additions & 0 deletions src/python_minifier/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def visit_comprehension(self, node):
self.set_version(3, 6)
self.generic_visit(node)

def visit_TryStar(self, node):
self.set_version(3, 11)
self.generic_visit(node)

def python_source_compat(module):
return PythonSourceCompatability()(module)

0 comments on commit 596a01e

Please sign in to comment.