mirror of
https://github.com/f-droid/fdroidserver.git
synced 2025-09-16 16:02:33 +03:00
use simple ast+operator based calculator for evaluating Vercode Operation
This commit is contained in:
parent
79a022400f
commit
24b20d7668
2 changed files with 25 additions and 1 deletions
|
@ -434,7 +434,7 @@ def checkupdates_app(app):
|
||||||
.format(field=app.VercodeOperation))
|
.format(field=app.VercodeOperation))
|
||||||
oldvercode = str(int(vercode))
|
oldvercode = str(int(vercode))
|
||||||
op = app.VercodeOperation.replace("%c", oldvercode)
|
op = app.VercodeOperation.replace("%c", oldvercode)
|
||||||
vercode = str(eval(op))
|
vercode = str(common.calculate_math_string(op))
|
||||||
logging.debug("Applied vercode operation: %s -> %s" % (oldvercode, vercode))
|
logging.debug("Applied vercode operation: %s -> %s" % (oldvercode, vercode))
|
||||||
|
|
||||||
if version and any(version.startswith(s) for s in [
|
if version and any(version.startswith(s) for s in [
|
||||||
|
|
|
@ -24,6 +24,7 @@ import io
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
import ast
|
||||||
import shutil
|
import shutil
|
||||||
import glob
|
import glob
|
||||||
import stat
|
import stat
|
||||||
|
@ -3205,3 +3206,26 @@ def get_git_describe_link():
|
||||||
else:
|
else:
|
||||||
logging.error(_("'{path}' failed to execute!").format(path='git describe'))
|
logging.error(_("'{path}' failed to execute!").format(path='git describe'))
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_math_string(expr):
|
||||||
|
ops = {ast.Add: operator.add, ast.Sub: operator.sub,
|
||||||
|
ast.Mult: operator.mul}
|
||||||
|
|
||||||
|
def execute_ast(node):
|
||||||
|
if isinstance(node, ast.Num): # <number>
|
||||||
|
return node.n
|
||||||
|
elif isinstance(node, ast.BinOp): # <left> <operator> <right>
|
||||||
|
return ops[type(node.op)](execute_ast(node.left),
|
||||||
|
execute_ast(node.right))
|
||||||
|
elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
|
||||||
|
return ops[type(node.op)](eval(node.operand))
|
||||||
|
else:
|
||||||
|
raise SyntaxError(node)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return execute_ast(ast.parse(expr, mode='eval').body)
|
||||||
|
except SyntaxError as e:
|
||||||
|
raise SyntaxError("could not parse expression '{expr}', "
|
||||||
|
"only basic math operations are allowed (+, -, *)"
|
||||||
|
.format(expr=expr))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue