2013-09-21 07:30:23 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2015-04-22 12:02:18 +00:00
|
|
|
* Copyright (C) 2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2020-08-18 14:17:16 +00:00
|
|
|
* Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-21 07:30:23 +00:00
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-07-24 19:02:59 +00:00
|
|
|
Note:
|
|
|
|
If you are using this plugin without the supporting nginx caching server, then you
|
|
|
|
will never be happy with its performance. However, it is the fastest plugin in
|
|
|
|
existence when used with a local nginx and the nginx configuration file in this
|
|
|
|
directory. Nginx can be running in house on your network anywhere for this statement
|
|
|
|
to be true.
|
|
|
|
|
|
|
|
Comments below pertain to use without nginx, so are not relevant in every case:
|
2014-01-27 15:51:14 +00:00
|
|
|
|
|
|
|
While exploring the possibility of local caching of the zip file, I discovered
|
|
|
|
this command to retrieve the time stamp of the last commit into any particular
|
|
|
|
repo:
|
|
|
|
|
2014-01-28 01:29:26 +00:00
|
|
|
$time curl -I -i https://api.github.com/repos/KiCad/Mounting_Holes.pretty/commits
|
2014-01-27 15:51:14 +00:00
|
|
|
|
|
|
|
This gets just the header to what would otherwise return information on the repo
|
|
|
|
in JSON format, and is reasonably faster than actually getting the repo
|
|
|
|
in zip form. However it still takes 5 seconds or more when github is busy, so
|
|
|
|
I have lost my enthusiasm for local caching until a faster time stamp retrieval
|
|
|
|
mechanism can be found, or github gets more servers. But note that the occasionally
|
|
|
|
slow response is the exception rather than the norm. Normally the response is
|
|
|
|
down around a 1/3 of a second. The information we would use is in the header
|
2015-12-30 07:46:32 +00:00
|
|
|
named "Last-Modified" as seen below.
|
2014-01-27 15:51:14 +00:00
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Server: GitHub.com
|
|
|
|
Date: Mon, 27 Jan 2014 15:46:46 GMT
|
|
|
|
Content-Type: application/json; charset=utf-8
|
|
|
|
Status: 200 OK
|
|
|
|
X-RateLimit-Limit: 60
|
|
|
|
X-RateLimit-Remaining: 49
|
|
|
|
X-RateLimit-Reset: 1390839612
|
|
|
|
Cache-Control: public, max-age=60, s-maxage=60
|
|
|
|
Last-Modified: Mon, 02 Dec 2013 10:08:51 GMT
|
|
|
|
ETag: "3d04d760f469f2516a51a56eac63bbd5"
|
|
|
|
Vary: Accept
|
|
|
|
X-GitHub-Media-Type: github.beta
|
|
|
|
X-Content-Type-Options: nosniff
|
|
|
|
Content-Length: 6310
|
|
|
|
Access-Control-Allow-Credentials: true
|
|
|
|
Access-Control-Expose-Headers: ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
|
|
|
|
Access-Control-Allow-Origin: *
|
|
|
|
X-GitHub-Request-Id: 411087C2:659E:50FD6E6:52E67F66
|
|
|
|
Vary: Accept-Encoding
|
2013-09-21 07:30:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <boost/ptr_container/ptr_map.hpp>
|
2013-11-27 06:51:49 +00:00
|
|
|
#include <set>
|
2013-11-11 08:01:04 +00:00
|
|
|
|
2020-02-21 16:12:25 +00:00
|
|
|
#include <kicad_curl/kicad_curl_easy.h> // Include before any wx file
|
|
|
|
|
2013-09-21 07:30:23 +00:00
|
|
|
#include <wx/zipstrm.h>
|
|
|
|
#include <wx/mstream.h>
|
|
|
|
#include <wx/uri.h>
|
|
|
|
|
|
|
|
#include <fctsys.h>
|
2013-11-11 08:01:04 +00:00
|
|
|
|
2013-09-21 07:30:23 +00:00
|
|
|
#include <io_mgr.h>
|
|
|
|
#include <richio.h>
|
|
|
|
#include <pcb_parser.h>
|
|
|
|
#include <class_board.h>
|
|
|
|
#include <github_plugin.h>
|
2013-09-23 15:57:15 +00:00
|
|
|
#include <class_module.h>
|
2013-11-11 08:01:04 +00:00
|
|
|
#include <macros.h>
|
2013-11-27 06:04:04 +00:00
|
|
|
#include <fp_lib_table.h> // ExpandSubstitutions()
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2015-12-21 20:30:33 +00:00
|
|
|
|
2013-12-13 16:39:46 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
static const char* PRETTY_DIR = "allow_pretty_writing_to_this_dir";
|
|
|
|
|
|
|
|
|
2017-07-25 19:14:31 +00:00
|
|
|
typedef boost::ptr_map< wxString, wxZipEntry > MODULE_MAP;
|
|
|
|
typedef MODULE_MAP::iterator MODULE_ITER;
|
|
|
|
typedef MODULE_MAP::const_iterator MODULE_CITER;
|
2013-09-21 07:30:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-10 14:31:00 +00:00
|
|
|
* GH_CACHE
|
2013-10-03 13:40:00 +00:00
|
|
|
* assists only within GITHUB_PLUGIN and holds a map of footprint name to wxZipEntry
|
2013-09-21 07:30:23 +00:00
|
|
|
*/
|
2013-09-22 03:57:40 +00:00
|
|
|
struct GH_CACHE : public MODULE_MAP
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
|
|
|
// MODULE_MAP is a boost::ptr_map template, made into a class hereby.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
GITHUB_PLUGIN::GITHUB_PLUGIN() :
|
2013-11-27 06:04:04 +00:00
|
|
|
PCB_IO(),
|
|
|
|
m_gh_cache( 0 )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GITHUB_PLUGIN::~GITHUB_PLUGIN()
|
|
|
|
{
|
2013-11-27 06:04:04 +00:00
|
|
|
delete m_gh_cache;
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
const wxString GITHUB_PLUGIN::PluginName() const
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2016-01-10 21:44:37 +00:00
|
|
|
return "Github";
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
const wxString GITHUB_PLUGIN::GetFileExtension() const
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2013-11-27 06:04:04 +00:00
|
|
|
return wxEmptyString;
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
void GITHUB_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibPath,
|
|
|
|
bool aBestEfforts, const PROPERTIES* aProperties )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2019-08-31 14:18:27 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
cacheLib( aLibPath, aProperties );
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
typedef std::set<wxString> MYSET;
|
2013-11-27 06:51:49 +00:00
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
MYSET unique;
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
if( m_pretty_dir.size() )
|
|
|
|
{
|
|
|
|
wxArrayString locals;
|
2017-06-11 20:20:44 +00:00
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
PCB_IO::FootprintEnumerate( locals, m_pretty_dir, aBestEfforts );
|
2013-11-27 06:51:49 +00:00
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
for( unsigned i=0; i<locals.GetCount(); ++i )
|
|
|
|
unique.insert( locals[i] );
|
|
|
|
}
|
2013-11-27 06:04:04 +00:00
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
for( MODULE_ITER it = m_gh_cache->begin(); it!=m_gh_cache->end(); ++it )
|
|
|
|
unique.insert( it->first );
|
2013-11-27 06:51:49 +00:00
|
|
|
|
2019-08-31 14:18:27 +00:00
|
|
|
for( MYSET::const_iterator it = unique.begin(); it != unique.end(); ++it )
|
|
|
|
aFootprintNames.Add( *it );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
2013-11-27 06:51:49 +00:00
|
|
|
{
|
2019-08-31 14:18:27 +00:00
|
|
|
if( !aBestEfforts )
|
|
|
|
throw ioe;
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-23 00:59:25 +00:00
|
|
|
void GITHUB_PLUGIN::PrefetchLib(
|
|
|
|
const wxString& aLibraryPath, const PROPERTIES* aProperties )
|
|
|
|
{
|
|
|
|
if( m_lib_path != aLibraryPath )
|
|
|
|
{
|
|
|
|
m_zip_image.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteGetZip( aLibraryPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-21 07:30:23 +00:00
|
|
|
MODULE* GITHUB_PLUGIN::FootprintLoad( const wxString& aLibraryPath,
|
2013-09-30 21:26:49 +00:00
|
|
|
const wxString& aFootprintName, const PROPERTIES* aProperties )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2013-11-27 06:04:04 +00:00
|
|
|
// clear or set to valid the variable m_pretty_dir
|
|
|
|
cacheLib( aLibraryPath, aProperties );
|
|
|
|
|
|
|
|
if( m_pretty_dir.size() )
|
|
|
|
{
|
|
|
|
// API has FootprintLoad() *not* throwing an exception if footprint not found.
|
|
|
|
MODULE* local = PCB_IO::FootprintLoad( m_pretty_dir, aFootprintName, aProperties );
|
|
|
|
|
|
|
|
if( local )
|
2013-12-03 18:37:21 +00:00
|
|
|
{
|
|
|
|
// It has worked, see <src>/scripts/test_kicad_plugin.py. So this was not firing:
|
2016-11-20 23:35:08 +00:00
|
|
|
// wxASSERT( aFootprintName == FROM_UTF8( local->GetFPID().GetLibItemName().c_str() ) );
|
2013-12-03 18:37:21 +00:00
|
|
|
// Moving it to higher API layer FP_LIB_TABLE::FootprintLoad().
|
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
return local;
|
2013-12-03 18:37:21 +00:00
|
|
|
}
|
2013-11-27 06:04:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:14:31 +00:00
|
|
|
MODULE_CITER it = m_gh_cache->find( aFootprintName );
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
if( it != m_gh_cache->end() ) // fp_name is present
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2016-02-08 13:04:42 +00:00
|
|
|
//std::string::data() ensures that the referenced data block is contiguous.
|
2016-02-04 16:49:20 +00:00
|
|
|
wxMemoryInputStream mis( m_zip_image.data(), m_zip_image.size() );
|
2013-09-21 07:30:23 +00:00
|
|
|
|
|
|
|
// This decoder should always be UTF8, since it was saved that way by git.
|
|
|
|
// That is, since pretty footprints are UTF8, and they were pushed to the
|
|
|
|
// github repo, they are still UTF8.
|
|
|
|
wxZipInputStream zis( mis, wxConvUTF8 );
|
|
|
|
wxZipEntry* entry = (wxZipEntry*) it->second; // remove "const"-ness
|
|
|
|
|
|
|
|
if( zis.OpenEntry( *entry ) )
|
|
|
|
{
|
2014-07-05 20:42:59 +00:00
|
|
|
INPUTSTREAM_LINE_READER reader( &zis, aLibraryPath );
|
2016-02-04 16:49:20 +00:00
|
|
|
|
2013-12-03 18:37:21 +00:00
|
|
|
// I am a PCB_IO derivative with my own PCB_PARSER
|
|
|
|
m_parser->SetLineReader( &reader ); // ownership not passed
|
|
|
|
|
|
|
|
MODULE* ret = (MODULE*) m_parser->Parse();
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2016-02-04 16:49:20 +00:00
|
|
|
// In a github library, (as well as in a "KiCad" library) the name of
|
|
|
|
// the pretty file defines the footprint name. That filename trumps
|
|
|
|
// any name found in the pretty file; any name in the pretty file
|
|
|
|
// must be ignored here. Also, the library nickname is unknown in
|
|
|
|
// this context so clear it just in case.
|
2018-07-26 14:38:30 +00:00
|
|
|
ret->SetFPID( LIB_ID( wxEmptyString, aFootprintName ) );
|
2013-09-23 15:57:15 +00:00
|
|
|
|
2013-09-21 07:30:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL; // this API function returns NULL for "not found", per spec.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GITHUB_PLUGIN::IsFootprintLibWritable( const wxString& aLibraryPath )
|
|
|
|
{
|
2013-11-27 06:04:04 +00:00
|
|
|
if( m_pretty_dir.size() )
|
|
|
|
return PCB_IO::IsFootprintLibWritable( m_pretty_dir );
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GITHUB_PLUGIN::FootprintSave( const wxString& aLibraryPath,
|
|
|
|
const MODULE* aFootprint, const PROPERTIES* aProperties )
|
|
|
|
{
|
|
|
|
// set m_pretty_dir to either empty or something in aProperties
|
|
|
|
cacheLib( aLibraryPath, aProperties );
|
|
|
|
|
|
|
|
if( GITHUB_PLUGIN::IsFootprintLibWritable( aLibraryPath ) )
|
|
|
|
{
|
|
|
|
PCB_IO::FootprintSave( m_pretty_dir, aFootprint, aProperties );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This typically will not happen if the caller first properly calls
|
|
|
|
// IsFootprintLibWritable() to determine if calling FootprintSave() is
|
|
|
|
// even legal, so I spend no time on internationalization here:
|
|
|
|
|
2017-12-15 11:37:46 +00:00
|
|
|
string msg = StrPrintf( "Github library\n\"%s\"\nis only writable if you set option \"%s\" in Library Tables dialog.",
|
2015-11-04 08:48:34 +00:00
|
|
|
TO_UTF8( aLibraryPath ), PRETTY_DIR );
|
2013-11-27 06:04:04 +00:00
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GITHUB_PLUGIN::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
|
|
|
|
const PROPERTIES* aProperties )
|
|
|
|
{
|
|
|
|
// set m_pretty_dir to either empty or something in aProperties
|
|
|
|
cacheLib( aLibraryPath, aProperties );
|
|
|
|
|
|
|
|
if( GITHUB_PLUGIN::IsFootprintLibWritable( aLibraryPath ) )
|
|
|
|
{
|
|
|
|
// Does the PCB_IO base class have this footprint?
|
|
|
|
// We cannot write to github.
|
|
|
|
|
2017-06-11 20:20:44 +00:00
|
|
|
wxArrayString pretties;
|
|
|
|
|
|
|
|
PCB_IO::FootprintEnumerate( pretties, m_pretty_dir, aProperties );
|
2013-11-27 06:04:04 +00:00
|
|
|
|
|
|
|
if( pretties.Index( aFootprintName ) != wxNOT_FOUND )
|
|
|
|
{
|
|
|
|
PCB_IO::FootprintDelete( m_pretty_dir, aFootprintName, aProperties );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
2017-12-15 11:37:46 +00:00
|
|
|
_( "Footprint\n\"%s\"\nis not in the writable portion of this Github library\n\"%s\"" ),
|
2013-11-27 06:04:04 +00:00
|
|
|
GetChars( aFootprintName ),
|
|
|
|
GetChars( aLibraryPath )
|
|
|
|
);
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This typically will not happen if the caller first properly calls
|
|
|
|
// IsFootprintLibWritable() to determine if calling FootprintSave() is
|
|
|
|
// even legal, so I spend no time on internationalization here:
|
|
|
|
|
2017-12-15 11:37:46 +00:00
|
|
|
string msg = StrPrintf( "Github library\n\"%s\"\nis only writable if you set option \"%s\" in Library Tables dialog.",
|
2015-11-04 08:48:34 +00:00
|
|
|
TO_UTF8( aLibraryPath ), PRETTY_DIR );
|
2013-11-27 06:04:04 +00:00
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GITHUB_PLUGIN::FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
|
|
|
|
{
|
|
|
|
// set m_pretty_dir to either empty or something in aProperties
|
|
|
|
cacheLib( aLibraryPath, aProperties );
|
|
|
|
|
|
|
|
if( m_pretty_dir.size() )
|
|
|
|
PCB_IO::FootprintLibCreate( m_pretty_dir, aProperties );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GITHUB_PLUGIN::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
|
|
|
|
{
|
|
|
|
// set m_pretty_dir to either empty or something in aProperties
|
|
|
|
cacheLib( aLibraryPath, aProperties );
|
|
|
|
|
|
|
|
if( m_pretty_dir.size() )
|
|
|
|
return PCB_IO::FootprintLibDelete( m_pretty_dir, aProperties );
|
2019-07-10 00:38:02 +00:00
|
|
|
|
|
|
|
return false;
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-12 00:17:27 +00:00
|
|
|
void GITHUB_PLUGIN::FootprintLibOptions( PROPERTIES* aListToAppendTo ) const
|
|
|
|
{
|
|
|
|
// inherit options supported by all PLUGINs.
|
|
|
|
PLUGIN::FootprintLibOptions( aListToAppendTo );
|
|
|
|
|
2014-01-02 02:17:07 +00:00
|
|
|
(*aListToAppendTo)[ PRETTY_DIR ] = UTF8( _(
|
2013-11-12 20:49:17 +00:00
|
|
|
"Set this property to a directory where footprints are to be written as pretty "
|
|
|
|
"footprints when saving to this library. Anything saved will take precedence over "
|
|
|
|
"footprints by the same name in the github repo. These saved footprints can then "
|
|
|
|
"be sent to the library maintainer as updates. "
|
2013-11-27 06:04:04 +00:00
|
|
|
"<p>The directory <b>must</b> have a <b>.pretty</b> file extension because the "
|
|
|
|
"format of the save is pretty.</p>"
|
2014-01-02 02:17:07 +00:00
|
|
|
));
|
2013-11-12 00:17:27 +00:00
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
/*
|
2014-01-02 02:17:07 +00:00
|
|
|
(*aListToAppendTo)["cache_github_zip_in_this_dir"] = UTF8( _(
|
2013-11-12 20:49:17 +00:00
|
|
|
"Set this property to a directory where the github *.zip file will be cached. "
|
|
|
|
"This should speed up subsequent visits to this library."
|
2014-01-02 02:17:07 +00:00
|
|
|
));
|
2013-11-27 06:04:04 +00:00
|
|
|
*/
|
2013-11-12 00:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-16 00:04:48 +00:00
|
|
|
void GITHUB_PLUGIN::cacheLib( const wxString& aLibraryPath, const PROPERTIES* aProperties )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2013-11-27 06:04:04 +00:00
|
|
|
// This is edge triggered based on a change in 'aLibraryPath',
|
|
|
|
// usually it does nothing. When the edge fires, m_pretty_dir is set
|
|
|
|
// to either:
|
|
|
|
// 1) empty or
|
|
|
|
// 2) a verified and validated, writable, *.pretty directory.
|
|
|
|
|
|
|
|
if( !m_gh_cache || m_lib_path != aLibraryPath )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2013-11-27 06:04:04 +00:00
|
|
|
delete m_gh_cache;
|
|
|
|
m_gh_cache = 0;
|
|
|
|
m_pretty_dir.clear();
|
|
|
|
|
2017-03-23 00:59:25 +00:00
|
|
|
if( !m_lib_path.empty() )
|
|
|
|
{
|
|
|
|
// Library path wasn't empty before - it's been changed. Flush out the prefetch cache.
|
|
|
|
m_zip_image.clear();
|
|
|
|
}
|
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
if( aProperties )
|
|
|
|
{
|
2014-01-02 02:17:07 +00:00
|
|
|
UTF8 pretty_dir;
|
2013-11-27 06:04:04 +00:00
|
|
|
|
|
|
|
if( aProperties->Value( PRETTY_DIR, &pretty_dir ) )
|
|
|
|
{
|
2014-01-02 02:17:07 +00:00
|
|
|
wxString wx_pretty_dir = pretty_dir;
|
2013-11-27 06:04:04 +00:00
|
|
|
|
2020-04-05 19:51:48 +00:00
|
|
|
wx_pretty_dir = ExpandEnvVarSubstitutions( wx_pretty_dir, nullptr );
|
2013-11-27 06:04:04 +00:00
|
|
|
|
|
|
|
wxFileName wx_pretty_fn = wx_pretty_dir;
|
|
|
|
|
|
|
|
if( !wx_pretty_fn.IsOk() ||
|
|
|
|
!wx_pretty_fn.IsDirWritable() ||
|
2016-01-10 21:44:37 +00:00
|
|
|
wx_pretty_fn.GetExt() != "pretty"
|
2013-11-27 06:04:04 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
2017-12-15 11:37:46 +00:00
|
|
|
_( "option \"%s\" for Github library \"%s\" must point to a writable directory ending with '.pretty'." ),
|
2013-11-27 06:04:04 +00:00
|
|
|
GetChars( FROM_UTF8( PRETTY_DIR ) ),
|
|
|
|
GetChars( aLibraryPath )
|
|
|
|
);
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pretty_dir = wx_pretty_dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 15:12:25 +00:00
|
|
|
// operator==( wxString, wxChar* ) does not exist, construct wxString once here.
|
2016-01-10 21:44:37 +00:00
|
|
|
const wxString kicad_mod( "kicad_mod" );
|
2013-09-30 21:26:49 +00:00
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
m_gh_cache = new GH_CACHE();
|
2013-10-03 15:12:25 +00:00
|
|
|
|
|
|
|
// INIT_LOGGER( "/tmp", "test.log" );
|
2015-12-21 20:30:33 +00:00
|
|
|
remoteGetZip( aLibraryPath );
|
2013-10-03 15:12:25 +00:00
|
|
|
// UNINIT_LOGGER();
|
|
|
|
|
2013-09-21 07:30:23 +00:00
|
|
|
m_lib_path = aLibraryPath;
|
|
|
|
|
|
|
|
wxMemoryInputStream mis( &m_zip_image[0], m_zip_image.size() );
|
|
|
|
|
2017-07-24 19:02:59 +00:00
|
|
|
// Recently the zip standard adopted UTF8 encoded filenames within the
|
|
|
|
// internal zip directory block. Please only use zip files that conform
|
|
|
|
// to that standard. Github seems to now, but may not have earlier.
|
2013-09-21 07:30:23 +00:00
|
|
|
wxZipInputStream zis( mis, wxConvUTF8 );
|
|
|
|
|
|
|
|
wxZipEntry* entry;
|
2017-07-25 19:14:31 +00:00
|
|
|
wxString fp_name;
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2013-10-03 13:40:00 +00:00
|
|
|
while( ( entry = zis.GetNextEntry() ) != NULL )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2013-10-03 15:12:25 +00:00
|
|
|
wxFileName fn( entry->GetName() ); // chop long name into parts
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2013-10-03 15:12:25 +00:00
|
|
|
if( fn.GetExt() == kicad_mod )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2017-07-25 19:14:31 +00:00
|
|
|
fp_name = fn.GetName(); // omit extension & path
|
2017-07-24 19:02:59 +00:00
|
|
|
|
2013-11-27 06:04:04 +00:00
|
|
|
m_gh_cache->insert( fp_name, entry );
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
delete entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-04 01:13:09 +00:00
|
|
|
long long GITHUB_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
|
|
|
|
{
|
|
|
|
// This plugin currently relies on the nginx server for caching (see comments
|
|
|
|
// at top of file).
|
|
|
|
// Since only the nginx server holds the timestamp information, we must defeat
|
|
|
|
// all caching above the nginx server.
|
|
|
|
return wxDateTime::Now().GetValue().GetValue();
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// If we have no cache, return a number which won't match any stored timestamps
|
|
|
|
if( !m_gh_cache || m_lib_path != aLibraryPath )
|
|
|
|
return wxDateTime::Now().GetValue().GetValue();
|
|
|
|
|
|
|
|
long long hash = m_gh_cache->GetTimestamp();
|
|
|
|
|
|
|
|
if( m_pretty_dir.size() )
|
|
|
|
hash += PCB_IO::GetLibraryTimestamp( m_pretty_dir );
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-10 21:44:37 +00:00
|
|
|
bool GITHUB_PLUGIN::repoURL_zipURL( const wxString& aRepoURL, std::string* aZipURL )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
|
|
|
// e.g. "https://github.com/liftoff-sr/pretty_footprints"
|
|
|
|
|
|
|
|
wxURI repo( aRepoURL );
|
|
|
|
|
|
|
|
if( repo.HasServer() && repo.HasPath() )
|
|
|
|
{
|
2015-04-22 12:02:18 +00:00
|
|
|
// scheme might be "http" or if truly github.com then "https".
|
2015-12-21 20:30:33 +00:00
|
|
|
wxString zip_url;
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2015-04-22 12:02:18 +00:00
|
|
|
if( repo.GetServer() == "github.com" )
|
|
|
|
{
|
2015-12-21 20:30:33 +00:00
|
|
|
//codeload.github.com only supports https
|
|
|
|
zip_url = "https://";
|
2015-04-22 12:02:18 +00:00
|
|
|
#if 0 // A proper code path would be this one, but it is not the fastest.
|
|
|
|
zip_url += repo.GetServer();
|
|
|
|
zip_url += repo.GetPath(); // path comes with a leading '/'
|
2015-05-26 15:12:43 +00:00
|
|
|
zip_url += "/archive/master.zip";
|
2013-11-12 01:14:17 +00:00
|
|
|
#else
|
2015-04-22 12:02:18 +00:00
|
|
|
// Github issues a redirect for the "master.zip". i.e.
|
|
|
|
// "https://github.com/liftoff-sr/pretty_footprints/archive/master.zip"
|
|
|
|
// would be redirected to:
|
|
|
|
// "https://codeload.github.com/liftoff-sr/pretty_footprints/zip/master"
|
|
|
|
|
|
|
|
// In order to bypass this redirect, saving time, we use the
|
|
|
|
// redirected URL on first attempt to save one HTTP GET hit.
|
|
|
|
zip_url += "codeload.github.com";
|
|
|
|
zip_url += repo.GetPath(); // path comes with a leading '/'
|
2015-05-26 15:12:43 +00:00
|
|
|
zip_url += "/zip/master";
|
2013-11-12 01:14:17 +00:00
|
|
|
#endif
|
2015-04-22 12:02:18 +00:00
|
|
|
}
|
2015-08-24 17:30:26 +00:00
|
|
|
|
2015-04-22 12:02:18 +00:00
|
|
|
else
|
|
|
|
{
|
2015-12-21 20:30:33 +00:00
|
|
|
zip_url = repo.GetScheme();
|
|
|
|
zip_url += "://";
|
|
|
|
|
2015-04-22 12:02:18 +00:00
|
|
|
// This is the generic code path for any server which can serve
|
2015-08-24 17:30:26 +00:00
|
|
|
// up zip files. The schemes tested include: http and https.
|
2015-04-22 12:02:18 +00:00
|
|
|
|
|
|
|
// zip_url goal: "<scheme>://<server>[:<port>]/<path>"
|
|
|
|
|
|
|
|
// Remember that <scheme>, <server>, <port> if present, and <path> all came
|
|
|
|
// from the lib_path in the fp-lib-table row.
|
|
|
|
|
2015-08-24 17:30:26 +00:00
|
|
|
// This code path is used with the nginx proxy setup, but is useful
|
|
|
|
// beyond that.
|
|
|
|
|
2015-04-22 12:02:18 +00:00
|
|
|
zip_url += repo.GetServer();
|
|
|
|
|
|
|
|
if( repo.HasPort() )
|
|
|
|
{
|
|
|
|
zip_url += ':';
|
|
|
|
zip_url += repo.GetPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
zip_url += repo.GetPath(); // path comes with a leading '/'
|
|
|
|
|
|
|
|
// Do not modify the path, we cannot anticipate the needs of all
|
|
|
|
// servers which are serving up zip files directly. URL modifications
|
|
|
|
// are more generally done in the server, rather than contaminating
|
|
|
|
// this code path with the needs of one particular inflexible server.
|
|
|
|
}
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2016-01-10 21:44:37 +00:00
|
|
|
*aZipURL = zip_url.utf8_str();
|
2013-09-21 07:30:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void GITHUB_PLUGIN::remoteGetZip( const wxString& aRepoURL )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2015-12-21 20:30:33 +00:00
|
|
|
std::string zip_url;
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2017-03-23 00:59:25 +00:00
|
|
|
if( !m_zip_image.empty() )
|
|
|
|
return;
|
|
|
|
|
2016-01-10 21:44:37 +00:00
|
|
|
if( !repoURL_zipURL( aRepoURL, &zip_url ) )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2017-12-15 11:37:46 +00:00
|
|
|
wxString msg = wxString::Format( _( "Unable to parse URL:\n\"%s\"" ), GetChars( aRepoURL ) );
|
2013-09-21 07:30:23 +00:00
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
2016-01-10 21:44:37 +00:00
|
|
|
KICAD_CURL_EASY kcurl; // this can THROW_IO_ERROR
|
2015-12-21 20:30:33 +00:00
|
|
|
|
2016-01-10 21:44:37 +00:00
|
|
|
kcurl.SetURL( zip_url.c_str() );
|
|
|
|
kcurl.SetUserAgent( "http://kicad-pcb.org" );
|
|
|
|
kcurl.SetHeader( "Accept", "application/zip" );
|
|
|
|
kcurl.SetFollowRedirects( true );
|
2013-09-21 07:30:23 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-12-21 20:30:33 +00:00
|
|
|
kcurl.Perform();
|
2016-01-10 21:44:37 +00:00
|
|
|
m_zip_image = kcurl.GetBuffer();
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
2015-12-21 20:30:33 +00:00
|
|
|
catch( const IO_ERROR& ioe )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2016-02-04 16:49:20 +00:00
|
|
|
// https "GET" has failed, report this to API caller.
|
2016-01-16 14:27:42 +00:00
|
|
|
// Note: kcurl.Perform() does not return an error if the file to download is not found
|
2016-02-04 16:49:20 +00:00
|
|
|
static const char errorcmd[] = "http GET command failed"; // Do not translate this message
|
|
|
|
|
2017-12-15 11:37:46 +00:00
|
|
|
UTF8 fmt( _( "%s\nCannot get/download Zip archive: \"%s\"\nfor library path: \"%s\".\nReason: \"%s\"" ) );
|
2013-10-03 15:12:25 +00:00
|
|
|
|
2015-12-30 07:46:32 +00:00
|
|
|
std::string msg = StrPrintf( fmt.c_str(),
|
2016-02-04 16:49:20 +00:00
|
|
|
errorcmd,
|
2015-12-30 07:46:32 +00:00
|
|
|
zip_url.c_str(),
|
2015-12-21 20:30:33 +00:00
|
|
|
TO_UTF8( aRepoURL ),
|
2016-09-14 22:36:45 +00:00
|
|
|
TO_UTF8( ioe.What() )
|
2016-01-10 21:44:37 +00:00
|
|
|
);
|
2015-08-24 17:30:26 +00:00
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
2015-12-30 07:46:32 +00:00
|
|
|
|
2016-12-07 17:06:03 +00:00
|
|
|
// If the zip archive is not existing, the received data is "Not Found" or "404: Not Found",
|
2015-12-30 07:46:32 +00:00
|
|
|
// and no error is returned by kcurl.Perform().
|
2016-12-07 17:06:03 +00:00
|
|
|
if( ( m_zip_image.compare( 0, 9, "Not Found", 9 ) == 0 ) ||
|
|
|
|
( m_zip_image.compare( 0, 14, "404: Not Found", 14 ) == 0 ) )
|
2015-12-30 07:46:32 +00:00
|
|
|
{
|
2017-12-15 11:37:46 +00:00
|
|
|
UTF8 fmt( _( "Cannot download library \"%s\".\nThe library does not exist on the server" ) );
|
2015-12-30 07:46:32 +00:00
|
|
|
std::string msg = StrPrintf( fmt.c_str(), TO_UTF8( aRepoURL ) );
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 && defined(STANDALONE)
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
2013-10-03 13:40:00 +00:00
|
|
|
INIT_LOGGER( ".", "test.log" );
|
2013-09-21 07:30:23 +00:00
|
|
|
|
|
|
|
GITHUB_PLUGIN gh;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
wxArrayString fps = gh.FootprintEnumerate(
|
2016-01-10 21:44:37 +00:00
|
|
|
"https://github.com/liftoff-sr/pretty_footprints",
|
2013-09-21 07:30:23 +00:00
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
for( int i=0; i<(int)fps.Count(); ++i )
|
|
|
|
{
|
|
|
|
printf("[%d]:%s\n", i, TO_UTF8( fps[i] ) );
|
|
|
|
}
|
|
|
|
}
|
2014-04-09 13:33:04 +00:00
|
|
|
catch( const IO_ERROR& ioe )
|
2013-09-21 07:30:23 +00:00
|
|
|
{
|
2016-09-14 22:36:45 +00:00
|
|
|
printf( "%s\n", TO_UTF8(ioe.What()) );
|
2013-09-21 07:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNINIT_LOGGER();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|