Skip to content

Instantly share code, notes, and snippets.

@theodox
Created April 1, 2020 20:01
Show Gist options
  • Save theodox/a7cd918dae5e5429f56d9aa2a8e9b355 to your computer and use it in GitHub Desktop.
Save theodox/a7cd918dae5e5429f56d9aa2a8e9b355 to your computer and use it in GitHub Desktop.
Edit P4 xm
def add_tool(name, exe, commandline, shortcut=''):
'''
Adds a command entry to a tools list container node.
* tool_list_element is ElementTree node representing the tools list
* exe is the path to the executable
* commandline is the arguments passed to the commandline.
* shortcut is a key shortcut
If a tool with the same name already exists, it will be overwritten
Perforce supports special characters in the command lines, which
will be replaced at call time. see
www.perforce.com/manuals/p4v/Content/P4V/advanced_options.custom.html
for the full list. Useful ones include
$c current client
%c current changelist
'''
existing = get_tool_by_name(name)
if existing is not None:
remove_tool(name)
'''
This is an example of the schema
<?xml version="1.0" encoding="UTF-8"?>
<!--perforce-xml-version=1.0-->
<CustomToolDefList varName="customtooldeflist">
<CustomToolDef>
<Definition>
<Name>example</Name>
<Command>python.exe</Command>
<Arguments>C:/Users/Steve/Desktop/dummy.py --client $c --changelist %c</Arguments>
<Shortcut></Shortcut>
</Definition>
<Console>
<CloseOnExit>false</CloseOnExit>
</Console>
<Refresh>true</Refresh>
</CustomToolDef>
</CustomToolDefList>
'''
definition = ETree.SubElement(_CATALOG, 'CustomToolDef')
tool_def = ETree.SubElement(definition, 'Definition')
name_node = ETree.SubElement(tool_def, 'Name')
name_node.text = name
command = ETree.SubElement(tool_def, 'Command')
command.text = exe
args = ETree.SubElement(tool_def, 'Arguments')
args.text = commandline
shortcut_node = ETree.SubElement(tool_def, 'Shortcut')
shortcut_node.text = shortcut
console = ETree.SubElement(tool_def, 'Console')
# this one force-closes p4v's console window, if present
# if somebody turns that on, they probably want to see the
# debug spew so default it to false
close_on_exit = ETree.SubElement(console, 'CloseOnExit')
close_on_exit.text = 'false'
refresh = ETree.SubElement(tool_def, 'Refresh')
refresh.text = 'true'
# this makes the tool available in a changelist context menu
# note that it's a child of 'definition' and not 'tool_def'
add_to_ctx = ETree.SubElement(definition, 'AddToContext')
add_to_ctx.text = 'true'
@theodox
Copy link
Author

theodox commented Apr 1, 2020

to find the location:

def get_p4v_config_dir():
    '''
    P4 stores config files in  C:/Users/<user>/.p4qt
    https://community.perforce.com/s/article/2911
    '''
    p4_dir = os.path.expanduser("~\\.p4qt")
    if not (os.path.exists(p4_dir)):
        raise IOError(f"P4V user directory {p4_dir} does not exist. Is P4V installed?")
    return p4_dir

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