r152 - in /debtorrent/trunk: DebTorrent/ConfigDir.py DebTorrent/download_bt1.py btdownloadheadless.py btlaunchmany.py config.debtorrent.ini debtorrent.py test.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Thu Jul 12 23:45:16 UTC 2007


Author: camrdale-guest
Date: Thu Jul 12 23:45:16 2007
New Revision: 152

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=152
Log:
Change the config file loading to use multiple config files.
Split the cache directory as a separate option.

Modified:
    debtorrent/trunk/DebTorrent/ConfigDir.py
    debtorrent/trunk/DebTorrent/download_bt1.py
    debtorrent/trunk/btdownloadheadless.py
    debtorrent/trunk/btlaunchmany.py
    debtorrent/trunk/config.debtorrent.ini
    debtorrent/trunk/debtorrent.py
    debtorrent/trunk/test.py

Modified: debtorrent/trunk/DebTorrent/ConfigDir.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/ConfigDir.py?rev=152&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/ConfigDir.py (original)
+++ debtorrent/trunk/DebTorrent/ConfigDir.py Thu Jul 12 23:45:16 2007
@@ -32,6 +32,7 @@
     False = 0
 
 DIRNAME = '.'+product_name
+MASTER_CONFIG = '/etc/debtorrent'
 
 hexchars = '0123456789abcdef'
 hexmap = []
@@ -110,8 +111,12 @@
     
     @type config_type: C{string}
     @ivar config_type: the extension to include in the saved files' names
-    @type dir_root: C{string}
-    @ivar dir_root: the root directory to save config files in
+    @type home_dir: C{string}
+    @ivar home_dir: the user's home directory
+    @type configfile: C{string}
+    @ivar configfile: the config file
+    @type cache_dir: C{string}
+    @ivar cache_dir: the directory to save cache files in
     @type dir_torrentcache: C{string}
     @ivar dir_torrentcache: the directory to save torrent files in
     @type dir_datacache: C{string}
@@ -127,9 +132,9 @@
     @type config: C{dictionary}
     @ivar config: the current configuration variables
     
-    @group Config Handling: setDefaults, checkConfig, loadConfig, saveConfig, getConfig
+    @group Config Handling: setDefaults, setCacheDir, checkConfig, loadConfigFile, loadConfig, saveConfig, getConfig
     @group State: getState, saveState
-    @group Torrent Files: getTorrents, getTorrentVariations, getTorrent, writeTorrent
+    @group Torrent Files: getTorrents, getTorrentVariations, getTorrentFile, getTorrent, writeTorrent
     @group Torrent Data: getTorrentData, writeTorrentData, deleteTorrentData, getPieceDir
     @group Expire Cache: deleteOldCacheData, deleteOldTorrents
     
@@ -137,69 +142,46 @@
 
     ###### INITIALIZATION TASKS ######
 
