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-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
|
2018-11-12 13:45:07 +00:00
|
|
|
if( extension == "xsl" )
|
|
|
|
{
|
|
|
|
m_info = readHeader( "-->" );
|
2021-02-12 02:17:45 +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
|
|
|
}
|
|
|
|
else if( extension == "py" )
|
|
|
|
{
|
|
|
|
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\"",
|
|
|
|
m_file.GetPath(), m_file.GetFullName(),
|
|
|
|
getOutputExtension( m_info ) );
|
2018-11-19 16:26:32 +00:00
|
|
|
#else
|
2021-02-12 02:17:45 +00:00
|
|
|
m_cmd = wxString::Format( "python \"%s\" \"%%I\" \"%%O%s\"", 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__
|
|
|
|
else if( extension == "pyw" )
|
|
|
|
{
|
|
|
|
m_info = readHeader( "\"\"\"" );
|
2021-02-12 02:17:45 +00:00
|
|
|
m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O%s\"",
|
|
|
|
m_file.GetPath(), m_file.GetFullName(),
|
|
|
|
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
|
|
|
|
|
|
|
wxLogTrace( BOM_TRACE, "%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();
|
|
|
|
|
|
|
|
for( const auto& pluginExt : { "xsl", "py", "pyw" } )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-12-12 18:11:51 +00:00
|
|
|
wxFFile fdata( m_file.GetFullPath(), "rb" ); // dtor will close the file
|
2018-11-12 13:45:07 +00:00
|
|
|
wxString data;
|
|
|
|
|
|
|
|
if( !fdata.ReadAll( &data ) )
|
|
|
|
return wxEmptyString;
|
|
|
|
|
|
|
|
const wxString header( "@package" );
|
|
|
|
|
|
|
|
// 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 ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( BOM_TRACE, "%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, "%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, "%s found in stock plugins path %s", m_file.GetFullName(),
|
|
|
|
PATHS::GetStockPluginsPath() );
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogTrace( BOM_TRACE, "Could not find %s (checked %s, %s)", m_file.GetFullName(),
|
|
|
|
PATHS::GetUserPluginsPath(), PATHS::GetStockPluginsPath() );
|
|
|
|
|
|
|
|
return m_file;
|
|
|
|
}
|