# Fedora Developer Shell # # 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; version 2 of the License. # # 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # Authors: Yaakov M. Nemoy # from __future__ import with_statement import tarfile from os import makedirs, getcwd, chdir, listdir from shutil import copyfileobj, copytree from os.path import abspath, join, split, splitext, basename, exists from configobj import ConfigObj from subprocess import Popen, PIPE from urllib import urlretrieve from base.module import Module from base.base import log from base.exceptions import ExecutionException from base.util import pwd, copy from base.profiles import ver_rel #TODO: Find universal library for parsing URLs and PATHs alike class Package(Module): def __init__(self, name=None): if not name: log.debug('no name with package') cwd = getcwd() log.debug(split(cwd)) if self.is_dir_pkg(cwd): self.load_pkg(cwd) else: self.make_pkg(cwd) name = split(getcwd())[1] self.code_dir = getcwd() #detect name somehow else: log.debug('package.init with name ' + name) dir = abspath(name) if not exists(dir): makedirs(dir) if self.is_dir_pkg(dir): self.load_pkg(dir) else: self.make_pkg(dir) def is_dir_pkg(self, dir): with pwd(dir): cfg = ConfigObj('.devshell') try: if cfg['type'] == 'package': log.debug('is type package') return True else: return False except KeyError, e: return False def load_pkg(self, dir): log.debug('package.load_pkg') with pwd(dir): self.cfg = ConfigObj('.devshell') self.name = self.cfg['name'] self.code_dir = self.cfg['code_dir'] def make_pkg(self, dir): log.debug('package.make_pkg') with pwd(dir): code_dir = dir name = split(getcwd())[1] self.cfg = ConfigObj('.devshell') self.cfg['type'] = 'package' self.cfg['name'] = self.name = name self.cfg['code_dir'] = self.code_dir = code_dir self.cfg.write() def add_spec(self, spec_file): log.debug('spec_file is %s' % spec_file) log.debug('spec_file_name is %s' % self.name + '.spec') with pwd(self.code_dir): try: copy(spec_file, self.name) except IOError, e: log.error(str(e)) raise ExecutionException(e, 'spec-file could not be added') def orig_dir(self, dir): return dir + '_orig' def add_sourceball(self, sourceball_name, extract_dir=None): log.debug('addincg sourceball with code_dir ' + self.code_dir) with pwd(self.code_dir): try: sourceball_name = urlretrieve(sourceball_name, split(sourceball_name)[1])[0] self.cfg['sourceball'] = sourceball_name 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) log.debug('config is of ' + str(self.cfg)) self.cfg['source'] = extract_dir log.debug('cfg[\'source\'] is ' + self.cfg['source']) log.debug('set source') orig_extract_dir = self.orig_dir(extract_dir) sourceball.extractall() copytree(abspath(extract_dir), abspath(orig_extract_dir)) except OSError, e: #TODO: Fill this in with something better #Chances are the _orig dir already exists raise ExecutionException(e, 'something went wrong') #TODO: figure out what exceptions TarFile will throw def close(self): log.debug('writing self.cfg for package') with pwd(self.code_dir): self.cfg.write() @property def spec_file(self): return self.name + '.spec' def get_srpm_name(self, profile): with pwd(self.code_dir): ver, rel = ver_rel(self.spec_file, profile.dist_defines()) return '%s-%s-%s.src.rpm' % (self.name, ver, rel) __all__ = ['Package']