From c5f9da98ce8cacf1d21641e0e6e9f18d738612ed Mon Sep 17 00:00:00 2001 From: "Yaakov M. Nemoy" Date: Thu, 2 Oct 2008 19:23:48 -0400 Subject: Adds a package module that can manage a package plus source in a traditional manner. This is going to be the API for package manipulation for what I have in mind. --- modules/package.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 modules/package.py (limited to 'modules') diff --git a/modules/package.py b/modules/package.py new file mode 100644 index 0000000..cb0548e --- /dev/null +++ b/modules/package.py @@ -0,0 +1,64 @@ +import tarfile + +from os import makedirs, getcwd, chdir +from shutil import copyfileobj, copytree +from os.path import abspath, join, split, splitext, basename + + +from base.module import Module +from base.base import log +from base.exceptions import ExecutionException + +#TODO: Find universal library for parsing URLs and PATHs alike + +class Package(Module): + def __init__(self, name=None): + if not name: + #detect name somehow + pass + # TODO: the following + #check location + #determine type of package + # to configure commands + self.name = name + + def create(self, name): + if not self.name: + makedirs(join(getcwd(), name)) + + def add_spec(self, spec_file, name=None): + log.debug('spec_file is %s' % spec_file) + log.debug('name might be %s' % name) + if self.name: + name = self.name + log.debug('spec_file_name is %s' % name + '.spec') + try: + # we're using copyfileobj so later we can do this from a URL + src = file(spec_file, 'rb') + dst = file(join(getcwd(), name, name + '.spec'), 'wb') + copyfileobj(src, dst) + src.close() + dst.close() + except IOError, e: + log.error(str(e)) + raise ExecutionException(e, 'spec-file could not be added') + + def add_sourceball(self, sourceball_name, name=None, extract_dir=None): + if self.name: + name = self.name + #TODO: this is temporary + else: + log.debug(abspath(name)) + chdir(abspath(name)) + try: + sourceball = tarfile.open(sourceball_name) + if not extract_dir: + extract_dir = min([(x.name, x) for x in sourceball])[0] + extract_dir = basename(abspath(extract_dir)) + log.debug('extract_dir is %s' % extract_dir) + orig_extract_dir = extract_dir + '_orig' + sourceball.extractall() + copytree(abspath(extract_dir), abspath(orig_extract_dir)) + finally: + #TODO: figure out what exceptions TarFile will throw + pass \ No newline at end of file -- cgit