Ejemplo n.º 1
0
def main(args=None):
    parser = argparse.ArgumentParser(description='Take properties files and '
                                     'sync them with a Kinto collection.')

    parser.add_argument('-s', '--host', help='Kinto Server',
                        type=str, default=DEFAULT_KINTO_SERVER)

    parser.add_argument('-u', '--auth', help='BasicAuth user:pass',
                        type=str, default=DEFAULT_USER_NAME)

    parser.add_argument('-b', '--bucket',
                        help='Bucket name, usually the app name',
                        type=str)

    parser.add_argument('-c', '--collection',
                        help='Collection name, usually the locale code',
                        type=str)

    parser.add_argument('files', metavar='N', type=str, nargs='+',
                        help='A list of properties file for the locale.')

    parser.add_argument('--verbose', '-v',
                        help='Display status',
                        dest='verbose',
                        action='store_true')

    args = vars(parser.parse_args(args=args))

    verbose = args['verbose']

    if verbose:
        logger.setLevel(logging.INFO)

    files = []
    for f in args['files']:
        if os.path.exists(f):
            files.append(os.path.abspath(f))
            logger.log(COMMAND, '%s: ✓' % os.path.abspath(f))
        else:
            logger.error('%s: ✗' % os.path.abspath(f))

    auth = args.get('auth')

    if auth:
        # Ask for the user password if needed
        auth = tuple(auth.split(':', 1))
        if len(auth) < 2:
            email = auth[0]
            password = getpass.getpass('Please enter a password for %s: '
                                       % email)
            auth = (email, password)

    synchronize(files,
                kinto_options={
                    'server': args['host'],
                    'bucket_name': args['bucket'],
                    'collection_name': args['collection'],
                    'auth': auth,
                    'permissions': COLLECTION_PERMISSIONS
                })
def main(args=None):
    parser = argparse.ArgumentParser(description='Take properties files and '
                                     'sync them with a Kinto collection.')

    parser.add_argument('-s',
                        '--host',
                        help='Kinto Server',
                        type=str,
                        default=DEFAULT_KINTO_SERVER)

    parser.add_argument('-u',
                        '--auth',
                        help='BasicAuth user:pass',
                        type=str,
                        default=DEFAULT_USER_NAME)

    parser.add_argument('-b',
                        '--bucket',
                        help='Bucket name, usually the app name',
                        type=str)

    parser.add_argument('-c',
                        '--collection',
                        help='Collection name, usually the locale code',
                        type=str)

    parser.add_argument('files',
                        metavar='N',
                        type=str,
                        nargs='+',
                        help='A list of properties file for the locale.')

    parser.add_argument('--verbose',
                        '-v',
                        help='Display status',
                        dest='verbose',
                        action='store_true')

    args = vars(parser.parse_args(args=args))

    verbose = args['verbose']

    if verbose:
        logger.setLevel(logging.INFO)

    files = []
    for f in args['files']:
        if os.path.exists(f):
            files.append(os.path.abspath(f))
            logger.log(COMMAND, '%s: ✓' % os.path.abspath(f))
        else:
            logger.error('%s: ✗' % os.path.abspath(f))

    auth = args.get('auth')

    if auth:
        # Ask for the user password if needed
        auth = tuple(auth.split(':', 1))
        if len(auth) < 2:
            email = auth[0]
            password = getpass.getpass('Please enter a password for %s: ' %
                                       email)
            auth = (email, password)

    synchronize(files,
                kinto_options={
                    'server': args['host'],
                    'bucket_name': args['bucket'],
                    'collection_name': args['collection'],
                    'auth': auth,
                    'permissions': COLLECTION_PERMISSIONS
                })
Ejemplo n.º 3
0
def synchronize(files, kinto_options):
    logger.log(COMMAND, 'Working on %r' % kinto_options['server'])
    logger.debug('Reading data from files')
    l10n = L10nRecords(files)
    kinto = KintoRecords(kinto_options)
    to_delete = []
    to_update = []
    to_create = []

    logger.log(COMMAND, 'Syncing to %s/buckets/%s/collections/%s/records' % (
        kinto_options['server'].rstrip('/'),
        kinto_options['bucket_name'],
        kinto_options['collection_name']))

    # looking at kinto to list records
    # to delete or to update
    for record in kinto.records:
        l10n_rec = l10n.find(record['id'])
        if l10n_rec is None:
            to_delete.append(record)
        else:
            if not same_record(FIELDS, l10n_rec, record):
                to_update.append(l10n_rec)

    # new records ?
    for record in l10n.records:
        kinto_rec = kinto.find(record['id'])
        if not kinto_rec:
            to_create.append(record)

    logger.log(COMMAND, '- %d records to create.' % len(to_create))
    logger.log(COMMAND, '- %d records to update.' % len(to_update))
    logger.log(COMMAND, '- %d records to delete.' % len(to_delete))

    if len(to_delete) > 0:
        logger.log(COMMAND, 'You may want to delete the following keys:')

    for record in to_delete:
        logger.log(COMMAND, '- %s: %s=%s' % (record['id'],
                                     record['key'],
                                     record['value']))

        # XXX: Add an option to ask for deletion
        # try:
        #     kinto.delete(record)
        # except KintoException as e:
        #     raise SynchronizationError(e.response.content)

    for record in to_create + to_update:
        try:
            kinto.create(record)
        except KintoException as e:
            raise SynchronizationError(e.response.content)

    print('Done!')