'''Common p4v addins functions''' # Copyright (c) 2006 Qualcomm # Miki Tebeka import wx from os.path import isfile, dirname, join import sys from p4 import P4, P4Error from optparse import OptionParser from os import environ from time import strftime def fix_path(path): '''Fix path in Perfroce path - Path to fix ''' if path.endswith("..."): return path if not path.endswith("/"): path += "/" return path + "..." def quick_label(p4, name, path, description, revision=""): '''Do a label "name" on path@revision with desctiption p4 - P4 object name - Label name path - Label root description - Label description revision - File revision (changelist, label ...) ''' lspec = p4.fetch_label(name) lspec["Description"] = description lspec["Owner"] = p4.user lspec["Options"] = "unlocked" lspec["View"] = [path] p4.save_label(lspec) if revision: path += "@%s" % revision p4.run_labelsync("-l", name, path) lspec["Options"] = "locked" p4.save_label(lspec) def new_change(p4, description=""): if not description: description = "Change list for %s" % p4.user change = p4.fetch_change() if "Files" in change: del change["Files"] change["Description"] = description + "\n" res = p4.save_change(change) return res[0].split()[1] def timestamp(): return strftime("%Y/%m/%d %H:%M:%S") def client_exists(p4, name): for client in p4.run_clients(): if client["client"] == name: return 1 return 0 def new_client(p4, name, root, mapping, description=""): if client_exists(p4, name): raise ValueError("client %s already exists" % name) if not description: description = "Created by %s" % p4.client client = p4.fetch_client() client = { "Description" : description + "\n", "Host" : "", "Update" : timestamp(), "Access" : timestamp(), "Client" : name, "Owner" : p4.user, "LineEnd" : "local", "Root" : root, "Options" : "noallwrite noclobber nocompress unlocked nomodtime normdir", "View" : mapping } p4.save_client(client) def branch_exists(p4, name): for branch in p4.run_branches(): if branch["branch"] == name: return 1 return 0 def new_branch(p4, name, source, dest, description=""): if branch_exists(p4, name): raise ValueError("branch %s already exists" % name) if not description: description = "Created by %s" % p4.user description += "\n" branch = p4.fetch_branch(name) branch["Owner"] = p4.user branch["View"] = ["%s %s" % (source, dest)] branch["Description"] = description p4.save_branch(branch) def make_error(name): def error(msg): '''Show error message msg - Error message to show ''' msg = str(msg) dlg = wx.MessageDialog(None, msg, "%s Error" % name, wx.OK|wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() return error def get(value, envkey): '''Get value from "value" or from environment value - Value to return if not "" envkey - Envrionment key to check if value is "" ''' if value: return value return environ.get(envkey, "") def _branch_view_item(p4, branch, index): '''Item in branch view p4 - P4 object branch - Branch name index - Item index ''' spec = p4.fetch_branch(branch) return spec["View"][0].split()[index] def branch_source(p4, branch): '''Source of branch p4 - P4 object branch - Branch name ''' return _branch_view_item(p4, branch, 0) def branch_target(p4, branch): '''Target of branch p4 - P4 object branch - Branch name ''' return _branch_view_item(p4, branch, 1) def optparser(usage): '''Generate command line parser usage - Usage string ''' parser = OptionParser(usage) parser.add_option("-p", help="Perforce port", dest="port") parser.add_option("-u", help="User name", dest="user") parser.add_option("-c", help="Client name", dest="client") return parser def gen_p4(opts): '''Genearte P4 object from command line options opt - Command line options ''' client = get(opts.client, "P4CLIENT") user = get(opts.user, "P4USER") port = get(opts.port, "P4PORT") p4 = P4() p4.parse_forms() p4.client = client p4.user = user p4.port = port p4.connect() return p4 class P4VAddin(wx.Dialog): def __init__(self, title=""): wx.Dialog.__init__(self, None, -1, title) self.set_icon() def set_icon(self): '''Set application icon''' # Icon appdir = sys.path[0] if isfile(appdir): # py2exe appdir = dirname(appdir) iconfile = join(appdir, "addins.ico") if isfile(iconfile): icon = wx.Icon(iconfile, wx.BITMAP_TYPE_ICO) self.SetIcon(icon) def getval(self, ctrl, name=""): '''Get value from control ctrl - Control to get value from name - Control name (for error) ''' value = ctrl.GetValue().strip() if not value: if not name: name = ctrl.GetName() raise ValueError(name) return value