You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.6 KiB

  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. #include "bom_plugins.h"
  25. BOM_PLUGIN::BOM_PLUGIN( const wxString& aFile )
  26. : m_file( aFile )
  27. {
  28. m_isOk = false;
  29. if( !wxFile::Exists( aFile ) )
  30. {
  31. m_info.Printf( _("Plugin file:\n%s\nnot found. Plugin not available."), aFile );
  32. return;
  33. }
  34. m_isOk = true;
  35. m_name = m_file.GetName();
  36. wxString extension = m_file.GetExt().Lower();
  37. // Important note:
  38. // On Windows the right command command to run a python script is:
  39. // python <script_path>/script.py
  40. // and *not* python <script_path>\script.py
  41. // Otherwise the script does not find some auxiliary pythons scripts needed by this script
  42. if( extension == "xsl" )
  43. {
  44. m_info = readHeader( "-->" );
  45. m_cmd = wxString::Format( "xsltproc -o \"%%O\" \"%s\" \"%%I\"", m_file.GetFullPath() );
  46. }
  47. else if( extension == "py" )
  48. {
  49. m_info = readHeader( "\"\"\"" );
  50. #ifdef __WINDOWS__
  51. m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O\"",
  52. m_file.GetPath(), m_file.GetFullName() );
  53. #else
  54. m_cmd = wxString::Format( "python \"%s\" \"%%I\" \"%%O\"", m_file.GetFullPath() );
  55. #endif
  56. }
  57. #ifdef __WINDOWS__
  58. else if( extension == "pyw" )
  59. {
  60. m_info = readHeader( "\"\"\"" );
  61. m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O\"",
  62. m_file.GetPath(),m_file.GetFullName() );
  63. }
  64. #endif /* __WINDOWS__ */
  65. else // fallback
  66. {
  67. m_cmd = m_file.GetFullPath();
  68. }
  69. }
  70. bool BOM_PLUGIN::IsPlugin( const wxString& aFile )
  71. {
  72. wxFileName fn( aFile );
  73. wxString ext = fn.GetExt().Lower();
  74. for( const auto& pluginExt : { "xsl", "py", "pyw" } )
  75. {
  76. if( pluginExt == ext )
  77. return true;
  78. }
  79. return false;
  80. }
  81. wxString BOM_PLUGIN::readHeader( const wxString& aEndSection )
  82. {
  83. if( aEndSection.IsEmpty() )
  84. return wxEmptyString;
  85. wxFile fdata( m_file.GetFullPath() ); // dtor will close the file
  86. wxString data;
  87. if( !fdata.ReadAll( &data ) )
  88. return wxEmptyString;
  89. const wxString header( "@package" );
  90. // Extract substring between @package and endsection
  91. int strstart = data.Find( header );
  92. if( strstart == wxNOT_FOUND )
  93. return wxEmptyString;
  94. strstart += header.Length();
  95. int strend = data.find( aEndSection, strstart );
  96. if( strend == wxNOT_FOUND)
  97. return wxEmptyString;
  98. // Remove empty line if any
  99. while( data[strstart] < ' ' )
  100. strstart++;
  101. return data.SubString( strstart, strend - 1 );
  102. }