Exemple #1
0
def main():
    import argparse

    parser = argparse.ArgumentParser(
        description="Generate new sonata workspaces and project layouts")

    parser.add_argument("--init",
                        help="Create a new sonata workspace",
                        action="store_true")

    parser.add_argument("--workspace",
                        help="location of existing (or new) workspace. "
                        "If not specified will assume '{}'".format(
                            Workspace.DEFAULT_WORKSPACE_DIR),
                        required=False)

    parser.add_argument("--project",
                        help="create a new project at the specified location",
                        required=False)

    parser.add_argument("--debug",
                        help="increases logging level to debug",
                        required=False,
                        action="store_true")

    args = parser.parse_args()

    log_level = "INFO"
    if args.debug:
        log_level = "DEBUG"
        coloredlogs.install(level=log_level)

    # Ensure that one argument is given (--init, --workspace or --project)
    if not args.init and not args.workspace and not args.project:
        parser.print_help()
        return

    # Ensure that argument --workspace is not alone
    if not args.init and args.workspace and not args.project:
        parser.print_help()
        return

    # If workspace arg is not given, create a workspace in user home
    if args.workspace is None:
        ws_root = Workspace.DEFAULT_WORKSPACE_DIR

        # If a workspace already exists at user home, throw an error and quit
        if args.init and os.path.isdir(ws_root):
            print("A workspace already exists in {}. "
                  "Please specify a different location.\n".format(ws_root),
                  file=sys.stderr)
            exit(1)

    else:
        ws_root = expanduser(args.workspace)

    if args.init:
        ws = Workspace(ws_root, log_level=log_level)
        if ws.check_ws_exists():
            print(
                "A workspace already exists at the "
                "specified location, exiting",
                file=sys.stderr)
            exit(1)

        log.debug("Attempting to create a new workspace")
        cwd = os.getcwd()
        ws.create_dirs()
        ws.create_files()
        os.chdir(cwd)
        log.debug("Workspace created.")
    else:
        ws = Workspace.__create_from_descriptor__(ws_root)
        if not ws:
            print(
                "Could not find a SONATA workspace "
                "at the specified location",
                file=sys.stderr)
            exit(1)

    if args.project is not None:
        log.debug("Attempting to create a new project")

        prj_root = os.path.expanduser(args.project)
        proj = Project(ws, prj_root)
        proj.create_prj()

        log.debug("Project created.")
Exemple #2
0
def main():
    import argparse

    parser = argparse.ArgumentParser(
        description="Generate new sonata workspaces and project layouts")

    parser.add_argument(
        "--init",
        help="Create a new sonata workspace",
        action="store_true")

    parser.add_argument(
        "--workspace",
        help="location of existing (or new) workspace. "
             "If not specified will assume '{}'"
             .format(Workspace.DEFAULT_WORKSPACE_DIR),
        required=False)

    parser.add_argument(
        "--project",
        help="create a new project at the specified location",
        required=False)

    parser.add_argument(
        "--debug",
        help="increases logging level to debug",
        required=False,
        action="store_true")

    args = parser.parse_args()

    log_level = "INFO"
    if args.debug:
        log_level = "DEBUG"
        coloredlogs.install(level=log_level)

    # Ensure that one argument is given (--init, --workspace or --project)
    if not args.init and not args.workspace and not args.project:
        parser.print_help()
        return

    # Ensure that argument --workspace is not alone
    if not args.init and args.workspace and not args.project:
        parser.print_help()
        return

    # If workspace arg is not given, create a workspace in user home
    if args.workspace is None:
        ws_root = Workspace.DEFAULT_WORKSPACE_DIR

        # If a workspace already exists at user home, throw an error and quit
        if args.init and os.path.isdir(ws_root):
            print("A workspace already exists in {}. "
                  "Please specify a different location.\n"
                  .format(ws_root), file=sys.stderr)
            exit(1)

    else:
        ws_root = expanduser(args.workspace)

    if args.init:
        ws = Workspace(ws_root, log_level=log_level)
        if ws.check_ws_exists():
            print("A workspace already exists at the "
                  "specified location, exiting",
                  file=sys.stderr)
            exit(1)

        log.debug("Attempting to create a new workspace")
        cwd = os.getcwd()
        ws.create_dirs()
        ws.create_files()
        os.chdir(cwd)
        log.debug("Workspace created.")
    else:
        ws = Workspace.__create_from_descriptor__(ws_root)
        if not ws:
            print("Could not find a SONATA workspace "
                  "at the specified location",
                  file=sys.stderr)
            exit(1)

    if args.project is not None:
        log.debug("Attempting to create a new project")

        prj_root = os.path.expanduser(args.project)
        proj = Project(ws, prj_root)
        proj.create_prj()

        log.debug("Project created.")