Esempio n. 1
0
def test_delete_wheel(mock_dynamodb, mock_participant_table, mock_wheel_table):
    test_wheel = {'id': get_uuid()}
    participant = {'id': get_uuid(), 'wheel_id': test_wheel['id']}

    mock_wheel_table.put_item(Item=test_wheel)
    mock_participant_table.put_item(Item=participant)

    event = {'body': {}, 'pathParameters': {'wheel_id': test_wheel['id']}}
    response = wheel.delete_wheel(event)

    assert response['statusCode'] == 201
    with pytest.raises(NotFoundError):
        mock_wheel_table.get_existing_item(Key=test_wheel)
    with pytest.raises(NotFoundError):
        mock_participant_table.get_existing_item(Key=participant)
Esempio n. 2
0
 def rpush(self, msg, timeout=0):
     self._conn.rpush(self.queue, msg)
     if timeout:
         msg = "%s%s" % (msg, get_uuid())
         self.zadd(get_time() + timeout, msg)
         return msg
     return True
Esempio n. 3
0
def test_update_participant(mock_dynamodb, mock_participant_table):
    participant = {
        'id': get_uuid(),
        'wheel_id': WHEEL_ID,
        'name': 'Old Name',
        'url': 'https://amazon.com',
        'weight': 1
    }
    mock_participant_table.put_item(Item=participant)

    event = {
        'pathParameters': {
            'wheel_id': WHEEL_ID,
            'participant_id': participant['id']
        },
        'body': {
            'name': 'New Name',
            'url': 'https://new-website.com'
        }
    }
    response = wheel_participant.update_participant(event)
    updated_participant = json.loads(response['body'])

    assert response['statusCode'] == 200
    assert updated_participant['name'] == event['body']['name']
    assert updated_participant['url'] == event['body']['url']
Esempio n. 4
0
def test_invalid_update_participant(mock_dynamodb, mock_participant_table):
    participant = {
        'id': get_uuid(),
        'wheel_id': WHEEL_ID,
        'name': 'Old Name',
        'url': 'https://amazon.com',
        'weight': 1
    }
    mock_participant_table.put_item(Item=participant)

    event = {
        'pathParameters': {
            'wheel_id': WHEEL_ID,
            'participant_id': participant['id']
        },
        'body': {
            'name': '',
            'url': ''
        }
    }
    response = wheel_participant.update_participant(event)

    assert response['statusCode'] == 400
    assert 'Participants names and urls must be at least 1 character in length' in response[
        'body']
Esempio n. 5
0
def save_note(title, content):
    query = "INSERT INTO note VALUES (?,?,?,?)"
    _id = get_uuid()
    db = get_db()
    db.cursor().execute(query, (session["uid"], _id, title, content))
    db.commit()
    return _id
Esempio n. 6
0
def main(args):
    zipfilepath = args.zip
    if zipfilepath is None:
        print "pass arguements correctly!"
        exit(-1)
    xmlfilepath = args.xmlfile
    zip_path = zipfilepath
    if utils.valid_file(zip_path) is not True:
        print "bad zip"
        exit(-1)
    data_for_all_files = []
    path_to_extract = utils.random_temp_path(TEMP_DIR)
    utils.extractor(zip_path, path_to_extract)
    list_of_all_files = utils.getListOfFiles(path_to_extract)
    
    for path_to_file in list_of_all_files:
        uid = utils.get_uuid()
        filename = utils.stripfilepath(path_to_file)
        rel_path = utils.get_relative_path(path_to_file, path_to_extract)
        md5hash = utils.md5sum(path_to_file)
        filesize = utils.get_file_size(filepath=path_to_file)
        data = FileDetails(file_uuid=uid, file_name=filename, file_full_path=path_to_file, relative_path=rel_path, file_md5hash=md5hash, file_size=filesize)
        data_for_all_files.append(data)
    
    XS.XMLSerialize(data_for_all_files, xmlfilepath)
    utils.cleanup(path_to_extract)
    exit(0)
Esempio n. 7
0
 def rpush(self, msg, timeout=0):
     self._conn.rpush(self.queue, msg)
     if timeout:
         msg = "%s%s"%(msg,get_uuid())
         self.zadd(get_time()+timeout ,msg)
         return msg
     return True
Esempio n. 8
0
def test_suggest_participant_hidden_rig(mock_dynamodb, mock_participant_table,
                                        mock_wheel_table):
    participants = [{
        'id': get_uuid(),
        'wheel_id': WHEEL_ID,
        'name': name,
    } for name in ['Rig me!', 'I cannot win!']]

    with mock_participant_table.batch_writer() as batch:
        for participant in participants:
            batch.put_item(Item=participant)
    mock_wheel_table.update_item(Key={'id': WHEEL_ID},
                                 **to_update_kwargs({
                                     'rigging': {
                                         'hidden': True,
                                         'participant_id':
                                         participants[0]['id']
                                     }
                                 }))

    response = wheel_participant.suggest_participant({
        'body': {},
        'pathParameters': {
            'wheel_id': WHEEL_ID
        }
    })
    body = json.loads(response['body'])
    assert response['statusCode'] == 200
    assert body['participant_id'] == participants[0]['id']
    assert 'rigged' not in body
