Пример #1
def deploy(name, directory, store_deployed_fact=True, temp_output=False):
    directory = abspath(directory)
    executable = get_unique_executable(directory)
    if not executable:
        stderr.write("No unique executable in %s for %s\n" % (directory, name))
        return False

    if temp_output is True:
        output_directory = mkdtemp(prefix=name)
        output_directory = directory

    stdout_file = open_output_file(output_directory, name, 'stdout')
    stderr_file = open_output_file(output_directory, name, 'stderr')

    process = Popen(executable, cwd=directory,
                    stdout=stdout_file, stderr=stderr_file)

    content = {'name': name,
               'location': getfqdn(),
               'identifier': process.pid,
               'stdout_file': stdout_file.name,
               'stderr_file': stderr_file.name}

    if store_deployed_fact is True:
        if not store_fact('kropotkin', 'component_deployed', content):
            stderr.write('Cannot store component_deployed fact for %s\n' %name)
    return content
Пример #2
def create_factspace(name, directory):
    if not directory:
        directory = mkdtemp()

    content = {'name': name, 'directory': directory}
    if not store_fact('kropotkin', 'factspace', content):
        raise Exception("Cannot store factspace fact")

    return directory
Пример #3
def deploy(name, directory, env={}):
    executable = get_unique_executable(directory)
    if not executable:
        stderr.write("No unique executable in %s for %s\n" % (directory, name))
        return False

    env = env.copy()
    inherit(env, environ, ['KROPOTKIN_QUEUE', 'KROPOTKIN_URL',
                           'TEMP', 'TMP', 'TMPDIR'])
    process = Popen(executable, cwd=directory, env=env)
    content = {'name': name, 'location': getfqdn(),
               'identifier': process.pid}
    if 'KROPOTKIN_URL' in environ:
        if not store_fact('kropotkin', 'component_deployed', content):
            stderr.write('Cannot store component_deployed fact for %s\n' %name)
    return process.pid
Пример #4
def check_log_file(log_file):
    current_size = getsize(log_file.path)
    bytes_to_read = min(current_size - log_file.position, MAX_FACT_SIZE)
    if bytes_to_read == 0:

    with open(log_file.path, 'r') as f:
        f.seek(-bytes_to_read, SEEK_END)
        log_data = f.read(MAX_FACT_SIZE)
        content = {'component': log_file.component,
                   'type':      log_file.type,
                   'file':      log_file.path,
                   'data':      log_data}
        if not store_fact('kropotkin', 'log_data', content):
            stderr.write("Could not store log_data\n")
        log_file.position += len(log_data)
Пример #5
def publish(location):
    if isdir(location):
        files = [join(location, f) for f in listdir(location) if not isdir(f)]
        name = basename(location)
        language = determine_language(files)
        bytes = archive(files)
        content_type = 'component-tar'
        name = basename(location)
        language = language_type(location)
        with open(location) as f:
            bytes = b64encode(f.read())
        content_type = language
    content = {'name': name, 'language': language,
               'content_type': content_type, 'bytes': bytes}
    if not store_fact('kropotkin', 'component', content):
        raise Exception("Cannot store component fact")
Пример #6
              'keys': dumps(['name', 'directory']),
              'translation': 'Factspace %(name)s requested'},
            {'type': 'factspace',
              'keys': dumps(['name', 'directory']),
              'translation': 'Factspace %(name)s created in %(directory)s'},
            {'type': 'library_available',
             'keys': dumps(['directory', 'language']),
             'translation': 'Library available in %(directory)s, ' \
                          + 'language %(language)s'},
            {'type': 'subscription',
             'keys': dumps(['type', 'confidence', 'queue']),
             'translation': 'Subscription to %(confidence)ss ' \
                          + 'of type %(type)s using queue %(queue)s'}]

for e in elements:
    if not kropotkin.store_fact('kropotkin', 'constitution_element', e):
        fail_and_exit('Could not store %s' % e)

content = {'name': 'http', 'location': gethostname(), 'identifier': http_pid}
if not kropotkin.store_fact('kropotkin', 'component_deployed', content):
    fail_and_exit('Cannot store component_deployed fact for http')

for c in dirs_in('core'):
    deploy(c, join('core', c), env)

sleep(1) # Hack to let publisher start

for c in listdir('components'):
    component_location = abspath(join('components', c))
    if not kropotkin.store_fact('kropotkin', 'component_available',
                                {'location': component_location}):
