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
|
2023-01-16 02:28:40 +00:00
|
|
|
if( extension == wxS( "xsl" ) )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
2023-01-16 02:28:40 +00:00
|
|
|
m_info = readHeader( wxS( "-->" ) );
|
2023-01-17 04:14:38 +00:00
|
|
|
m_cmd = wxString::Format( wxS( "xsltproc -o \"%%O%s\" \"%s\" \"%%I\"" ),
|
2021-11-08 21:04:48 +00:00
|
|
|
getOutputExtension( m_info ),
|
|
|
|
m_file.GetFullPath() );
|
2018-11-12 13:45:07 +00:00
|
|
|
}
|
2023-01-16 02:28:40 +00:00
|
|
|
else if( extension == wxS( "py" ) )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
2023-01-16 02:28:40 +00:00
|
|
|
m_info = readHeader( wxS( "\"\"\"" ) );
|
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.
|
2022-01-31 23:57:56 +00:00
|
|
|
#ifdef __APPLE__
|
2021-12-10 20:36:01 +00:00
|
|
|
// 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
|
|
|
|
2021-11-08 21:04:48 +00:00
|
|
|
m_cmd = wxString::Format( "%s \"%s\" \"%%I\" \"%%O%s\"",
|
|
|
|
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__
|
2023-01-16 02:28:40 +00:00
|
|
|
else if( extension == wxS( "pyw" ) )
|
2018-11-12 13:45:07 +00:00
|
|
|
{
|
2023-01-16 02:28:40 +00:00
|
|
|
m_info = readHeader( wxS( "\"\"\"" ) );
|
2023-01-17 04:14:38 +00:00
|
|
|
m_cmd = wxString::Format( wxS( "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
|
|
|
|
2023-01-17 04:14:38 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxS( "%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();
|
|
|
|
|
2023-01-16 02:28:40 +00:00
|
|
|
for( const auto& pluginExt : { wxS( "xsl" ), wxS( "py" ), wxS( "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;
|
|
|
|
|
2023-01-17 04:14:38 +00:00
|
|
|
wxFFile fdata( m_file.GetFullPath(), wxS( "rb" ) ); // dtor will close the file
|
2018-11-12 13:45:07 +00:00
|
|
|
wxString data;
|
|
|
|
|
|
|
|
if( !fdata.ReadAll( &data ) )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
2023-01-16 02:28:40 +00:00
|
|
|
const wxString header( wxS( "@package" ) );
|
2018-11-12 13:45:07 +00:00
|
|
|
|
|
|
|
// Extract substring between @package and endsection
|
2024-05-28 14:30:16 +00:00
|
|
|
size_t strstart = data.find( header );
|
2018-11-12 13:45:07 +00:00
|
|
|
|
2024-05-28 14:30:16 +00:00
|
|
|
if( strstart == wxString::npos )
|
2018-11-12 13:45:07 +00:00
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
strstart += header.Length();
|
2024-05-28 14:30:16 +00:00
|
|
|
size_t strend = data.find( aEndSection, strstart );
|
2018-11-12 13:45:07 +00:00
|
|
|
|
2024-05-28 14:30:16 +00:00
|
|
|
if( strend == wxString::npos )
|
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"`
|
2023-01-17 04:14:38 +00:00
|
|
|
const wxString outputarg( wxS( "\"%O" ) );
|
2021-02-12 02:17:45 +00:00
|
|
|
|
2024-05-28 14:30:16 +00:00
|
|
|
size_t strstart = aHeader.find( outputarg );
|
2021-02-12 02:17:45 +00:00
|
|
|
|
2024-05-28 14:30:16 +00:00
|
|
|
if( strstart == wxString::npos )
|
2021-02-12 02:17:45 +00:00
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
strstart += outputarg.Length();
|
2024-05-28 14:30:16 +00:00
|
|
|
size_t strend = aHeader.find( wxS( "\"" ), strstart );
|
2021-02-12 02:17:45 +00:00
|
|
|
|
2024-05-28 14:30:16 +00:00
|
|
|
if( strend == wxString::npos )
|
2021-02-12 02:17:45 +00:00
|
|
|
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 ) )
|
|
|
|
{
|
2023-01-16 02:28:40 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxS( "%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 ) )
|
|
|
|
{
|
2023-01-16 02:28:40 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxS( "%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 ) )
|
|
|
|
{
|
2023-01-16 02:28:40 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxS( "%s found in stock plugins path %s" ), m_file.GetFullName(),
|
2021-02-25 03:37:30 +00:00
|
|
|
PATHS::GetStockPluginsPath() );
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
2023-01-16 02:28:40 +00:00
|
|
|
wxLogTrace( BOM_TRACE, wxS( "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;
|
|
|
|
}
|