Esempio n. 9
0
def test_delete_participant(mock_dynamodb, mock_participant_table):
    participants = [{
        'id': get_uuid(),
        'wheel_id': WHEEL_ID,
        'name': name,
        'url': 'https://amazon.com',
        'weight': 1
    } for name in ['Dan', 'Alexa']]

    with mock_participant_table.batch_writer() as batch:
        for participant in participants:
            batch.put_item(Item=participant)

    event = {
        'body': {},
        'pathParameters': {
            'wheel_id': WHEEL_ID,
            'participant_id': participants[0]['id']
        }
    }
    response = wheel_participant.delete_participant(event)

    assert response['statusCode'] == 201
    with pytest.raises(NotFoundError):
        mock_participant_table.get_existing_item(Key={
            'id': participants[0]['id'],
            'wheel_id': WHEEL_ID
        })
Esempio n. 10
0
def add_user():
    name = request.form.get('name', default=None)
    password = request.form.get('password', default=None)
    email = request.form.get('email', default=None)
    sex = request.form.get('sex', default=None)
    db = DBSession()
    u = db.query(User).filter(User.name == name).first()
    if u is not None:
        db.close()
        error_message = '用户名' + name + '已存在,请重新输入'
        res = {
            'code': 100,
            'message': error_message,
            'data': {
                'email': email,
                'name': name
            }
        }
        return render_template('user/register.html', result=res)
    else:
        user = User(id=get_uuid(),
                    name=name,
                    password=password,
                    email=email,
                    sex=sex,
                    create_time=datetime.now())
        db.add(user)
        db.commit()
        db.close()
        res = {'code': 200, 'message': 'success'}
        return render_template('login.html', result=res)
Esempio n. 11
0
def set_inpt_fn(n_df, n_dfn, path='', subdir=True):
    '''Set Input filename (ifn), Back filename (bfn) and Destinatin filename (dfn)'''

    #t=datetime.datetime.now()
    #ts=str(time.mktime(t.timetuple()))

    id = utils.get_uuid()
    #ifn = path + '/tstorm-input-file-' + ts + '.txt'
    #bfn = path + '/tstorm-back-input-file-' + ts + '.txt'
    ifn = path + '/tstorm-input-file-' + id + '.txt'
    bfn = path + '/tstorm-back-input-file-' + id + '.txt'

    if n_df:
        if '/' in n_dfn:
            dfn = '/'
            tmp_d = os.path.dirname(n_dfn).split('/')[1:]
            for x in tmp_d:
                dfn = dfn + x + id + '/'
                #dfn = dfn + x + ts + '/'
            #dfn = dfn + os.path.basename(n_dfn) + '.' + ts
            dfn = dfn + os.path.basename(n_dfn) + '.' + id
    else:
        if subdir:
            dfn = '/a'+ id + '/b' + id + '/tstorm-output-file-' + id + '.txt'
            #dfn = '/a'+ ts + '/b' + ts + '/tstorm-output-file-' + ts + '.txt'
        else:
            dfn = '/tstorm-output-file-' + id + '.txt'
            #dfn = '/tstorm-output-file-' + ts + '.txt'

    return ifn,dfn,bfn
Esempio n. 12
0
async def create_remove_api(loop):
    name = naminator("api")
    specs = {
        "repository": "cassinyio",
        "blueprint": "notebook:4018e4ee",
        "cpu": 1,
        "ram": 100,
        "service_type": "api",
        "command": "python -c 'for i in range(100): print(i)'",
        "uuid": get_uuid().hex,
    }

    await Spawner.api.create(
        name=name,
        user_id=1,
        specs=specs,
        cargo=1,
        s3_key="test",
        s3_skey="test",
    )
    service = await Spawner.get_service(name=name)
    assert service is not None
    await Spawner.api.delete(name=name)
    service = await Spawner.get_service(name=name)
    assert service is None
Esempio n. 13
0
def test_select_participant_removes_rigging(mock_dynamodb,
                                            mock_participant_table,
                                            mock_wheel_table):
    mock_wheel_table.update_item(Key={'id': WHEEL_ID},
                                 **to_update_kwargs({'rigging': {}}))

    participant = {
        'id': get_uuid(),
        'wheel_id': WHEEL_ID,
        'name': 'Pick me!',
        'url': 'https://amazon.com',
        'weight': 1
    }
    mock_participant_table.put_item(Item=participant)

    event = {
        'body': {},
        'pathParameters': {
            'wheel_id': WHEEL_ID,
            'participant_id': participant['id']
        }
    }
    response = wheel_participant.select_participant(event)

    assert response['statusCode'] == 201
    assert 'rigging' not in mock_wheel_table.get_existing_item(
        Key={'id': WHEEL_ID})
