def __interactive_login(self, connection): if not isatty(): raise GenestackException("Interactive login is not possible") connection.check_version() email = self.email message = 'Connecting to %s' % self.host login_by_token = 'by token' login_by_email = 'by email and password' login_anonymously = 'anonymously' choice = interactive_select( [login_by_token, login_by_email, login_anonymously], 'How do you want to login') if choice == login_anonymously: return while True: if message: print(message) if choice == login_by_email: input_message = 'e-mail [%s]: ' % email if email and '@' in email else 'e-mail: ' email = raw_input(input_message).strip() or email password = getpass('password for %s: ' % email) try: connection.login(email, password) self.email = email self.password = password return except GenestackAuthenticationException: message = ( 'Your username and password have been rejected by %s, ' 'please try again' % self.host) else: token = getpass('token: ') try: connection.login_by_token(token) self.token = token return except GenestackAuthenticationException: message = 'Your token has been rejected by %s, please try again' % self.host
def __interactive_login(self, connection): if not isatty(): raise GenestackException("Interactive login is not possible") connection.check_version() email = self.email message = 'Connecting to %s' % self.host login_by_token = 'by token' login_by_email = 'by email and password' login_anonymously = 'anonymously' choice = interactive_select([login_by_token, login_by_email, login_anonymously], 'How do you want to login') if choice == login_anonymously: return while True: if message: print(message) if choice == login_by_email: input_message = 'e-mail [%s]: ' % email if email and '@' in email else 'e-mail: ' email = raw_input(input_message).strip() or email password = getpass('password for %s: ' % email) try: connection.login(email, password) self.email = email self.password = password return except GenestackAuthenticationException: message = ('Your username and password have been rejected by %s, ' 'please try again' % self.host) else: token = getpass('token: ') try: connection.login_by_token(token) self.token = token return except GenestackAuthenticationException: message = 'Your token has been rejected by %s, please try again' % self.host
def upload_file(self, file_path, token): """ Upload a file to the current Genestack instance. This action requires a special token that can be generated by the application. :param file_path: path to existing local file. :type file_path: str :param token: upload token :type file_path: str :rtype: None """ if isatty(): progress = TTYProgress() else: progress = DottedProgress(40) file_to_upload = FileWithCallback(file_path, 'rb', progress) filename = os.path.basename(file_path) path = '/application/upload/%s/%s/%s' % (self.application_id, token, urllib.quote(filename)) return self.__invoke(path, file_to_upload)
def __interactive_login(self, connection): if not isatty(): raise GenestackException("Interactive login is not possible") email = self.email message = "Connecting to %s" % self.host while True: if message: print message if email and "@" in email: email = raw_input("E-mail [%s]: " % email).strip() or email else: email = raw_input("E-mail: ").strip() or email if not email: continue password = getpass("password for %s: " % email) try: connection.login(email, password) self.email = email self.password = password return except GenestackException: message = "Your username or password was incorrect for %s. Please try again." % self.host
def __interactive_login(self, connection): if not isatty(): raise GenestackException("Interactive login is not possible") email = self.email message = 'Connecting to %s' % self.host while True: if message: print message if email and '@' in email: email = raw_input('E-mail [%s]: ' % email).strip() or email else: email = raw_input('E-mail: ').strip() or email if not email: continue password = getpass('password for %s: ' % email) try: connection.login(email, password) self.email = email self.password = password return except GenestackException: message = 'Your username or password was incorrect for %s. Please try again.' % self.host
def upload_file(self, file_path, token): """ Upload a file to the current Genestack instance. This action requires a special token that can be generated by the application. :param file_path: path to existing local file. :type file_path: str :param token: upload token :type file_path: str :rtype: None """ if isatty(): progress = TTYProgress() else: progress = DottedProgress(40) file_to_upload = FileWithCallback(file_path, 'rb', progress) filename = os.path.basename(file_path) path = '/application/upload/%s/%s/%s' % ( self.application_id, token, urllib.quote(filename) ) return self.__invoke(path, file_to_upload).result
def check_tty(): if not isatty(): raise GenestackException("Prompt cannot be called")
def __init__(self, application, path, chunk_size=None): if chunk_size is None: chunk_size = CHUNK_SIZE if chunk_size <= 0: raise GenestackException("Chunk size should be positive") self.chunk_upload_url = '/application/uploadChunked/%s/unusedToken' % application.application_id self.connection = application.connection self.lock = Lock() self.__iterator_lock = Lock() self.__output_lock = Lock() self.__application_result = None self.__has_application_result = False self.__finished = False self.__error = None self.thread_counter = 0 self.condition = Condition() modified = datetime.fromtimestamp(os.path.getmtime(path)) total_size = os.path.getsize(path) # TODO change according to javascript token token = '{total_size}-{name}-{date}'.format( total_size=total_size, name=re.sub('[^A-z0-9_-]', '_', os.path.basename(path)), date=modified.strftime('%a_%b_%d_%Y_%H_%M_%S')) self.token = token self.path = path # Last chunk can be larger than CHUNK_SIZE but less then two chunks. # Example: CHUNK_SIZE = 2 # file size 2 > 1 chunk # file size 3 > 1 chunk # file size 4 > 2 chunk # file size 5 > 2 chunk if total_size < chunk_size * 2: chunk_count = 1 else: chunk_count = total_size / chunk_size self.total_size = total_size self.filename = os.path.basename(path) self.path = path self.chunk_count = chunk_count launch_time = int(time.time() * 1000) # import from here to avoid circular imports # TODO move progress functions to other module. if isatty(): from genestack_connection import TTYProgress self.progress = TTYProgress() else: from genestack_connection import DottedProgress self.progress = DottedProgress(40) def _iterator(): start = 0 info = [ chunk_size, total_size, token, self.filename, path, chunk_count, launch_time ] for x in xrange(1, chunk_count + 1): if x == chunk_count: curren_chunk_size = self.total_size - start else: curren_chunk_size = chunk_size yield Chunk(x, start, curren_chunk_size, *info) start += curren_chunk_size self.iterator = _iterator()
def __init__(self, application, path, chunk_size=None): if chunk_size is None: chunk_size = CHUNK_SIZE if chunk_size <= 0: raise GenestackException("Chunk size should be positive") self.chunk_upload_url = '/application/uploadChunked/%s/unusedToken' % application.application_id self.connection = application.connection self.lock = Lock() self.__iterator_lock = Lock() self.__output_lock = Lock() self.__application_result = None self.__has_application_result = False self.__finished = False self.__error = None self.thread_counter = 0 self.condition = Condition() modified = datetime.fromtimestamp(os.path.getmtime(path)) total_size = os.path.getsize(path) # TODO change according to javascript token token = '{total_size}-{name}-{date}'.format(total_size=total_size, name=re.sub('[^A-z0-9_-]', '_', os.path.basename(path)), date=modified.strftime('%a_%b_%d_%Y_%H_%M_%S')) self.token = token self.path = path # Last chunk can be larger than CHUNK_SIZE but less then two chunks. # Example: CHUNK_SIZE = 2 # file size 2 > 1 chunk # file size 3 > 1 chunk # file size 4 > 2 chunk # file size 5 > 2 chunk if total_size < chunk_size * 2: chunk_count = 1 else: chunk_count = total_size / chunk_size self.total_size = total_size self.filename = os.path.basename(path) self.path = path self.chunk_count = chunk_count launch_time = int(time.time() * 1000) # import from here to avoid circular imports # TODO move progress functions to other module. if isatty(): from genestack_connection import TTYProgress self.progress = TTYProgress() else: from genestack_connection import DottedProgress self.progress = DottedProgress(40) def _iterator(): start = 0 info = [chunk_size, total_size, token, self.filename, path, chunk_count, launch_time] for x in xrange(1, chunk_count + 1): if x == chunk_count: curren_chunk_size = self.total_size - start else: curren_chunk_size = chunk_size yield Chunk(x, start, curren_chunk_size, *info) start += curren_chunk_size self.iterator = _iterator()