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 <paths.h>
27 #include <wx/ffile.h>
28 #include <wx/log.h>
29 
30 
31 const wxChar BOM_TRACE[] = wxT( "BOM_GENERATORS" );
32 
33 
35  : m_storedPath( aFile )
36 {
37  m_isOk = false;
38  m_file = wxFileName( aFile );
39 
40  if( !wxFile::Exists( m_file.GetFullPath() ) )
41  m_file = FindFilePath();
42 
43  if( !wxFile::Exists( m_file.GetFullPath() ) )
44  {
45  m_info.Printf( _("Script file:\n%s\nnot found. Script not available."), aFile );
46  return;
47  }
48 
49  m_isOk = true;
50 
51  m_name = m_file.GetName();
52  wxString extension = m_file.GetExt().Lower();
53 
54  // Important note:
55  // On Windows the right command command to run a python script is:
56  // python <script_path>/script.py
57  // and *not* python <script_path>\script.py
58  // Otherwise the script does not find some auxiliary pythons scripts needed by this script
59  if( extension == "xsl" )
60  {
61  m_info = readHeader( "-->" );
62  m_cmd = wxString::Format( "xsltproc -o \"%%O%s\" \"%s\" \"%%I\"",
63  getOutputExtension( m_info ), m_file.GetFullPath() );
64  }
65  else if( extension == "py" )
66  {
67  m_info = readHeader( "\"\"\"" );
68 #ifdef __WINDOWS__
69  m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O%s\"",
70  m_file.GetPath(), m_file.GetFullName(),
72 #else
73  m_cmd = wxString::Format( "python \"%s\" \"%%I\" \"%%O%s\"", m_file.GetFullPath(),
75 #endif
76  }
77 #ifdef __WINDOWS__
78  else if( extension == "pyw" )
79  {
80  m_info = readHeader( "\"\"\"" );
81  m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O%s\"",
82  m_file.GetPath(), m_file.GetFullName(),
84  }
85 #endif /* __WINDOWS__ */
86  else // fallback
87  {
88  m_cmd = m_file.GetFullPath();
89  }
90 
91  wxLogTrace( BOM_TRACE, "%s: extracted command line %s", m_name, m_cmd );
92 }
93 
94 
95 bool BOM_GENERATOR_HANDLER::IsValidGenerator( const wxString& aFile )
96 {
97  wxFileName fn( aFile );
98  wxString ext = fn.GetExt().Lower();
99 
100  for( const auto& pluginExt : { "xsl", "py", "pyw" } )
101  {
102  if( pluginExt == ext )
103  return true;
104  }
105 
106  return false;
107 }
108 
109 
110 wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
111 {
112  if( aEndSection.IsEmpty() )
113  return wxEmptyString;
114 
115  wxFFile fdata( m_file.GetFullPath(), "rb" ); // dtor will close the file
116  wxString data;
117 
118  if( !fdata.ReadAll( &data ) )
119  return wxEmptyString;
120 
121  const wxString header( "@package" );
122 
123  // Extract substring between @package and endsection
124  int strstart = data.Find( header );
125 
126  if( strstart == wxNOT_FOUND )
127  return wxEmptyString;
128 
129  strstart += header.Length();
130  int strend = data.find( aEndSection, strstart );
131 
132  if( strend == wxNOT_FOUND )
133  return wxEmptyString;
134 
135  // Remove empty line if any
136  while( data[strstart] < ' ' )
137  strstart++;
138 
139  return data.SubString( strstart, strend - 1 );
140 }
141 
142 
143 wxString BOM_GENERATOR_HANDLER::getOutputExtension( const wxString& aHeader )
144 {
145  // search header for extension after %O (extension includes '.')
146  // looks for output argument of the form `"%O.extension"`
147  const wxString outputarg( "\"%O" );
148 
149  int strstart = aHeader.Find( outputarg );
150 
151  if( strstart == wxNOT_FOUND )
152  return wxEmptyString;
153 
154  strstart += outputarg.Length();
155  int strend = aHeader.find( "\"", strstart );
156 
157  if( strend == wxNOT_FOUND )
158  return wxEmptyString;
159 
160  return aHeader.SubString( strstart, strend - 1 );
161 }
162 
163 
165 {
166  if( m_file.IsAbsolute() && m_file.Exists( wxFILE_EXISTS_REGULAR ) )
167  {
168  wxLogTrace( BOM_TRACE, "%s found directly", m_file.GetFullPath() );
169  return m_file;
170  }
171 
172  wxFileName test( PATHS::GetUserPluginsPath(), m_file.GetName(), m_file.GetExt() );
173 
174  if( test.Exists( wxFILE_EXISTS_REGULAR ) )
175  {
176  wxLogTrace( BOM_TRACE, "%s found in user plugins path %s", m_file.GetFullName(),
178  return test;
179  }
180 
181  test = wxFileName( PATHS::GetStockPluginsPath(), m_file.GetName(), m_file.GetExt() );
182 
183  if( test.Exists( wxFILE_EXISTS_REGULAR ) )
184  {
185  wxLogTrace( BOM_TRACE, "%s found in stock plugins path %s", m_file.GetFullName(),
187  return test;
188  }
189 
190  wxLogTrace( BOM_TRACE, "Could not find %s (checked %s, %s)", m_file.GetFullName(),
192 
193  return m_file;
194 }
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:157
wxString m_cmd
Description of the plugin (normally from the plugin header)
Definition: bom_plugins.h:166
const wxChar BOM_TRACE[]
Definition: bom_plugins.cpp:31
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:52
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.
Definition: bom_plugins.cpp:95
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:34
static wxString getOutputExtension(const wxString &aHeader)
Extracts the output BOM file's extension, including the '.
#define _(s)
Definition: 3d_actions.cpp:33
wxString m_name
Command to execute the plugin.
Definition: bom_plugins.h:163
wxString m_info
Plugin specific options.
Definition: bom_plugins.h:169
bool m_isOk
Path to the plugin.
Definition: bom_plugins.h:154
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:165