Esempio n. 14
0
    def __init__(self, **kwargs):
        """
        Initialize common settings.  The following are properties common to most all objects
        """
        # Set 'database' container first as needed to validate name uniqueness

        now = datetime.datetime.utcnow()
        self._create_time = now
        self._last_update_time = None
        self._config = kwargs.pop('config', None)

        self._last_synced_time = None
        self._last_unsynced_time = now
        self._i_am_synced = False
        self._sync_thread = None
        self._update_interval = kwargs.pop('update_interval', DEFAULT_UPDATE_INTERVAL_SECS)
        self._update_message = ['Initial discovery has not yet occured']
        self._no_sync = kwargs.pop('no_sync', False)
        self._cache_client = kwargs.pop('cache_client', False)

        self._name = kwargs.get('name')
        self._id = UUID(kwargs.pop('id', str(get_uuid())))

        self._parent = kwargs.get('parent', None)
        self._children = kwargs.get('children', [])

        self._enabled = kwargs.get('enabled', True)
        self._delete_pending = False
        self._modified = False

        self._metadata = {}

        self._client = None

        # Validate parameters

        if self.name is None or len(self.name) == 0:
            raise ValueError('Invalid/missing unique name not provided')

        if self.parent is not None and not isinstance(self.parent, Base):
            raise ValueError("Parent of {} must be derived from 'Base', type is '{}".format(self.name,
                                                                                            type(self.parent)))
        if not isinstance(self.children, list):
            raise ValueError("Children of {} must be supplied as a list or 'None', type is '{}".
                             format(self.name, type(self.children)))
        cnt = 1
        for child in self.children:
            if not isinstance(child, Base):
                raise ValueError("Child #{} of {} must be derived from 'Base', type is '{}".
                                 format(cnt, self.name, type(child)))
            cnt += 1

        # Set up state machine to manage states

        self._machine = Machine(model=self, states=Base._states,
                                transitions=Base._transitions,
                                initial='out_of_sync',
                                queued=True,
                                name='{} [{}]'.format(self.__class__.__name__, self.name))
Esempio n. 15
0
def test_get_wheel(mock_dynamodb, mock_wheel_table):
    test_wheel = {'id': get_uuid(), 'name': 'Test Wheel'}
    mock_wheel_table.put_item(Item=test_wheel)

    event = {'body': {}, 'pathParameters': {'wheel_id': test_wheel['id']}}
    response = wheel.get_wheel(event)

    assert response['statusCode'] == 200
    assert json.loads(response['body']) == test_wheel
Esempio n. 16
0
def test_unrig_participant(mock_dynamodb, mock_wheel_table):
    test_wheel = {
        'id': get_uuid(),
        'name': 'Test Wheel',
        'rigging': {
            'participant_id': get_uuid(),
            'hidden': False
        }
    }

    mock_wheel_table.put_item(Item=test_wheel)

    event = {'body': {}, 'pathParameters': {'wheel_id': test_wheel['id']}}
    response = wheel.unrig_participant(event)

    assert response['statusCode'] == 201
    assert 'rigging' not in mock_wheel_table.get_existing_item(
        Key={'id': test_wheel['id']})
Esempio n. 17
0
 def user_phone_manage_modify(self, user_info):
     uid = user_info.get('uid','')
     user = user_info.get('user','')
     uname = user.get('name','')
     phones = user.get('phones','')
     phones_list = []
     mails_list = []
     for phone in phones:
         pid = phone.get('pid','')
         phone = phone.get('phone','')
         if pid: #update
             vars = dict(pid=pid)
             intime_new = utils.get_cur_time()
             db.update(utils.DB_TABLE_PHONE, where="pid=$pid",phone=phone,intime=intime_new,vars=vars)
             phone_dic= dict(pid=pid,phone=phone,intime=intime_new)
             phones_list.append(phone_dic)
             pass
         else: #insert
             pid_new = utils.get_uuid()
             intime_new = utils.get_cur_time()
             db.insert(utils.DB_TABLE_PHONE,uid=uid,phone=phone,pid=pid_new,intime=intime_new)
             phone_dic= dict(pid=pid_new,phone=phone,intime=intime_new)
             phones_list.append(phone_dic)
             pass
     mails = user.get('mails','')
     for mail in mails:
         mid = mail.get('mid','')
         mail_str = mail.get('mail','')
         if mid:
             vars = dict(mid=mid)
             intime_new = utils.get_cur_time()
             db.update(utils.DB_TABLE_MAIL, where="mid=$mid",mail=mail_str ,intime=intime_new,vars=vars)
             mail_dic = dict(mid=mid,mail=mail_str,intime=intime_new)
             mails_list.append(mail_dic)
             pass
         else:
             mid_new = utils.get_uuid()
             intime_new = utils.get_cur_time()
             db.insert(utils.DB_TABLE_MAIL,uid=uid,mail=mail_str,mid=mid_new,intime=intime_new)
             mail_dic = dict(mid=mid_new,mail=mail_str,intime=intime_new)
             mails_list.append(mail_dic)
             pass
     user_dic = dict(uid=uid,name=uname,phones=phones_list,mails=mails_list)
     return (0,'success',uid,user_dic)
