Skip to content

packaging: generate scripts from callable (dotted paths) #56603

Closed

Description

@vsajip
BPO 12394
Nosy @mhammond, @freddrake, @vsajip, @tjguk, @tarekziade, @merwok
Files
  • 6382acfb1685.diff
  • c5692393c621.diff
  • 4be1917b2a9e.diff
  • 9a7dba6e6f1a.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/merwok'
    closed_at = <Date 2014-03-13.03:02:17.570>
    created_at = <Date 2011-06-24.09:14:23.730>
    labels = ['type-feature', 'library']
    title = 'packaging: generate scripts from callable (dotted paths)'
    updated_at = <Date 2014-03-13.03:02:17.569>
    user = 'https://github.com/vsajip'

    bugs.python.org fields:

    activity = <Date 2014-03-13.03:02:17.569>
    actor = 'eric.araujo'
    assignee = 'eric.araujo'
    closed = True
    closed_date = <Date 2014-03-13.03:02:17.570>
    closer = 'eric.araujo'
    components = ['Library (Lib)', 'Distutils2']
    creation = <Date 2011-06-24.09:14:23.730>
    creator = 'vinay.sajip'
    dependencies = []
    files = ['22711', '22727', '22922', '24659']
    hgrepos = ['42']
    issue_num = 12394
    keywords = ['patch']
    message_count = 58.0
    messages = ['138896', '138897', '138899', '138900', '138902', '138906', '138909', '138911', '138915', '138918', '138922', '138938', '138943', '138944', '138945', '138983', '139021', '139022', '139115', '139122', '140815', '140841', '140847', '140882', '140883', '140888', '140925', '140951', '140956', '140978', '140979', '140983', '141047', '141048', '141059', '141152', '142282', '142302', '142303', '142322', '142323', '142342', '142349', '142377', '142378', '142400', '146218', '146296', '146298', '146306', '146311', '146378', '146382', '146383', '150463', '150502', '154467', '154468']
    nosy_count = 10.0
    nosy_names = ['mhammond', 'fdrake', 'vinay.sajip', 'ceder', 'tim.golden', 'tarek', 'eric.araujo', 'alexis', 'higery', 'guyrozendorn']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue12394'
    versions = ['3rd party', 'Python 3.3']

    Activity

    1. vsajip commented on Jun 24, 2011

      @vsajip
      MemberAuthor

      At present, packaging support for scripts on Windows is the same as for any other system. This is sub-optimal, for the following reasons:

      1. Windows doesn't support #! lines to find the correct executable for a script. On a system with multiple Python versions and scripts written for particular versions, support for locating the correct needs to be present.
      2. Windows has two types of executables - console applications and GUI applications - and Windows users expect correct usage of either python.exe or pythonw.exe, depending on the individual script being run.

      Setuptools (and therefore Distribute) support these requirements by installing a script "demo", on Windows, as "demo.exe" and "demo-script.py" (or "demo-script.pyw"), where demo.exe is a stock Windows executable (either console or GUI) which invokes the appropriate Python executable on the "demo-script.py[w]" file.

      Packaging should provide a similar mechanism, which can be implemented very simply by changing the build_scripts command appropriately. It should work like this:

      1. When writing a script, the developer simply provides a #!line as normal, but if intended for deployment on Windows, ensures the executable is named as "pythonw" rather than "python". The script should have no extension, as would be for case for a script Linux or OS X.
      2. On Windows, the build-scripts command will build the script as it does now - substituting the correct executable for the #! line - but on Windows, instead of writing the script out as e.g. "demo", it will write it as either "demo-script.py" or "demo-script.pyw" (depending on whether the #! line had "pythonw" in it or not), and will also write a stock executable (either console or GUI, depending) with the corresponding name "demo.exe".
      3. Since install_scripts just copies files from the build directory, there shouldn't need to be any changes here.

      The stock executables can be the same as Distribute uses (setuptools/cli.exe and setuptools/gui.exe), if there is no licensing (or other) issue with having them in Python. If there is such an issue, they can be written from scratch to do the same job (it's just one C file).

      I have this working in the pythonv branch, and if this feature request is accepted then I can work up a patch with test and doc changes. (The build_scripts changes are quite straightforward.)

    2. added
      stdlib
      type-feature
      on Jun 24, 2011
    3. tjguk commented on Jun 24, 2011

      @tjguk
      Member
    4. vsajip commented on Jun 24, 2011

      @vsajip
      MemberAuthor

      @tim: It had gone under my radar, thanks for the link!

      I don't know if/when it will be accepted (i.e. whether before 3.3), so my suggestion could be considered as a fallback alternative which works now. If the PEP-397 launcher is available, then of course we should use that.

      Of course PEP-397 does not support 'pythonw' in shebang lines, but the "pythonw" I am suggesting here will not appear in the final output script anyway. It's more of a hint to build_scripts indicating the launcher to use. It seems this choice is still required, since from a quick reading of PEP-397, I couldn't see how the user could just type "demo" for an eponymous script in a virtualenv and get the correct launcher (console or GUI).

    5. tjguk commented on Jun 24, 2011

      @tjguk
      Member

      Adding Mark H as the author of PEP-397

    6. merwok commented on Jun 24, 2011

      @merwok
      Member

      My GSoC student will work on integrating the scripts generation from setuptools into packaging.

    7. vsajip commented on Jun 24, 2011

      @vsajip
      MemberAuthor

      Éric, what will be the scope of that integration? Please bear in mind, I have a working solution, so there's no need to cover this part again unless you think there's a problem with my implementation.

      The changes were straightforward, see

      https://bitbucket.org/vinay.sajip/pythonv/changeset/d2453f281baf

    8. merwok commented on Jun 24, 2011

      @merwok
      Member

      In the setup.cfg files, scripts will now be a mapping of names to callables, like the setuptools scripts and gui_scripts entry points:

      scripts =
          sphinx-build = sphinx.build.run

      On UNIX, a Python script named sphinx-build will be created, on Windows, a binary sphinx-build.exe will be created.

    9. vsajip commented on Jun 24, 2011

      @vsajip
      MemberAuthor

      Does that mean that you can't just put an arbitrary Python script in your application? You have to structure it as a callable?

      Of course, I see the applicability of it for the entry_points functionality of setuptools.

    10. merwok commented on Jun 24, 2011

      @merwok
      Member

      Does that mean that you can't just put an arbitrary Python script in
      your application?

      Arbitrary script files are supported via the resources feature (when the bug you reported is fixed :) The generation of scripts from callables is a widely-used setuptools feature that will solve a number of problems (Python not on PATH on Windows, unnecessary .py extension on UNIX, such things).

    11. vsajip commented on Jun 24, 2011

      @vsajip
      MemberAuthor

      Then for Unix at least, how will the installer know which resources need the execute permission turned on? Just by the destination?

    12. merwok commented on Jun 24, 2011

      @merwok
      Member

      The copy function used will preserve rights. IOW, the +x will be needed in the source.

    13. changed the title [-]Packaging should provide better support for executable scripts on Windows[/-] [+]packaging: generate scripts from callable (dotted paths)[/+] on Jun 24, 2011
    14. 36 remaining items

    15. vsajip commented on Aug 18, 2011

      @vsajip
      MemberAuthor

      Just to clarify: I'm -1 on heuristics too; it's better to have some way of explicitly declaring the intention. I've no problem with e.g. the [scripts] section being used just for generated scripts, as long as there is a clear way of designating arbitrary .py/.pyw files as scripts in the [resources] section, which would have the shebang line transmutation applied during installation.

      Re. the unwanted attachments - sorry, I don't normally use HTML email - I need to change the settings I was using.

    16. merwok commented on Oct 23, 2011

      @merwok
      Member

      FTR, distribute recently committed two fixes for the exe wrappers: https://bitbucket.org/tarek/distribute/issue/238 and https://bitbucket.org/tarek/distribute/issue/207

    17. vsajip commented on Oct 24, 2011

      @vsajip
      MemberAuthor

      FYI: In pythonv, the build_scripts functionality provides identical support for dotted callables to what Éric proposed, while preserving existing functionality for ordinary script files. Thus:

      scripts = demo1
        demo2 = amodule.main
        demo3 = apackage.asubpackage.main gui

      copies demo1 (adjusting its shebang) and creates demo2 and demo3 from the dotted callables (using the appropriate shebang).

      While working on this, I came up against a problem with build_scripts in virtual environments: if I install a project into virtual env 1, then scripts are built with shebangs pointing to that env. If I then install the same project into virtual env 2, then the scripts are not re-created, so they would be installed into virtual env 2 with shebangs referencing virtual env 1. One solution is to set the force flag for virtual environments, but then if you later install into the system Python, then files with wrong shebangs could be installed there. I think the force flag should default to True for build_scripts; the extra script build time would be negligible except in pathological cases. Do you agree?

    18. vsajip commented on Oct 24, 2011

      @vsajip
      MemberAuthor

      Re. the launcher changes, those improvements by Guy Rozendorn are welcome. I noticed some differences from the approach taken by Mark Hammond and Curt Hagenlocher in the PEP-397 implementation, which I ported to C:

      The Ctrl-C is ignored by the PEP-397 launcher. AFAICT it's passed to the child process anyway by Windows. The ignoring prevents the default behaviour (premature termination of the launcher).

      The PEP-397 launcher also duplicates the standard handles before creating the child process.

      The PEP-397 launcher also associates the launcher and the child together using the Job API.

    19. merwok commented on Oct 24, 2011

      @merwok
      Member

      FYI: In pythonv, the build_scripts functionality provides identical support for dotted
      callables to what Éric proposed, while preserving existing functionality for ordinary script
      files.

      My current preference is to use only new-style generated scripts for the scripts field in the files section, and to let people use the data-files/resources system for old-style file scripts.

      We’ll have to think about the shebang munging and decide if we keep it. I think recommending that people use “/usr/bin/env python” (or python3) and not doing anything to the shebang may be the best thing.

      While working on this, I came up against a problem with build_scripts in virtual environments:
      This is also a bug in current distutils and distutils2 scripts. Scripts should always be regenerated, as they depend on the running Python, not on other files.

    20. vsajip commented on Oct 24, 2011

      @vsajip
      MemberAuthor

      We’ll have to think about the shebang munging and decide if we keep it.  I think 

      recommending that people use “/usr/bin/env python” (or python3) and not doing
      anything to the shebang may be the best thing.

      What about Windows support? Of course there is PEP-397 which brings shebang-line functionality to Windows, but that PEP has not yet been finalised. Without the existing shebang functionality, IMO there will be problems for Windows users with the “/usr/bin/env python” approach you suggest.

    21. merwok commented on Oct 25, 2011

      @merwok
      Member

      What about Windows support?
      Just like with distutils: the file extension is used, not the shebang.

    22. vsajip commented on Oct 25, 2011

      @vsajip
      MemberAuthor

      > What about Windows support?
      Just like with distutils: the file extension is used, not the shebang.

      Please spell out for me how you see this working: I don't see it. Note that scripts have to use the correct Python even if they are invoked using an explicit path pointing into a virtual environment.

    23. vsajip commented on Oct 25, 2011

      @vsajip
      MemberAuthor

      To expand on what I said about not seeing how things will work under Windows: are we going to place .exe launchers adjacent to the script, like setuptools does? If the script just has a shebang of "#!/usr/bin/env python", how is the launcher supposed to determine the exact Python to use from that, in a venv scenario where multiple Pythons will be installed? Scripts in virtual envs are supposed to run if invoked, even if the env is not on the PATH.

    24. merwok commented on Jan 2, 2012

      @merwok
      Member

      I’ll get back to this issue later, but now I just wanted to add a link about building the binary exe files: http://mail.python.org/pipermail/python-dev/2006-April/063846.html (it comes from the time where setuptools was supposed to be added to the stdlib)

    25. vsajip commented on Jan 3, 2012

      @vsajip
      MemberAuthor

      @Éric: you may also be interested in a standalone launcher which I wrote for the pythonv branch:

      https://bitbucket.org/vinay.sajip/simple_launcher/

      This is built using Visual Studio and is not based on setuptools code, but uses the same Windows API for child process creation and synchronisation as the PEP-397 launcher (rather than execv/spawnv as the setuptools launcher does). It also links with the runtime statically rather than linking with msvcrt.dll.

    26. merwok commented on Feb 27, 2012

      @merwok
      Member

      I merged default yesterday and produced this patch. I’ll use the review site to make comments.

    27. merwok commented on Feb 27, 2012

      @merwok
      Member

      Or I won’t, as even a dumb no-git-style diff does not create a review link, maybe because of the binary file change. *Sigh* Trying again.

    28. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions