diff --git a/library/pytorrent.py b/library/pytorrent.py index cabbe7d90..be6733fe2 100644 --- a/library/pytorrent.py +++ b/library/pytorrent.py @@ -30,6 +30,10 @@ import pickle import time +# Constants + +TORRENTS_SUBDIR = "torrentfiles" + # Information for a single torrent class torrent: @@ -65,11 +69,11 @@ class persistent_state: self.max_connections = 80 self.use_DHT = True - self.base_dir = "~/Temp" - self.torrents_subdir + "torrentfiles" self.max_active_torrents = 1 self.auto_seed_ratio = -1 + self.temp = 0 + # Prepare queue (queue is pickled, just like everything else) self.queue = [] # queue[x] is the unique_ID of the x-th queue position. Simple. @@ -80,12 +84,13 @@ class persistent_state: # The manager for the torrent system class manager: - def __init__(self, client_ID, version, user_agent, state_filename): + def __init__(self, client_ID, version, user_agent, base_dir, state_filename): self.state_filename = state_filename + self.base_dir = base_dir # Ensure directories exist - if not self.torrents_subdir in os.listdir(self.base_dir): - os.mkdir(self.base_dir + "/" + self.torrents_subdir) + if not TORRENTS_SUBDIR in os.listdir(self.base_dir): + os.mkdir(self.base_dir + "/" + TORRENTS_SUBDIR) # Start up the core assert(len(version) == 4) @@ -101,8 +106,8 @@ class manager: # Unpickle the state, or create a new one try: - pkl_file = open(state_filename, 'rb') - self.state = pickle.load(pkl_file) + pkl_file = open(self.base_dir + "/" + self.state_filename, 'rb') + self.state = pickle.load(pkl_file) #xxx LOCALIZE to base_dir! pkl_file.close() # Sync with the core: tell core about torrents, and get unique_IDs @@ -112,7 +117,7 @@ class manager: def quit(self): # Pickle the state - output = open(self.state_filename, 'wb') + output = open(self.base_dir + "/" + self.state_filename, 'wb') pickle.dump(self.state, output) output.close() @@ -120,7 +125,7 @@ class manager: self.save_fastresume_data() # Shutdown torrent core - pytorrent.quit() + pytorrent_core.quit() def add_torrent(self, filename, save_dir, compact): print "add_torrent" @@ -198,7 +203,7 @@ class manager: return self.unique_IDs[unique_ID].user_paused def is_paused(self, unique_ID): - return pytorrent_core.is_paused(unique_ID]) + return pytorrent_core.is_paused(unique_ID) # Enforce the queue: pause/unpause as needed, based on queue and user_pausing # This should be called after changes to relevant parameters (user_pausing, or @@ -218,16 +223,16 @@ class manager: # Pause and resume torrents for index in range(len(self.state.queue)): unique_ID = self.state.queue[index] - if (index < self.state.max_active_torrents or self.state_max_active_torrents == -1) - and self.is_paused(unique_ID) + if (index < self.state.max_active_torrents or self.state_max_active_torrents == -1) \ + and self.is_paused(unique_ID) \ and not self.is_user_paused(unique_ID): pytorrent_core.resume(unique_ID) - elif not self.is_paused(unique_ID) and + elif not self.is_paused(unique_ID) and \ (index >= self.state.max_active_torrents or self.is_user_paused(unique_ID)): pytorrent_core.pause(unique_ID) def calc_ratio(self, unique_ID, torrent_state): - up = float(torrent_state['total_upload'] + self.unique_IDs[unique_ID].uploaded_memory + up = float(torrent_state['total_upload'] + self.unique_IDs[unique_ID].uploaded_memory) down = float(torrent_state["total_done"]) try: @@ -250,9 +255,9 @@ class manager: time.sleep(0.01) # Ensure we use a unique time for the new filename new_name = str(time.time()) + ".torrent" - full_new_name = self.state.base_dir + "/" + self.torrents_subdir + newName + full_new_name = self.base_dir + "/" + TORRENTS_SUBDIR + "/" + new_name - if new_name in os.listdir(self.state.base_dir + "/" + self.torrents_subdir): + if new_name in os.listdir(self.base_dir + "/" + TORRENTS_SUBDIR): raise PyTorrentCoreError("Could not cache torrent file locally, failed: " + new_name) shutil.copy(filename, full_new_name) diff --git a/library/pytorrent_core.cpp b/library/pytorrent_core.cpp index cb579f4d4..795c6d822 100755 --- a/library/pytorrent_core.cpp +++ b/library/pytorrent_core.cpp @@ -368,6 +368,8 @@ static PyObject *torrent_save_fastresume(PyObject *self, PyObject *args) out.unsetf(std::ios_base::skipws); bencode(std::ostream_iterator(out), data); + + Py_INCREF(Py_None); return Py_None; } else PYTORRENTCORE_RAISE_PTR(PyTorrentCoreError, "Invalid handle or no metadata for fastresume."); } @@ -1205,6 +1207,7 @@ static PyMethodDef pytorrent_core_methods[] = { {"get_num_torrents", torrent_get_num_torrents, METH_VARARGS, "."}, {"reannounce", torrent_reannounce, METH_VARARGS, "."}, {"is_paused", torrent_is_paused, METH_VARARGS, "."}, + {"is_seeding", torrent_is_seeding, METH_VARARGS, "."}, {"pause", torrent_pause, METH_VARARGS, "."}, {"resume", torrent_resume, METH_VARARGS, "."}, {"get_torrent_info", torrent_get_torrent_info, METH_VARARGS, "."}, diff --git a/library/test.py b/library/test.py index 3ed9357c7..9ccd35da9 100644 --- a/library/test.py +++ b/library/test.py @@ -11,16 +11,21 @@ import pytorrent from time import sleep +import os -manager = pytorrent.manager("PT", "0500", "pytorrent - testing only", "test_state.dat") +manager = pytorrent.manager("PT", "0500", "pytorrent - testing only", + os.path.expanduser("~") + "/Temp", + "test_state.dat") -my_torrent = manager.add_torrent("ubuntu.torrent", ".", True) +#my_torrent = manager.add_torrent("ubuntu.torrent", ".", True) -print "Unique ID:", my_torrent +#print "Unique ID:", my_torrent -while True: +for i in range(2): print "STATE:" - print manager.get_state(my_torrent) + print manager.get_state(0)#my_torrent) print "" sleep(2) + +manager.quit()