Esempio n. 18
0
 def mount_device(self, device_path, mount_point=None, filesystem=None, options=None, passphrase=None):
     if mount_point is None:
         mount_point = ''
     if filesystem is None:
         filesystem = ''
     if options is None:
         options = ''
     if passphrase is None:
         passphrase = ''
         
     # Connect encrypted partition
     connected = False
     if passphrase:
         if is_encrypted(device_path) and not is_connected(device_path):
             device_path, filesystem = connect_block_device(device_path, passphrase)
             
     # Do not mount swap partition
     if filesystem == 'swap':
         #print((">>>> swap partition: return device_path=%s, filesystem=%s" % (device_path, filesystem)))
         return (device_path, '', filesystem)
     
     # Try udisks way if no mount point has been given
     if not mount_point:
         fs = self._get_filesystem(device_path)
         if fs is not None:
             mount_points = self._mount_filesystem(fs)
             if mount_points:
                 # Set mount point and free space for this device
                 total, free, used = self.get_mount_size(mount_points[0])
                 self.devices[device_path]['mount_point'] = mount_points[0]
                 self.devices[device_path]['free_size'] = free
                 filesystem = get_filesystem(device_path)
                 return (device_path, mount_points[0], filesystem)
         # Try a temporary mount point on uuid
         uuid = get_uuid(device_path)
         if uuid:
             mount_point = join('/media', uuid)
         else:
             return (device_path, mount_point, filesystem)
     
     # Mount the device to the given mount point
     if not exists(mount_point):
         makedirs(mount_point, exist_ok=True)
     if exists(mount_point):
         if options:
             if options[0:1] != '-':
                 options = '-o ' + options
         else:
             options = ''
         fs = '-t ' + filesystem if filesystem else ''
         cmd = "mount {options} {fs} {device_path} {mount_point}".format(**locals())
         shell_exec(cmd)
     if self.is_mounted(device_path):
         filesystem = get_filesystem(device_path)
         return (device_path, mount_point, filesystem)
     return (device_path, '', filesystem)
Esempio n. 19
0
    async def post(self, payload):
        """Create blueprints."""
        user_id = payload["user_id"]
        uuid = get_uuid().hex

        # when passing a file
        reader = await self.request.multipart()

        while True:
            part = await reader.next()

            if part is None:
                break

            if part.headers[aiohttp.hdrs.CONTENT_TYPE] == 'application/json':
                data = await part.json()
                blueprint, errors = CreateBlueprint().load(data)
                if errors:
                    return json_response({"error": errors}, status=400)

                base_blueprint = await get_blueprint(
                    self.db,
                    blueprint_ref=blueprint['base_image'],
                    user_id=user_id)
                if base_blueprint is None:
                    error = "Did you select the right blueprint?"
                    return json_response({"error": error}, status=400)

                continue

            file_uuid = f"{uuid}.tar.gz"
            file_path = os.path.join(Config.TEMP_BUILD_FOLDER, file_uuid)

            await write_file(file_path=file_path, part=part)

            if tarfile.is_tarfile(file_path) is False:
                os.remove(file_path)
                return json_response({"error": "No proper file format."},
                                     status=400)

        base_image = f"{base_blueprint.repository}/{base_blueprint.name}:{base_blueprint.tag}"

        event = {
            "uuid": uuid,
            "user_id": user_id,
            "email": payload['email'],
            "path": file_path,
            "blueprint": blueprint,
            "base_image": base_image
        }

        await streaming.publish("service.blueprint.create", event)

        return json_response(
            {"message": f"We are creating your blueprint({uuid})."})
Esempio n. 20
0
def file_upload(file_, path='/', filename=get_uuid()):
    global bucket

    if not bucket:
        return None

    file_.seek(0)
    filename = os.path.join(path, filename)
    k = Key(bucket)
    k.key = filename
    k.set_contents_from_file(file_)
    return filename
Esempio n. 21
0
def file_upload(file_, path='/', filename=get_uuid()):
    global bucket

    if not bucket:
        return None

    file_.seek(0)
    filename = os.path.join(path, filename)
    k = Key(bucket)
    k.key = filename
    k.set_contents_from_file(file_)
    return filename
