# Path: yithlibraryserver/backups/email.py
# def send_passwords(request, user, preferences_link, backups_link):
#     passwords = get_user_passwords(request.db, user)
#     if not passwords:
#         return False
# 
#     context = {
#         'user': user,
#         'preferences_link': preferences_link,
#         'backups_link': backups_link,
#     }
# 
#     today = request.date_service.today()
#     attachment = Attachment(get_backup_filename(today),
#                             "application/yith",
#                             compress(passwords))
# 
#     send_email(
#         request,
#         'yithlibraryserver.backups:templates/email_passwords',
#         context,
#         "Your Yith Library's passwords",
#         [user['email']],
#         attachments=[attachment],
#     )
# 
#     return True
# 
# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/scripts/utils.py
# def safe_print(value):
#     if PY3:  # pragma: no cover
#         print(value)
#     else:  # pragma: no cover
#         print(value.encode('utf-8'))
# 
# def setup_simple_command(name, description):
#     usage = name + ": %prog config_uri"
#     parser = optparse.OptionParser(
#         usage=usage,
#         description=textwrap.dedent(description)
#         )
#     options, args = parser.parse_args(sys.argv[1:])
#     if not len(args) >= 1:
#         safe_print('You must provide at least one argument')
#         return 2
#     config_uri = args[0]
#     env = bootstrap(config_uri)
#     settings, closer = env['registry'].settings, env['closer']
# 
#     return settings, closer, env, args[1:]
# 
# Path: yithlibraryserver/scripts/utils.py
# def get_user_display_name(user):
#     return '%s %s <%s>' % (user.get('first_name', ''),
#                            user.get('last_name', ''),
#                            user.get('email', ''))


import transaction
from yithlibraryserver.backups.email import send_passwords
from yithlibraryserver.compat import urlparse
from yithlibraryserver.scripts.utils import safe_print, setup_simple_command
from yithlibraryserver.scripts.utils import get_user_display_name
        for user in request.db.users.find({
                'email': email,
                }).sort('date_joined'):
            yield user


def send_backups_via_email():
    result = setup_simple_command(
        "send_backups_via_email",
        "Report information about users and their passwords.",
        )
    if isinstance(result, int):
        return result
    else:
        settings, closer, env, args = result

    try:
        request = env['request']

        if len(args) == 0:
            user_iterator = get_all_users(request)
        else:
            user_iterator = get_selected_users(request, *args)

        tx = transaction.begin()

        public_url_root = settings['public_url_root']
        preferences_link = urlparse.urljoin(
            public_url_root,
            request.route_path('user_preferences'))

# Path: yithlibraryserver/backups/email.py
# def send_passwords(request, user, preferences_link, backups_link):
#     passwords = get_user_passwords(request.db, user)
#     if not passwords:
#         return False
# 
#     context = {
#         'user': user,
#         'preferences_link': preferences_link,
#         'backups_link': backups_link,
#     }
# 
#     today = request.date_service.today()
#     attachment = Attachment(get_backup_filename(today),
#                             "application/yith",
#                             compress(passwords))
# 
#     send_email(
#         request,
#         'yithlibraryserver.backups:templates/email_passwords',
#         context,
#         "Your Yith Library's passwords",
#         [user['email']],
#         attachments=[attachment],
#     )
# 
#     return True
# 
# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/scripts/utils.py
# def safe_print(value):
#     if PY3:  # pragma: no cover
#         print(value)
#     else:  # pragma: no cover
#         print(value.encode('utf-8'))
# 
# def setup_simple_command(name, description):
#     usage = name + ": %prog config_uri"
#     parser = optparse.OptionParser(
#         usage=usage,
#         description=textwrap.dedent(description)
#         )
#     options, args = parser.parse_args(sys.argv[1:])
#     if not len(args) >= 1:
#         safe_print('You must provide at least one argument')
#         return 2
#     config_uri = args[0]
#     env = bootstrap(config_uri)
#     settings, closer = env['registry'].settings, env['closer']
# 
#     return settings, closer, env, args[1:]
# 
# Path: yithlibraryserver/scripts/utils.py
# def get_user_display_name(user):
#     return '%s %s <%s>' % (user.get('first_name', ''),
#                            user.get('last_name', ''),
#                            user.get('email', ''))


# Path: yithlibraryserver/scripts/backups.py
import transaction
from yithlibraryserver.backups.email import send_passwords
from yithlibraryserver.compat import urlparse
from yithlibraryserver.scripts.utils import safe_print, setup_simple_command
from yithlibraryserver.scripts.utils import get_user_display_name

        for user in request.db.users.find({
                'email': email,
                }).sort('date_joined'):
            yield user


def send_backups_via_email():
    result = setup_simple_command(
        "send_backups_via_email",
        "Report information about users and their passwords.",
        )
    if isinstance(result, int):
        return result
    else:
        settings, closer, env, args = result

    try:
        request = env['request']

        if len(args) == 0:
            user_iterator = get_all_users(request)
        else:
            user_iterator = get_selected_users(request, *args)

        tx = transaction.begin()

        public_url_root = settings['public_url_root']
        preferences_link = urlparse.urljoin(
            public_url_root,
            request.route_path('user_preferences'))

        backups_link = urlparse.urljoin(