Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

115
LINES

< > BotCompany Repo | #1000855 // backtick + backtickToFile

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (4011L) is out of date.

1  
static int backtick_exitValue;
2  
static boolean backtick_verbose, backtick_keepScript;
3  
static new ThreadLocal<File> backtick_scriptFile;
4  
static new ThreadLocal<Bool> backtick_uninterruptable; // Great trick, thanks to Tim Bunce @ http://stackoverflow.com/questions/12856620/how-to-handle-signals-in-bash-during-synchronous-execution
5  
6  
sbool backtick_win_cmd; // bugfixing
7  
8  
static String backtick(String cmd) ctex {
9  
  ping();
10  
  File outFile = File.createTempFile("_backtick", "");
11  
  backtickToFile(cmd, outFile);
12  
  S result = loadTextFile(outFile.getPath(), "");
13  
  if (backtick_verbose) {
14  
    //print("backtick: script length after=" + backtick_scriptFile->length());
15  
    print("[[\n" + result + "]]");
16  
  }
17  
  outFile.delete();
18  
  ret result;
19  
}
20  
21  
static java.lang.Process backtickToFile(String cmd, File outFile) ctex {
22  
  try {
23  
    java.lang.Process process = backtickToFile_noWait(cmd, outFile);
24  
    process.waitFor();
25  
    backtick_exitValue = process.exitValue();
26  
    if (backtick_verbose)
27  
      System.out.println("Process return code: " + backtick_exitValue);
28  
    ret process;
29  
  } finally {
30  
    if (!backtick_keepScript)
31  
      deleteFile(backtick_scriptFile.get());
32  
    backtick_scriptFile.set(null);
33  
  }
34  
}
35  
36  
static java.lang.Process backtickToFile_noWait(String cmd, File outFile) ctex {
37  
  ping();
38  
  File scriptFile;
39  
  S ext = isWindows() ? ".bat" : "";
40  
  if (backtick_keepScript)
41  
    scriptFile = makeFileNameUnique_withExtension(javaxCachesDir("Cmd Scripts/backtick"), ".bat");
42  
  else
43  
    scriptFile = File.createTempFile("_backtick", ext);
44  
  backtick_scriptFile.set(scriptFile);
45  
  if (backtick_verbose)
46  
    print("backtick: scriptFile " + f2s(scriptFile));
47  
    
48  
  bool makeInterruptable = !isTrue(backtick_uninterruptable!) && !isWindows();
49  
50  
  cmd = trim(cmd);
51  
  if (makeInterruptable && numLines(cmd) > 1) fail("No multi-line commands allowed when making interruptable");
52  
  
53  
  S command = cmd + " >" + bashQuote(outFile.getPath()) + " 2>&1";
54  
  if (makeInterruptable) command = fixNewLines([[
55  
interruptable() {
56  
57  
    # handle options
58  
    local setsid=""
59  
    local debug=false
60  
    while true; do
61  
        case "${1:-}" in
62  
            --killall)      setsid=setsid; shift ;;
63  
            --debug)        debug=true; shift ;;
64  
            --*)            echo "Invalid option: $1" 1>&2; exit 1;;
65  
            *)              break;; # no more options
66  
        esac
67  
    done
68  
69  
    # start the specified command
70  
    $setsid "$@" &
71  
    local child_pid=$!
72  
73  
    # arrange to propagate a signal to the child process
74  
    trap '
75  
        exec 1>&2
76  
        set +e
77  
        trap "" SIGPIPE # ensure a possible sigpipe from the echo does not prevent the kill
78  
        echo "${BASH_SOURCE[0]} caught SIGTERM while executing $* (pid $child_pid), sending SIGTERM to it"
79  
        # (race) child may have exited in which case kill will report an error
80  
        # if setsid is used then prefix the pid with a "-" to indicate that the signal
81  
        # should be sent to the entire process group
82  
        kill ${setsid:+-}$child_pid
83  
        exit 143
84  
    ' SIGTERM
85  
    # ensure that the trap doesn't persist after we return
86  
    trap 'trap - SIGTERM' RETURN
87  
88  
    $debug && echo "interruptable wait (child $child_pid, self $$) for: $*"
89  
90  
    # An error status from the child process will trigger an exception (via set -e)
91  
    # here unless the caller is checking the return status
92  
    wait $child_pid # last command, so status of waited for command is returned
93  
}
94  
95  
interruptable ]]) + command;
96  
  //Log.info("[Backtick] " + command);
97  
  if (backtick_verbose) {
98  
    print("backtick: command " + command);
99  
    print("backtick: saving to " + scriptFile.getPath());
100  
  }
101  
  saveTextFile(scriptFile.getPath(), command);
102  
  if (backtick_verbose)
103  
    print("backtick: command length=" + l(command) + ", file length=" + scriptFile.length());
104  
  String[] command2;
105  
  if (isWindows())
106  
    if (backtick_win_cmd)
107  
      command2 = new S[] { "cmd", "/c", scriptFile.getPath() };
108  
    else
109  
      command2 = new S[] { scriptFile.getPath() };
110  
  else
111  
    command2 = new String[] { "/bin/bash", scriptFile.getPath() };
112  
  if (backtick_verbose)
113  
    print("backtick: command2 " + structure(command2));
114  
  ret Runtime.getRuntime().exec(command2);
115  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, odhhsrjjbcgr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000855
Snippet name: backtick + backtickToFile
Eternal ID of this version: #1000855/30
Text MD5: 29b8c186c2fa48697381c32322d199bf
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-20 15:54:36
Source code size: 4293 bytes / 115 lines
Pitched / IR pitched: No / No
Views / Downloads: 996 / 1531
Version history: 29 change(s)
Referenced in: [show references]