Esempio n. 22
0
def test_rig_participant(mock_dynamodb, mock_wheel_table):
    event = {
        'body': {'hidden': True},
        'pathParameters': {
            'wheel_id': WHEEL_ID,
            'participant_id': get_uuid()
        }
    }
    response = wheel_participant.rig_participant(event)

    assert response['statusCode'] == 201
    assert 'rigging' in mock_wheel_table.get_existing_item(Key={'id': WHEEL_ID})
Esempio n. 23
0
def parse_annotation(file_path, start_row=1, verbose=True, version='1.1'):
    if verbose:
        print("Parsing annotation", file_path)

    csv_file_in = open(file_path, 'rt')
    csv_reader = csv.reader(csv_file_in, delimiter=',')

    data = {'data': [], 'version': version}

    for i, row in enumerate(csv_reader):
        if verbose and i % 10 == 0:
            print("Read row", str(i))
        if i >= start_row:
            title, context, question, answer, dept, chapter = row[:6]
            if len(question) == 0:
                continue
            has_added_title = False
            found_doc = None
            for doc in data['data']:
                if utils.is_similar_str(doc["title"], title):
                    has_added_title = True
                    found_doc = doc
            if not has_added_title:
                found_doc = {
                    'paragraphs': [],
                    'title': title,
                    'department': dept,
                    'chapter': chapter
                }
                data['data'].append(found_doc)
            has_added_context = False
            found_paragraph = None
            for paragraph in found_doc["paragraphs"]:
                if utils.is_similar_str(paragraph["context"], context):
                    has_added_context = True
                    found_paragraph = paragraph
            if not has_added_context:
                found_paragraph = {'context': context, 'qas': []}
                found_doc["paragraphs"].append(found_paragraph)
            answer_start = context.lower().find(answer.lower())
            qa = {
                'answers': [{
                    'answer_start': answer_start,
                    'text': answer
                }],
                'question': question,
                'id': utils.get_uuid()
            }
            found_paragraph['qas'].append(qa)

    csv_file_in.close()
    return data
def create_participant(event):
    """
    Create a participant

    :param event: Lambda event containing the API Gateway request body including a name and a url and the
    path parameter wheel_id
    {
      "pathParameters":
      {
        "wheel_id": string ID of the wheel (DDB Hash Key)
      },
      "body":
      {
        "name": participant name string,
        "url: Valid URL for the participant,
      }
    }
    :return: response dictionary containing new participant object if successful
    {
      "body":
      {
        "id": string ID of the participant (DDB Hash Key),
        "wheel_id": string ID of the wheel (DDB Hash Key),
        "name": string name of the wheel,
        "url: URL for the participant,
        "created_at": creation timestamp,
        "updated_at": updated timestamp,
      }
    }
    """
    wheel_id = event['pathParameters']['wheel_id']
    body = event['body']
    if not check_string(body.get('name', None)) or not check_string(
            body.get('url', None)):
        raise base.BadRequestError(
            "Participants require a name and url which must be at least 1 character in length"
        )

    wheel = Wheel.get_existing_item(Key={'id': wheel_id})
    create_timestamp = get_utc_timestamp()

    participant = {
        'wheel_id': wheel_id,
        'id': get_uuid(),
        'name': body['name'],
        'url': body['url'],
        'created_at': create_timestamp,
        'updated_at': create_timestamp,
    }
    with choice_algorithm.wrap_participant_creation(wheel, participant):
        WheelParticipant.put_item(Item=participant)
    return participant
Esempio n. 25
0
    def transformer(self):
        self.event['eventTime'] = utils.get_current_date_time_iso8601_format()
        self.envelope['sendTime'] = utils.get_current_date_time_iso8601_format(
        )
        self.event['id'] = utils.get_uuid()
        user = self.change_user_in_event()
        self.change_course_info(user)
        self.change_session_info()
        self.change_duration_and_current_time()

        logging.debug(json.dumps(self.eventData, indent=4))
        logging.debug("-------------------------------------------")
        return self.eventData
Esempio n. 26
0
    def __init__(self, config_data, parent):
        self.type = 'Site'
        self.config = self
        self.config_parent = parent

        self.name = config_data.get('name', '{}.Site.{}'.format(parent.name, str(get_uuid())))
        self.seed_file = config_data.get('seed-file', parent.seed_file)
        self.logging_level = config_data.get('logging-level', parent.logging_level)
        self.cache_client = config_data.get('cache-client', parent.cache_client)

        self.vims = self._load_vims(config_data.get('vims', []))
        self.sdn_controllers = self._load_sdns(config_data.get('sdn-controllers', []))
        self.ssh_username_and_passwords = Config._load_ssh(config_data.get('ssh-credentials', []))
