KiCad PCB EDA Suite
Loading...
Searching...
No Matches
bom_plugins.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include "bom_plugins.h"
27#include <config.h>
28#include <paths.h>
29#include <wx/ffile.h>
30#include <wx/log.h>
31
32
33const wxChar BOM_TRACE[] = wxT( "BOM_GENERATORS" );
34
35
37 : m_storedPath( aFile )
38{
39 m_isOk = false;
40 m_file = wxFileName( aFile );
41
42 if( !wxFile::Exists( m_file.GetFullPath() ) )
44
45 if( !wxFile::Exists( m_file.GetFullPath() ) )
46 {
47 m_info.Printf( _("Script file:\n%s\nnot found. Script not available."), aFile );
48 return;
49 }
50
51 m_isOk = true;
52
53 m_name = m_file.GetName();
54 wxString extension = m_file.GetExt().Lower();
55
56 // Important note:
57 // On Windows the right command command to run a python script is:
58 // python <script_path>/script.py
59 // and *not* python <script_path>\script.py
60 // Otherwise the script does not find some auxiliary pythons scripts needed by this script
61 if( extension == wxS( "xsl" ) )
62 {
63 m_info = readHeader( wxS( "-->" ) );
64 m_cmd = wxString::Format( wxS( "xsltproc -o \"%%O%s\" \"%s\" \"%%I\"" ),
66 m_file.GetFullPath() );
67 }
68 else if( extension == wxS( "py" ) )
69 {
70 m_info = readHeader( wxS( "\"\"\"" ) );
71#ifdef __WINDOWS__
72 m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O%s\"",
73 m_file.GetPath(),
74 m_file.GetFullName(),
76#else
77// For macOS, we want to use the Python we bundle along, rather than just PYTHON_EXECUTABLE.
78// For non-Windows, non-macOS, we can call out to PYTHON_EXECUTABLE.
79#ifdef __APPLE__
80 // python is at Contents/Frameworks/Python.framework/Versions/Current/bin/python3
81
82 // Of course, for macOS, it's not quite that simple, since the relative path
83 // will depend on if we are in standalone mode or not.
84
85 // (If we're going to call out to python like this in other places, we probably want to
86 // think about pulling this into PATHS.)
87
88 wxFileName python( PATHS::GetOSXKicadDataDir(), wxEmptyString );
89 python.RemoveLastDir();
90 python.AppendDir( wxT( "Frameworks" ) );
91 python.AppendDir( wxT( "Python.framework" ) );
92 python.AppendDir( wxT( "Versions" ) );
93 python.AppendDir( wxT( "Current" ) );
94 python.AppendDir( wxT( "bin" ) );
95 python.SetFullName(wxT( "python3" ) );
96
97 wxString interpreter = python.GetFullPath();
98#else
99 wxString interpreter = wxString::FromUTF8Unchecked( PYTHON_EXECUTABLE );
100#endif
101 if( interpreter.IsEmpty() )
102 interpreter = wxT( "python" ); // For macOS, should we log here? Error here?
103
104 m_cmd = wxString::Format( "%s \"%s\" \"%%I\" \"%%O%s\"",
105 interpreter,
106 m_file.GetFullPath(),
108#endif
109 }
110#ifdef __WINDOWS__
111 else if( extension == wxS( "pyw" ) )
112 {
113 m_info = readHeader( wxS( "\"\"\"" ) );
114 m_cmd = wxString::Format( wxS( "pythonw \"%s/%s\" \"%%I\" \"%%O%s\"" ),
115 m_file.GetPath(),
116 m_file.GetFullName(),
118 }
119#endif /* __WINDOWS__ */
120 else // fallback
121 {
122 m_cmd = m_file.GetFullPath();
123 }
124
125 wxLogTrace( BOM_TRACE, wxS( "%s: extracted command line %s" ), m_name, m_cmd );
126}
127
128
129bool BOM_GENERATOR_HANDLER::IsValidGenerator( const wxString& aFile )
130{
131 wxFileName fn( aFile );
132 wxString ext = fn.GetExt().Lower();
133
134 for( const auto& pluginExt : { wxS( "xsl" ), wxS( "py" ), wxS( "pyw" ) } )
135 {
136 if( pluginExt == ext )
137 return true;
138 }
139
140 return false;
141}
142
143
144wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
145{
146 if( aEndSection.IsEmpty() )
147 return wxEmptyString;
148
149 wxFFile fdata( m_file.GetFullPath(), wxS( "rb" ) ); // dtor will close the file
150 wxString data;
151
152 if( !fdata.ReadAll( &data ) )
153 return wxEmptyString;
154
155 const wxString header( wxS( "@package" ) );
156
157 // Extract substring between @package and endsection
158 size_t strstart = data.find( header );
159
160 if( strstart == wxString::npos )
161 return wxEmptyString;
162
163 strstart += header.Length();
164 size_t strend = data.find( aEndSection, strstart );
165
166 if( strend == wxString::npos )
167 return wxEmptyString;
168
169 // Remove empty line if any
170 while( data[strstart] < ' ' )
171 strstart++;
172
173 return data.SubString( strstart, strend - 1 );
174}
175
176
177wxString BOM_GENERATOR_HANDLER::getOutputExtension( const wxString& aHeader )
178{
179 // search header for extension after %O (extension includes '.')
180 // looks for output argument of the form `"%O.extension"`
181 const wxString outputarg( wxS( "\"%O" ) );
182
183 size_t strstart = aHeader.find( outputarg );
184
185 if( strstart == wxString::npos )
186 return wxEmptyString;
187
188 strstart += outputarg.Length();
189 size_t strend = aHeader.find( wxS( "\"" ), strstart );
190
191 if( strend == wxString::npos )
192 return wxEmptyString;
193
194 return aHeader.SubString( strstart, strend - 1 );
195}
196
197
199{
200 if( m_file.IsAbsolute() && m_file.Exists( wxFILE_EXISTS_REGULAR ) )
201 {
202 wxLogTrace( BOM_TRACE, wxS( "%s found directly" ), m_file.GetFullPath() );
203 return m_file;
204 }
205
206 wxFileName test( PATHS::GetUserPluginsPath(), m_file.GetName(), m_file.GetExt() );
207
208 if( test.Exists( wxFILE_EXISTS_REGULAR ) )
209 {
210 wxLogTrace( BOM_TRACE, wxS( "%s found in user plugins path %s" ), m_file.GetFullName(),
212 return test;
213 }
214
215 test = wxFileName( PATHS::GetStockPluginsPath(), m_file.GetName(), m_file.GetExt() );
216
217 if( test.Exists( wxFILE_EXISTS_REGULAR ) )
218 {
219 wxLogTrace( BOM_TRACE, wxS( "%s found in stock plugins path %s" ), m_file.GetFullName(),
221 return test;
222 }
223
224 wxLogTrace( BOM_TRACE, wxS( "Could not find %s (checked %s, %s)" ), m_file.GetFullName(),
226
227 return m_file;
228}
const wxChar BOM_TRACE[]
Definition: bom_plugins.cpp:33
const wxChar BOM_TRACE[]
Definition: bom_plugins.cpp:33
wxString m_name
Command to execute the plugin.
Definition: bom_plugins.h:161
static bool IsValidGenerator(const wxString &aFile)
Return true if a file name matches a recognized plugin format.
wxString readHeader(const wxString &aEndSection)
Read the plugin file header.
bool m_isOk
Path to the plugin.
Definition: bom_plugins.h:152
BOM_GENERATOR_HANDLER(const wxString &aFile)
Definition: bom_plugins.cpp:36
wxString m_info
Plugin specific options.
Definition: bom_plugins.h:167
wxFileName m_file
Path to the plugin stored in config (can be absolute or just a filename)
Definition: bom_plugins.h:155
wxString m_cmd
Description of the plugin (normally from the plugin header)
Definition: bom_plugins.h:164
wxFileName FindFilePath() const
Returns the calculated path to the plugin: if the path is already absolute and exists,...
static wxString getOutputExtension(const wxString &aHeader)
Extracts the output BOM file's extension, including the '.
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:55
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:332
#define _(s)