def get_settings_site(annroot, userhome, options):
    """
    Get settings and site data based on command line options provided

    returns:

        (status, settings, site)

    where 'settings' and/or 'site' are None if not found.
    """
    status = am_errors.AM_SUCCESS
    settings = am_get_settings(annroot, userhome, options)
    site = None
    if not settings:
        print("Settings not found (%s)" % (options.configuration),
              file=sys.stderr)
        status = am_errors.AM_NOSETTINGS
    if status == am_errors.AM_SUCCESS:
        sitesettings = am_get_site_settings(annroot, userhome, options)
        if not sitesettings:
            print("Site settings not found (%s)" % (options.configuration),
                  file=sys.stderr)
            status = am_errors.AM_NOSETTINGS
    if status == am_errors.AM_SUCCESS:
        site = am_get_site(sitesettings)
    return (status, settings, site)
Exemple #2
0
def am_deleteuser(annroot, userhome, options):
    """
    Delete Annalist/Django user account.  

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    prompt_prefix = "Delete user"
    if len(options.args) > 1:
        print("Unexpected arguments for %s: (%s)"%(options.command, " ".join(options.args)), file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    #
    from django.contrib.auth.models import User     # import deferred until after sitesettings import
    userqueryset = User.objects.filter(username=user_name)
    if not userqueryset:
        print("User %s does not exist"%user_name, file=sys.stderr)
        return am_errors.AM_USERNOTEXISTS
    userqueryset.delete()
    site = am_get_site(sitesettings)
    delete_user_permissions(site, user_name)
    return am_errors.AM_SUCCESS
Exemple #3
0
def am_setpublicpermissions(annroot, userhome, options):
    """
    Set site-wide default permissions for unauthenticated public access

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    prompt_prefix = "Public access "
    if len(options.args) > 1:
        print("Unexpected arguments for %s: (%s)"%(options.command, " ".join(options.args)), file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_permissions = get_user_permissions(options, 0, prompt_prefix)
    user_details = (
        { 'name':       "_unknown_user_perms"
        , 'uri':        "annal:User/_unknown_user_perms"
        , 'label':      "Unknown user"
        , 'comment':    "Permissions for unauthenticated user."
        })
    status = create_site_permissions(sitesettings, user_details, user_permissions)
    return status
Exemple #4
0
def am_setuserpermissions(annroot, userhome, options):
    """
    Set Annalist permissions for designated user

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    prompt_prefix = "User "
    if len(options.args) > 1:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    user_details = read_django_user_details(user_name)
    if not user_details:
        print("User %s does not exist" % user_name, file=sys.stderr)
        return am_errors.AM_USERNOTEXISTS
    user_permissions = get_user_permissions(options, 0, prompt_prefix)
    status = create_site_permissions(sitesettings, user_details,
                                     user_permissions)
    return status
Exemple #5
0
def am_setpublicpermissions(annroot, userhome, options):
    """
    Set site-wide default permissions for unauthenticated public access

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    prompt_prefix = "Public access "
    if len(options.args) > 1:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_permissions = get_user_permissions(options, 0, prompt_prefix)
    user_details = ({
        'name': "_unknown_user_perms",
        'uri': "annal:User/_unknown_user_perms",
        'label': "Unknown user",
        'comment': "Permissions for unauthenticated user."
    })
    status = create_site_permissions(sitesettings, user_details,
                                     user_permissions)
    return status
Exemple #6
0
def am_deleteuser(annroot, userhome, options):
    """
    Delete Annalist/Django user account.  

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    prompt_prefix = "Delete user"
    if len(options.args) > 1:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    #
    from django.contrib.auth.models import User  # import deferred until after sitesettings import
    userqueryset = User.objects.filter(username=user_name)
    if not userqueryset:
        print("User %s does not exist" % user_name, file=sys.stderr)
        return am_errors.AM_USERNOTEXISTS
    userqueryset.delete()
    site = am_get_site(sitesettings)
    delete_user_permissions(site, user_name)
    return am_errors.AM_SUCCESS
Exemple #7
0
def am_updateadminuser(annroot, userhome, options):
    """
    Update existing Django user to admin status

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    # see:
    #   https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#django.contrib.auth.models.User
    prompt_prefix = "Update user"
    if len(options.args) > 1:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    django_user = read_django_user(user_name)
    if not django_user:
        print("User %s does not exist" % user_name, file=sys.stderr)
        return am_errors.AM_USERNOTEXISTS
    #@@@@@@@@
    # # @@ read_django_user_details
    # from django.contrib.auth.models import User     # import deferred until after sitesettings import
    # userqueryset = User.objects.filter(username=user_name)
    # if not userqueryset:
    #     print("User %s does not exist"%user_name, file=sys.stderr)
    #     return am_errors.AM_USERNOTEXISTS
    # # Have all the details - now update the user in the Django user database
    # # see:
    # #   https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#django.contrib.auth.models.User
    # #   https://docs.djangoproject.c om/en/1.7/ref/contrib/auth/#manager-methods
    # user = userqueryset[0]
    #@@@@@@@@
    django_user.is_staff = True
    django_user.is_superuser = True
    django_user.save()
    # Create site permissions record for admin user
    #@@@@@@@@
    # user_details = (
    #     { 'name':       user_name
    #     , 'email':      user.email
    #     , 'uri':        "mailto:%s"%user.email
    #     , 'first_name': user.first_name
    #     , 'last_name':  user.last_name
    #     })
    #@@@@@@@@
    user_details = make_django_user_details(user_name, django_user)
    status = create_site_permissions(
        sitesettings, user_details,
        ["VIEW", "CREATE", "UPDATE", "DELETE", "CONFIG", "ADMIN"])
    return status
Exemple #8
0
def am_createsite(annroot, userhome, options):
    """
    Create Annalist empty site data.

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    status       = am_errors.AM_SUCCESS
    sitesettings = am_get_site_settings(annroot, userhome, options) 
    if not sitesettings:
        print("Settings not found (%s)"%(options.configuration), file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print(
            "Unexpected arguments for %s: (%s)"%
            (options.command, " ".join(options.args)), 
            file=sys.stderr
            )
        return am_errors.AM_UNEXPECTEDARGS
    site_layout = layout.Layout(sitesettings.BASE_DATA_DIR)
    sitebasedir = site_layout.SITE_PATH
    sitebaseurl = "/annalist/"     # @@TODO: figure more robust way to define this
    # --- If old site exists and --force option given, remove it
    if os.path.exists(os.path.join(sitebasedir, site_layout.SITEDATA_DIR)):
        if options.force:
            print("Removing old Annalist site at %s"%(sitebasedir))
            log.info("rmtree: %s"%(sitebasedir))
            removetree(sitebasedir)
        else:
            print(
                "Old data already exists at %s (use --force or -f to overwrite)"%
                (sitebasedir), file=sys.stderr
                )
            return am_errors.AM_EXISTS
    # --- Initialize empty site data in target directory
    print("Initializing Annalist site in %s"%(sitebasedir))
    site = Site.create_empty_site_data(
        sitebaseurl, sitebasedir,
        label="Annalist site (%s configuration)"%options.configuration, 
        description="Annalist %s site metadata and site-wide values."%options.configuration
        )
    sitedata = site.site_data_collection()
    Site.create_site_readme(site)
    site_data_src = os.path.join(annroot, "annalist/data/sitedata")     # @@TODO: more robust definition
    site_data_tgt, site_data_file = sitedata._dir_path()
    print("Copy Annalist site data")
    print("from %s"%site_data_src)
    for sdir in layout.COLL_DIRS:
        print("- %s -> %s"%(sdir, site_data_tgt))
        Site.replace_site_data_dir(sitedata, sdir, site_data_src)
    print("Generating %s"%(site_layout.SITEDATA_CONTEXT_DIR))
    sitedata.generate_coll_jsonld_context()
    print("Now run 'annalist-manager initialize' to create site admin database")
    return status
Exemple #9
0
def am_updateadminuser(annroot, userhome, options):
    """
    Update existing Django user to admin status

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    # see:
    #   https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#django.contrib.auth.models.User
    prompt_prefix = "Update user"
    if len(options.args) > 1:
        print("Unexpected arguments for %s: (%s)"%(options.command, " ".join(options.args)), file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    #
    from django.contrib.auth.models import User     # import deferred until after sitesettings import
    userqueryset = User.objects.filter(username=user_name)
    if not userqueryset:
        print("User %s does not exist"%user_name, file=sys.stderr)
        return am_errors.AM_USERNOTEXISTS
    # Have all the details - now update the user in the Django user database
    # see:
    #   https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#django.contrib.auth.models.User
    #   https://docs.djangoproject.c om/en/1.7/ref/contrib/auth/#manager-methods
    user = userqueryset[0]
    user.is_staff     = True
    user.is_superuser = True
    user.save()
    # Create site permissions record for admin user
    user_details = (
        { 'name':       user_name
        , 'email':      user.email
        , 'uri':        "mailto:%s"%user.email
        , 'first_name': user.first_name
        , 'last_name':  user.last_name
        })
    status = create_site_permissions(
        sitesettings, user_details, 
        ["VIEW", "CREATE", "UPDATE", "DELETE", "CONFIG", "ADMIN"]
        )
    return status
Exemple #10
0
def am_defaultadminuser(annroot, userhome, options):
    """
    Create default Annalist/Django admin/superuser account.  

    Creates an admin accoubnt with default values:
        username:   admin
        email:      admin@localhost
        firstname:  Admin
        lastname:   User

    Prompts for password as before

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    default_admin = ({
        'name': "admin",
        'email': "admin@localhost",
        'uri': "mailto:admin@localhost",
        'first_name': "Admin",
        'last_name': "User"
    })
    print("Creating user %(name)s" % default_admin, file=sys.stderr)
    status = create_django_user("superuser", default_admin)
    if status == am_errors.AM_SUCCESS:
        status = create_site_permissions(
            sitesettings, default_admin,
            ["VIEW", "CREATE", "UPDATE", "DELETE", "CONFIG", "ADMIN"])
    return status
Exemple #11
0
def am_defaultadminuser(annroot, userhome, options):
    """
    Create default Annalist/Django admin/superuser account.  

    Creates an admin accoubnt with default values:
        username:   admin
        email:      admin@localhost
        firstname:  Admin
        lastname:   User

    Prompts for password as before

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)"%(options.command, " ".join(options.args)), file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    default_admin = (
        { 'name':       "admin"
        , 'email':      "admin@localhost"
        , 'uri':        "mailto:admin@localhost"
        , 'first_name': "Admin"
        , 'last_name':  "User"
        })
    print("Creating user %(name)s"%default_admin, file=sys.stderr)
    status = create_django_user("superuser", default_admin)
    if status == am_errors.AM_SUCCESS:
        status = create_site_permissions(
            sitesettings, default_admin, 
            ["VIEW", "CREATE", "UPDATE", "DELETE", "CONFIG", "ADMIN"]
            )
    return status
