Revision ca092af4 kamaki/cli/commands/pithos_cli.py

b/kamaki/cli/commands/pithos_cli.py
696 696
            default=False)
697 697
    )
698 698

  
699
    def main(self, local_path, container___path):
700
        super(self.__class__, self).main(
701
            container___path,
702
            path_is_optional=False)
703
        progress_bar = self.arguments['progress_bar']
704
        try:
705
            upload_cb = progress_bar.get_generator('Appending blocks')
706
        except Exception:
707
            upload_cb = None
699
    @errors.generic.all
700
    @errors.pithos.connection
701
    @errors.pithos.container
702
    @errors.pithos.object_path
703
    def _run(self, local_path):
704
        (progress_bar, upload_cb) = self._safe_progress_bar('Appending')
708 705
        try:
709 706
            f = open(local_path, 'rb')
710 707
            self.client.append_object(self.path, f, upload_cb)
711
        except ClientError as err:
712
            progress_bar.finish()
713
            if err.status == 404:
714
                if 'container' in ('%s' % err).lower():
715
                    raiseCLIError(
716
                        err,
717
                        'No container %s in account %s'\
718
                        % (self.container, self.account),
719
                        details=errors.pithos.container_howto)
720
                elif 'object' in ('%s' % err).lower():
721
                    raiseCLIError(
722
                        err,
723
                        'No object %s in container %s'\
724
                        % (self.path, self.container),
725
                        details=errors.pithos.container_howto)
726
            raise_connection_errors(err)
727
            raiseCLIError(err)
728
        except Exception as e:
729
            progress_bar.finish()
730
            raiseCLIError(e)
708
        except Exception:
709
            self._safe_progress_bar_finish(progress_bar)
710
            raise
731 711
        finally:
732
            progress_bar.finish()
712
            self._safe_progress_bar_finish(progress_bar)
713

  
714
    def main(self, local_path, container___path):
715
        super(self.__class__, self)._run(
716
            container___path,
717
            path_is_optional=False)
718
        self._run(local_path)
733 719

  
734 720

  
735 721
@command(pithos_cmds)
736 722
class store_truncate(_store_container_command):
737 723
    """Truncate remote file up to a size (default is 0)"""
738 724

  
725
    @errors.generic.all
726
    @errors.pithos.connection
727
    @errors.pithos.container
728
    @errors.pithos.object_path
729
    @errors.pithos.object_size
730
    def _run(self, size=0):
731
        self.client.truncate_object(self.path, size)
732

  
739 733
    def main(self, container___path, size=0):
740
        super(self.__class__, self).main(container___path)
741
        try:
742
            self.client.truncate_object(self.path, size)
743
        except ClientError as err:
744
            if err.status == 404:
745
                if 'container' in ('%s' % err).lower():
746
                    raiseCLIError(
747
                        err,
748
                        'No container %s in account %s'\
749
                        % (self.container, self.account),
750
                        details=errors.pithos.container_howto)
751
                elif 'object' in ('%s' % err).lower():
752
                    raiseCLIError(
753
                        err,
754
                        'No object %s in container %s'\
755
                        % (self.path, self.container),
756
                        details=errors.pithos.container_howto)
757
            if err.status == 400 and\
758
                'object length is smaller than range length'\
759
                in ('%s' % err).lower():
760
                raiseCLIError(err, 'Object %s:%s <= %sb' % (
761
                    self.container,
762
                    self.path,
763
                    size))
764
            raise_connection_errors(err)
765
            raiseCLIError(err)
766
        except Exception as e:
767
            raiseCLIError(e)
734
        super(self.__class__, self)._run(container___path)
735
        self._run(size=size)
768 736

  
769 737

  
770 738
@command(pithos_cmds)
......
784 752
            default=False)
785 753
    )
786 754

  
787
    def main(self, local_path, container____path__, start, end):
788
        (start, end) = check_range(start, end)
789
        super(self.__class__, self).main(container____path__)
790
        try:
791
            f = open(local_path, 'rb')
792
            f.seek(0, 2)
793
            f_size = f.tell()
794
            f.seek(start, 0)
795
        except Exception as e:
796
            raiseCLIError(e)
797
        progress_bar = self.arguments['progress_bar']
798
        try:
799
            upload_cb = progress_bar.get_generator(
800
                'Overwriting %s blocks' % end - start)
801
        except Exception:
802
            upload_cb = None
755
    def _open_file(self, local_path, start):
756
        f = open(path.abspath(local_path), 'rb')
757
        f.seek(0, 2)
758
        f_size = f.tell()
759
        f.seek(start, 0)
760
        return (f, f_size)
761

  
762
    @errors.generic.all
763
    @errors.pithos.connection
764
    @errors.pithos.container
765
    @errors.pithos.object_path
766
    @errors.pithos.object_size
767
    def _run(self, local_path, start, end):
768
        (start, end) = (int(start), int(end))
769
        (f, f_size) = self._open_file(local_path, start)
770
        (progress_bar, upload_cb) = self._safe_progress_bar(
771
            'Overwrite %s bytes' % (end - start))
803 772
        try:
804
            self.path = self.path if self.path else path.basename(local_path)
805 773
            self.client.overwrite_object(
806 774
                obj=self.path,
807 775
                start=start,
808 776
                end=end,
809 777
                source_file=f,
810 778
                upload_cb=upload_cb)
811
        except ClientError as err:
812
            progress_bar.finish()
813
            if (err.status == 400 and\
814
            'content length does not match range' in ('%s' % err).lower()) or\
815
            err.status == 416:
816
                raiseCLIError(err, details=[
817
                    'Content size: %s' % f_size,
818
                    'Range: %s-%s (size: %s)' % (start, end, end - start)])
819
            elif err.status == 404:
820
                if 'container' in ('%s' % err).lower():
821
                    raiseCLIError(
822
                        err,
823
                        'No container %s in account %s'\
824
                        % (self.container, self.account),
825
                        details=errors.pithos.container_howto)
826
                elif 'object' in ('%s' % err).lower():
827
                    raiseCLIError(
828
                        err,
829
                        'No object %s in container %s'\
830
                        % (self.path, self.container),
831
                        details=errors.pithos.container_howto)
832
            raise_connection_errors(err)
833
            raiseCLIError(err)
834
        except Exception as e:
835
            progress_bar.finish()
836
            raiseCLIError(e)
779
        except Exception:
780
            self._safe_progress_bar_finish(progress_bar)
781
            raise
837 782
        finally:
838
            progress_bar.finish()
783
            self._safe_progress_bar_finish(progress_bar)
784

  
785
    def main(self, local_path, container____path__, start, end):
786
        super(self.__class__, self)._run(container____path__)
787
        self.path = self.path if self.path else path.basename(local_path)
788
        self._run(local_path=local_path, start=start, end=end)
839 789

  
840 790

  
841 791
@command(pithos_cmds)

Also available in: Unified diff