Skip to content

Make --add-data and --add-binary path separators OS independent - #6724

Merged
bwoodsend merged 1 commit into
pyinstaller:developfrom
bwoodsend:alternate-pathsep
Jul 18, 2023
Merged

bwoodsend merged 1 commit into
pyinstaller:developfrom
bwoodsend:alternate-pathsep

Conversation

@bwoodsend

Copy link
Copy Markdown
Member

I can't make up my mind which separator to use. I've done a commit for both. I don't like the look of the syntax for =>. --add-data="foo=>bar" looks like something you'd find in a perl script. And in either case, I'm not sure if allowing \ escaping of colons is a good idea. I think we should be providing a complete syntax but it makes something like --add-data=C:\some\folder\:. invalid because the colon is treated literally. Any thoughts?

@bwoodsend

Copy link
Copy Markdown
Member Author

@rpodgorny, @johnthagen, @danyeaw, @Iniquitatis, @RaSan147 Any votes for --add-data "/path/to/foo=>." or using both ; and : as path separators on all platforms? And for having or not having backslash as an escape character?

@johnthagen

Copy link
Copy Markdown
Contributor

Linking to #6320

@bwoodsend bwoodsend linked an issue Apr 5, 2022 that may be closed by this pull request
@johnthagen

Copy link
Copy Markdown
Contributor

I don't have a strong opinion as long as it can work cross platform without modification.

The purest in me thinks there should be a single, universal character that is used.

@RaSan147

RaSan147 commented Apr 6, 2022

Copy link
Copy Markdown

Not voting for colon, since may cause some trouble since C:/ as a permanent path or relative path.
Is ? Legal on Linux?
But imo "//" seems good 😅 coz they don't bother in any OS and doesn't look that bad

@rpodgorny

rpodgorny commented Apr 6, 2022 •

Copy link
Copy Markdown

@rpodgorny, @johnthagen, @danyeaw, @Iniquitatis, @RaSan147 Any votes for --add-data "/path/to/foo=>." or using both ; and : as path separators on all platforms? And for having or not having backslash as an escape character?

i agree with @johnthagen -> as long as it works on all platforms without modification, i don't really care.

