python.project/src/org/netbeans/modules/python/project/ui/actions/CleanBuildCommand.java
author Julien Enselme <jenselme@netbeans.org>
Sun, 25 Jun 2017 18:17:37 +0200
changeset 18427 1520ed2e78e3
parent 17225 96973646ad68
permissions -rw-r--r--
#250340: Building egg fails with IOException: No such file or directory

Thanks to ratanak for the patch
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    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.
    37  *
    38  * Contributor(s):
    39  *
    40  * Portions Copyrighted 2009 Sun Microsystems, Inc.
    41  */
    42 package org.netbeans.modules.python.project.ui.actions;
    43 
    44 import java.io.IOException;
    45 import java.util.HashMap;
    46 import java.util.Map;
    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;
    61 
    62 /**
    63  *
    64  * @author Amit Saha <amitksaha@netbeans.org>
    65  * 
    66  */
    67 public class CleanBuildCommand extends Command {
    68 
    69     public CleanBuildCommand(PythonProject project) {
    70         super(project);
    71     }
    72 
    73     @Override
    74     public String getCommandId() {
    75         return ActionProvider.COMMAND_REBUILD;
    76     }
    77 
    78     @Override
    79     public void invokeAction(Lookup context) throws IllegalArgumentException {
    80         final PythonProject pyProject = getProject();
    81         final PythonPlatform platform = checkProjectPythonPlatform(pyProject);
    82 
    83 
    84         //@todo investigate into the use cases for this action. Do we *really*
    85         // need this?
    86 
    87 
    88 
    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
    95 
    96         //Find the source root(s) directory in  which all the sources live
    97        FileObject[] roots = pyProject.getSourceRoots().getRoots();
    98 
    99         for (FileObject root : roots) {
   100             System.out.println("Src Folder:  " + root.getPath());
   101         }
   102 
   103 
   104 
   105         if (findSetupFile(pyProject) != null) {
   106             try {
   107                 deleteSetupFile(roots[0]);
   108             } catch (IOException ex) {
   109                 Exceptions.printStackTrace(ex);
   110             }
   111 
   112 
   113         }
   114 
   115         
   116         try {
   117             createSetupFile(Repository.getDefault().getDefaultFileSystem().findResource("Templates/Python/_setup.py"), roots[0], "setup.py");
   118         } catch (IOException ex) {
   119             Exceptions.printStackTrace(ex);
   120         }
   121 
   122 
   123 
   124 
   125 
   126 
   127         if (platform == null) {
   128             return; // invalid platform user has been warn in check so safe to return
   129         }
   130 
   131         if (getProperties().getMainModule() == null ||
   132                 getProperties().getMainModule().equals("")) {
   133             String main = Utils.chooseMainModule(getProject().getSourceRoots().getRoots());
   134             getProperties().setMainModule(main);
   135             getProperties().save();
   136         }
   137 
   138 
   139 
   140 
   141         // Obtain the FileObject of the 'setup.py' file
   142         FileObject script=null;
   143         for (FileObject root : roots) {
   144             script = root.getFileObject("setup", "py");
   145         }
   146 
   147         assert script != null; //check
   148 
   149 
   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();
   154 
   155         //System.out.println("Working directory" + path);
   156 
   157         pyexec.setWorkingDirectory(path.getPath());
   158         pyexec.setCommand(platform.getInterpreterCommand());
   159         //Set python script
   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
   164         //build path & set
   165         //build path & set
   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();
   172 
   173         //System.out.println("Executing::" + pyexec.getScript() + " with::" + pyexec.getScriptArgs());
   174         pyexec.run();
   175     }
   176 
   177     @Override
   178     public boolean isActionEnabled(Lookup context) throws IllegalArgumentException {
   179         return true;
   180     }
   181 
   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) {
   186             return null;
   187         }
   188         FileObject fo = null;
   189         for (FileObject root : roots) {
   190             fo = root.getFileObject(setupFile);
   191             if (fo != null) {
   192                 break;
   193             }
   194         }
   195         return fo;
   196     }
   197 
   198     private void createSetupFile(FileObject template, FileObject parent, String filename) throws IOException {
   199         try {
   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);
   206             }
   207 
   208             //create the map of objects
   209             Map ftl_objects = new HashMap();
   210             ftl_objects.put("project_name", getProject().getName());
   211 
   212 
   213             dataTemplate.createFromTemplate(dataFolder, filename, ftl_objects);
   214 
   215 
   216         } catch (DataObjectNotFoundException ex) {
   217             Exceptions.printStackTrace(ex);
   218         }
   219     }
   220 
   221     private void deleteSetupFile(FileObject src) throws IOException {
   222         src.getFileObject("setup", "py").delete();
   223 
   224     }
   225 }