2018-11-12 13:45:07 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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"
|
2021-06-16 02:52:16 +00:00
|
|
|
#include <config.h>
|
2021-02-25 03:37:30 +00:00
|
|
|
#include <paths.h>
|
2020-12-12 18:11:51 +00:00
|
|
|
#include <wx/ffile.h>
|
2021-02-25 03:37:30 +00:00
|
|
|
#include <wx/log.h>
|
|
|
|
|
|
|
|
|
2021-02-25 09:44:14 +00:00
|
|
|
const wxChar BOM_TRACE[] = wxT( "BOM_GENERATORS" );
|
2021-02-25 03:37:30 +00:00
|
|
|
|
2018-11-12 13:45:07 +00:00
|
|
|
|
2019-05-27 18:43:39 +00:00
|
|
|
BOM_GENERATOR_HANDLER::BOM_GENERATOR_HANDLER( const wxString& aFile )
|
2021-02-26 03:31:44 +00:00
|
|
|
: m_storedPath( aFile )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
2019-01-14 12:14:13 +00:00
|
|
|
m_isOk = false;
|
2021-02-26 03:31:44 +00:00
|
|
|
m_file = wxFileName( aFile );
|
2019-01-14 12:14:13 +00:00
|
|
|
|
2021-02-25 03:37:30 +00:00
|
|
|
if( !wxFile::Exists( m_file.GetFullPath() ) )
|
|
|
|
m_file = FindFilePath();
|
|
|
|
|
|
|
|
if( !wxFile::Exists( m_file.GetFullPath() ) )
|
2019-01-14 12:14:13 +00:00
|
|
|
{
|
2019-05-27 18:43:39 +00:00
|
|
|
m_info.Printf( _("Script file:\n%s\nnot found. Script not available."), aFile );
|
2019-01-14 12:14:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_isOk = true;
|
2018-11-12 13:45:07 +00:00
|
|
|
|
|
|
|
m_name = m_file.GetName();
|
|
|
|
wxString extension = m_file.GetExt().Lower();
|
|
|
|
|
2018-11-19 16:26:32 +00:00
|
|
|
// 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
|
2022-02-08 23:14:18 +00:00
|
|
|
if( extension == wxT( "xsl" ) )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
2022-02-08 23:14:18 +00:00
|
|
|
m_info = readHeader( wxT( "-->" ) );
|
2021-11-08 21:04:48 +00:00
|
|
|
m_cmd = wxString::Format( "xsltproc -o \"%%O%s\" \"%s\" \"%%I\"",
|
|
|
|
getOutputExtension( m_info ),
|
|
|
|
m_file.GetFullPath() );
|
2018-11-12 13:45:07 +00:00
|
|
|
}
|
2022-02-08 23:14:18 +00:00
|
|
|
else if( extension == wxT( "py" ) )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
|
|
|
m_info = readHeader( "\"\"\"" );
|
2018-11-19 16:26:32 +00:00
|
|
|
#ifdef __WINDOWS__
|
2021-02-12 02:17:45 +00:00
|
|
|
m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O%s\"",
|
2021-11-08 21:04:48 +00:00
|
|
|
m_file.GetPath(),
|
|
|
|
m_file.GetFullName(),
|
2021-02-12 02:17:45 +00:00
|
|
|
getOutputExtension( m_info ) );
|
2018-11-19 16:26:32 +00:00
|
|
|
#else
|
2021-12-10 20:36:01 +00:00
|
|
|
// 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" ) );
|
2021-06-16 02:52:16 +00:00
|
|
|
|
2021-12-10 20:36:01 +00:00
|
|
|
wxString interpreter = python.GetFullPath();
|
|
|
|
#else
|
|
|
|
wxString interpreter = wxString::FromUTF8Unchecked( PYTHON_EXECUTABLE );
|
|
|
|
#endif
|
2021-06-16 02:52:16 +00:00
|
|
|
if( interpreter.IsEmpty() )
|
2021-12-10 20:36:01 +00:00
|
|
|
interpreter = wxT( "python" ); // For macOS, should we log here? Error here?
|
2021-06-16 02:52:16 +00:00
|
|
|
|
2022-02-09 18:53:09 +00:00
|
|
|
m_cmd = wxString::Format( wxT( "\"%s\" \"%s\" \"%%I\" \"%%O%s\"" ),
|
2021-11-08 21:04:48 +00:00
|
|
|
interpreter,
|
|
|
|
m_file.GetFullPath(),
|
|
|
|
getOutputExtension( m_info ) );
|
2018-11-19 16:26:32 +00:00
|
|
|
#endif
|
2018-11-12 13:45:07 +00:00
|
|
|
}
|
|
|
|
#ifdef __WINDOWS__
|
2022-02-08 23:14:18 +00:00
|
|
|
else if( extension == wxT( "pyw" ) )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
|
|
|
m_info = readHeader( "\"\"\"" );
|
2021-02-12 02:17:45 +00:00
|
|
|
m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O%s\"",
|
2021-11-08 21:04:48 +00:00
|
|
|
m_file.GetPath(),
|
|
|
|
m_file.GetFullName(),
|
2021-02-12 02:17:45 +00:00
|
|
|
getOutputExtension( m_info ) );
|
2018-11-12 13:45:07 +00:00
|
|
|
}
|
|
|
|
#endif /* __WINDOWS__ */
|
|
|
|
else // fallback
|
|
|
|
{
|
|
|
|
m_cmd = m_file.GetFullPath();
|
|
|
|
}
|
2021-02-25 03:37:30 +00:00
|
|
|
|
2022-02-08 23:14:18 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxT( "%s: extracted command line %s" ), m_name, m_cmd );
|
2018-11-12 13:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 18:43:39 +00:00
|
|
|
bool BOM_GENERATOR_HANDLER::IsValidGenerator( const wxString& aFile )
|
2018-11-19 13:44:26 +00:00
|
|
|
{
|
|
|
|
wxFileName fn( aFile );
|
|
|
|
wxString ext = fn.GetExt().Lower();
|
|
|
|
|
2022-02-08 23:14:18 +00:00
|
|
|
for( const auto& pluginExt : { wxT( "xsl" ), wxT( "py" ), wxT( "pyw" ) } )
|
2018-11-19 13:44:26 +00:00
|
|
|
{
|
|
|
|
if( pluginExt == ext )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 18:43:39 +00:00
|
|
|
wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
|
|
|
if( aEndSection.IsEmpty() )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
2022-02-08 23:14:18 +00:00
|
|
|
wxFFile fdata( m_file.GetFullPath(), wxT( "rb" ) ); // dtor will close the file
|
2018-11-12 13:45:07 +00:00
|
|
|
wxString data;
|
|
|
|
|
|
|
|
if( !fdata.ReadAll( &data ) )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
2022-02-08 23:14:18 +00:00
|
|
|
const wxString header( wxT( "@package" ) );
|
2018-11-12 13:45:07 +00:00
|
|
|
|
|
|
|
// Extract substring between @package and endsection
|
|
|
|
int strstart = data.Find( header );
|
|
|
|
|
|
|
|
if( strstart == wxNOT_FOUND )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
strstart += header.Length();
|
|
|
|
int strend = data.find( aEndSection, strstart );
|
|
|
|
|
2021-02-12 02:17:45 +00:00
|
|
|
if( strend == wxNOT_FOUND )
|
2018-11-12 13:45:07 +00:00
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
// Remove empty line if any
|
|
|
|
while( data[strstart] < ' ' )
|
|
|
|
strstart++;
|
|
|
|
|
|
|
|
return data.SubString( strstart, strend - 1 );
|
|
|
|
}
|
2021-02-12 02:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
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( "\"%O" );
|
|
|
|
|
|
|
|
int strstart = aHeader.Find( outputarg );
|
|
|
|
|
|
|
|
if( strstart == wxNOT_FOUND )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
strstart += outputarg.Length();
|
|
|
|
int strend = aHeader.find( "\"", strstart );
|
|
|
|
|
|
|
|
if( strend == wxNOT_FOUND )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
return aHeader.SubString( strstart, strend - 1 );
|
|
|
|
}
|
2021-02-25 03:37:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
wxFileName BOM_GENERATOR_HANDLER::FindFilePath() const
|
|
|
|
{
|
|
|
|
if( m_file.IsAbsolute() && m_file.Exists( wxFILE_EXISTS_REGULAR ) )
|
|
|
|
{
|
2022-02-08 23:14:18 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxT( "%s found directly" ), m_file.GetFullPath() );
|
2021-02-25 03:37:30 +00:00
|
|
|
return m_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxFileName test( PATHS::GetUserPluginsPath(), m_file.GetName(), m_file.GetExt() );
|
|
|
|
|
|
|
|
if( test.Exists( wxFILE_EXISTS_REGULAR ) )
|
|
|
|
{
|
2022-02-08 23:14:18 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxT( "%s found in user plugins path %s" ), m_file.GetFullName(),
|
2021-02-25 03:37:30 +00:00
|
|
|
PATHS::GetUserPluginsPath() );
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
|
|
|
test = wxFileName( PATHS::GetStockPluginsPath(), m_file.GetName(), m_file.GetExt() );
|
|
|
|
|
|
|
|
if( test.Exists( wxFILE_EXISTS_REGULAR ) )
|
|
|
|
{
|
2022-02-08 23:14:18 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxT( "%s found in stock plugins path %s" ), m_file.GetFullName(),
|
2021-02-25 03:37:30 +00:00
|
|
|
PATHS::GetStockPluginsPath() );
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
2022-02-08 23:14:18 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxT( "Could not find %s (checked %s, %s)" ), m_file.GetFullName(),
|
2021-02-25 03:37:30 +00:00
|
|
|
PATHS::GetUserPluginsPath(), PATHS::GetStockPluginsPath() );
|
|
|
|
|
|
|
|
return m_file;
|
|
|
|
}
|