-    def __init__(self, config_type = None, dir_root = ''):
+    def __init__(self, config_type = None):
         """Initialize the instance, create directories and file names.
         
         @type config_type: C{string}
         @param config_type: the extension to include in the saved files' names
             (optional, default is to use no extension)
-        @type dir_root: C{string}
-        @param dir_root: the root directory to save config files in
-            (optional, default is to use L{DIRNAME} in the user's home directory)
-        
-        """
-        
-        self.config_type = config_type
+        
+        """
+        
         if config_type:
-            config_ext = '.'+config_type
+            self.config_type = '.'+config_type
         else:
-            config_ext = ''
-
-        if not dir_root:
-            def check_sysvars(x):
-                """Check a system variable to see if it expands to something.
-                
-                @type x: C{string}
-                @param x: the system variable to check
-                @rtype: C{string}
-                @return: the expanded variable, or None if it doesn't expand
-                
-                """
-                
-                y = os.path.expandvars(x)
-                if y != x and os.path.isdir(y):
-                    return y
-                return None
-    
-            for d in ['${APPDATA}', '${HOME}', '${HOMEPATH}', '${USERPROFILE}']:
-                dir_root = check_sysvars(d)
-                if dir_root:
-                    break
-            else:
-                dir_root = os.path.expanduser('~')
-                if not os.path.isdir(dir_root):
-                    dir_root = os.path.abspath(os.path.dirname(sys.argv[0]))
-    
-            dir_root = os.path.join(dir_root,DIRNAME)
-        self.dir_root = dir_root
-
-        if not os.path.isdir(self.dir_root):
-            os.mkdir(self.dir_root,0700)    # exception if failed
-
-        self.dir_torrentcache = os.path.join(dir_root,'torrentcache')
-        if not os.path.isdir(self.dir_torrentcache):
-            os.mkdir(self.dir_torrentcache)
-
-        self.dir_datacache = os.path.join(dir_root,'datacache')
-        if not os.path.isdir(self.dir_datacache):
-            os.mkdir(self.dir_datacache)
-
-        self.dir_piececache = os.path.join(dir_root,'piececache')
-        if not os.path.isdir(self.dir_piececache):
-            os.mkdir(self.dir_piececache)
-
-        self.configfile = os.path.join(dir_root,'config'+config_ext+'.ini')
-        self.statefile = os.path.join(dir_root,'state'+config_ext)
+            self.config_type = ''
+
+        def check_sysvars(x):
+            """Check a system variable to see if it expands to something.
+            
+            @type x: C{string}
+            @param x: the system variable to check
+            @rtype: C{string}
+            @return: the expanded variable, or None if it doesn't expand
+            
+            """
+            
+            y = os.path.expandvars(x)
+            if y != x and os.path.isdir(y):
+                return y
+            return None
+
+        for d in ['${HOME}']:
+            self.home_dir = check_sysvars(d)
+            if self.home_dir:
+                break
+        else:
+            self.home_dir = os.path.expanduser('~')
+            if not os.path.isdir(self.home_dir):
+                self.home_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
+
+        if not os.path.isdir(os.path.join(self.home_dir,DIRNAME)):
+            os.mkdir(os.path.join(self.home_dir,DIRNAME))    # exception if failed
 
         self.TorrentDataBuffer = {}
 
@@ -222,21 +204,53 @@
             if self.config.has_key(k):
                 del self.config[k]
 
-    def checkConfig(self):
+    def setCacheDir(self, cache_dir):
+        """Sets the various cache directory locations.
+        
+        @type cache_dir: C{string}
+        @param cache_dir: the directory to save cache files in
+        
+        """
+
+        if cache_dir:
+            self.cache_dir = cache_dir
+        else:
+            self.cache_dir = os.path.join(self.home_dir,DIRNAME)
+
+        if not os.path.isdir(self.cache_dir):
+            os.mkdir(self.cache_dir)    # exception if failed
+
+        self.dir_torrentcache = os.path.join(self.cache_dir,'torrentcache')
+        if not os.path.isdir(self.dir_torrentcache):
+            os.mkdir(self.dir_torrentcache)
+
+        self.dir_datacache = os.path.join(self.cache_dir,'datacache')
+        if not os.path.isdir(self.dir_datacache):
+            os.mkdir(self.dir_datacache)
+
+        self.dir_piececache = os.path.join(self.cache_dir,'piececache')
+        if not os.path.isdir(self.dir_piececache):
+            os.mkdir(self.dir_piececache)
+
+        self.statefile = os.path.join(self.cache_dir,'state'+self.config_type)
+
+    def checkConfig(self, configfile):
         """Check if a config file already exists.
         
+        @type configfile: C{string}
+        @param configfile: the config file to use
         @rtype: C{boolean}
         @return: whether the config file exists
         
         """
         
-        return os.path.exists(self.configfile)
-
-    def loadConfig(self):
+        return os.path.exists(configfile)
+
+    def loadConfigFile(self, configfile):
         """Load a configuration from a config file.
         
-        @rtype: C{dictionary}
-        @return: the loaded configuration variables
+        @type configfile: C{string}
+        @param configfile: the config file to use
         
         """
         
@@ -258,8 +272,37 @@
                     l.remove(k)
                 except:
                     pass
-        if l: # new default values since last save
-            self.saveConfig()
+
+    def loadConfig(self, params):
+        """Load the configuration from any config files.
+        
+        @type params: C{list} of C{strings}
+        @param params: a list of the command-line arguments
+        @rtype: C{dictionary}
+        @return: the loaded configuration variables
+        @raise IOError: if the specified config file can not be found
+        
+        """
+        
+        try:
+            self.configfile = params[params.index('--configfile')+1]
+            configfile = True
+        except:
+            configfile = False
+            self.configfile = os.path.join(os.path.join(self.home_dir,DIRNAME),
+                                           'config'+self.config_type+'.ini')
+        
+        master_configfile = os.path.join(MASTER_CONFIG, 
+                                         'config'+self.config_type+'.ini')
+        
+        if self.checkConfig(master_configfile):
+            self.loadConfigFile(master_configfile)
+
+        if self.checkConfig(self.configfile):
+            self.loadConfigFile(self.configfile)
+        elif configfile:
+            raise IOError('config file could not be found')
+
         return self.config
 
     def saveConfig(self, new_config = None):

Modified: debtorrent/trunk/DebTorrent/download_bt1.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/download_bt1.py?rev=152&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/download_bt1.py (original)
+++ debtorrent/trunk/DebTorrent/download_bt1.py Thu Jul 12 23:45:16 2007
@@ -200,7 +200,9 @@
     ('logfile', '', 'file to write the tracker logs, use - for stdout (default)'),
     ('allow_get', 0, 'adds a /file?hash={hash} url that allows users to download the torrent file'),
     ('default_tracker', 'http://dttracker.debian.net:6969/announce', 'the default tracker address to use for new torrents'),
-    ('config_dir', '', 'the directory to use to get/store configuration and cache files, if not ' + 
+    ('configfile', '', 'the configuration file to use, if not ' + 
+             'specified then a file in the .DebTorrent directory in the user\'s home directory will be used'),
+    ('cache_dir', '', 'the directory to use to get/store cache files, if not ' + 
              'specified then a .DebTorrent directory in the user\'s home directory will be used'),
     ('disable_http_downloader', 0, '(for testing purposes only) whether to disable the backup HTTP downloader'),
     ]
@@ -650,7 +652,7 @@
     
     def __init__(self, statusfunc, finfunc, errorfunc, excfunc, doneflag,
                  config, response, infohash, id, rawserver, port,
-                 appdataobj = None):
+                 appdataobj):
         """Initialize the instance.
         
         @type statusfunc: C{method}
