Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use __main__ #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
],
}
)