Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.0, 1.0.1, and 1.0.2

hgcmd: introduce CmdRunner class

Changeset 82af965e84c0

Parent c55aff60648c

by Yuki KODAMA

Changes to one file · Browse files at 82af965e84c0 Showing diff from parent c55aff60648c Diff from another changeset...

 
654
655
656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
@@ -654,3 +654,117 @@
  if self.close_hook(self):   self.hide()   return True + +class CmdRunner(object): + """ + Interactive command runner without GUI. + + In default, it doesn't have any GUI, such as CmdDialog. + If it needs something user interaction (i.e. HTTPS auth) while + running, simple input dialog will be popup. + """ + def __init__(self): + self.hgthread = None + self.clear_buffers() + + ### public functions ### + + def execute(self, cmdline, callback, *args, **kargs): + """ + Execute passed command line using 'hgthread'. + When the command terminated, callback function is called + with return code. + + cmdline: command line string. + callback: function called after terminated the thread. + + def callback(returncode, msg, err, ...) + + returncode: See the description of 'hgthread'. + msg: Message buffer. + err: Error messages buffer. + + return: True if it starts correctly, otherwise False. + """ + if self.hgthread: + return False + + # clear previous logs + self.clear_buffers() + + # thread start + self.hgthread = hgthread.HgThread(cmdline[1:]) + self.hgthread.start() + gobject.timeout_add(10, self.process_queue, callback, args, kargs) + + return True + + def is_alive(self): + """ + Return whether the thread is alive. + """ + return self.hgthread and self.hgthread.isAlive() + + def stop(self): + """ + Terminate the thread forcibly. + """ + if self.hgthread: + self.hgthread.terminate() + + def get_msg_buffer(self): + """ + Return message buffer. + + Note that the buffer will be cleared when called 'execute' method. + So you need to store this before next execution. + """ + return self.msg_buffer + + def get_err_buffer(self): + """ + Return error message buffer. + + Note that the buffer will be cleared when called 'execute' method. + So you need to store this before next execution. + """ + return self.err_buffer + + ### internal use functions ### + + def clear_buffers(self): + """ + Clear both message and error buffers. + """ + self.msg_buffer = '' + self.err_buffer = '' + + def process_queue(self, callback, args, kargs): + # process queue + self.hgthread.process_dialogs() + + # receive messages from queue + while self.hgthread.getqueue().qsize(): + try: + msg = self.hgthread.getqueue().get(0) + self.msg_buffer += hglib.toutf(msg) + except Queue.Empty: + pass + while self.hgthread.geterrqueue().qsize(): + try: + msg = hglib.toutf(self.hgthread.geterrqueue().get(0)) + self.err_buffer += hglib.toutf(msg) + except Queue.Empty: + pass + + # check thread state + if not self.hgthread.isAlive(): + returncode = self.hgthread.return_code() + self.hgthread = None + def call_callback(): + callback(returncode, self.msg_buffer, self.err_buffer, + *args, **kargs) + gtklib.idle_add_single_call(call_callback) + return False # Stop polling this function + else: + return True # Continue polling