2009-02-21 09:39:26 +01:00
|
|
|
// Copyright Daniel Wallin & Arvid Norberg 2009. Use, modification and distribution is
|
|
|
|
// subject to the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
2016-04-08 04:45:23 +02:00
|
|
|
#include "boost_python.hpp"
|
2009-02-21 09:39:26 +01:00
|
|
|
#include <libtorrent/create_torrent.hpp>
|
|
|
|
#include <libtorrent/file_storage.hpp>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2014-09-13 00:38:07 +02:00
|
|
|
#include "bytes.hpp"
|
2009-02-21 09:39:26 +01:00
|
|
|
|
|
|
|
using namespace boost::python;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2009-08-28 05:46:19 +02:00
|
|
|
namespace
|
|
|
|
{
|
2014-09-13 00:38:07 +02:00
|
|
|
void set_hash(create_torrent& c, int p, bytes const& b)
|
2009-08-28 05:46:19 +02:00
|
|
|
{
|
2014-09-13 00:38:07 +02:00
|
|
|
c.set_hash(p, sha1_hash(b.arr));
|
2009-08-28 05:46:19 +02:00
|
|
|
}
|
2009-10-02 03:26:31 +02:00
|
|
|
|
2014-09-13 00:38:07 +02:00
|
|
|
void set_file_hash(create_torrent& c, int f, bytes const& b)
|
2013-05-03 10:24:26 +02:00
|
|
|
{
|
2014-09-13 00:38:07 +02:00
|
|
|
c.set_file_hash(f, sha1_hash(b.arr));
|
2013-05-03 10:24:26 +02:00
|
|
|
}
|
|
|
|
|
2009-10-02 03:26:31 +02:00
|
|
|
void call_python_object(boost::python::object const& obj, int i)
|
|
|
|
{
|
|
|
|
obj(i);
|
|
|
|
}
|
|
|
|
|
2010-08-22 18:45:12 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-10-26 02:29:39 +01:00
|
|
|
void set_piece_hashes_callback(create_torrent& c, std::string const& p
|
2009-10-02 03:26:31 +02:00
|
|
|
, boost::python::object cb)
|
|
|
|
{
|
|
|
|
set_piece_hashes(c, p, boost::bind(call_python_object, cb, _1));
|
|
|
|
}
|
2010-08-22 18:45:12 +02:00
|
|
|
#else
|
|
|
|
void set_piece_hashes_callback(create_torrent& c, std::string const& p
|
|
|
|
, boost::python::object cb)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
set_piece_hashes(c, p, boost::bind(call_python_object, cb, _1), ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_piece_hashes0(create_torrent& c, std::string const & s)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
set_piece_hashes(c, s, ec);
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-22 10:21:40 +02:00
|
|
|
|
|
|
|
void add_node(create_torrent& ct, std::string const& addr, int port)
|
|
|
|
{
|
|
|
|
ct.add_node(std::make_pair(addr, port));
|
|
|
|
}
|
2010-11-15 06:10:36 +01:00
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
#if !defined TORRENT_NO_DEPRECATE
|
2010-11-29 06:44:29 +01:00
|
|
|
void add_file(file_storage& ct, file_entry const& fe)
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2010-11-29 06:44:29 +01:00
|
|
|
ct.add_file(fe);
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
2015-04-05 00:37:11 +02:00
|
|
|
|
|
|
|
struct FileIter
|
|
|
|
{
|
|
|
|
typedef libtorrent::file_entry value_type;
|
|
|
|
typedef libtorrent::file_entry reference;
|
|
|
|
typedef libtorrent::file_entry* pointer;
|
|
|
|
typedef int difference_type;
|
|
|
|
typedef std::forward_iterator_tag iterator_category;
|
|
|
|
|
|
|
|
FileIter(file_storage const& fs, int i) : m_fs(&fs), m_i(i) {}
|
|
|
|
FileIter(FileIter const& fi) : m_fs(fi.m_fs), m_i(fi.m_i) {}
|
|
|
|
FileIter() : m_fs(NULL), m_i(0) {}
|
|
|
|
libtorrent::file_entry operator*() const
|
|
|
|
{ return m_fs->at(m_i); }
|
|
|
|
|
|
|
|
FileIter operator++() { m_i++; return *this; }
|
|
|
|
FileIter operator++(int) { return FileIter(*m_fs, m_i++); }
|
|
|
|
|
|
|
|
bool operator==(FileIter const& rhs) const
|
|
|
|
{ return m_fs == rhs.m_fs && m_i == rhs.m_i; }
|
|
|
|
|
|
|
|
int operator-(FileIter const& rhs) const
|
|
|
|
{
|
|
|
|
assert(rhs.m_fs == m_fs);
|
|
|
|
return m_i - rhs.m_i;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileIter& operator=(FileIter const& rhs)
|
|
|
|
{
|
|
|
|
m_fs = rhs.m_fs;
|
|
|
|
m_i = rhs.m_i;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_storage const* m_fs;
|
|
|
|
int m_i;
|
|
|
|
};
|
|
|
|
|
|
|
|
FileIter begin_files(file_storage const& self)
|
|
|
|
{ return FileIter(self, 0); }
|
|
|
|
|
|
|
|
FileIter end_files(file_storage const& self)
|
|
|
|
{ return FileIter(self, self.num_files()); }
|
2015-01-21 16:46:12 +01:00
|
|
|
#endif
|
2012-03-22 04:33:54 +01:00
|
|
|
|
2015-06-06 23:38:46 +02:00
|
|
|
bool call_python_object2(boost::python::object& obj, std::string const& i)
|
2014-03-23 01:49:58 +01:00
|
|
|
{
|
|
|
|
return obj(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_files_callback(file_storage& fs, std::string const& file
|
|
|
|
, boost::python::object cb, boost::uint32_t flags)
|
|
|
|
{
|
|
|
|
add_files(fs, file, boost::bind(&call_python_object2, cb, _1), flags);
|
|
|
|
}
|
2015-04-05 00:37:11 +02:00
|
|
|
|
2009-08-28 05:46:19 +02:00
|
|
|
}
|
|
|
|
|
2009-02-21 09:39:26 +01:00
|
|
|
void bind_create_torrent()
|
|
|
|
{
|
2015-01-21 16:46:12 +01:00
|
|
|
void (file_storage::*add_file0)(std::string const&, boost::int64_t
|
2015-06-06 23:38:46 +02:00
|
|
|
, int, std::time_t, std::string const&) = &file_storage::add_file;
|
2015-01-21 16:46:12 +01:00
|
|
|
#if !defined TORRENT_NO_DEPRECATE
|
|
|
|
#if TORRENT_USE_WSTRING
|
|
|
|
void (file_storage::*add_file1)(std::wstring const&, boost::int64_t
|
2015-06-06 23:38:46 +02:00
|
|
|
, int, std::time_t, std::string const&) = &file_storage::add_file;
|
2015-01-21 16:46:12 +01:00
|
|
|
#endif // TORRENT_USE_WSTRING
|
|
|
|
#endif // TORRENT_NO_DEPRECATE
|
2009-03-01 09:13:26 +01:00
|
|
|
|
|
|
|
void (file_storage::*set_name0)(std::string const&) = &file_storage::set_name;
|
2012-03-22 04:33:54 +01:00
|
|
|
void (file_storage::*rename_file0)(int, std::string const&) = &file_storage::rename_file;
|
2013-08-27 18:04:19 +02:00
|
|
|
#if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
|
2009-03-01 09:13:26 +01:00
|
|
|
void (file_storage::*set_name1)(std::wstring const&) = &file_storage::set_name;
|
2012-03-22 04:33:54 +01:00
|
|
|
void (file_storage::*rename_file1)(int, std::wstring const&) = &file_storage::rename_file;
|
2009-10-29 07:45:46 +01:00
|
|
|
#endif
|
2009-02-21 09:39:26 +01:00
|
|
|
|
2010-08-22 18:45:12 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-10-26 02:29:39 +01:00
|
|
|
void (*set_piece_hashes0)(create_torrent&, std::string const&) = &set_piece_hashes;
|
2010-08-22 18:45:12 +02:00
|
|
|
#endif
|
2010-04-01 18:48:33 +02:00
|
|
|
void (*add_files0)(file_storage&, std::string const&, boost::uint32_t) = add_files;
|
2009-08-28 05:46:19 +02:00
|
|
|
|
2015-06-06 23:38:46 +02:00
|
|
|
std::string const& (file_storage::*file_storage_symlink)(int) const = &file_storage::symlink;
|
2015-01-21 16:46:12 +01:00
|
|
|
sha1_hash (file_storage::*file_storage_hash)(int) const = &file_storage::hash;
|
2015-06-06 23:38:46 +02:00
|
|
|
std::string (file_storage::*file_storage_file_path)(int, std::string const&) const = &file_storage::file_path;
|
|
|
|
boost::int64_t (file_storage::*file_storage_file_size)(int) const = &file_storage::file_size;
|
|
|
|
boost::int64_t (file_storage::*file_storage_file_offset)(int) const = &file_storage::file_offset;
|
|
|
|
int (file_storage::*file_storage_file_flags)(int) const = &file_storage::file_flags;
|
2015-01-21 16:46:12 +01:00
|
|
|
|
|
|
|
#if !defined TORRENT_NO_DEPRECATE
|
2010-11-29 06:44:29 +01:00
|
|
|
file_entry (file_storage::*at)(int) const = &file_storage::at;
|
2015-01-21 16:46:12 +01:00
|
|
|
#endif
|
2010-11-29 06:44:29 +01:00
|
|
|
|
2015-07-26 01:40:07 +02:00
|
|
|
// TODO: 3 move this to its own file
|
2009-02-21 09:39:26 +01:00
|
|
|
class_<file_storage>("file_storage")
|
|
|
|
.def("is_valid", &file_storage::is_valid)
|
2010-11-15 06:10:36 +01:00
|
|
|
.def("add_file", add_file0, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = ""))
|
2013-08-27 18:04:19 +02:00
|
|
|
#if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
|
2010-11-15 06:10:36 +01:00
|
|
|
.def("add_file", add_file1, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = ""))
|
2009-07-16 09:10:58 +02:00
|
|
|
#endif
|
2009-02-21 09:39:26 +01:00
|
|
|
.def("num_files", &file_storage::num_files)
|
2015-01-21 16:46:12 +01:00
|
|
|
#if !defined TORRENT_NO_DEPRECATE
|
2010-11-29 06:44:29 +01:00
|
|
|
.def("at", at)
|
2015-01-21 16:46:12 +01:00
|
|
|
.def("add_file", add_file, arg("entry"))
|
2015-04-05 00:37:11 +02:00
|
|
|
.def("__iter__", boost::python::range(&begin_files, &end_files))
|
|
|
|
.def("__len__", &file_storage::num_files)
|
2015-01-21 16:46:12 +01:00
|
|
|
#endif
|
|
|
|
.def("hash", file_storage_hash)
|
|
|
|
.def("symlink", file_storage_symlink, return_value_policy<copy_const_reference>())
|
|
|
|
.def("file_path", file_storage_file_path, (arg("idx"), arg("save_path") = ""))
|
|
|
|
.def("file_size", file_storage_file_size)
|
|
|
|
.def("file_offset", file_storage_file_offset)
|
|
|
|
.def("file_flags", file_storage_file_flags)
|
|
|
|
|
2009-02-21 09:39:26 +01:00
|
|
|
.def("total_size", &file_storage::total_size)
|
|
|
|
.def("set_num_pieces", &file_storage::set_num_pieces)
|
|
|
|
.def("num_pieces", &file_storage::num_pieces)
|
|
|
|
.def("set_piece_length", &file_storage::set_piece_length)
|
|
|
|
.def("piece_length", &file_storage::piece_length)
|
|
|
|
.def("piece_size", &file_storage::piece_size)
|
2009-03-01 09:13:26 +01:00
|
|
|
.def("set_name", set_name0)
|
2012-03-22 04:33:54 +01:00
|
|
|
.def("rename_file", rename_file0)
|
2013-08-27 18:04:19 +02:00
|
|
|
#if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
|
2009-03-01 09:13:26 +01:00
|
|
|
.def("set_name", set_name1)
|
2012-03-22 04:33:54 +01:00
|
|
|
.def("rename_file", rename_file1)
|
2009-10-29 07:45:46 +01:00
|
|
|
#endif
|
2015-01-21 16:46:12 +01:00
|
|
|
.def("name", &file_storage::name, return_value_policy<copy_const_reference>())
|
2009-02-21 09:39:26 +01:00
|
|
|
;
|
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
enum_<file_storage::file_flags_t>("file_flags_t")
|
|
|
|
.value("flag_pad_file", file_storage::flag_pad_file)
|
|
|
|
.value("flag_hidden", file_storage::flag_hidden)
|
|
|
|
.value("flag_executable", file_storage::flag_executable)
|
|
|
|
.value("flag_symlink", file_storage::flag_symlink)
|
|
|
|
;
|
|
|
|
|
2009-02-21 09:39:26 +01:00
|
|
|
class_<create_torrent>("create_torrent", no_init)
|
|
|
|
.def(init<file_storage&>())
|
2012-03-11 17:21:51 +01:00
|
|
|
.def(init<torrent_info const&>(arg("ti")))
|
2011-04-10 10:41:07 +02:00
|
|
|
.def(init<file_storage&, int, int, int>((arg("storage"), arg("piece_size") = 0
|
2016-03-19 06:44:21 +01:00
|
|
|
, arg("pad_file_limit") = -1, arg("flags") = int(libtorrent::create_torrent::optimize_alignment))))
|
2009-02-21 09:39:26 +01:00
|
|
|
|
|
|
|
.def("generate", &create_torrent::generate)
|
|
|
|
|
|
|
|
.def("files", &create_torrent::files, return_internal_reference<>())
|
|
|
|
.def("set_comment", &create_torrent::set_comment)
|
|
|
|
.def("set_creator", &create_torrent::set_creator)
|
2009-08-28 05:46:19 +02:00
|
|
|
.def("set_hash", &set_hash)
|
2013-05-03 10:24:26 +02:00
|
|
|
.def("set_file_hash", &set_file_hash)
|
2009-02-21 09:39:26 +01:00
|
|
|
.def("add_url_seed", &create_torrent::add_url_seed)
|
2013-05-03 10:24:26 +02:00
|
|
|
.def("add_http_seed", &create_torrent::add_http_seed)
|
2010-05-22 10:21:40 +02:00
|
|
|
.def("add_node", &add_node)
|
2011-04-10 10:41:07 +02:00
|
|
|
.def("add_tracker", &create_torrent::add_tracker, (arg("announce_url"), arg("tier") = 0))
|
2009-02-21 09:39:26 +01:00
|
|
|
.def("set_priv", &create_torrent::set_priv)
|
|
|
|
.def("num_pieces", &create_torrent::num_pieces)
|
|
|
|
.def("piece_length", &create_torrent::piece_length)
|
|
|
|
.def("piece_size", &create_torrent::piece_size)
|
|
|
|
.def("priv", &create_torrent::priv)
|
2012-06-27 01:53:59 +02:00
|
|
|
.def("set_root_cert", &create_torrent::set_root_cert, (arg("pem")))
|
2009-02-21 09:39:26 +01:00
|
|
|
;
|
2009-08-28 05:46:19 +02:00
|
|
|
|
2011-04-10 10:41:07 +02:00
|
|
|
enum_<create_torrent::flags_t>("create_torrent_flags_t")
|
2016-03-19 06:44:21 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2011-04-10 10:41:07 +02:00
|
|
|
.value("optimize", create_torrent::optimize)
|
2016-03-19 06:44:21 +01:00
|
|
|
#endif
|
|
|
|
.value("optimize_alignment", create_torrent::optimize_alignment)
|
2011-04-10 10:41:07 +02:00
|
|
|
.value("merkle", create_torrent::merkle)
|
|
|
|
.value("modification_time", create_torrent::modification_time)
|
|
|
|
.value("symlinks", create_torrent::symlinks)
|
|
|
|
;
|
|
|
|
|
2010-10-28 18:28:18 +02:00
|
|
|
def("add_files", add_files0, (arg("fs"), arg("path"), arg("flags") = 0));
|
2014-03-23 01:49:58 +01:00
|
|
|
def("add_files", add_files_callback, (arg("fs"), arg("path")
|
|
|
|
, arg("predicate"), arg("flags") = 0));
|
2009-08-28 05:46:19 +02:00
|
|
|
def("set_piece_hashes", set_piece_hashes0);
|
2009-10-02 03:26:31 +02:00
|
|
|
def("set_piece_hashes", set_piece_hashes_callback);
|
2009-08-28 05:46:19 +02:00
|
|
|
|
2009-02-21 09:39:26 +01:00
|
|
|
}
|