python.project/src/org/netbeans/modules/python/project/ui/actions/CleanBuildCommand.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
27 * If you wish your version of this file to be governed by only the CDDL
28 * or only the GPL Version 2, indicate your decision by adding
29 * "[Contributor] elects to include this software in this distribution
30 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31 * single choice of license, a recipient has the option to distribute
32 * your version of this file under either the CDDL, the GPL Version 2 or
33 * to extend the choice of license to its licensees as provided above.
34 * However, if you add GPL Version 2 code and therefore, elected the GPL
35 * Version 2 license, then the option applies only if the new code is
36 * made subject to such option by the copyright holder.
40 * Portions Copyrighted 2009 Sun Microsystems, Inc.
42 package org.netbeans.modules.python.project.ui.actions;
44 import java.io.IOException;
45 import java.util.HashMap;
47 import org.netbeans.api.project.ProjectUtils;
48 import org.netbeans.modules.python.api.PythonExecution;
49 import org.netbeans.modules.python.api.PythonPlatform;
50 import org.netbeans.modules.python.project.PythonProject;
51 import org.netbeans.modules.python.project.ui.Utils;
52 import org.netbeans.spi.project.ActionProvider;
53 import org.openide.filesystems.FileObject;
54 import org.openide.filesystems.FileUtil;
55 import org.openide.filesystems.Repository;
56 import org.openide.loaders.DataFolder;
57 import org.openide.loaders.DataObject;
58 import org.openide.loaders.DataObjectNotFoundException;
59 import org.openide.util.Exceptions;
60 import org.openide.util.Lookup;
64 * @author Amit Saha <amitksaha@netbeans.org>
67 public class CleanBuildCommand extends Command {
69 public CleanBuildCommand(PythonProject project) {
74 public String getCommandId() {
75 return ActionProvider.COMMAND_REBUILD;
79 public void invokeAction(Lookup context) throws IllegalArgumentException {
80 final PythonProject pyProject = getProject();
81 final PythonPlatform platform = checkProjectPythonPlatform(pyProject);
84 //@todo investigate into the use cases for this action. Do we *really*
89 // A 'setup.py' file is needed build a Python egg
90 // If a 'setup.py' already exists in the source root, do not create a new
91 // file, else create a bare minimal 'setup.py'
92 // file for the Egg building process
93 // the template file is defined in /org/netbeans/modules/python/editor
94 ///templates/setup.py.ftl
96 //Find the source root(s) directory in which all the sources live
97 FileObject[] roots = pyProject.getSourceRoots().getRoots();
99 for (FileObject root : roots) {
100 System.out.println("Src Folder: " + root.getPath());
105 if (findSetupFile(pyProject) != null) {
107 deleteSetupFile(roots[0]);
108 } catch (IOException ex) {
109 Exceptions.printStackTrace(ex);
117 createSetupFile(Repository.getDefault().getDefaultFileSystem().findResource("Templates/Python/_setup.py"), roots[0], "setup.py");
118 } catch (IOException ex) {
119 Exceptions.printStackTrace(ex);
127 if (platform == null) {
128 return; // invalid platform user has been warn in check so safe to return
131 if (getProperties().getMainModule() == null ||
132 getProperties().getMainModule().equals("")) {
133 String main = Utils.chooseMainModule(getProject().getSourceRoots().getRoots());
134 getProperties().setMainModule(main);
135 getProperties().save();
141 // Obtain the FileObject of the 'setup.py' file
142 FileObject script=null;
143 for (FileObject root : roots) {
144 script = root.getFileObject("setup", "py");
147 assert script != null; //check
150 PythonExecution pyexec = new PythonExecution();
151 pyexec.setDisplayName(ProjectUtils.getInformation(pyProject).getDisplayName());
152 //Set work dir - probably we need a property to store work dir
153 FileObject path = script.getParent();
155 //System.out.println("Working directory" + path);
157 pyexec.setWorkingDirectory(path.getPath());
158 pyexec.setCommand(platform.getInterpreterCommand());
160 //path = FileUtil.toFile(script).getAbsolutePath();
161 pyexec.setScript(FileUtil.toFile(script).getAbsolutePath());
162 pyexec.setCommandArgs(platform.getInterpreterArgs());
163 pyexec.setScriptArgs("bdist_egg"); //build the Egg
166 pyexec.setPath(PythonPlatform.buildPath(super.buildPythonPath(platform, pyProject)));
167 pyexec.setJavaPath(PythonPlatform.buildPath(super.buildJavaPath(platform, pyProject)));
168 pyexec.setShowControls(true);
169 pyexec.setShowInput(true);
170 pyexec.setShowWindow(true);
171 pyexec.addStandardRecognizers();
173 //System.out.println("Executing::" + pyexec.getScript() + " with::" + pyexec.getScriptArgs());
178 public boolean isActionEnabled(Lookup context) throws IllegalArgumentException {
182 protected static FileObject findSetupFile(final PythonProject pyProject) {
183 final FileObject[] roots = pyProject.getSourceRoots().getRoots();
184 final String setupFile = "setup.py";
185 if (setupFile == null) {
188 FileObject fo = null;
189 for (FileObject root : roots) {
190 fo = root.getFileObject(setupFile);
198 private void createSetupFile(FileObject template, FileObject parent, String filename) throws IOException {
200 DataFolder dataFolder = DataFolder.findFolder(parent);
201 DataObject dataTemplate = DataObject.find(template);
202 //Strip extension when needed
203 int index = filename.lastIndexOf('.');
204 if (index > 0 && index < filename.length() - 1 && "py".equalsIgnoreCase(filename.substring(index + 1))) {
205 filename = filename.substring(0, index);
208 //create the map of objects
209 Map ftl_objects = new HashMap();
210 ftl_objects.put("project_name", getProject().getName());
213 dataTemplate.createFromTemplate(dataFolder, filename, ftl_objects);
216 } catch (DataObjectNotFoundException ex) {
217 Exceptions.printStackTrace(ex);
221 private void deleteSetupFile(FileObject src) throws IOException {
222 src.getFileObject("setup", "py").delete();