def create(self): """ Create default configuration files at either the default location or the given directory. """ # Check and create configuration directory if os.path.exists(self.config_dir): self.LOG.debug("Configuration directory %r already exists!" % (self.config_dir, )) else: os.mkdir(self.config_dir) # Create default configuration files for filepath in walk_resources("pyrocore", "data/config"): # Load from package data text = pymagic.resource_string("pyrocore", "data/config" + filepath) # Create missing subdirs config_file = self.config_dir + filepath if not os.path.exists(os.path.dirname(config_file)): os.makedirs(os.path.dirname(config_file)) # Write configuration files config_trail = [".default"] if os.path.exists(config_file): self.LOG.debug("Configuration file %r already exists!" % (config_file, )) else: config_trail.append('') for i in config_trail: with closing(open(config_file + i, "w")) as handle: handle.write(text) self.LOG.info("Configuration file %r written!" % (config_file + i, ))
def download_resource(self, download_url, target, guard): """ Helper to download and install external resources. """ download_url = download_url.strip() if not os.path.isabs(target): target = os.path.join(config.config_dir, target) if os.path.exists(os.path.join(target, guard)): self.LOG.info("Already have '%s' in '%s'..." % (download_url, target)) return if not os.path.isdir(target): os.makedirs(target) self.LOG.info("Downloading '%s' to '%s'..." % (download_url, target)) with closing(urllib2.urlopen(download_url)) as url_handle: if download_url.endswith(".zip"): with closing(ZipFile(StringIO( url_handle.read()))) as zip_handle: zip_handle.extractall(target) else: with closing(open(os.path.join(target, guard), "wb")) as file_handle: shutil.copyfileobj(url_handle, file_handle)
def create(self): """ Create default configuration files at either the default location or the given directory. """ # Check and create configuration directory if os.path.exists(self.config_dir): self.LOG.debug("Configuration directory %r already exists!" % (self.config_dir,)) else: os.mkdir(self.config_dir) # Create default configuration files for filepath in walk_resources("pyrocore", "data/config"): # Load from package data text = pymagic.resource_string("pyrocore", "data/config" + filepath) # Create missing subdirs config_file = self.config_dir + filepath if not os.path.exists(os.path.dirname(config_file)): os.makedirs(os.path.dirname(config_file)) # Write configuration files config_trail = [".default"] if os.path.exists(config_file): self.LOG.debug("Configuration file %r already exists!" % (config_file,)) else: config_trail.append('') for i in config_trail: with closing(open(config_file + i, "w")) as handle: handle.write(text) self.LOG.info("Configuration file %r written!" % (config_file + i,))
def create(self, remove_all_rc_files=False): """ Create default configuration files at either the default location or the given directory. """ # Check and create configuration directory if os.path.exists(self.config_dir): self.LOG.debug("Configuration directory %r already exists!" % (self.config_dir, )) else: os.mkdir(self.config_dir) if remove_all_rc_files: for subdir in ('.', 'rtorrent.d'): config_files = list( glob.glob( os.path.join(os.path.abspath(self.config_dir), subdir, '*.rc'))) config_files += list( glob.glob( os.path.join(os.path.abspath(self.config_dir), subdir, '*.rc.default'))) for config_file in config_files: self.LOG.info("Removing %r!" % (config_file, )) os.remove(config_file) # Create default configuration files for filepath in sorted(walk_resources("pyrocore", "data/config")): # Load from package data text = pymagic.resource_string("pyrocore", "data/config" + filepath) # Create missing subdirs config_file = self.config_dir + filepath if not os.path.exists(os.path.dirname(config_file)): os.makedirs(os.path.dirname(config_file)) # Write configuration files config_trail = [".default"] if os.path.exists(config_file): self.LOG.debug("Configuration file %r already exists!" % (config_file, )) else: config_trail.append('') for i in config_trail: with open(config_file + i, "w") as handle: handle.write(text) self.LOG.info("Configuration file %r written!" % (config_file + i, ))
def download_resource(self, download_url, target, guard): """ Helper to download and install external resources. """ if not os.path.isabs(target): target = os.path.join(config.config_dir, target) if os.path.exists(os.path.join(target, guard)): self.LOG.info("Already have '%s' in '%s'..." % (download_url, target)) return if not os.path.isdir(target): os.makedirs(target) self.LOG.info("Downloading '%s' to '%s'..." % (download_url, target)) with closing(urllib2.urlopen(download_url)) as url_handle: if download_url.endswith(".zip"): with closing(ZipFile(StringIO(url_handle.read()))) as zip_handle: zip_handle.extractall(target) else: with closing(open(os.path.join(target, guard), "wb")) as file_handle: shutil.copyfileobj(url_handle, file_handle)