Skip to content

Commit

Permalink
Use __main__
Browse files Browse the repository at this point in the history
  • Loading branch information
kavigupta committed Jan 16, 2020
1 parent db7022f commit 93186b9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
42 changes: 0 additions & 42 deletions bombast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,45 +139,3 @@ def configure(path):
else:
print('Warning: option "{}" is unused.'.format(option),
file=sys.stderr)

def main():
parser = argparse.ArgumentParser(description='Obfuscate Python source code.')
parser.add_argument('infile', type=argparse.FileType('rb'),
default=sys.stdin,
help='input')
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
default='obfuscated.py',
help='output [default: obfuscated.py]')
parser.add_argument('--seed', type=int, default=0,
help='random seed [default: 0]')
parser.add_argument('--iters', type=int, default=1,
help='number of iterations [default: 1]')
parser.add_argument('--config', type=str,
help='configuration file [default: bombast.config]')
parser.add_argument('--show-translations', action='store_true',
help='print translations to stdout')
args = parser.parse_args()
configure(args.config)

random.seed(args.seed)
root = ast.parse(args.infile.read())

# Choose renamings
preprocess = Preprocess()
preprocess.visit(root)

bombast = Bombast(preprocess)
for _ in range(args.iters):
root = bombast.visit(root)

# Postprocessing
root.body.sort(key=lambda x: not isinstance(x, ast.Import)) # move imports
ast.fix_missing_locations(root) # fix AST

print(astunparse.unparse(root), file=args.outfile)
if args.show_translations:
for original, obfuscated in preprocess.mapping.items():
print(original, '=', obfuscated)

if __name__ == '__main__':
main()
43 changes: 43 additions & 0 deletions bombast/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from bombast import *

def main():
parser = argparse.ArgumentParser(description='Obfuscate Python source code.')
parser.add_argument('infile', type=argparse.FileType('rb'),
default=sys.stdin,
help='input')
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
default='obfuscated.py',
help='output [default: obfuscated.py]')
parser.add_argument('--seed', type=int, default=0,
help='random seed [default: 0]')
parser.add_argument('--iters', type=int, default=1,
help='number of iterations [default: 1]')
parser.add_argument('--config', type=str,
help='configuration file [default: bombast.config]')
parser.add_argument('--show-translations', action='store_true',
help='print translations to stdout')
args = parser.parse_args()
configure(args.config)

random.seed(args.seed)
root = ast.parse(args.infile.read())

# Choose renamings
preprocess = Preprocess()
preprocess.visit(root)

bombast = Bombast(preprocess)
for _ in range(args.iters):
root = bombast.visit(root)

# Postprocessing
root.body.sort(key=lambda x: not isinstance(x, ast.Import)) # move imports
ast.fix_missing_locations(root) # fix AST

print(astunparse.unparse(root), file=args.outfile)
if args.show_translations:
for original, obfuscated in preprocess.mapping.items():
print(original, '=', obfuscated)


main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
install_requires=['astunparse'],
entry_points={
'console_scripts': [
'bombast=bombast.__init__:main',
'bombast=bombast.__main__:main',
],
}
)

0 comments on commit 93186b9

Please sign in to comment.