Exemple #12
0
def am_createlocaluser(annroot,
                       userhome,
                       options,
                       prompt_prefix="Local user",
                       user_type="other",
                       user_perms=["VIEW"]):
    """
    Create Annalist/Django local user account.

    annroot         is the root directory for theannalist software installation.
    userhome        is the home directory for the host system user issuing the command.
    options         contains options parsed from the command line.
    prompt_prefix   string used in prompts to solicit user details.
    user_type       "superuser", "staff" or "other".  
                    ("staff" can access django admin site, but doesn't bypass permissions.)

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    if len(options.args) > 4:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    #
    from django.contrib.auth.models import User  # import deferred until after sitesettings import
    if User.objects.filter(username=user_name):
        print("User %s already exists" % user_name, file=sys.stderr)
        return am_errors.AM_USEREXISTS
    user_details = get_user_details(user_name, options, prompt_prefix)
    status = create_django_user(user_type, user_details)
    if status == am_errors.AM_SUCCESS:
        status = create_site_permissions(sitesettings, user_details,
                                         user_perms)
    return status
Exemple #13
0
def _x_am_createadminuser(annroot, userhome, options):
    """
    Create Annalist/Django admin/superuser account.  

    Once created, this can be used to create additional users through the 
    site 'admin' link, and can also create collections and

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    prompt_prefix = "Admin user"
    if len(options.args) > 4:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    #
    from django.contrib.auth.models import User  # import deferred until after sitesettings import
    if User.objects.filter(username=user_name):
        print("User %s already exists" % user_name, file=sys.stderr)
        return am_errors.AM_USEREXISTS
    user_details = get_user_details(user_name, options, prompt_prefix)
    status = create_django_user("superuser", user_details)
    if status == am_errors.AM_SUCCESS:
        status = create_site_permissions(
            sitesettings, user_details,
            ["VIEW", "CREATE", "UPDATE", "DELETE", "CONFIG", "ADMIN"])
    return status
