def main():
    args = parse_args()

    oer2go_mod_name = args.module

    dated_oer2go_cat = adm.read_json_file(CONST.oer2go_catalog_file)

    oer2go_mod = dated_oer2go_cat['modules'][oer2go_mod_name]
    mv_src = CONST.rachel_working_dir + oer2go_mod_name
    mv_dest = CONST.iiab_modules_dir + oer2go_mod_name

    if not os.path.isdir(mv_src):
        print('Module working directory not found. Exiting.')
        sys.exit(1)

    if not os.path.isdir(mv_dest):  # only mv is target not exist
        shutil.move(mv_src, mv_dest)

    is_downloaded, has_menu_def = adm.get_module_status(oer2go_mod)
    if not has_menu_def:
        print('Generating Menu Definition')
        working_dir = adm.CONST.rachel_working_dir + str(uuid.uuid4()) + "/"
        os.mkdir(working_dir)
        menu_item_name = adm.create_module_menu_def(oer2go_mod,
                                                    working_dir,
                                                    incl_extra_html=False)
        shutil.rmtree(working_dir)

    print('Adding to Home Page Menu')
    adm.update_menu_json(oer2go_mod_name, no_lang=False)
예제 #2
0
def main():

    global verbose
    oer2go_catalog = {}

    args = parse_args()
    if args.verbose:
        verbose = True

    # make sure we have menu js_menu_dir if args.menu true
    if args.menu:
        if not os.path.isdir(adm.CONST.js_menu_dir):
            sys.stdout.write(
                "GET-OER2GO-CAT ERROR - iiab-menu not installed and --menu option given\n"
            )
            sys.stdout.flush()
            sys.exit(99)

    # for now we will assume that old modules are still in the current catalog
    # get new oer2go catalog unless told not to

    if not args.no_download:
        try:
            url_handle = urllib.request.urlopen(adm.CONST.oer2go_cat_url)
            oer2go_catalog_json = url_handle.read()
            url_handle.close()
        except (urllib.error.URLError) as exc:
            sys.stdout.write("GET-OER2GO-CAT ERROR - " + str(exc.reason) +
                             '\n')
            sys.stdout.flush()
            sys.exit(1)
        try:
            url_handle = urllib.request.urlopen(adm.CONST.iiab_module_cat_url)
            iiab_catalog_json = url_handle.read()
            url_handle.close()
        except (urllib.error.URLError) as exc:
            sys.stdout.write("GET-OER2GO-CAT ERROR - " + str(exc.reason) +
                             '\n')
            sys.stdout.flush()
            sys.exit(2)

        # now try to parse
        try:
            oer2go_catalog = json.loads(oer2go_catalog_json)
            iiab_catalog = json.loads(iiab_catalog_json)
        except:
            sys.stdout.write("GET-OER2GO-CAT ERROR - " +
                             str(sys.exc_info()[0]) + "," +
                             str(sys.exc_info()[1]) + '\n')
            sys.stdout.flush()
            sys.exit(3)

        # merge iiab_catalog.json if was downloaded otherwise assume was previously merged
        for item in iiab_catalog:
            moddir = item['moddir']
            id = item['module_id']
            module = item
            iiab_oer2go_catalog[moddir] = module

    else:
        local_oer2go_catalog = adm.read_json(adm.CONST.oer2go_catalog_file)
        oer2go_catalog = local_oer2go_catalog['modules']

    working_dir = adm.CONST.rachel_working_dir + str(uuid.uuid4()) + "/"
    os.mkdir(working_dir)
    #os.mkdir(iiab_menu_download_dir)

    for item in oer2go_catalog:  # structure of local and remote catalogs is different
        if args.no_download:  # local
            moddir = item
            module = oer2go_catalog[moddir]
            module_id = module['module_id']
        else:  # remote
            moddir = item['moddir']
            module_id = item['module_id']
            module = item

        if moddir is None:  # skip items with no moddir
            continue

        menu_item_name = moddir
        if module_id not in dup_list:
            is_downloaded, has_menu_def = adm.get_module_status(module)
            if args.menu and is_downloaded:
                if not has_menu_def:
                    menu_item_name = adm.create_module_menu_def(
                        module, working_dir, incl_extra_html=False)
                    msg = "Generating menu files"
                    if verbose:
                        print("%s %s %s" % (msg, module_id, moddir))
                adm.update_menu_json(
                    menu_item_name)  # only adds if not already in menu
        else:
            msg = "Skipping module not needed by Internet in a Box"
            if verbose:
                print("%s %s %s" % (msg, module_id, moddir))
            continue
        iiab_oer2go_catalog[moddir] = module

    # no need to write catalog if not downloaded as we don't need wip and other extra menu def fields
    if not args.no_download:
        dated_oer2go_cat = {}
        dated_oer2go_cat['download_date'] = time.strftime("%Y-%m-%d.%H:%M:%S")
        dated_oer2go_cat['modules'] = iiab_oer2go_catalog

        with open(adm.CONST.oer2go_catalog_file, 'w') as outfile:
            json.dump(dated_oer2go_cat, outfile, indent=2)

    shutil.rmtree(working_dir)

    sys.stdout.write("SUCCESS")
    sys.stdout.flush()
    sys.exit(0)
