Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active July 4, 2016 21:20
Show Gist options
  • Save maphew/865c02c9143fd954e5653a8ffb1fb441 to your computer and use it in GitHub Desktop.
Save maphew/865c02c9143fd954e5653a8ffb1fb441 to your computer and use it in GitHub Desktop.
# ./my_cli/cli.py
# wrapper created for console_scripts to run 'tools/multi_main'
import os
import sys
here = os.path.abspath(os.path.dirname(__file__))
def multi_main(argv):
fname = os.path.join(here, '../tools', 'multi_main.py')
print(fname)
with open(fname) as f:
code = compile(f.read(), fname, 'exec')
#exec(code, global_vars, local_vars)
exec(code)
if __name__ == '__main__':
multi_main(sys.argv)
# ./tools/multi_main
# This file from upstream source. We want to avoid editing.
import sys
def prologue(argv):
print("Arguments recieved: {}".format(argv))
def do_thing():
print("Doing something here")
def finish():
print("All done now, cleaning up")
if __name__ == '__main__':
print("Running 'multi_main' script")
prologue(sys.argv)
do_thing()
finish()
#./setup.py
import os
from setuptools import setup
setup(
name="multi_main",
description="Migrating from scripts to console_scripts entry points",
# Old, 'nix-only way
scripts=[os.path.join("tools", name) for name in [
"multi_main",
]],
# new, multi-platform way (when get to working)
entry_points = {
'console_scripts': [
'my_single_main = my_cli.single_main:main',
'my_multi_main = my_cli.cli:multi_main',
],
},
)
# ./my_cli/single_main.py
# This works when 1st 3 functions are in the same file, but what to do when
# they're somewhere else, and meant to be called as a commandline script?
import sys
# --- copied from 'tools/multi_main' but we really want to run in-situ ---
def prologue(argv):
print("Arguments recieved: {}".format(argv))
def do_thing():
print("Doing something here")
def finish():
print("All done now, cleaning up")
# --- end external ---
def main():
prologue(sys.argv)
do_thing()
finish()
if __name__ == '__main__':
print("Running 'single_main' script")
main()
@maphew
Copy link
Author

maphew commented Jul 4, 2016

Results of above:

[py34_x64] D:\b\code\console_script_multi
> pip install -e .
Obtaining file:///D:/b/code/console_script_multi
Installing collected packages: multi-main
  Running setup.py develop for multi-main
Successfully installed multi-main-0.0.0

[py34_x64] D:\b\code\console_script_multi
> my_single_main
Arguments recieved: ['C:\\Python34_x64\\Scripts\\my_single_main-script.py']
Doing something here
All done now, cleaning up

[py34_x64] D:\b\code\console_script_multi
> my_multi_main
Traceback (most recent call last):
  File "C:\Python34_x64\Scripts\my_multi_main-script.py", line 9, in <module>
    load_entry_point('multi-main==0.0.0', 'console_scripts', 'my_multi_main')()
TypeError: multi_main() missing 1 required positional argument: 'argv'

[py34_x64] D:\b\code\console_script_multi
> multi_main
'multi_main' is not recognized as an internal or external command,
operable program or batch file.

[py34_x64] D:\b\code\console_script_multi
> python c:\Python34_x64\Scripts\multi_main
Running 'multi_main' script
Arguments recieved: ['c:\\Python34_x64\\Scripts\\multi_main']
Doing something here
All done now, cleaning up

[py34_x64] D:\b\code\console_script_multi
>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment