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