def findFile(name):
    """Finds the itinerary Google Doc with NAME.
    """
    page_token = None
    while True:
        response = service.create_service().files().list(
            q="name='" + name + "'",
            spaces='drive',
            fields='nextPageToken, files(id, name)',
            pageToken=page_token).execute()
        for file in response.get('files', []):
            # Process change
            print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
            return file
def downloadFile(file, mimeType, filepath):
    """Downloads the itinerary as a plain text file.
    """
    request = service.create_service().files().export_media(
        fileId=file.get('id'), mimeType=mimeType)
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))
    with io.open(filepath, 'wb') as f:
        fh.seek(0)
        f.write(fh.read())
Esempio n. 3
0
''' Base Unit Tests '''

from json import loads, dumps
from flask_testing import TestCase
from service import create_service

SERVICE = create_service()

class BaseTestCase(TestCase):
    ''' Base Class for Unit Tests '''

    def create_app(self):
        ''' Create Service '''

        SERVICE.config.from_object('service.config.TestingConfig')
        return SERVICE

    def send_get(self, path, headers=None):
        ''' Sending GET Requests '''

        if headers is None:
            headers = dict()

        with self.client:
            response = self.client.get(path, headers=headers)
            payload = loads(response.data.decode())

        return response, payload

    def send_post(self, path, data, headers=None):
        ''' Sending POST Requests '''
Esempio n. 4
0
    def build_service_thread(self,
                             platform_name,
                             subid,
                             host,
                             port,
                             username,
                             password,
                             installer='apt'):
        try:
            self._gauge_value = 0
            self.Bind(wx.EVT_IDLE, self.gauge_progress_update)
            wx.CallAfter(self.callback_update_status, '开始连接主机')
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(host, port, username, password)

            # 开始安装
            current_service = self.get_current_selection_service()
            service_item = self._services[current_service]
            if 'Install' not in service_item.keys():
                wx.CallAfter(self.callback_update_status, '服务配置错误')
                return
            else:
                service_item_install = service_item['Install']
                self.write_log(platform_name,
                               '%s install start.' % current_service)
                wx.CallAfter(self.callback_update_status,
                             '%s开始安装' % current_service)
                service_config_file = ''
                config_content = {}
                if 'Config' in service_item_install.keys():
                    install_item_config = service_item_install['Config']
                    if 'Content' in install_item_config.keys():
                        config_content = install_item_config['Content']
                    if 'File' in install_item_config.keys():
                        service_config_file = install_item_config['File']
                name = self.get_select_apikey_name()
                # 设置IP、Password等
                config_content['server'] = host
                config_content['server_port'] = random.randint(20001, 59999)
                random_str1 = ''.join(
                    random.choice(string.ascii_lowercase) for i in range(10))
                random_num1 = ''.join(
                    random.choice(string.digits) for i in range(10))
                random_str = list(random_str1 + random_num1)
                random.shuffle(random_str)
                config_content['password'] = ''.join(random_str)

                # 设置分享
                if 'Generation' in service_item_install.keys():
                    install_item_generation = service_item_install[
                        'Generation']
                    if 'ShareLink' in install_item_generation.keys():
                        generation_sharelink = install_item_generation[
                            'ShareLink']
                        generation_sharelink['Content'] = '%s:%s' % (
                            config_content['method'],
                            config_content['password'])
                        generation_sharelink['Host'] = '%s:%s' % (
                            host, config_content['server_port'])

                # config_content['service_list'] = '%s'%(current_service)
                # service_config_item = {current_service:{}}
                # service_config_item[current_service] = install_item_generation
                # config_content[current_service] = service_config_item
                if service.create_service(platform_name, name, subid,
                                          config_content):
                    if 'Command' in service_item_install.keys():
                        install_command = service_item_install['Command']
                        for install_item in install_command:
                            stdin, stdout, stderr = ssh.exec_command(
                                install_item)
                            wx.CallAfter(self.callback_update_message,
                                         ''.join(stdout.readlines()))
                            wx.CallAfter(self.callback_update_message,
                                         ''.join(stderr.readlines()))
                            err = self.get_available_err(stderr.readlines())
                            if '' != err:
                                self.write_log(platform_name, err)
                                wx.CallAfter(self.callback_update_status,
                                             '%s创建失败' % current_service)
                            else:
                                self.write_log(platform_name,
                                               ''.join(stdout.readlines()))
                                self.write_log(
                                    platform_name,
                                    '%s install end.' % current_service)
                                wx.CallAfter(self.callback_update_status,
                                             '%s创建成功' % current_service)
                                wx.CallAfter(self.callback_insert_service,
                                             platform_name, subid,
                                             current_service)
                    if service_config_file != '':
                        try:
                            with open('tmp.json', 'w') as f:
                                json.dump(config_content, f)
                            t = paramiko.Transport((host, port))
                            t.connect(username=username, password=password)
                            sftp = paramiko.SFTPClient.from_transport(t)
                            sftp.put('tmp.json', service_config_file)
                            t.close()
                            os.remove('tmp.json')
                            self.write_log(platform_name,
                                           '%s 配置成功 .' % current_service)
                            wx.CallAfter(self.callback_update_status,
                                         '%s配置成功' % current_service)
                            wx.CallAfter(self.callback_insert_service,
                                         platform_name, subid, current_service)
                        except Exception as e:
                            print(str(e))
                            self.write_log(platform_name, err)
                            wx.CallAfter(self.callback_update_status,
                                         '%s配置失败' % current_service)
                else:
                    wx.CallAfter(self.callback_update_message,
                                 '%s创建失败' % current_service)
        except Exception as e:
            self.write_log(platform_name, '%s,%s' % (host, str(e)))
            wx.CallAfter(self.callback_update_status, '连接失败')
            wx.CallAfter(self.callback_update_message, str(e))
        wx.CallAfter(self.callback_update_data)