(btw, as a unix-y guy, i'd prefer : but @RaSan147 is correct about the c: case)

...oh, and also, please try to avoid playing with the backslash at all cost - it's a real pain in the ass to work with, especially in nested shells etc.

@RaSan147

RaSan147 commented Apr 6, 2022

Copy link
Copy Markdown

Also checked on SO, => is valid for filenames in linux. So won't vote for that either.

@rpodgorny

rpodgorny commented Apr 6, 2022 •

Copy link
Copy Markdown

well, there's always another option, the "sed" way -> specify the separator character explicitly...

--add-data ":/some/path:/other/path" (as in sed "s/replace_from/replace_to/" or sed "s|replace_from|replace_to|")

another option would be to have --add-data-comma, --add-data-semicolon, --add-data-colon etc.

@bwoodsend

Copy link
Copy Markdown
Member Author

Not voting for colon, since may cause some trouble since C:/ as a permanent path or relative path.

It's not quite ambiguous. As the drive separator the colon must be followed by either a / or a \ so a

# Split by colons not immediately followed by a slash.
source, dest = re.split(r":(?![/\\])", input`)

does do the job. It makes the invalid but understandable case where someone does --add-data source:/dest a bit more obscure but other than that, colon is OK I believe.


Also checked on SO, => is valid for filenames in linux. So won't vote for that either.

The problem is though that they're all valid file names. There aren't a lot of type-able characters that Linux won't let you use.

> touch '\\' '=>' ':' ';' '?'
> ls
 :  ';'  '=>'  '?' '\'

That's why I thought \ escaping would be needed but I suppose the sed way might be nicer.

@RaSan147

RaSan147 commented Apr 8, 2022

Copy link
Copy Markdown

Just use // since NUL is not available for file names
So, // will also mean that the 2nd part of the directory is from the root / of the app (like /grand/parent/child//adopted/parent/house/) i hope you understand

Very 1st slash means permanent path on linux, without it, it will be counted as relative path. Windows got c:/path...

@rpodgorny

Copy link
Copy Markdown

Just use // since NUL is not available for file names So, // will also mean that the 2nd part of the directory is from the root / of the app (like /grand/parent/child//adopted/parent/house/) i hope you understand

Very 1st slash means permanent path on linux, without it, it will be counted as relative path. Windows got c:/path...

please NO!

multiple slashes are valid path in unix systems -> /home/radek = /home///////radek

also, the path may be calculated programatically and several consecutive slashes are often a valid result of such calculations...

@RaSan147

RaSan147 commented Apr 8, 2022

Copy link
Copy Markdown

Just use // since NUL is not available for file names So, // will also mean that the 2nd part of the directory is from the root / of the app (like /grand/parent/child//adopted/parent/house/) i hope you understand
Very 1st slash means permanent path on linux, without it, it will be counted as relative path. Windows got c:/path...

please NO!

multiple slashes are valid path in unix systems -> /home/radek = /home///////radek

also, the path may be calculated programatically and several consecutive slashes are often a valid result of such calculations...

That's a news to me, sorry, ignore my opinion 😥

@bwoodsend

Copy link
Copy Markdown
Member Author

Does the top half of this not make everyone comfortable with using :? It certainly does for me.

Just use // since NUL is not available for file names

Just to give an example of what @rpodgorny is saying - this happens on macOS but not Linux:

> mkdir -p foo/bar
> find ./
./
.//foo
.//foo/bar

@danyeaw

danyeaw commented Apr 9, 2022

Copy link
Copy Markdown
Contributor

I think supporting both ; and : would be ideal. 👍

@MSLeiter

Copy link
Copy Markdown

If this isn't settled yet - why not make it Python syntax, for a Python tool?

Dict:

--add-data "{'c:\\my\\folder\\and\\file.ext': 'target\\folder'}" # Windows style, enclosed in quotes, need escapted backslashes?
--add-data "{'~/repos/my/folder/and/file.ext': 'target/folder'}" # unix style
--add-data {'source.file':'target_folder'} # if there are no spaces, don't need enclosing quotes

Or List:

--add-data ['source.file','target_folder']
--add-data "['source.file','target_folder']"

Might be disadvantages to this from the quotes (need an extra layer for the command line parsing) and slashes - though if it can always accept fore or back slashes, regardless of platform, and with the ability to use both single and double quotes, it should be good.

@Legorooj

Copy link
Copy Markdown
Member

May I ask what the objections are to my original suggestion of splitting it like --add-data "src" "dest"? Objections excluding compatability that is. If my suggestion does get implemented, it would only be a small change among many other breaking changes.

@bwoodsend

Copy link
Copy Markdown
Member Author

May I ask what the objections are to my original suggestion of splitting it like --add-data "src" "dest"? Objections excluding compatability that is.

Just the compatibility for me - in particular that using the old syntax with the new version or vice versa will i almost no cases lead to an error message that correctly identifies where the user went wrong.

If my suggestion does get implemented, it would only be a small change among many other breaking changes.

I'm not sure that it would anymore. #6660 takes away the need to split pyinstaller code.py and pyinstaller code.spec so I use of subcommands in #6460 isn't really needed anymore. All that leaves is the axing of obscure/obsolete options like --win-no-prefer-redirects.

@MSLeiter

Copy link
Copy Markdown

May I ask what the objections are to my original suggestion of splitting it like --add-data "src" "dest"? Objections excluding compatability that is. If my suggestion does get implemented, it would only be a small change among many other breaking changes.

If the old style is kept for compatibility, then the parser would just need to see if there is 1 argument (old style) or 2 (new style) to the flag - if the file argument is required and is always a single argument, then that should be possible.

@bwoodsend

Copy link
Copy Markdown
Member Author

If this isn't settled yet - why not make it Python syntax, for a Python tool?

Dict:

--add-data "{'c:\\my\\folder\\and\\file.ext': 'target\\folder'}" # Windows style, enclosed in quotes, need escapted backslashes?
--add-data "{'~/repos/my/folder/and/file.ext': 'target/folder'}" # unix style
--add-data {'source.file':'target_folder'} # if there are no spaces, don't need enclosing quotes

Not quite. The whole expression will always have to be enclosed in quotes on unix (or even PowerShell) to stop the string's quotes from being evaluated causing {"foo":"bar"} to become {foo:bar}. You'd then also need to be careful to use double quotes on the outside and single quotes on the inside because Windows's cmd only respects " as a quote and treats ' literally. "{'c:\\my\\folder\\and\\file.ext': 'target\\folder'}" would need to be "{r'c:\\my\\folder\\and\\file.ext': r'target\\folder'}" if using bash for Windows because both bash and Python will de-escape those backslashes otherwise. All that really leaves you with is "{'C:/use/forward/slashes/even/on/windows': 'target/folder'}".

@rokm

rokm commented Apr 13, 2022

Copy link
Copy Markdown
Member

At this point, I'm starting to think that breaking the compatibility and having --add-data and --add-binary accept two arguments (nargs=2) might be the sanest thing to do. Painful, but sanest in the long run. Because then the arguments can contain any valid character, under the quoting and escaping rules of the shell. The error messages when old syntax is used will be confusing (invalid or missing subsequent arguments, script file not provided, etc.), but at least the overall semantics will be clear.

But if we were to retain compatibility for old one-argument syntax, we're going to start running into ambiguities:

pyinstaller --add-data "/path/to/file:dst/file" program.py
pyinstaller --add-data "/path/to/file:dst/file" script1.py script2.py

and to properly interpret those, we'd need to first interpret the argument as single one, and see if it splits into two under the old rules. Which gets complicated even further, because under the two-argument form, the path separator character would actually be a valid one...

@Legorooj

Copy link
Copy Markdown
Member

I should also note that if we did go with splitting it into 2 arguments then that would get released with version 5, which is a major version change, where there will definitely be other breaking changes.

@bwoodsend

Copy link
Copy Markdown
Member Author

I should also note that if we did go with splitting it into 2 arguments then that would get released with version 5, which is a major version change, where there will definitely be other breaking changes.

Again, not many though.

> tail -n +1 news/*breaking.rst 
==> news/6024.breaking.rst <==
The ``matplotlib.backends`` hook no longer collects all available
``matplotlib`` backends, but rather tries to auto-detect the used
backend(s) by default. The old behavior can be re-enabled via the
:ref:`hook configuration option <matplotlib hook options>`.

==> news/6089.breaking.rst <==
(macOS) App bundles built in ``onefile`` mode do not perform
:ref:`argv emulation <macos event forwarding and argv emulation>` by
default anymore. The functionality of converting initial open document/URL
events into ``sys.argv`` entries must now be explicitly opted-in,
via ``argv_emulation=True`` argument to ``EXE()`` in the .spec file
or via :option:`--argv-emulation` command-line flag.

==> news/6169.breaking.rst <==
The :func:`PyInstaller.utils.hooks.get_module_attribute` function now
returns the actual attribute value instead of its string representation.
The external users (e.g., 3rd party hooks) of this function must adjust
their handling of the return value accordingly.

==> news/6223.breaking.rst <==
(Windows) By default, manifest is now embedded into the executable in
``onedir`` mode. The old behavior of generating the external manifest
file can be re-enabled using the :option:`--no-embed-manifest`
command-line switch, or via the ``embed_manifest=False`` argument to
``EXE()`` in the .spec file.

==> news/6660.breaking.rst <==
Issue an error report if a `.spec` file will not be generated, but
command-line options specific to that functionality are given.

@Legorooj

Copy link
Copy Markdown
Member

Again, not many though.

My point is that it's a major release, breaking changes are ok. You can't really complain about a breaking change in a major release.

@RussNelson

RussNelson commented Aug 23, 2022 •

Copy link
Copy Markdown

https://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems is applicable here. You want to be able to express ANY sequence of characters BUT you have to split them into sub-sequences using only characters in the set of characters. The ONLY solution is to create new characters which are outside the set of characters but which can be expressed by the set of characters.
This is why you need to type <pause>+++<pause> to exit a modem stream and be able to ATH.
This is why you have to type <pause> to express a delay in a string of characters.
This is why you have to type &lt;name&gt; to name a meta-character in a string of characters.
This is why you have to type a backslash in front of many Unix contexts to include them in the string, including backslash itself.
This is why you have to type <enter>~. to talk to ssh rather than through ssh.
This is why you have to type <escape><escape> in minicom to send an escape.
This is why UTF-8 has characters which means that subsequent characters should be included in the UTF-8 character.
This is why you have to type &character reference; or &#UTF-8 code AND &amp; to get an ampersand in HTML.
And on and on and on.

A lot of people here are struggling with inventing a new system for delimiting path components. Rather than invent a new one, why not stick with an already-invented one? Use \ to quote characters, including : and \ itself. When you're done, you've got a Windows path if you interpret the backslashes, and a Unix path if you turn them into forward slashes. Is this ugly? Sure, but you already dug that ditch when you wanted to have a meta-character.

May I suggest something less ugly? Specify paths as a list, possibly using "/foo/bar/piffle.dat".split("/") for easier reading. Then, at the last moment, you os.pathsep.join(pathname).

@bwoodsend
bwoodsend force-pushed the alternate-pathsep branch from 6aaeaea to 76d0d19 Compare July 16, 2023 14:47
@bwoodsend

Copy link
Copy Markdown
Member Author

Just to summarise what we eventually decided:

  • On all platforms, --add-data src:dest works (with a little bit of filtering so that --add data C:\some\path:dest does the right thing). This matches the behaviour of Docker's --volume flag.
  • On Windows, --add-data src;dest remains supported to avoid unnecessarily breaking existing code but is discouraged and removed from documentation. We chose not to make it a valid separator on UNIX to avoid the (admittedly very unlikely) scenario where someone is using ;s in a filename.
  • Any previously mentioned ideas about \: escaping are not implemented.
  • We're not going to support => as a separator.

Because : is not allowed in Windows filenames, making it a path separator can't break any existing code using :s in filenames.

@bwoodsend
bwoodsend marked this pull request as ready for review July 16, 2023 17:17
Comment thread PyInstaller/building/makespec.py Resolved
@bwoodsend
bwoodsend force-pushed the alternate-pathsep branch from 76d0d19 to a43108f Compare July 17, 2023 20:57
Comment thread PyInstaller/building/makespec.py Resolved
…ip ci]

`--add-data=source:dest` is now allowed (and preferred) on Windows as
well as UNIX. On Windows, the prior behaviour of using `;` is still
allowed to avoid breaking backwards compatibility. Special handling is
given to ignore colons at the start of the string, preceded by a single
letter and followed by a slash so as to avoid splitting
`--add-data=C:\foo\bar:dest` in the wrong place. Because `:` is an
illegal character in Windows filenames, this avoids being a breaking
change since no Windows users could have been `:` literally.
@bwoodsend
bwoodsend force-pushed the alternate-pathsep branch from a43108f to 622429a Compare July 18, 2023 17:32
@bwoodsend
bwoodsend merged commit 79f62ef into pyinstaller:develop Jul 18, 2023
@bwoodsend
bwoodsend deleted the alternate-pathsep branch July 18, 2023 17:33
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

please make path separator os-independent (don't use os.pathsep)

9 participants