Jump to content


Problem with triggers, python, and files with spaces

trigger

  • Please log in to reply
14 replies to this topic

#1 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 13 September 2019 - 10:18 PM

Hi!

For reference, some details:

The server version is P4D/LINUX26X86_64/2018.2/1724420 (2018/11/02) and the version of the P4V is Rev. P4V/NTX64/2019.1/1830398 on Windows 10 Enterprise

I am working on some perforce triggers, and I've hit a problem when performing actions on files that have spaces in our depot.

My p4 trigger description looks like this:

pre_checkout command pre-user-edit "C:\\Python37\\python.exe D:\\PerforceDev\\Server\\Triggers\\p4trigger_precheckout.py %user% %serverport% %client% %clientcwd% %args%"

I try to check-out a file in 'd:\mydepot\test - test.txt'

The arguments my script receive are these:

print("Args: " , str(sys.argv))


Args:  ['D:\\\\PerforceDev\\\\Server\\\\Triggers\\\\p4trigger_precheckout.py', 'username', '127.0.0.1:1666', 'myclient', 'd:\\mycwd', 'd:\\mydepot\\test', '-', 'test.txt']

As you can see, the file name is split in multiple arguments, which prevents me from using it properly. Would there be any way to receive the files with quotes so that I can fetch the arguments properly?

Thank you very much!

#2 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 947 posts

Posted 18 September 2019 - 04:59 PM

I checked "p4 help triggers" because I could swear there was some kind of %quote% argument but I couldn't remember what it was, and sure enough:

					%argsQuoted% -- command argument string, CSV delimited

Give that a try?  :)

#3 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 18 September 2019 - 05:39 PM

Hi Sam

Thank you. I had already tried that, expecting that it would do what it says, but the result is exactly the same.

pre_checkout command pre-user-edit "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_precheckout.py %user% %serverport% %client% %clientcwd% %argsQuoted%"

result:

Args:  ['f:\\\\Perforce\\\\david.punset_MTL-BJ015_49\\\\tools\\\\P4Triggers\\\\p4trigger_precheckout.py', 'david.punset', '127.0.0.1:1666', 'david.punset_MTL-BJ015_ProdData', 'd:\\PerforceDev\\Prod_Data', '//xxx/data/dev/prod_data/engine/ActionMaps/test', '-', 'test.txt']

I'm not missing something, am I?

#4 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 947 posts

Posted 18 September 2019 - 06:30 PM

Are you sure that the command itself is being executed correctly on the client end?  I.e. if you removed the trigger from the equation, are you doing:

D:>p4 edit "//xxx/data/dev/prod_data/engine/ActionMaps/test - test.txt"
//xxx/data/dev/prod_data/engine/ActionMaps/test - test.txt - opened for edit

or are you doing:
D:>p4 edit //xxx/data/dev/prod_data/engine/ActionMaps/test - test.txt
//xxx/data/dev/prod_data/engine/ActionMaps/test - no such file(s)
- - no such file(s)
test.txt - no such file(s)
?

#5 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 18 September 2019 - 06:42 PM

I am doing the checkout from P4V, not from the command line.

#6 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 947 posts

Posted 18 September 2019 - 07:14 PM

Just to check, when you tried %argsQuoted%, you replaced your original trigger entry rather than adding a new one under it with the same name, right?  Multiple trigger entries with the same name will use the command string for the first one.

#7 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 18 September 2019 - 07:23 PM

Yes, I replaced all. This is how it looks now:

pre_delete command pre-user-delete "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_predelete.py %user% %serverport% %client% %clientcwd% %argsQuoted%"
pre_add command pre-user-add "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_preadd.py %user% %serverport% %client% %clientcwd% %argsQuoted%"
pre_checkout command pre-user-edit "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_precheckout.py %user% %serverport% %client% %clientcwd% %argsQuoted%"
post_checkout command pre-user-edit "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_postcheckout.py %user% %serverport% %client% %clientcwd% %argsQuoted%"
post_delete command post-user-delete "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_postdelete.py %user% %serverport% %client% %clientcwd% %argsQuoted%"
post_revert command post-user-revert "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_postrevert.py %user% %serverport% %client% %clientcwd% %argsQuoted%"
pre_submit change-submit //xxx/data/dev/... "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_presubmit.py %user% %serverport% %client% %clientcwd% %change%"
post_submit change-commit //xxx/data/dev/... "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_postsubmit.py %user% %serverport% %client% %clientcwd% %change%"

