def create_config(self): basedirs = self._get_basedirs() for basedir in basedirs: bd = path(basedir) if not bd.isdir(): bd.makedirs() confdirs = self._get_confdirs() for confdir in confdirs: cfd = path(confdir) if not cfd.isdir(): cfd.makedirs() # handling too many confdirs is irritating # the commented line below uses the same list # for all the keys. This is not immediately apparent # and was the cause of much confusion. #confdir_map = dict().fromkeys(confdirs, []) confdir_map = dict().fromkeys(confdirs) # use another method to initialize the dictionary # with *separate* empty lists for confdir in confdir_map: confdir_map[confdir] = [] for section in self.config.sections(): confdir = self.config.get(section, 'confdir') confdir_map[confdir].append(section) for confdir in confdir_map: sections = confdir_map[confdir] distlines, updatelines = self._generate_section_configs(sections) self._write_config(confdir, distlines, updatelines)
def _update_configfile(self, filename, lines): configfile = path(filename) origlines = configfile.lines() marker = self._comment_lines()[0] line = origlines.pop(0) while not line.startswith(marker): line = origlines.pop(0) all_lines = lines + ['', line] + origlines configfile.write_lines(all_lines)
def seturl(self, url): """Uses urlparse to set the attributes for url""" url = str(url) protocol, host, path_, parameters, query, frag_id = urlparse.urlparse(url) self.protocol = protocol self.host = host self.path = path(path_) self.parameters = parameters self.query = query self.frag_id = frag_id
def seturl(self, url): """Uses urlparse to set the attributes for url""" url = str(url) protocol, host, path_, parameters, query, frag_id = urlparse.urlparse( url) self.protocol = protocol self.host = host self.path = path(path_) self.parameters = parameters self.query = query self.frag_id = frag_id
def make_simple_seed_files(packages, listname='mypacks'): tmpdir = tempfile.mkdtemp('seeds', 'repserve') tmpdir = path(tmpdir) blacklist = tmpdir / 'blacklist' blacklist.write_bytes('') structure = tmpdir / 'STRUCTURE' structure.write_lines(['%s:' % listname]) package_lines = make_seed_contents(packages) package_list = tmpdir / listname package_list.write_lines(package_lines) return tmpdir
def _write_config(self, confdir, distlines, updatelines): confdir = path(confdir) distfile = confdir / 'distributions' updatesfile = confdir / 'updates' if not distfile.isfile(): print "creating new distributions file: %s" % distfile self._new_configfile(distfile, distlines) else: self._update_configfile(distfile, distlines) if not updatesfile.isfile(): print "creating new updates file: %s" % updatesfile self._new_configfile(updatesfile, updatelines) else: self._update_configfile(updatesfile, updatelines)
def unzip_list_file(filename): filename = path(filename) cmd = ['gzip', '-cd', filename] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) tmpfile = tempfile.TemporaryFile() block = proc.stdout.read(BLOCK_SIZE) while block: tmpfile.write(block) block = proc.stdout.read(BLOCK_SIZE) retval = proc.wait() if retval: raise CalledProcessError, "Problem reading/unzipping %s" % filename tmpfile.seek(0) return tmpfile
def unzip_list_file(filename): filename = path(filename) cmd = ['gzip', '-cd', filename] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) tmpfile = tempfile.TemporaryFile() block = proc.stdout.read(BLOCK_SIZE) while block: tmpfile.write(block) block = proc.stdout.read(BLOCK_SIZE) retval = proc.wait() if retval: raise CalledProcessError , "Problem reading/unzipping %s" % filename tmpfile.seek(0) return tmpfile
def parse_sources_list(filename='/etc/apt/sources.list', arch=None): if arch is None: arch = get_architecture() debarch = {'deb': arch, 'deb-src': 'source'} parsed_sources = [] source_lines = path(filename).lines() for line in source_lines: line = line.strip() if line.startswith('deb'): parsed = parse_aptsource_line(line) if parsed['arch'] in debarch: parsed['arch'] = debarch[parsed['arch']] else: raise RuntimeError, "can't handle debtype %s" % parsed['arch'] parsed_sources.append(parsed) return parsed_sources
def parse_sources_list(filename='/etc/apt/sources.list', arch=None): if arch is None: arch = get_architecture() debarch = {'deb':arch, 'deb-src':'source'} parsed_sources = [] source_lines = path(filename).lines() for line in source_lines: line = line.strip() if line.startswith('deb'): parsed = parse_aptsource_line(line) if parsed['arch'] in debarch: parsed['arch'] = debarch[parsed['arch']] else: raise RuntimeError , "can't handle debtype %s" % parsed['arch'] parsed_sources.append(parsed) return parsed_sources
def add_filterlist(self, opts, args): sections = self._determine_sections(opts) filename = None if len(args) != 1: raise ImproperArgumentsError , "Need a name/filename argument" arg = path(args[0]) if not arg.isfile(): name = arg filename = use_stdin_instead_of_filename() else: name = arg.basename() filename = arg flist = self.flmanager.make_filterlist(name, filename=filename) self.flmanager.write_filterlist_to_confdirs(flist, sections) for section in sections: self.flmanager.add_filterlist_to_section(section, flist)
def add_filterlist(self, opts, args): sections = self._determine_sections(opts) filename = None if len(args) != 1: raise ImproperArgumentsError, "Need a name/filename argument" arg = path(args[0]) if not arg.isfile(): name = arg filename = use_stdin_instead_of_filename() else: name = arg.basename() filename = arg flist = self.flmanager.make_filterlist(name, filename=filename) self.flmanager.write_filterlist_to_confdirs(flist, sections) for section in sections: self.flmanager.add_filterlist_to_section(section, flist)
def initialize(self): outparent = self.config.getpath('DEFAULT', 'reprepro_parent_outdir') here = self._pushd_home() gnupg_confdir = path('.gnupg') if gnupg_confdir.isdir(): print "It appears that repserve's ~/.gnupg has already been initialized" return fullname = self.config.get('DEFAULT', 'fullname') email = self.config.get('DEFAULT', 'email') if not email: email = None make_default_signing_key(fullname=fullname, email=email) keyid = get_gpg_keyid(fullname) #print "found key id", keyid self.config.set('DEFAULT', 'signwith', keyid) self.config.write_file() self.export_archive_key(keyid=keyid, outdir=outparent) os.chdir(here)
def _write_file(self, filename): filename = path(filename) lines = self._create_lines() filename.write_lines(lines)
def set_path(self, path_): """Method for setting the path attribute, and coercing it to be a path instance.""" if not isinstance(path_, path): path_ = path(path_) self.path = path_
def parse_file(self, filename): lines = path(filename).lines() return self.parse_lines(lines)
def write_file(self, confdir): filename = path(confdir) / self.name if filename.isfile(): print "Overwriting previous filterlist", str(filename) self._write_file(filename)
import sys import tempfile import apt_pkg from repserve.base import get_architecture from repserve.base import unzip_list_file from repserve.path import path germsite = path('/usr/lib/germinate') if not germsite.isdir(): raise RuntimeError , "You need to have germinate installed to use this module." sys.path.append(germsite) from Germinate import Germinator import Germinate.seeds # a function to add " * " to each package # and return a list of lines def make_seed_contents(packages): lines = [] for package in packages: lines.append(' * %s' % package) return lines # a function to make simple seed files # from a list of packages # returns the path to the temporary directory # it creates (which will be used as the # --seed-dist option for the germinate command def make_simple_seed_files(packages, listname='mypacks'):
def _new_configfile(self, filename, lines): configfile = path(filename) all_lines = lines + [''] + self._comment_lines() configfile.write_lines(all_lines)
def _pushd_home(self): here = path.getcwd() homedir = path('~/').expand() os.chdir(homedir) return here
def getpath(self, section, option): return path(self.get(section, option))
def parse_filterlist(self, confdir): filename = path(confdir) / self.name return self.parse_file(filename)