static int cmd_nodeset (optparse_t *p, int ac, char *av[]) { int ix = optparse_option_index (p); int nsc = ac - ix; nodeset_t *nsp, **nsv = nsc > 0 ? xzmalloc (sizeof (nsv[0]) * nsc) : NULL; int i; for (i = 0; i < nsc; i++) if (!(nsv[i] = nodeset_create_string (av[ix + i]))) log_errn_exit (EINVAL, "%s", av[ix + i]); if (optparse_hasopt (p, "intersection")) nsp = nsv_intersection (nsc, nsv); else nsp = nsv_union (nsc, nsv); if (optparse_hasopt (p, "subtract")) { const char *s = optparse_get_str (p, "subtract", ""); nodeset_t *ns = nodeset_create_string (s); if (!ns) log_errn_exit (EINVAL, "%s", s); ns_subtract (nsp, ns); } if (optparse_hasopt (p, "cardinality")) { printf ("%" PRIu32 "\n", nsp ? nodeset_count (nsp) : 0); } else if (nsp) { const char *delim = optparse_get_str (p, "delimiter", "," ); if (optparse_hasopt (p, "expand")) { nodeset_config_ranges (nsp, false); nodeset_config_brackets (nsp, false); } nodeset_config_separator (nsp, delim[0]); if (nodeset_count (nsp) > 0) printf ("%s\n", nodeset_string (nsp)); } if (nsv) { for (i = 0; i < nsc; i++) nodeset_destroy (nsv[i]); free (nsv); } return (0); }
static int internal_hwloc_reload (optparse_t *p, int ac, char *av[]) { int n = optparse_optind (p); const char *default_nodeset = "all"; const char *nodeset = optparse_get_str (p, "rank", default_nodeset); const char *walk_topology = optparse_get_str (p, "walk-topology", NULL); char *dirpath = NULL; flux_t h; if (!(h = builtin_get_flux_handle (p))) log_err_exit ("flux_open"); if (av[n] && !(dirpath = realpath (av[n], NULL))) log_err_exit ("%s", av[n]); config_hwloc_paths (h, dirpath); request_hwloc_reload (h, nodeset, walk_topology); free (dirpath); flux_close (h); return (0); }
int mod_main (flux_t h, int argc, char **argv) { optparse_t *p = optparse_create ("pymod"); if (optparse_add_option_table (p, opts) != OPTPARSE_SUCCESS) msg_exit ("optparse_add_option_table"); if (optparse_set (p, OPTPARSE_USAGE, usage_msg) != OPTPARSE_SUCCESS) msg_exit ("optparse_set usage"); int option_index = optparse_parse_args (p, argc, argv); if (option_index <= 0 || optparse_hasopt(p, "help") || option_index >= argc){ optparse_print_usage(p); return (option_index < 0); } const char * module_name = argv[option_index]; Py_SetProgramName("pymod"); Py_Initialize(); PyObject *search_path = PySys_GetObject("path"); // Add installation search paths add_if_not_present(search_path, optparse_get_str(p, "path", "")); add_if_not_present(search_path, FLUX_PYTHON_PATH); PySys_SetObject("path", search_path); if(optparse_hasopt(p, "verbose")){ PyObject_Print(search_path, stderr, 0); } flux_log(h, LOG_INFO, "loading python module named: %s", module_name); PyObject *module = PyImport_ImportModule("flux.core"); if(!module){ PyErr_Print(); return EINVAL; } PyObject *mod_main = PyObject_GetAttrString(module, "mod_main_trampoline"); if(mod_main && PyCallable_Check(mod_main)){ //maybe unpack args directly? probably easier to use a dict PyObject *py_args = PyTuple_New(3); PyTuple_SetItem(py_args, 0, PyString_FromString(module_name)); PyTuple_SetItem(py_args, 1, PyLong_FromVoidPtr(h)); //Convert zhash to native python dict, should preserve mods //through switch to argc-style arguments PyObject *arg_list = PyList_New(0); char ** it = argv + option_index; int i; for (i=0; *it; i++, it++){ PyList_Append(arg_list, PyString_FromString(*it)); } PyTuple_SetItem(py_args, 2, arg_list); // Call into trampoline PyObject_CallObject(mod_main, py_args); if(PyErr_Occurred()){ PyErr_Print(); } Py_DECREF(py_args); Py_DECREF(arg_list); } Py_Finalize(); return 0; }