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