Esempio n. 27
0
 def get_luks_info(self, partition_path):
     mapper_path = ''
     mount_points = []
     mapper = '/dev/mapper'
     mapper_name = getoutput("ls %s | grep %s$" % (mapper, basename(partition_path)))[0]
     if not mapper_name:
         uuid = get_uuid(partition_path)
         if uuid:
             mapper_name = getoutput("ls %s | grep %s$" % (mapper, uuid))[0]
     if mapper_name:
         mapper_path = join(mapper, mapper_name)
     if mapper_path:
         mount_points = get_mount_points(mapper_path)
     return (mapper_path, mount_points[0])
Esempio n. 28
0
def judge():
    user_id = g.user.id

    data = g.data
    code = data['code']
    language = data['language']
    problem_id = data['problem_id']

    problem = Problem.query.filter_by(id=problem_id).first()
    if not problem:
        return jsonify(data=None, message='The problem not found'), 404

    if language == 'java':
        err, info = valid_java_format(code)
        if not err:
            return jsonify(data=None, message=info), 400

    user_path = 'e:/usr/pushy/{}'.format(user_id)

    if not os.path.exists(user_path):
        os.mkdir(user_path)

    with open('{}/{}'.format(user_path, get_file_name(language)),
              'w',
              encoding='utf-8') as f:
        f.write(code)

    task_id = get_uuid()
    task = JudgementTask(task_id=task_id,
                         problem_id=problem_id,
                         user_id=user_id,
                         language=language,
                         time_limit=problem.time_limit,
                         memory_limit=problem.memory_limit)
    submission = Submission(id=task_id,
                            user_id=user_id,
                            problem_id=problem_id,
                            language=language,
                            code=code)
    resp = submission.to_dict()
    db.session.add(submission)
    db.session.commit()

    logger.info("Send task => {}".format(task))
    rabbitmq.send(body=task.to_json_string(),
                  exchange='',
                  key='go-docker-judger')

    return jsonify(data=resp), 202
Esempio n. 29
0
def test_list_wheels(mock_dynamodb, mock_wheel_table):
    test_wheels = [{
        'id': get_uuid(),
        'name': 'Wheel ' + num
    } for num in ['0', '1']]

    with mock_wheel_table.batch_writer() as batch:
        for test_wheel in test_wheels:
            batch.put_item(Item=test_wheel)

    response = wheel.list_wheels({'body': {}})

    assert response['statusCode'] == 200
    assert 'Wheel 0' in response['body'] and 'Wheel 1' in response['body']
    assert json.loads(response['body'])['Count'] == len(test_wheels)
Esempio n. 30
0
    def user_phone_manage_add(self, user_info):
        rid = user_info.get('rid','')
        users = user_info.get('users','')
        user_list = []
        for user in users:
            uname = user.get('name','')
            phone_list = []
            mail_list = []
            if uname.strip == '':
                return (-1, 'failed')
            phones = user.get('phones','')
            uid = utils.get_uuid()
            curtime = utils.get_cur_time()
            ret = db.insert(utils.DB_TABLE_USER,uid=uid,rid=rid,name=uname,intime=curtime)
            print('ret:',ret)

            for phone in phones:   #insert into t_phone
                p = phone.get('phone','')
                if p.strip() != '':
                    pid = utils.get_uuid()
                    db.insert(utils.DB_TABLE_PHONE,pid=pid,uid=uid,phone=p,intime=curtime)
                    phone_dic = dict(pid=pid,uid=uid,phone=p,intime=curtime)
                    phone_list.append(phone_dic)
                    pass
            mails = user.get('mails','')
            for mail in mails:
                mail_str = mail.get('mail','')
                if mail_str.strip() != '':
                    mid = utils.get_uuid()
                    db.insert(utils.DB_TABLE_MAIL,mid=mid,uid=uid,mail=mail_str,intime=curtime)
                    mail_dic = dict(mid=mid,uid=uid, mail=mail_str,intime=curtime)
                    mail_list.append(mail_dic)
            user_dic = dict(uid=uid,name=uname,phones=phone_list,mails=mail_list,intime=curtime)
            user_list.append(user_dic)
        return (0,'success',rid,user_list)
        pass
Esempio n. 31
0
def test_list_participants(mock_dynamodb, mock_participant_table):
    participants = [{
        'id': get_uuid(),
        'wheel_id': WHEEL_ID,
        'name': name,
    } for name in ['Dan', 'Alexa']]

    with mock_participant_table.batch_writer() as batch:
        for participant in participants:
            batch.put_item(Item=participant)

    response = wheel_participant.list_participants({'body': {}, 'pathParameters': {'wheel_id': WHEEL_ID}})

    assert response['statusCode'] == 200
    assert 'Dan' in response['body'] and 'Alexa' in response['body']
    assert len(json.loads(response['body'])) == len(participants)