Пример #7
    original_dir = fact['directory']
    language = fact['language']

    build_dir = mkdtemp(prefix='library-build-%s' % basename(original_dir))
    output_dir = mkdtemp(prefix='library-%s' % basename(original_dir))
    copy_all(original_dir, build_dir)

    executable = get_unique_executable(build_dir)
    if not executable:
        stderr.write("Cannot locate unique executable in %s\n" % build_dir)

    process = Popen([executable, output_dir], cwd=build_dir)
    files = listdir(output_dir)
    if len(files) != 1:
        stderr.write('Library %s output %d files, should be exactly 1\n' \
                         % (basename(original_dir), len(files)))
    output_file = join(output_dir, files[0])

    with open(output_file) as f:
        bytes = b64encode(f.read())
    content = {'name': basename(output_file),
               'language': language,
               'content_type': MODULE_TYPES[language],
               'bytes': bytes}
    if not store_fact('kropotkin', 'component', content):
        stderr.write('Could not store component for %s\n' % name)
Пример #8
from base64 import b64encode
from hashlib import sha512
from json import loads
from kropotkin import get_next_fact, store_fact, subscribe
from os import urandom

def generate_salt():
    return b64encode(urandom(24))

def create_hash(salt, password):
    return sha512(salt+password).hexdigest()

subscribe('whooshingby', 'fact', 'registration_request')
while True:
    request = get_next_fact('whooshingby', 'registration_request')
    if not request:

    name = request['name']
    password = request['password']
    salt = generate_salt()
    hash = create_hash(salt, password)
    content = {'name': name, 'salt': salt, 'hash': hash}
    if not store_fact('whooshingby', 'user', content):
        stderr.write('Could not store user fact, name %s\n' % name)
Пример #9
from core.deployer.deployer import deploy

KROPOTKIN_URL="http://localhost:%s" % PORT

except KeyError:
    KROPOTKIN_QUEUE=abspath(join('bin', 'boringq'))

if len(argv) > 1 and argv[1] == 'stop':
    print 'Stopping Kropotkin instance running on %s' % KROPOTKIN_URL
    content = {'location': 'all', 'identifier': 'all'}
    if not kropotkin.store_fact('kropotkin', 'stop_requested', content):
        fail_and_exit('Could not store stop_requested fact')
    print 'Kropotkin stopping'

except KeyError:

if 'KROPOTKIN_CAN_BUILD' not in environ:
    environ['KROPOTKIN_CAN_BUILD'] = 'python,javascript'
can_build = environ['KROPOTKIN_CAN_BUILD'].split(',')

print "----Environment variables----"
Пример #10
            {'type': 'registration_request',
             'keys': dumps(['name', 'password']),
             'translation': 'User %(name)s requested registration',
             'options': 'memory_only'},
            {'type': 'user',
              'keys': dumps(['name', 'salt', 'hash']),
              'translation': 'User %(name)s is registered',
              'options': ''},
            {'type': 'user_nonce',
             'keys': dumps(['name', 'nonce', 'expiry']),
             'translation': 'User %(name)s can be identified by %(nonce)s' \
                          + 'until %(expiry)s',
             'options': ''}]

for e in elements:
    if not store_fact('whooshingby', 'constitution_element', e):
        fail_and_exit("Could not store constitution element fact")

reward_percentages = get_newest_fact('whooshingby', 'reward_percentages', {})
if not reward_percentages:
    with open('rewards.json') as f:
        reward_percentages = f.read()
        if not store_fact('whooshingby', 'reward_percentages',
                          {'percentages': reward_percentages}):
            fail_and_exit("Could not store reward percentages")

judge_percentages = get_newest_fact('whooshingby', 'judge_percentages', {})
if not judge_percentages:
    if not store_fact('whooshingby', 'judge_percentages',
                      {'percentages': '[["python", 100], ["ruby", 0]]'}):
        fail_and_exit("Could not store judge percentages")
Пример #11
def compare_opinions(opinions, expected_opinions):
    if len(opinions) < expected_opinions:
        return False
    names = set([opinion['name'] for opinion in opinions])
    return len(names) == 1

def classify_opinion(opinion):
    return opinion['task_id']

subscribe_sets('whooshingby', 'opinion', 'reward', classify_opinion, 2, 1)
while True:
    opinions = get_next_set('whooshingby', 'opinion', 'reward')

    if not compare_opinions(opinions, 2):
        if not store_fact('whooshingby', 'opinion_difference',
                          {'opinions': dumps(opinions)}):
            print "Could not store opinion difference fact"

    percentages_fact = get_newest_fact('whooshingby', 'judge_percentages', {})
    percentages = loads(percentages_fact['percentages'])
    random_value = randrange(100)
    source = choose(random_value, percentages, default='python')

    to_promote = next((o for o in opinions if o['source'] == source), None)
    if to_promote:
        for key in to_promote.keys():
            if key.startswith('kropotkin_'):
                del to_promote[key]
        if not store_fact('whooshingby', 'reward', to_promote):
            print "Could not store reward fact"