Exemple #14
0
def am_createadminuser(annroot, userhome, options):
    """
    Create Annalist/Django admin/superuser account.  

    Once created, this can be used to create additional users through the 
    site 'admin' link, and can also create collections and

    annroot     is the root directory for theannalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    prompt_prefix = "Admin user"
    if len(options.args) > 4:
        print("Unexpected arguments for %s: (%s)"%(options.command, " ".join(options.args)), file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        return am_errors.AM_NOSETTINGS
    user_name = get_user_name(options, prompt_prefix)
    #
    from django.contrib.auth.models import User     # import deferred until after sitesettings import
    if User.objects.filter(username=user_name):
        print("User %s already exists"%user_name, file=sys.stderr)
        return am_errors.AM_USEREXISTS
    user_details = get_user_details(user_name, options, prompt_prefix)
    status = create_django_user("superuser", user_details)
    if status == am_errors.AM_SUCCESS:
        status = create_site_permissions(
            sitesettings, user_details, 
            ["VIEW", "CREATE", "UPDATE", "DELETE", "CONFIG", "ADMIN"]
            )
    return status
def get_settings_site(annroot, userhome, options):
    """
    Get settings and site data based on command line options provided

    returns:

        (status, settings, site)

    where 'settings' and/or 'site' are None if not found.
    """
    status   = am_errors.AM_SUCCESS
    settings = am_get_settings(annroot, userhome, options)
    site     = None
    if not settings:
        print("Settings not found (%s)"%(options.configuration), file=sys.stderr)
        status = am_errors.AM_NOSETTINGS
    if status == am_errors.AM_SUCCESS:
        sitesettings = am_get_site_settings(annroot, userhome, options)
        if not sitesettings:
            print("Site settings not found (%s)"%(options.configuration), file=sys.stderr)
            status = am_errors.AM_NOSETTINGS
    if status == am_errors.AM_SUCCESS:
        site        = am_get_site(sitesettings)
    return (status, settings, site)
def am_createsite(annroot, userhome, options):
    """
    Create Annalist empty site data.

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    status = am_errors.AM_SUCCESS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        print("Settings not found (%s)" % (options.configuration),
              file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    site_layout = layout.Layout(sitesettings.BASE_DATA_DIR)
    sitebasedir = site_layout.SITE_PATH
    sitebaseurl = "/annalist/"  # @@TODO: figure more robust way to define this
    # --- If old site exists and --force option given, remove it
    if os.path.exists(os.path.join(sitebasedir, site_layout.SITEDATA_DIR)):
        if options.force:
            print("Removing old Annalist site at %s" % (sitebasedir))
            log.info("rmtree: %s" % (sitebasedir))
            removetree(sitebasedir)
        else:
            print(
                "Old data already exists at %s (use --force or -f to overwrite)"
                % (sitebasedir),
                file=sys.stderr)
            return am_errors.AM_EXISTS
    # --- Initialize empty site data in target directory
    print("Initializing Annalist site in %s" % (sitebasedir))
    site = Site.create_empty_site_data(
        sitebaseurl,
        sitebasedir,
        label="Annalist site (%s configuration)" % options.configuration,
        description="Annalist %s site metadata and site-wide values." %
        options.configuration)
    sitedata = site.site_data_collection()
    Site.create_site_readme(site)
    site_data_src = os.path.join(
        annroot, "annalist/data/sitedata")  # @@TODO: more robust definition
    site_data_tgt, site_data_file = sitedata._dir_path()
    print("Copy Annalist site data")
    print("from %s" % site_data_src)
    for sdir in layout.COLL_DIRS:
        print("- %s -> %s" % (sdir, site_data_tgt))
        Site.replace_site_data_dir(sitedata, sdir, site_data_src)
    print("Generating %s" % (site_layout.SITEDATA_CONTEXT_DIR))
    sitedata.generate_coll_jsonld_context()
    print(
        "Now run 'annalist-manager initialize' to create site admin database")
    return status
Exemple #17
0
def am_updatesite(annroot, userhome, options):
    """
    Update site data, leaving user data alone

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    status       = am_errors.AM_SUCCESS
    sitesettings = am_get_site_settings(annroot, userhome, options) 
    if not sitesettings:
        print("Settings not found (%s)"%(options.configuration), file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print(
            "Unexpected arguments for %s: (%s)"%
            (options.command, " ".join(options.args)), 
            file=sys.stderr
            )
        return am_errors.AM_UNEXPECTEDARGS
    site_layout   = layout.Layout(sitesettings.BASE_DATA_DIR)
    sitebasedir   = site_layout.SITE_PATH
    sitebaseurl   = "/annalist/"                # @@TODO: figure more robust way to define this
    site          = Site(sitebaseurl, site_layout.SITE_PATH)
    sitedata      = site.site_data_collection(test_exists=False)
    if sitedata is None:
        print("Initializing Annalist site metadata in %s (migrating to new layout)"%(sitebasedir))
        site = Site.create_empty_site_data(
            sitebaseurl, sitebasedir,
            label="Annalist site (%s configuration)"%options.configuration, 
            description="Annalist %s site metadata and site-wide values."%options.configuration
            )
        sitedata      = site.site_data_collection()
    site_data_src = os.path.join(annroot, "annalist/data/sitedata")  # @@TODO: more robust definition
    site_data_tgt, site_data_file = sitedata._dir_path()
    # --- Migrate old site data to new site directory
    site_data_old = os.path.join(sitebasedir, site_layout.SITEDATA_OLD_DIR)
    old_users     = os.path.join(site_data_old, layout.USER_DIR_PREV)
    old_vocabs    = os.path.join(site_data_old, layout.VOCAB_DIR_PREV)
    if os.path.isdir(old_users) or os.path.isdir(old_vocabs):
        print("Copy Annalist old user and/or vocab data from %s"%site_data_old)
        migrate_old_data(site_data_old, layout.USER_DIR_PREV,  site_data_tgt, layout.USER_DIR )
        migrate_old_data(site_data_old, layout.VOCAB_DIR_PREV, site_data_tgt, layout.VOCAB_DIR)
    #@@
    # if os.path.isdir(old_users) or os.path.isdir(old_vocabs):
    #     print("Copy Annalist old user and/or vocab data from %s"%site_data_old)
    #     for sdir in ("users", "vocabs"):
    #         s     = os.path.join(site_data_old, sdir)
    #         old_s = os.path.join(site_data_old, "old_"+sdir)
    #         d     = os.path.join(site_data_tgt, sdir)
    #         if os.path.isdir(s):
    #             print("- %s +> %s (migrating)"%(sdir, d))
    #             updatetree(s, d)
    #             print("- %s >> %s (rename)"%(sdir, old_s))
    #             os.rename(s, old_s)
    #@@
    # --- Copy latest site data to target directory
    print("Copy Annalist site data")
    print("from %s"%site_data_src)
    for sdir in layout.DATA_DIRS:
        print("- %s -> %s"%(sdir, site_data_tgt))
        Site.replace_site_data_dir(sitedata, sdir, site_data_src)
    for sdir in (layout.USER_DIR, layout.VOCAB_DIR):
        print("- %s +> %s"%(sdir, site_data_tgt))
        Site.update_site_data_dir(sitedata, sdir, site_data_src)
    for sdir in layout.COLL_DIRS_PREV:
        remove_old_data(site_data_tgt, sdir)
    print("Generating %s"%(site_layout.SITEDATA_CONTEXT_DIR))
    sitedata.generate_coll_jsonld_context()
    # --- Copy provider data to site config provider directory
    provider_dir_tgt = os.path.join(sitesettings.CONFIG_BASE, "providers")
    provider_dir_src = os.path.join(annroot, "annalist/data/identity_providers")
    print("Copy identity provider data:")
    print("- from: %s"%(provider_dir_src,))
    print("-   to: %s"%(provider_dir_tgt,))
    updatetree(provider_dir_src, provider_dir_tgt)
    return status
def am_updatesite(annroot, userhome, options):
    """
    Update site data, leaving user data alone

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    status = am_errors.AM_SUCCESS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        print("Settings not found (%s)" % (options.configuration),
              file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    site_layout = layout.Layout(sitesettings.BASE_DATA_DIR)
    sitebasedir = site_layout.SITE_PATH
    sitebaseurl = "/annalist/"  # @@TODO: figure more robust way to define this
    site = Site(sitebaseurl, site_layout.SITE_PATH)
    sitedata = site.site_data_collection(test_exists=False)
    if sitedata is None:
        print(
            "Initializing Annalist site metadata in %s (migrating to new layout)"
            % (sitebasedir))
        site = Site.create_empty_site_data(
            sitebaseurl,
            sitebasedir,
            label="Annalist site (%s configuration)" % options.configuration,
            description="Annalist %s site metadata and site-wide values." %
            options.configuration)
        sitedata = site.site_data_collection()
    site_data_src = os.path.join(
        annroot, "annalist/data/sitedata")  # @@TODO: more robust definition
    site_data_tgt, site_data_file = sitedata._dir_path()
    # --- Migrate old site data to new site directory
    site_data_old = os.path.join(sitebasedir, site_layout.SITEDATA_OLD_DIR)
    old_users = os.path.join(site_data_old, layout.USER_DIR_PREV)
    old_vocabs = os.path.join(site_data_old, layout.VOCAB_DIR_PREV)
    if os.path.isdir(old_users) or os.path.isdir(old_vocabs):
        print("Copy Annalist old user and/or vocab data from %s" %
              site_data_old)
        migrate_old_data(site_data_old, layout.USER_DIR_PREV, site_data_tgt,
                         layout.USER_DIR)
        migrate_old_data(site_data_old, layout.VOCAB_DIR_PREV, site_data_tgt,
                         layout.VOCAB_DIR)
    #@@
    # if os.path.isdir(old_users) or os.path.isdir(old_vocabs):
    #     print("Copy Annalist old user and/or vocab data from %s"%site_data_old)
    #     for sdir in ("users", "vocabs"):
    #         s     = os.path.join(site_data_old, sdir)
    #         old_s = os.path.join(site_data_old, "old_"+sdir)
    #         d     = os.path.join(site_data_tgt, sdir)
    #         if os.path.isdir(s):
    #             print("- %s +> %s (migrating)"%(sdir, d))
    #             updatetree(s, d)
    #             print("- %s >> %s (rename)"%(sdir, old_s))
    #             os.rename(s, old_s)
    #@@
    # --- Copy latest site data to target directory
    print("Copy Annalist site data")
    print("from %s" % site_data_src)
    for sdir in layout.DATA_DIRS:
        print("- %s -> %s" % (sdir, site_data_tgt))
        Site.replace_site_data_dir(sitedata, sdir, site_data_src)
    for sdir in (layout.USER_DIR, layout.VOCAB_DIR):
        print("- %s +> %s" % (sdir, site_data_tgt))
        Site.update_site_data_dir(sitedata, sdir, site_data_src)
    for sdir in layout.COLL_DIRS_PREV:
        remove_old_data(site_data_tgt, sdir)
    print("Generating %s" % (site_layout.SITEDATA_CONTEXT_DIR))
    sitedata.generate_coll_jsonld_context()
    # --- Copy provider data to site config provider directory
    provider_dir_tgt = os.path.join(sitesettings.CONFIG_BASE, "providers")
    provider_dir_src = os.path.join(annroot,
                                    "annalist/data/identity_providers")
    print("Copy identity provider data:")
    print("- from: %s" % (provider_dir_src, ))
    print("-   to: %s" % (provider_dir_tgt, ))
    updatetree(provider_dir_src, provider_dir_tgt)
    return status
Exemple #19
0
def am_createsite(annroot, userhome, options):
    """
    Create Annalist empty site data.

    annroot     is the root directory for the Annalist software installation.
    userhome    is the home directory for the host system user issuing the command.
    options     contains options parsed from the command line.

    returns     0 if all is well, or a non-zero status code.
                This value is intended to be used as an exit status code
                for the calling program.
    """
    status = am_errors.AM_SUCCESS
    sitesettings = am_get_site_settings(annroot, userhome, options)
    if not sitesettings:
        print("Settings not found (%s)" % (options.configuration),
              file=sys.stderr)
        return am_errors.AM_NOSETTINGS
    if len(options.args) > 0:
        print("Unexpected arguments for %s: (%s)" %
              (options.command, " ".join(options.args)),
              file=sys.stderr)
        return am_errors.AM_UNEXPECTEDARGS
    site_layout = layout.Layout(sitesettings.BASE_DATA_DIR)
    sitebasedir = site_layout.SITE_PATH
    sitebaseurl = "/annalist/"  # @@TODO: figure more robust way to define this
    # --- If old site exists and --force option given, remove it
    if os.path.exists(os.path.join(sitebasedir, site_layout.SITEDATA_DIR)):
        if options.force:
            print("Removing old Annalist site at %s" % (sitebasedir))
            log.info("rmtree: %s" % (sitebasedir))
            removetree(sitebasedir)
        else:
            print(
                "Old data already exists at %s (use '--force' or '-f' to overwrite)."
                % (sitebasedir),
                file=sys.stderr)
            print(
                "NOTE: using '--force' or '-f' " +
                "removes old site user permissions and namespace data " +
                "and requires re-initialization of Django database with local usernames; "
                + "consider using 'annalist-manager updatesite'.")
            return am_errors.AM_EXISTS
    # --- Initialize empty site data in target directory
    print("Initializing Annalist site in %s" % (sitebasedir))
    site = Site.create_site_metadata(
        sitebaseurl,
        sitebasedir,
        label="Annalist site (%s configuration)" % options.configuration,
        description="Annalist %s site metadata and site-wide values." %
        options.configuration)
    sitedata = site.site_data_collection()
    Site.create_site_readme(site)
    site_data_src = os.path.join(
        annroot, "annalist/data/sitedata")  # @@TODO: more robust definition
    site_data_tgt, site_data_file = sitedata._dir_path()
    print("Copy Annalist site data")
    print("from %s" % site_data_src)
    for sdir in layout.COLL_DIRS:
        print("- %s -> %s" % (sdir, site_data_tgt))
        Site.replace_site_data_dir(sitedata, sdir, site_data_src)
    # @@TODO: filename logic copied from EntityRoot and Collection - create separate method for getting this
    (sitedata_dir, sitedata_file) = sitedata._dir_path()
    context_dir = os.path.join(sitedata_dir, layout.META_COLL_BASE_REF)
    context_file = os.path.join(context_dir, layout.COLL_CONTEXT_FILE)
    #@@
    print("Generating %s" % (context_file))
    sitedata.generate_coll_jsonld_context()
    # --- Copy provider data to site config provider directory
    provider_dir_src = os.path.join(annroot,
                                    "annalist/data/identity_providers")
    provider_dir_tgt = os.path.join(sitesettings.CONFIG_BASE, "providers")
    print("Copy identity provider data:")
    print("- from: %s" % (provider_dir_src, ))
    print("-   to: %s" % (provider_dir_tgt, ))
    ensure_dir(provider_dir_tgt)
    updatetree(provider_dir_src, provider_dir_tgt)
    # --- Created
    print(
        "Now run 'annalist-manager initialize' to create site admin database")
    return status