Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run python3 #520

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions apps/cli/cli_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,63 @@ cli_set_mode(clixon_handle h,
return retval;
}

/**
* @brief Runs a Python script in a new process
*
* @param h Clicon handle
* @param cvv CLIgen variable vector
* @param argv function arguments
*
* Function arguments include:<br>
* * h : not used<br>
* * cvv : unused parameters will be set as environment variables<br>
* * argv : contains the arguments passed to the function;
* is expected to be the path to the Python script
*
* The function creates a new child process in which the Python script is launched.
* In the child process, before launching the script, environment variables are set
* taken from cvv.
*
* The function returns the exit code of the Python script, or a value of (-1) on error.
*
* @return Returns 0 on success, -1 in case of error
*/
int cli_start_python3(clixon_handle h, cvec *cvv, cvec *argv) {
cg_var *cv = NULL;
char buf[64];
int pid;
int ret;
int status;
char *python_program_path = NULL;
char *python_interpreter_path = "/usr/bin/python3";

if (cvec_len(argv) > 1){
clixon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected: [<path to script>]",
cvec_len(argv));
return -1;
}

if ((pid = fork()) == 0) {
while ((cv = cvec_each1(cvv, cv)) != NULL) {
if (cv_const_get(cv))
continue;
cv2str(cv, buf, sizeof(buf) - 1);
setenv(cv_name_get(cv), buf, 1);
}
python_program_path = buf;
char *args[] = {python_interpreter_path, python_program_path, NULL};
execv(python_interpreter_path, args);
perror("Error run script");
exit(0);
}

if (waitpid(pid, &status, 0) == pid)
ret = WEXITSTATUS(status);
else
ret = -1;
return ret;
}

/*! Start bash from cli callback
*
* Typical usage: shell("System Bash") <source:rest>, cli_start_shell();
Expand Down
1 change: 1 addition & 0 deletions apps/cli/clixon_cli_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ int cli_debug_backend(clixon_handle h, cvec *vars, cvec *argv);
int cli_debug_restconf(clixon_handle h, cvec *vars, cvec *argv);
int cli_set_mode(clixon_handle h, cvec *vars, cvec *argv);
int cli_start_shell(clixon_handle h, cvec *vars, cvec *argv);
int cli_start_python3(clixon_handle h, cvec *vars, cvec *argv);
int cli_quit(clixon_handle h, cvec *vars, cvec *argv);
int cli_commit(clixon_handle h, cvec *vars, cvec *argv);
int cli_validate(clixon_handle h, cvec *vars, cvec *argv);
Expand Down
8 changes: 4 additions & 4 deletions configure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a generated file and should most probably not be here (unless you changed configure.ac)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this is a mistake, I'm sorry

Original file line number Diff line number Diff line change
Expand Up @@ -5302,11 +5302,11 @@ if test x$ac_prog_cxx_stdcxx = xno
then :
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for $CXX option to enable C++11 features" >&5
printf %s "checking for $CXX option to enable C++11 features... " >&6; }
if test ${ac_cv_prog_cxx_11+y}
if test ${ac_cv_prog_cxx_cxx11+y}
then :
printf %s "(cached) " >&6
else $as_nop
ac_cv_prog_cxx_11=no
ac_cv_prog_cxx_cxx11=no
ac_save_CXX=$CXX
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
Expand Down Expand Up @@ -5348,11 +5348,11 @@ if test x$ac_prog_cxx_stdcxx = xno
then :
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for $CXX option to enable C++98 features" >&5
printf %s "checking for $CXX option to enable C++98 features... " >&6; }
if test ${ac_cv_prog_cxx_98+y}
if test ${ac_cv_prog_cxx_cxx98+y}
then :
printf %s "(cached) " >&6
else $as_nop
ac_cv_prog_cxx_98=no
ac_cv_prog_cxx_cxx98=no
ac_save_CXX=$CXX
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
Expand Down
4 changes: 4 additions & 0 deletions example/main/example_cli.cli
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ debug("Debugging parts of the system"){
shell("System command"), cli_start_shell("bash");{
<source:rest>("Single shell command"), cli_start_shell("bash");
}
python3("Path to script"), cli_start_python3("path");{
<source:rest>("Path to script"), cli_start_python3("path");
}

copy("Copy and create a new object") {
running("Copy from running db") startup("Copy to startup config"), db_copy("running", "startup");
interface("Copy interface"){
Expand Down