label2change.py #3

  • //
  • guest/
  • miki_tebeka/
  • p4vaddins/
  • main/
  • label2change.py
  • View
  • Commits
  • Open Download .zip Download (4 KB)
#!/usr/bin/env python
'''Convert label name to change list number'''

# Copyright (c) 2006 Qualcomm
# Miki Tebeka <[email protected]>

import wx
from wx.grid import Grid, GridCellAttr

from p4 import P4, P4Error
from p4v_common import fix_path
from os.path import join, isfile, dirname
from os import environ
import sys


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 error(msg):
    '''Show error message

        msg - Error message to show
    '''
    msg = str(msg)
    dlg = wx.MessageDialog(None, msg, "Label->Change Error", wx.OK|wx.ICON_ERROR)
    dlg.ShowModal()
    dlg.Destroy()


class Label2Change(wx.Dialog):
    '''Main UI'''
    def __init__(self, p4, path):
        wx.Dialog.__init__(self, None, -1, "Label --> Change")
        self.copy = "" # Text to copy to clipboard
        sizer = wx.BoxSizer(wx.VERTICAL)
        list = wx.ListCtrl(self, -1, size=(400, -1),
                style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.BORDER_NONE)
        list.InsertColumn(0, "Label")
        list.InsertColumn(1, "Changelist")

        # Add label -> change information
        try:
            l2c = self.get_l2c(p4, path)
        except P4Error, e:
            error(e)
            self.EndModal(wx.ID_CANCEL)

        for label, change in l2c:
            i = list.InsertStringItem(0, label)
            list.SetStringItem(i, 1, change)

        list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        list.SetColumnWidth(1, wx.LIST_AUTOSIZE_USEHEADER)

        sizer.Add(list, 1, wx.EXPAND)
        sizer.Add(wx.Button(self, wx.ID_CANCEL, "OK"), 0, wx.ALIGN_CENTER)

        # Handle right click on list
        self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.OnRight)
        # Handl popup menu
        self.Bind(wx.EVT_MENU, self.OnPopup)

        self.SetSizer(sizer)
        sizer.Fit(self)
        self.CenterOnScreen()

    def OnRight(self, evt):
        '''Right click on list handler'''

        # Set self.copy to selected text
        list = evt.GetEventObject()
        item = list.GetItem(evt.m_itemIndex, 1)
        self.copy = item.GetText()
        menu = wx.Menu()
        menu.Append(-1, "Copy")
        self.PopupMenu(menu)

    def OnPopup(self, evt):
        if not self.copy:
            return

        if not wx.TheClipboard.Open():
            error("Can't open clipboard")
            return
        data = wx.TextDataObject()
        data.SetText(self.copy)
        wx.TheClipboard.SetData(data)
        wx.TheClipboard.Close()

    def get_l2c(self, p4, path):
        '''Get latest changlist in labels in path

            p4 - P4 object
            path - Path in depot
        '''
        path = fix_path(path)

        l2c = []
        for label in p4.run_labels(path):
            lname = label["label"]
            change = p4.run_changes("-m1", "%s@%s" % (path, lname))[0]
            l2c.append((lname, change["change"]))

        return l2c

    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)

app = wx.PySimpleApp()

# Command line handling
from optparse import OptionParser
parser = OptionParser("usage: %prog [options] PATH")
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")

opts, args = parser.parse_args()
if len(args) != 1:
    error(parser.get_usage())
    raise SystemExit(parser.get_usage())

# Get arguments
path = args[0]
client = get(opts.client, "P4CLIENT")
user = get(opts.user, "P4USER")
port = get(opts.port, "P4PORT")

# P4 client
p4 = P4()
p4.parse_forms()
p4.client = client
p4.user = user
p4.port = port
try:
    p4.connect()
except P4Error, e:
    error(e)
    raise SystemExit(1)

dlg = Label2Change(p4, path)
dlg.ShowModal()
dlg.Destroy()
# Change User Description Committed
#3 5628 Miki Tebeka Initial support for P4V new XML settings file
#2 5382 Miki Tebeka * Start of UNIX - File format and #!
* getvalue returns string (not unicode in some cases)
#1 5280 Miki Tebeka Initial checkin of project