KiCad PCB EDA Suite
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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * https://www.gnu.org/licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include "bom_plugins.h"
26 #include <config.h>
27 #include <paths.h>
28 #include <wx/ffile.h>
29 #include <wx/log.h>
30 
31 
32 const wxChar BOM_TRACE[] = wxT( "BOM_GENERATORS" );
33 
34 
36  : m_storedPath( aFile )
37 {
38  m_isOk = false;
39  m_file = wxFileName( aFile );
40 
41  if( !wxFile::Exists( m_file.GetFullPath() ) )
42  m_file = FindFilePath();
43 
44  if( !wxFile::Exists( m_file.GetFullPath() ) )
45  {
46  m_info.Printf( _("Script file:\n%s\nnot found. Script not available."), aFile );
47  return;
48  }
49 
50  m_isOk = true;
51 
52  m_name = m_file.GetName();
53  wxString extension = m_file.GetExt().Lower();
54 
55  // Important note:
56  // On Windows the right command command to run a python script is:
57  // python <script_path>/script.py
58  // and *not* python <script_path>\script.py
59  // Otherwise the script does not find some auxiliary pythons scripts needed by this script
60  if( extension == "xsl" )
61  {
62  m_info = readHeader( "-->" );
63  m_cmd = wxString::Format( "xsltproc -o \"%%O%s\" \"%s\" \"%%I\"",
64  getOutputExtension( m_info ), m_file.GetFullPath() );
65  }
66  else if( extension == "py" )
67  {
68  m_info = readHeader( "\"\"\"" );
69 #ifdef __WINDOWS__
70  m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O%s\"",
71  m_file.GetPath(), m_file.GetFullName(),
73 #else
74  wxString interpreter = wxString::FromUTF8Unchecked( PYTHON_EXECUTABLE );
75 
76  if( interpreter.IsEmpty() )
77  interpreter = wxT( "python" );
78 
79  m_cmd = wxString::Format( "%s \"%s\" \"%%I\" \"%%O%s\"", interpreter, m_file.GetFullPath(),
81 #endif
82  }
83 #ifdef __WINDOWS__
84  else if( extension == "pyw" )
85  {
86  m_info = readHeader( "\"\"\"" );
87  m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O%s\"",
88  m_file.GetPath(), m_file.GetFullName(),
90  }
91 #endif /* __WINDOWS__ */
92  else // fallback
93  {
94  m_cmd = m_file.GetFullPath();
95  }
96 
97  wxLogTrace( BOM_TRACE, "%s: extracted command line %s", m_name, m_cmd );
98 }
99 
100 
101 bool BOM_GENERATOR_HANDLER::IsValidGenerator( const wxString& aFile )
102 {
103  wxFileName fn( aFile );
104  wxString ext = fn.GetExt().Lower();
105 
106  for( const auto& pluginExt : { "xsl", "py", "pyw" } )
107  {
108  if( pluginExt == ext )
109  return true;
110  }
111 
112  return false;
113 }
114 
115 
116 wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
117 {
118  if( aEndSection.IsEmpty() )
119  return wxEmptyString;
120 
121  wxFFile fdata( m_file.GetFullPath(), "rb" ); // dtor will close the file
122  wxString data;
123 
124  if( !fdata.ReadAll( &data ) )
125  return wxEmptyString;
126 
127  const wxString header( "@package" );
128 
129  // Extract substring between @package and endsection
130  int strstart = data.Find( header );
131 
132  if( strstart == wxNOT_FOUND )
133  return wxEmptyString;
134 
135  strstart += header.Length();
136  int strend = data.find( aEndSection, strstart );
137 
138  if( strend == wxNOT_FOUND )
139  return wxEmptyString;
140 
141  // Remove empty line if any
142  while( data[strstart] < ' ' )
143  strstart++;
144 
145  return data.SubString( strstart, strend - 1 );
146 }
147 
148 
149 wxString BOM_GENERATOR_HANDLER::getOutputExtension( const wxString& aHeader )
150 {
151  // search header for extension after %O (extension includes '.')
152  // looks for output argument of the form `"%O.extension"`
153  const wxString outputarg( "\"%O" );
154 
155  int strstart = aHeader.Find( outputarg );
156 
157  if( strstart == wxNOT_FOUND )
158  return wxEmptyString;
159 
160  strstart += outputarg.Length();
161  int strend = aHeader.find( "\"", strstart );
162 
163  if( strend == wxNOT_FOUND )
164  return wxEmptyString;
165 
166  return aHeader.SubString( strstart, strend - 1 );
167 }
168 
169 
171 {
172  if( m_file.IsAbsolute() && m_file.Exists( wxFILE_EXISTS_REGULAR ) )
173  {
174  wxLogTrace( BOM_TRACE, "%s found directly", m_file.GetFullPath() );
175  return m_file;
176  }
177 
178  wxFileName test( PATHS::GetUserPluginsPath(), m_file.GetName(), m_file.GetExt() );
179 
180  if( test.Exists( wxFILE_EXISTS_REGULAR ) )
181  {
182  wxLogTrace( BOM_TRACE, "%s found in user plugins path %s", m_file.GetFullName(),
184  return test;
185  }
186 
187  test = wxFileName( PATHS::GetStockPluginsPath(), m_file.GetName(), m_file.GetExt() );
188 
189  if( test.Exists( wxFILE_EXISTS_REGULAR ) )
190  {
191  wxLogTrace( BOM_TRACE, "%s found in stock plugins path %s", m_file.GetFullName(),
193  return test;
194  }
195 
196  wxLogTrace( BOM_TRACE, "Could not find %s (checked %s, %s)", m_file.GetFullName(),
198 
199  return m_file;
200 }
wxString readHeader(const wxString &aEndSection)
Read the plugin file header.
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
const wxChar BOM_TRACE[]
Definition: bom_plugins.cpp:32
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:53
wxFileName FindFilePath() const
Returns the calculated path to the plugin: if the path is already absolute and exists,...
static bool IsValidGenerator(const wxString &aFile)
Return true if a file name matches a recognized plugin format.
#define _(s)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
BOM_GENERATOR_HANDLER(const wxString &aFile)
Definition: bom_plugins.cpp:35
static wxString getOutputExtension(const wxString &aHeader)
Extracts the output BOM file's extension, including the '.
wxString m_name
Command to execute the plugin.
Definition: bom_plugins.h:161
wxString m_info
Plugin specific options.
Definition: bom_plugins.h:167
bool m_isOk
Path to the plugin.
Definition: bom_plugins.h:152
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:212