[p4python] Client mappings

John Wiseman john.wiseman at rovicorp.com
Wed Sep 30 16:44:51 PDT 2009

On 9/29/09 2:46 PM, "John Wiseman" <john.wiseman at rovicorp.com> wrote:

> Does anyone have code for building a P4.Map object corresponding to a
> particular client?  I just want to make sure I¹m doing it right so that I
> get exactly the same translations that Perforce would give me.

Here's the solution I came up with for mapping between depot paths and
physical pathnames:

# --------------------
# Build a map that lets us translate between depot paths and physical
# pathnames, like //depot/users/wiseman ->
# /home/wiseman/work/spdc/wiseman and vice versa.
# --------------------

g_cached_depot_to_phys_map = None

# I got the inspiration for this from the "P4::Map class" section of
# Just note that he's trying to map from //depot/... to the physical
# pathname of the file *on the depot server*, whereas I'm mapping
# between depot paths and physical pathnames on the client machine.

def ensure_translation_map(p4=None):
    global g_cached_depot_to_phys_map
    if not g_cached_depot_to_phys_map:

        if not p4:
            p4 = P4.P4()

        client_info = p4.run_client('-o')[0]
        client_name = client_info['Client']
        client_root = client_info['Root']
        client_views = client_info['View']
        depot_to_ws_map = P4.Map()

        ws_to_physical_map = P4.Map()
        ws_to_physical_map.insert('//%s/...' % (client_name,),
os.path.join(client_root, '...'))

        g_cached_depot_to_phys_map = P4.Map.join(depot_to_ws_map,
    return g_cached_depot_to_phys_map

def translate_phys_to_depot(path):
    trans_map = ensure_translation_map.reverse()
    return trans_map.translate_phys_to_depot(path)

def translate_depot_to_phys(path):
    trans_map = ensure_translation_map()
    return trans_map.translate(path)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://maillist.perforce.com/pipermail/p4python/attachments/20090930/5ff74e5c/attachment-0001.html>

More information about the p4python mailing list