예제 #3
0
def main ():
    global verbose
    global download_flag

    oer2go_catalog = {}
    err_num = 0
    err_str = "SUCCESS"

    args = parse_args()
    if args.verbose:
        verbose = True
    if args.no_download:
        download_flag = False

    # make sure we have menu js_menu_dir if args.menu true
    if args.menu:
        if not os.path.isdir(adm.CONST.js_menu_dir):
            sys.stdout.write("GET-OER2GO-CAT ERROR - iiab-menu not installed and --menu option given\n")
            sys.stdout.flush()
            sys.exit(99)

    # always get our catalog
    # failure is fatal
    try:
        url_handle = urllib.request.urlopen(adm.CONST.iiab_module_cat_url)
        iiab_catalog_json = url_handle.read()
        url_handle.close()
        iiab_catalog = json.loads(iiab_catalog_json)
    except (urllib.error.URLError) as exc:
        sys.stdout.write("GET-OER2GO-CAT ERROR - " + str(exc.reason) +'\n')
        sys.stdout.flush()
        sys.exit(2)

    # for now we will assume that old modules are still in the current catalog
    # get new oer2go catalog unless told not to

    if download_flag:
        err_num, err_str, oer2go_catalog = get_oer2go_cat()
        if err_num != 0:
            download_flag = False
    if not download_flag: # get local copy
        local_oer2go_catalog = adm.read_json(adm.CONST.oer2go_catalog_file)
        oer2go_catalog = local_oer2go_catalog['modules']

    # start with iiab_catalog.json
    for item in iiab_catalog:
        moddir = item['moddir']
        id = item['module_id']
        module = item
        iiab_oer2go_catalog[moddir] = module

    working_dir = adm.CONST.rachel_working_dir + str(uuid.uuid4()) + "/"
    os.mkdir(working_dir)
    #os.mkdir(iiab_menu_download_dir)

    for item in oer2go_catalog: # structure of local and remote catalogs is different
        if not download_flag: # local
            moddir = item
            module = oer2go_catalog[moddir]
            module_id = module['module_id']
        else: # remote
            moddir = item['moddir']
            module_id = item['module_id']
            module = item

        if moddir is None: # skip items with no moddir
            continue

        menu_item_name = moddir

        if str(module_id) in dup_list:
            msg = "Skipping module not needed by Internet in a Box"
            if verbose:
                print("%s %s %s" % (msg, module_id, moddir))
            continue
        if module.get('type') != 'html':
            continue

        is_downloaded, has_menu_def = adm.get_module_status (module)
        #if args.menu and is_downloaded:
        if args.menu:
            if not has_menu_def:
                menu_item_name = adm.create_module_menu_def(module, working_dir, incl_extra_html = False)
                msg = "Generating menu files"
                if verbose:
                    print("%s %s %s" % (msg, module_id, moddir))
            if is_downloaded:
                adm.update_menu_json(menu_item_name) # only adds if not already in menu

        iiab_oer2go_catalog[moddir] = module

    # write catalog even if not downloaded as our could have changed
    dated_oer2go_cat = {}
    dated_oer2go_cat['download_date'] = time.strftime("%Y-%m-%d.%H:%M:%S")
    dated_oer2go_cat['modules'] = iiab_oer2go_catalog

    adm.write_json_file(dated_oer2go_cat, adm.CONST.oer2go_catalog_file)

    shutil.rmtree(working_dir)

    sys.stdout.write(err_str)
    sys.stdout.flush()
    sys.exit(err_num)