safe-sync.py #2

  • //
  • guest/
  • miki_tebeka/
  • p4vaddins/
  • main/
  • safe-sync.py
  • View
  • Commits
  • Open Download .zip Download (3 KB)
'''Saves a .keep file for each checked out file when syncing'''

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

import wx
from p4v_common import P4VAddin, P4, make_error, optparser, gen_p4, \
        fix_path, P4Error
from shutil import copy2
from os.path import isfile
from itertools import count

# Path: ________________
# [Sync] [Quit]

error = make_error("Safe Sync")

class SafeSync(P4VAddin):
    def __init__(self, p4, path):
        P4VAddin.__init__(self, "Safe Sync")
        self.p4 = p4

        sizer = wx.BoxSizer(wx.VERTICAL)

        # Path: ____________-
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        hsizer.Add(wx.StaticText(self, -1, "Path:"), 0,
                wx.WEST|wx.ALIGN_CENTER_VERTICAL, 5)
        self._path = wx.TextCtrl(self, -1, size=(500, -1))
        self._path.SetValue(path)
        hsizer.Add(self._path, 0, wx.EXPAND)
        sizer.Add(hsizer, 0, wx.EXPAND)

        # [Synchronize] [Quit]
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        b = wx.Button(self, -1, "Synchronize")
        self.Bind(wx.EVT_BUTTON, self.OnSync, b)
        hsizer.Add(b)
        sync = b
        b = wx.Button(self, wx.ID_CANCEL, "Quit")
        hsizer.Add(b)
        sizer.Add(hsizer, 0, wx.EXPAND)

        self.SetSizer(sizer)
        self.SetAutoLayout(1)
        sizer.Fit(self)
        self.CenterOnScreen()
        sync.SetFocus()
    

    def OnSync(self, evt):
        '''Handle click on "Sync"'''
        try:
            path = self.getval(self._path, "Path")
        except ValueError, e:
            error("%s can't be empty" % e)
            return

        num_saved = 0
        for filespec in self.p4.run_opened():
            fstat = self.p4.run_fstat(filespec["depotFile"])[0]
            local = fstat["clientFile"]
            if not isfile(local):
                continue

            # Find good keep file
            keep = local + ".keep"
            if isfile(keep):
                for i in count(1):
                    keep = local + ".keep%d" % i
                    if not isfile(keep):
                        break
            try:
                copy2(local, keep)
                num_saved += 1
            except IOError, e:
                error(e)
                return

        num_synced = len(self.p4.run_sync())
        dlg = wx.MessageDialog(self, "Saved %d files, synced %d" % (num_saved,
            num_synced), "Saved & Synced", wx.OK|wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

def main(argv = None):
    if argv is None:
        import sys
        argv = sys.argv

    app = wx.PySimpleApp()

    parser = optparser("usage: %prog [options] PATH")
    opts, args = parser.parse_args(argv[1:])
    if len(args) not in (0, 1):
        err_msg = "Wrong number of arguments"
        error(err_msg)
        raise SystemExit(err_msg)

    if args:
        path = args[0]
    else:
        path = ""

    try:
        p4 = gen_p4(opts)
    except P4Error, e:
        error(e)
        raise SystemExit

    try:
        dlg = SafeSync(p4, path)
        dlg.ShowModal()
        dlg.Destroy()
    except Exception, e:
        error(e)
        
if __name__ == "__main__":
    main()
# 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