Esempio n. 32
0
def write_crypttab(device,
                   fs_type,
                   crypttab_path=None,
                   keyfile_path=None,
                   remove_device=False):
    #print(("++++ device=%s, fs_type=%s, crypttab_path=%s, keyfile_path=%s, remove_device=%s" % (device, fs_type, str(crypttab_path), str(keyfile_path), str(remove_device))))
    if crypttab_path is None or not '/' in crypttab_path:
        crypttab_path = '/etc/crypttab'
    device = device.replace('/mapper', '')

    if not exists(crypttab_path):
        with open(crypttab_path, 'w') as f:
            f.write(
                '# <target name>\t<source device>\t<key file>\t<options>\n')

    if keyfile_path is None or keyfile_path == '':
        keyfile_path = 'none'
    crypttab_uuid = "UUID=%s" % get_uuid(device)
    new_line = ''
    if not remove_device:
        swap = ''
        if fs_type == 'swap':
            swap = 'swap,'
        new_line = "%s %s %s %sluks,timeout=60\n" % (
            basename(device), crypttab_uuid, keyfile_path, swap)

    #print(("++++ new_line=%s" % new_line))

    # Create new crypttab contents
    cont = ''
    with open(crypttab_path, 'r') as f:
        cont = f.read()
    regexp = ".*\s{}\s.*".format(crypttab_uuid)
    matchObj = re.search(regexp, cont)
    if matchObj:
        cont = (re.sub(regexp, new_line, cont))
    else:
        if not remove_device:
            cont += new_line

    #print(("++++ cont=%s" % cont))

    # Save the new crypttab
    with open(crypttab_path, 'w') as f:
        f.write(cont)
Esempio n. 33
0
async def test_create_remove_probe(loop):
    name = naminator("probe")
    token = token_urlsafe(30)
    specs = {
        "repository": "cassinyio",
        "blueprint": "notebook:4018e4ee",
        "cpu": 1,
        "ram": 100,
        "service_type": "probe",
        "uuid": get_uuid().hex,
    }

    await Spawner.probe.create(name=name, user_id=1, specs=specs, token=token)
    service = await Spawner.get_service(name=name)
    assert service is not None
    await Spawner.probe.delete(name=name)
    service = await Spawner.get_service(name=name)
    assert service is None
Esempio n. 34
0
    def __init__(self, config_data, parent):
        self.type = 'Site'
        self.config = self
        self.config_parent = parent

        self.name = config_data.get(
            'name', '{}.Site.{}'.format(parent.name, str(get_uuid())))
        self.seed_file = config_data.get('seed-file', parent.seed_file)
        self.logging_level = config_data.get('logging-level',
                                             parent.logging_level)
        self.cache_client = config_data.get('cache-client',
                                            parent.cache_client)

        self.vims = self._load_vims(config_data.get('vims', []))
        self.sdn_controllers = self._load_sdns(
            config_data.get('sdn-controllers', []))
        self.ssh_username_and_passwords = Config._load_ssh(
            config_data.get('ssh-credentials', []))
Esempio n. 35
0
    async def post(self, payload: Mapping[str, Any]):
        """Create a new API."""
        user_id = payload['user_id']
        data = await self.request.json()

        data, errors = ApiSchema().load(data)
        if errors:
            log.debug(errors)
            return json_response({"error": errors}, status=400)

        event = {"uuid": get_uuid().hex, "user_id": user_id, "data": data}

        await streaming.publish("service.api.create", event)

        return json_response({
            "uuid":
            event['uuid'],
            "message":
            f"We are creating your api ({event['uuid']})."
        })
Esempio n. 36
0
 def __init__(self, fPath = '/var/log/tstorm/stress', fName = "tstorm-stress", report = True):
     t=datetime.datetime.now()
     ts=str(time.mktime(t.timetuple()))
     id = utils.get_uuid()
     #self.fpath = fPath + '/' + ts
     self.fpath = fPath + '/' + id
     if not os.path.isdir(self.fpath):
         os.makedirs(self.fpath)
     self.fname = fName + '_' + ts + '.log'
     self.log_file = ""
     self.report = report
     if not os.path.isdir(self.fpath):
         self.fpath = os.getcwd()
     if not os.path.isdir(self.fpath):
         sys.stderr.write ("in LogFile initialization\n")
         sys.stderr.write ("file path set to: <<" + self.fpath + ">>\n")
         sys.stderr.write ("This appears to not be a valid directory.")
         sys.stderr.write ("This is a serious internal error")
         fail ("Log file path must be valid")
     if self.report:
         self.log_file = open(os.path.join(self.fpath, self.fname), 'a+')
Esempio n. 37
0
 def __init__(self, expires=-1, user_id=0):
     self.id = utils.get_uuid()
     self.expires = expires
     self.user_id = user_id
Esempio n. 38
0
def _generate_uuid_token():
    return utils.get_uuid()