#8 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 947 posts

Posted 18 September 2019 - 07:32 PM

Maybe the idea of %argsQuoted% is that it does the escaping to allow you to handle multiple arguments if they're quoted -- it doesn't actually mention anything about adding the quotes itself, so it might be a no-op for the single-argument case that you're testing.  What if you add them in using the %quote% trigger var, like:

pre_checkout command pre-user-edit "C:\\Python37\\python.exe f:\\Perforce\\david.punset_MTL-BJ015_49\\tools\\P4Triggers\\p4trigger_precheckout.py %user% %serverport% %client% %clientcwd% %quote%%argsQuoted%%quote%"


Any better?

#9 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 18 September 2019 - 09:45 PM

I tried %quote% before, it's just that all it does is put all the args together in one quoted argument, so all I receive is one argument with everything inside, making it not possible either to get the files :(

#10 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 947 posts

Posted 18 September 2019 - 11:51 PM

View Postdpunset, on 18 September 2019 - 09:45 PM, said:

I tried %quote% before, it's just that all it does is put all the args together in one quoted argument, so all I receive is one argument with everything inside, making it not possible either to get the files :(

Hm -- and %argsQuoted% with multiple arguments doesn't comma-separate them like it claims it does?

When I get home I'll take a sec to play with a test Perforce install and figure this out.  Seems like it should be doable.

#11 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 19 September 2019 - 12:34 AM

Thank you Sam for the time testing on your side.

For me using %args% or %argsQuoted% did not show any difference when I printed the arguments from the python script.

This said, I just don't understand why not quote always filenames with spaces :(

https://www.perforce...strictions.html


#12 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 947 posts

Posted 19 September 2019 - 05:53 AM

Works for me:

C:\Perforce\test>p4 triggers -o | tail -n2
		edit command post-user-edit "echo %quote%%argsQuoted%%quote%"

C:\Perforce\test>p4 edit "foo - with spaces" bar
foo - with spaces - file(s) not on client.
//stream/main/bar#2 - currently opened for edit
foo - with spaces,bar

Note the comma in between the two paths (per the documentation of %argsQuoted%)


C:\Perforce\test>p4 edit "foo, with spaces" bar
foo, with spaces - file(s) not on client.
//stream/main/bar#2 - currently opened for edit
foo%2C with spaces,bar

Commas get escaped with % codes so you can parse the CSV unambiguously.  All exactly like the documentation indicates.

Maybe you need to update to a newer server version?

#13 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 19 September 2019 - 11:25 AM

Hey Sam,

Sorry for the confusion.

Using %argsQuoted% gives exactly the same result as %args%.
Using %quote%%argsQuoted%%quote% gives you one long argument separated with commas, and this one I can work with at least.

I think with this I can get somewhere, but it seems overly complicated. I'll do something to properly split those arguments myself, but everything would be much simpler if filenames with spaces were always quoted when the server invokes the script.

Thank you so much for the help!

David

#14 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 947 posts

Posted 19 September 2019 - 02:42 PM

View Postdpunset, on 19 September 2019 - 11:25 AM, said:

I'll do something to properly split those arguments myself

You're using Python, right?  This ought to do it:

from urllib.parse import unquote

arg_list = [unquote(arg) for arg in args.split(",")]

Note that this will  work if the filenames themselves contain not only spaces, but also quotes, commas, or other special characters.  :)

#15 dpunset

dpunset

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 19 September 2019 - 05:44 PM

That will help too, thank you again Sam!





Also tagged with one or more of these keywords: trigger

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users