File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\tasks\Shell.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jene Jasper, Yahoo! Inc., Seiji Sogabe
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.tasks;
25
26 import hudson.FilePath;
27 import hudson.Functions;
28 import hudson.Util;
29 import hudson.Extension;
30 import hudson.model.AbstractProject;
31 import hudson.remoting.Callable;
32 import hudson.remoting.VirtualChannel;
33 import hudson.util.FormValidation;
34 import java.io.IOException;
35 import net.sf.json.JSONObject;
36 import org.kohsuke.stapler.DataBoundConstructor;
37 import org.kohsuke.stapler.StaplerRequest;
38 import org.kohsuke.stapler.QueryParameter;
39
40 import java.util.List;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.logging.Logger;
44
45 /**
46 * Executes a series of commands by using a shell.
47 *
48 * @author Kohsuke Kawaguchi
49 */
50 public class Shell extends CommandInterpreter {
51 @DataBoundConstructor
52 public Shell(String command) {
53 super(fixCrLf(command));
54 }
55
56 /**
57 * Fix CR/LF and always make it Unix style.
58 */
59 private static String fixCrLf(String s) {
60 // eliminate CR
61 int idx;
62 while((idx=s.indexOf("\r\n"))!=-1)
63 s = s.substring(0,idx)+s.substring(idx+1);
64
65 //// add CR back if this is for Windows
66 //if(isWindows()) {
67 // idx=0;
68 // while(true) {
69 // idx = s.indexOf('\n',idx);
70 // if(idx==-1) break;
71 // s = s.substring(0,idx)+'\r'+s.substring(idx);
72 // idx+=2;
73 // }
74 //}
75 return s;
76 }
77
78 /**
79 * Older versions of bash have a bug where non-ASCII on the first line
80 * makes the shell think the file is a binary file and not a script. Adding
81 * a leading line feed works around this problem.
82 */
83 private static String addCrForNonASCII(String s) {
84 if(!s.startsWith("#!")) {
85 if (s.indexOf('\n')!=0) {
86 return "\n" + s;
87 }
88 }
89
90 return s;
91 }
92
93 public String[] buildCommandLine(FilePath script) {
94 if(command.startsWith("#!")) {
95 // interpreter override
96 int end = command.indexOf('\n');
97 if(end<0) end=command.length();
98 List<String> args = new ArrayList<String>();
99 args.addAll(Arrays.asList(Util.tokenize(command.substring(0,end).trim())));
100 args.add(script.getRemote());
101 args.set(0,args.get(0).substring(2)); // trim off "#!"
102 return args.toArray(new String[args.size()]);
103 } else
104 return new String[] { getDescriptor().getShellOrDefault(script.getChannel()), "-xe", script.getRemote()};
105 }
106
107 protected String getContents() {
108 return addCrForNonASCII(fixCrLf(command));
109 }
110
111 protected String getFileExtension() {
112 return ".sh";
113 }
114
115 @Override
116 public DescriptorImpl getDescriptor() {
117 return (DescriptorImpl)super.getDescriptor();
118 }
119
120 @Extension
121 public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
122 /**
123 * Shell executable, or null to default.
124 */
125 private String shell;
126
127 public DescriptorImpl() {
128 load();
129 }
130
131 public boolean isApplicable(Class<? extends AbstractProject> jobType) {
132 return true;
133 }
134
135 public String getShell() {
136 return shell;
137 }
138
139 /**
140 * @deprecated 1.403
141 * Use {@link #getShellOrDefault(hudson.remoting.VirtualChannel) }.
142 */
143 public String getShellOrDefault() {
144 if(shell==null)
145 return Functions.isWindows() ?"sh":"/bin/sh";
146 return shell;
147 }
148
149 public String getShellOrDefault(VirtualChannel channel) {
150 if (shell != null)
151 return shell;
152
153 String interpreter = null;
154 try {
155 interpreter = channel.call(new Shellinterpreter());
156 } catch (IOException e) {
157 LOGGER.warning(e.getMessage());
158 } catch (InterruptedException e) {
159 LOGGER.warning(e.getMessage());
160 }
161 if (interpreter == null) {
162 interpreter = getShellOrDefault();
163 }
164
165 return interpreter;
166 }
167
168 public void setShell(String shell) {
169 this.shell = Util.fixEmptyAndTrim(shell);
170 save();
171 }
172
173 public String getDisplayName() {
174 return Messages.Shell_DisplayName();
175 }
176
177 @Override
178 public Builder newInstance(StaplerRequest req, JSONObject data) {
179 return new Shell(data.getString("command"));
180 }
181
182 @Override
183 public boolean configure(StaplerRequest req, JSONObject data) {
184 setShell(req.getParameter("shell"));
185 return true;
186 }
187
188 /**
189 * Check the existence of sh in the given location.
190 */
191 public FormValidation doCheck(@QueryParameter String value) {
192 // Executable requires admin permission
193 return FormValidation.validateExecutable(value);
194 }
195
196 private static final class Shellinterpreter implements Callable<String, IOException> {
197
198 private static final long serialVersionUID = 1L;
199
200 public String call() throws IOException {
201 return Functions.isWindows() ? "sh" : "/bin/sh";
202 }
203 }
204
205 }
206
207 private static final Logger LOGGER = Logger.getLogger(Shell.class.getName());
208 }
209