Esempio n. 39
0
def twitter_response(db, session):

    oauth_token = bottle.request.query.oauth_token
    oauth_verifier = bottle.request.query.oauth_verifier

    if not oauth_token == session['oauth_token']:
        return bottle.redirect('/')

    token = oauth2.Token(oauth_token, session['oauth_token_secret'])
    token.set_verifier(oauth_verifier)

    consumer = oauth2.Consumer(twitter_config['consumer_key'], twitter_config['consumer_secret'])
    client = oauth2.Client(consumer, token)

    resp, content = client.request(twitter_config['access_token_url'], "POST")
    if resp['status'] != '200':
        raise Exception("Invalid response %s." % resp['status'])
    access_token = dict(urlparse.parse_qsl(content))
    user_access_token = access_token['oauth_token']
    user_access_secret = access_token['oauth_token_secret']

    auth = OAuth1(twitter_config['consumer_key'], twitter_config['consumer_secret'],
                  user_access_token, user_access_secret)

    twitter_user_data = get(twitter_config['verify_credentials_url'], auth=auth).json()

    new_user_dict = dict(user_name=twitter_user_data['screen_name'],
                         real_name=twitter_user_data['name'],
                         password=str(get_uuid()),
                         avatar_url=twitter_user_data['profile_image_url_https'],
                         twitter_access_token=user_access_token,
                         twitter_access_secret=user_access_secret)

    # determine if this is a sign-in or a new user
    user_record_ids = User.get_all_ids(db)

    for user_record_id in user_record_ids:
        user = User(db, doc_id=user_record_id)
        if twitter_user_data['screen_name'] == user.data['user_name']:
            # this is a sign-in
            user_id = user.update(db, new_data=new_user_dict)

            session['user_id'] = user_id
            session['user_name'] = twitter_user_data['screen_name']
            session['real_name'] = twitter_user_data['name']
            session['twitter_access_token'] = user_access_token
            session['twitter_access_secret'] = user_access_secret
            session['avatar_url'] = twitter_user_data['profile_image_url_https']
            return bottle.redirect('/')

    # this is a new user

    new_user_id = User(db, data=new_user_dict)

    session['user_id'] = new_user_id
    session['user_name'] = new_user_dict['user_name']
    session['real_name'] = new_user_dict['real_name']
    session['twitter_access_token'] = user_access_token
    session['twitter_access_secret'] = user_access_secret
    session['avatar_url'] = twitter_user_data['profile_image_url_https']
    return bottle.redirect('/')
Esempio n. 40
0
def upload():
    # Get priority
    priority = int(request.form.get('priority', PRIORITY.medium))
    if priority not in PRIORITY.get_values():
        priority = PRIORITY.medium

    # Get output formats
    output_formats = request.form.get('output-formats', '')
    output_formats = list(set(
        filter(
            lambda format: format in app.config['ALLOWED_EXTENSIONS'],
            output_formats.split(';')
        )
    ))
    if not output_formats:
        return jsonify({'Error': 'Must provide valid output formats'}), 400

    # Get file (either directly or via URL)
    file = request.files.get('file')
    allowed_extensions = app.config['ALLOWED_EXTENSIONS']

    if file:
        if allowed_filename(file.filename, allowed_extensions):
            filename = secure_filename(file.filename).strip()[-FILE_NAME_LIMIT]
            local_path = os.path.join(app.config['UPLOAD_FOLDER'],
                                      timestamp_filename(filename))
            file.save(local_path)
        else:
            return jsonify({'Error': 'File format not allowed'}), 400
    else:
        fileURL = request.form.get('fileURL')
        if fileURL:
            filename = get_filename_from_url(fileURL)

            try:
                local_path = download_url(
                    fileURL, app.config['UPLOAD_FOLDER'], timestamp=True)
            except FileAccessDenied as fad:
                return jsonify({
                    'status': 'error',
                    'code': fad.status_code,
                    'message': fad.message
                }), 500

        else:
            return jsonify({'status': 'error',
                            'message': 'Unable to decode uploaded file'}), 500

    # Upload to remote and remove file from local
    remote_destination = os.path.join(app.config['REMOTE_INPUT_FOLDER'],
                                      get_uuid(), filename)
    upload_to_remote(remote_destination, local_path)
    os.remove(local_path)

    # Register the file for conversions and return docIds
    docIds = Conversion.register_file(filename, remote_destination,
                                      g.user, output_formats, priority)

    # Call request fetcher
    request_fetcher.delay()

    return jsonify({'status': STATUS.introduced, 'doc_ids': docIds})
Esempio n. 41
0
 def get_key_for_doc_type(cls):
     return cls.document_prefix + '_' + str(get_uuid())
Esempio n. 42
0
 def new_session_id(self):
     uid = get_uuid()
     self.session_hash = 'session_%s' % str(uid)
     self.set_cookie(uid.hex)