@@ -713,12 +715,7 @@
         self.tcp_ack_fudge = config['tcp_ack_fudge']
 
         self.selector_enabled = config['selector_enabled']
-        if appdataobj:
-            self.appdataobj = appdataobj
-        elif self.selector_enabled:
-            self.appdataobj = ConfigDir(dir_root = config['config_dir'])
-            self.appdataobj.deleteOldCacheData( config['expire_cache_data'],
-                                                [self.infohash] )
+        self.appdataobj = appdataobj
 
         self.excflag = self.rawserver.get_exception_flag()
         self.failed = False

Modified: debtorrent/trunk/btdownloadheadless.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/btdownloadheadless.py?rev=152&op=diff
==============================================================================
--- debtorrent/trunk/btdownloadheadless.py (original)
+++ debtorrent/trunk/btdownloadheadless.py Thu Jul 12 23:45:16 2007
@@ -252,19 +252,16 @@
     
     h = HeadlessDisplayer()
     while 1:
-        try:
-            configdir = ConfigDir('downloadheadless', params[params.index('--config_dir')+1])
-        except:
-            configdir = ConfigDir('downloadheadless')
-        defaultsToIgnore = ['responsefile', 'url', 'priority']
-        configdir.setDefaults(defaults,defaultsToIgnore)
-        configdefaults = configdir.loadConfig()
+        configdir = ConfigDir('downloadheadless')
         defaults.extend([('save_options',0,
              "whether to save the current options as the new default configuration " +
              "(only for btdownloadheadless.py)"),
              ('separate_all',0, 'whether to separate the architecture:all packages, ' +
               "0 = don't separate, 1 = separate and run architecture:all, " +
               "2 = separate and run all architectures but all")])
+        defaultsToIgnore = ['responsefile', 'url', 'priority', 'save_options']
+        configdir.setDefaults(defaults,defaultsToIgnore)
+        configdefaults = configdir.loadConfig(params)
         try:
             config = parse_params(params, configdefaults)
         except ValueError, e:
@@ -273,6 +270,7 @@
         if not config:
             print get_usage(defaults, 80, configdefaults)
             break
+        configdir.setCacheDir(config['cache_dir'])
         if config['save_options']:
             configdir.saveConfig(config)
         configdir.deleteOldCacheData(config['expire_cache_data'])

Modified: debtorrent/trunk/btlaunchmany.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/btlaunchmany.py?rev=152&op=diff
==============================================================================
--- debtorrent/trunk/btlaunchmany.py (original)
+++ debtorrent/trunk/btlaunchmany.py Thu Jul 12 23:45:16 2007
@@ -145,18 +145,17 @@
           "0 = don't separate, 1 = separate and run architecture:all, " +
           "2 = separate and run all architectures but all, " + "3 = separate and run both"),
     ] )
+    configdefaults = {}
     try:
-        try:
-            configdir = ConfigDir('launchmany', params[params.index('--config_dir')+1])
-        except:
-            configdir = ConfigDir('launchmany')
+        configdir = ConfigDir('launchmany')
         defaultsToIgnore = ['responsefile', 'url', 'priority']
         configdir.setDefaults(defaults,defaultsToIgnore)
