#!/usr/bin/env python3 # # Copyright (C) 2012 Sven Erik Knop, Perforce Software Ltd # # Idea: # Sync files from the default location (or, with -p, -u, -c, -H, -P options) # Show progress using the Tkinter.tkk ProgressBar, P4.OutputHandler and P4.Progress # from __future__ import print_function from argparse import ArgumentParser import P4 import Tkinter as tk import ttk class P4Command: def __init__(self): self.parser = ArgumentParser( description=self.description(), epilog="Copyright (C) 2012 Sven Erik Knop, Perforce Software Ltd" ) self.parser.add_argument('-p', '--port', help="P4PORT") self.parser.add_argument('-c', '--client', help="P4CLIENT") self.parser.add_argument('-u', '--user', help="P4USER") self.parser.add_argument('-H', '--host', help="P4HOST") self.parser.add_argument('-P', '--password', help="P4PASSWD") self.parser.add_argument('-d', '--directory', help="CWD") self.addArguments() self.myOptions = self.parser.parse_args() self.p4 = P4.P4() if self.myOptions.port: self.p4.port = self.myOptions.port if self.myOptions.client: self.p4.client = self.myOptions.client if self.myOptions.user: self.p4.user = self.myOptions.user if self.myOptions.host: self.p4.host = self.myOptions.host if self.myOptions.password: self.p4.password = self.myOptions.password if self.myOptions.directory: self.p4.cwd = self.myOptions.directory def description(self): return "P4Command" def addArguments(self): pass class P4Sync(P4Command, tk.Tk, P4.Progress, P4.OutputHandler): def __init__(self): P4Command.__init__(self) tk.Tk.__init__(self) P4.Progress.__init__(self) P4.OutputHandler.__init__(self) self.progress = ttk.Progressbar(self, orient="horizontal", length=200, mode="determinate") self.progress.pack() self.button = ttk.Button(text="Sync", command=self.start) self.button.pack() self.totalFiles = 0 self.totalSizes = 0 def addArguments(self): self.parser.add_argument('-n', '--preview', dest='syncOptions', action='append_const', const='-n', help="Preview only, no syncing") self.parser.add_argument('--force', dest='syncOptions', action='append_const', const='-f', help="Force the update regardless of previous syncs") self.parser.add_argument('--print', dest='syncOptions', action='append_const', const='-p', help="Update local workspace without updating the database") self.parser.add_argument('--keep', dest='syncOptions', action='append_const', const='-k', help="Update database without updating the local workspace") self.parser.add_argument('filepaths', type=str, nargs='*', help="[file[revRange] ...]") def description(self): return "P4Sync" def outputStat(self, stat): if 'totalFileCount' in stat: self.totalFileCount = int(stat['totalFileCount']) if 'totalFileSize' in stat: self.totalFileSize = int(stat['totalFileSize']) return P4.OutputHandler.HANDLED def init(self, type): self.type = type def setDescription(self, description, unit): print("Description: " , description) self.title(description) print("TotalFileCount: ", self.totalFileCount) print("TotalFileSize: ", self.totalFileSize) self.progress["value"] = 0 self.progress["maximum"] = int(self.totalFileCount) def setTotal(self, total): print("Total: %s" % total) def update(self, position): self.position = position print("Position: %s" % position) self.progress["value"] = int(position) def done(self, fail): self.fail = fail print("Done") def start(self): with self.p4.connect(): args = ['-q'] if self.myOptions.syncOptions: # is None if no options specified args += self.myOptions.syncOptions args += self.myOptions.filepaths result = self.p4.run_sync(args, progress=self, handler=self) if __name__ == '__main__': p4sync = P4Sync() p4sync.mainloop()