genesis/genesis.py
def _upload_file(self, fn):
        """Upload a single file on the platform.

        File is uploaded in chunks of 1,024 bytes.

        :param fn: File path
        :type fn: string

        """
        size = os.path.getsize(fn)
        counter = 0
        base_name = os.path.basename(fn)
        session_id = str(uuid.uuid4())

        with open(fn, 'rb') as f:
            while True:
                response = None
                chunk = f.read(CHUNK_SIZE)
                if not chunk:
                    break

                for i in range(5):
                    content_range = 'bytes {}-{}/{}'.format(counter * CHUNK_SIZE,
                                                            counter * CHUNK_SIZE + len(chunk) - 1, size)
                    if i > 0 and response is not None:
                        print("Chunk upload failed (error {}): repeating {}"
                              .format(response.status_code, content_range))

                    response = requests.post(urlparse.urljoin(self.url, 'upload/'),
                                             auth=self.auth,
                                             data=chunk,
                                             headers={
                                                 'Content-Disposition': 'attachment; filename="{}"'.format(base_name),
                                                 'Content-Length': size,
                                                 'Content-Range': content_range,
                                                 'Content-Type': 'application/octet-stream',
                                                 'Session-Id': session_id})

                    if response.status_code in [200, 201]:
                        break
                else:
                    # Upload of a chunk failed (5 retries)
                    return None

                progress = 100. * (counter * CHUNK_SIZE + len(chunk)) / size
                sys.stdout.write("\r{:.0f} % Uploading {}".format(progress, fn))
                sys.stdout.flush()
                counter += 1
        print()
        return session_id
def _upload_file(self, fn):
        size = os.path.getsize(fn)
        counter = 0
        base_name = os.path.basename(fn)
        session_id = str(uuid.uuid4())

        with open(fn, 'rb') as f:
            while True:
                response = None
                chunk = f.read(CHUNK_SIZE)
                if not chunk:
                    break

                for i in range(5):
                    content_range = 'bytes {}-{}/{}'.format(counter * CHUNK_SIZE,
                                                            counter * CHUNK_SIZE + len(chunk) - 1, size)
                    if i > 0 and response is not None:
                        print("Chunk upload failed (error {}): repeating {}"
                              .format(response.status_code, content_range))

                    response = requests.post(urlparse.urljoin(self.url, 'upload/'),
                                             auth=self.auth,
                                             data=chunk,
                                             headers={
                                                 'Content-Disposition': 'attachment; filename="{}"'.format(base_name),
                                                 'Content-Length': size,
                                                 'Content-Range': content_range,
                                                 'Content-Type': 'application/octet-stream',
                                                 'Session-Id': session_id})

                    if response.status_code in [200, 201]:
                        break
                else:
                    # Upload of a chunk failed (5 retries)
                    return None

                progress = 100. * (counter * CHUNK_SIZE + len(chunk)) / size
                sys.stdout.write("\r{:.0f} % Uploading {}".format(progress, fn))
                sys.stdout.flush()
                counter += 1
        print()
        return session_id
def _upload_file ( self , fn ) : size = os . path . getsize ( fn ) counter = 0 base_name = os . path . basename ( fn ) session_id = str ( uuid . uuid4 ( ) ) with open ( fn , 'rb' ) as f : while True : response = None chunk = f . read ( CHUNK_SIZE ) if not chunk : break for i in range ( 5 ) : content_range = 'bytes {}-{}/{}' . format ( counter * CHUNK_SIZE , counter * CHUNK_SIZE + len ( chunk ) - 1 , size ) if i > 0 and response is not None : print ( "Chunk upload failed (error {}): repeating {}" . format ( response . status_code , content_range ) ) response = requests . post ( urlparse . urljoin ( self . url , 'upload/' ) , auth = self . auth , data = chunk , headers = { 'Content-Disposition' : 'attachment; filename="{}"' . format ( base_name ) , 'Content-Length' : size , 'Content-Range' : content_range , 'Content-Type' : 'application/octet-stream' , 'Session-Id' : session_id } ) if response . status_code in [ 200 , 201 ] : break else : # Upload of a chunk failed (5 retries) return None progress = 100. * ( counter * CHUNK_SIZE + len ( chunk ) ) / size sys . stdout . write ( "\r{:.0f} % Uploading {}" . format ( progress , fn ) ) sys . stdout . flush ( ) counter += 1 print ( ) return session_id
Upload a single file on the platform.

        File is uploaded in chunks of 1,024 bytes.

        :param fn: File path
        :type fn: string
Upload a single file on the platform .