summaryrefslogtreecommitdiffstats
path: root/deluge/scripts/state_upgrade.py
blob: cb5b460cbc605c2dd2f32b94d74f356b890831b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) Martijn Voncken 2008 <mvoncken@gmail.com>
#
# 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, 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, write to:
#     The Free Software Foundation, Inc.,
#     51 Franklin Street, Fifth Floor
#     Boston, MA  02110-1301, USA.
#


import sys, pickle , shutil , os
from deluge.ui.client import sclient

options = {
    "new_torrents_dir" :"~/torrents06",
    "state05":"~/.config/deluge/persistent.state",
    "all_paused":True
}

#start : http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286203
def makeFakeClass(module, name):
    class FakeThing(object):
        pass
    FakeThing.__name__ = name
    FakeThing.__module__ = '(fake)' + module
    return FakeThing

class PickleUpgrader(pickle.Unpickler):
    def find_class(self, module, cname):
        # Pickle tries to load a couple things like copy_reg and
        # __builtin__.object even though a pickle file doesn't
        # explicitly reference them (afaict): allow them to be loaded
        # normally.
        if module in ('copy_reg', '__builtin__'):
            thing = pickle.Unpickler.find_class(self, module, cname)
            return thing
        return makeFakeClass(module, cname)
# end: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286203

def load_05(state_05_file, new_torrent_dir,all_paused):
    state5 = PickleUpgrader(open(state_05_file)).load()
    for torrent in state5.torrents:
        #print [x for x in dir(torrent) if not x.startswith("_")]
        print("file:%s, save_dir:%s, compact:%s, paused:%s " % (torrent.filename,torrent.save_dir,torrent.compact,torrent.user_paused))

        new_file = os.path.join(new_torrent_dir,os.path.basename(torrent.filename))
        print("copy" , torrent.filename , new_file)
        shutil.copyfile(torrent.filename , new_file)

        sclient.add_torrent_file([torrent.filename],[{
            "add_paused" : (all_paused or torrent.user_paused),
            "compact_allocation":torrent.compact,
            "download_location":torrent.save_dir
        }])

if __name__ == "__main__":
    sclient.set_core_uri()
    new_torrents_dir = os.path.expanduser(options["new_torrents_dir"])
    state_05_file = os.path.expanduser(options['state05']);
    load_05(state_05_file, new_torrents_dir, options["all_paused"])