-        configdefaults = configdir.loadConfig()
+        configdefaults = configdir.loadConfig(params)
         defaults.append(('save_options',0,
-         "whether to save the current options as the new default configuration " +
-         "(only for btlaunchmany.py)"))
+            "whether to save the current options as the new default configuration " +
+            "(only for btlaunchmany.py)"))
         config, args = parseargs(params, defaults, 0, 0, configdefaults)
+        configdir.setCacheDir(config['cache_dir'])
         if config['save_options']:
             configdir.saveConfig(config)
         configdir.deleteOldCacheData(config['expire_cache_data'])
@@ -165,6 +164,9 @@
         print "Usage: btlaunchmany.py <global options>\n"
         print get_usage(defaults, 80, configdefaults)
         exit(1)
+    except IOError, e:
+        print 'error: ' + str(e) + '\n'
+        exit(2)
 
     LaunchMany(config, HeadlessDisplayer(), configdir)
     if Exceptions:

Modified: debtorrent/trunk/config.debtorrent.ini
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/config.debtorrent.ini?rev=152&op=diff
==============================================================================
--- debtorrent/trunk/config.debtorrent.ini (original)
+++ debtorrent/trunk/config.debtorrent.ini Thu Jul 12 23:45:16 2007
@@ -23,7 +23,8 @@
 #
 # SaveAs
 #
-# The directory to save the downloaded files in.
+# The directory to save the downloaded files in. If left blank then the user's
+# home directory will be used.
 #
 
 # saveas = "/var/cache/debtorrent"
@@ -43,20 +44,20 @@
 #
 # Cache Directory
 #
-# The directory to use to get/store cache files, if left blank then 
-# ~/.DebTorrent will be used.
-#
-
-# cache_dir = "/var/cache/debtorrent/.cache"
+# The directory to use to get/store cache files. If left blank then a
+# .DebTorrent directory in the user's home directory will be used.
+#
+
+# cache_dir = "/var/cache/debtorrent/.DebTorrent"
 
 #
 # Expire Cache Data
 #
-# The number of days after which you wish to expire old cached data. Set this
-# to 0 to never expire cached data.
-#
-
-# expire_cache_data = 10
+# The number of days after which you wish to expire old cached data so that it
+# will no longer be used. Set this to 0 to always use old cached data.
+#
+
+# expire_cache_data = 30
 
 ###############################################################################
 #                              R A T E   L I M I T S

Modified: debtorrent/trunk/debtorrent.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/debtorrent.py?rev=152&op=diff
==============================================================================
--- debtorrent/trunk/debtorrent.py (original)
+++ debtorrent/trunk/debtorrent.py Thu Jul 12 23:45:16 2007
@@ -134,18 +134,17 @@
           "0 = don't separate, 1 = separate and run architecture:all, " +
           "2 = separate and run all architectures but all, " + "3 = separate and run both"),
     ] )
+    configdefaults = {}
     try:
-        try:
-            configdir = ConfigDir('debtorrent', params[params.index('--config_dir')+1])
-        except:
-            configdir = ConfigDir('debtorrent')
+        configdir = ConfigDir('debtorrent')
         defaultsToIgnore = ['responsefile', 'url', 'priority']
         configdir.setDefaults(defaults,defaultsToIgnore)
-        configdefaults = configdir.loadConfig()
+        configdefaults = configdir.loadConfig(params)
         defaults.append(('save_options',0,
          "whether to save the current options as the new default configuration " +
          "(only for debtorrent.py)"))
         config, args = parseargs(params, defaults, 0, 0, configdefaults)
+        configdir.setCacheDir(config['cache_dir'])
         if config['save_options']:
             configdir.saveConfig(config)
         configdir.deleteOldCacheData(config['expire_cache_data'])
@@ -154,6 +153,9 @@
         print "Usage: debtorrent.py <global options>\n"
         print get_usage(defaults, 80, configdefaults)
         exit(1)
+    except IOError, e:
+        print 'error: ' + str(e) + '\n'
+        exit(2)
 
     LaunchMany(config, HeadlessDisplayer(), configdir)
     if Exceptions:

Modified: debtorrent/trunk/test.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/test.py?rev=152&op=diff
==============================================================================
--- debtorrent/trunk/test.py (original)
+++ debtorrent/trunk/test.py Thu Jul 12 23:45:16 2007
@@ -463,7 +463,7 @@
     # Reset the peer ID so it will be different
     resetPeerIDs()
 
-    pid = start(btlaunchmany.run, ['--config_dir', downloader_dir,
+    pid = start(btlaunchmany.run, ['--cache_dir', downloader_dir,
                                    '--port', str(num_down) + '988', 
                                    '--max_upload_rate', '100',
                                    '--minport', '1' + str(num_down) + '000', 




More information about the Debtorrent-commits mailing list