def merge(self): cookie.logger.info('merging content in rootfs') archive = '%s/%s-%s.tar.xz' % (self.archives(), self.name(), self.version()) (status, entries, errors) = cookie.shell(quiet=True).run('tar -tf %s' % archive) conflicts = [ e[2:] for e in entries if os.path.isfile('%s/%s' % (self.rootfs(), e[2:])) or os.path.islink('%s/%s' % (self.rootfs(), e[2:])) ] if not os.path.isdir(self.rootfs()): os.makedirs(self.rootfs()) elif conflicts: cookie.logger.abort('conflicts detected: %s' % str(conflicts)) else: cookie.shell().run('cd %s && tar xJf %s/%s-%s.tar.xz' % (self.rootfs(), self.archives(), self.name(), self.version())) open('%s/%s.list' % (self.installed(), self.name()), 'w').write('\n'.join([x[1:] for x in reversed(entries)])) try: path = '%s/packages.json' % self.installed() meta = json.load( open(path)) if os.path.isfile(path) else {} except Exception, e: meta = {} meta[self.name()] = {'version': self.version()} json.dump(meta, open('%s/packages.json' % self.installed(), 'w'))
def clone(self, url, reponame): try: shell = cookie.shell() path = cookie.layout.gitsource(reponame) if os.path.isdir(path): cookie.logger.debug('repository %s already exists' % reponame) else: cookie.logger.debug('cloning repository %s from %s' % (reponame, url)) cookie.shell().run('git clone --bare %s %s' % (url, path)) except Exception, e: raise Exception('could not clone or update %s: %s' % (reponame, e.message))
def mkarchive(self): archive = '%s-%s.tar.xz' % (self.name(), self.version()) cookie.logger.info('creating archive %s' % archive) if not os.path.isdir(self.archives()): os.makedirs(self.archives()) cookie.shell().run('tar cJf %s/%s -C %s .' % (self.archives(), archive, self.destdir())) with open( '%s/%s-%s.sha1' % (self.archives(), self.name(), self.version()), 'w') as handle: print >> handle, cookie.sha1.compute(self.makefile()) handle.close()
def patch(self): cookie.logger.info('running patch on package') profile = self._target.profile() if self._target else None srcdir = '%s/%s' % ( self.workdir(), self._meta['SRCDIR'] ) if 'SRCDIR' in self._meta else '%s/srcdir' % self.workdir() patchfile = '%s/%s/%s-%s.patch' % ( cookie.layout.profiles(), profile, self._name, self._version) if os.path.isdir(srcdir) and os.path.isfile(patchfile): cookie.shell().run('patch -d %s -p1 < %s' % (srcdir, patchfile)) else: cookie.logger.debug('Package does not need patching')
def checkout(self, reponame, revision, destdir): try: shell = cookie.shell() local = cookie.layout.gitsource(reponame) shell.run('mkdir -p %s' % destdir) try: (status, out, err) = shell.run('cd %s && git cat-file -t %s' % (local, revision)) cookie.logger.debug('revision is present in the repository') except Exception, e: cookie.logger.debug('revision not found, updating first') cookie.shell().run('cd %s && git fetch -v' % local) cookie.logger.debug('cloning from the local repository') shell.run('git clone --shared --no-checkout %s %s' % (local, destdir)) cookie.logger.debug('resetting to revision %s' % revision) shell.run('cd %s && git reset --hard %s' % (destdir, revision))
def make(self, rule): cookie.logger.info('running %s on package' % rule) if self._arch not in self.archs(): raise Exception('package %s does not support %s architecture' % (self.name(), self._arch)) else: if not os.path.isdir(self.workdir()): os.makedirs(self.workdir()) srcdir = '%s/%s' % ( self.workdir(), self._meta['SRCDIR'] ) if 'SRCDIR' in self._meta else '%s/srcdir' % self.workdir() envfile = '%s/%s.env' % (cookie.layout.toolchains(), self._profile.toolchain()) s = cookie.shell() s.loadenv() s.setenv('P_WORKDIR', self.workdir()) s.setenv('P_DESTDIR', self.destdir()) s.setenv('P_SYSROOT', self.rootfs()) s.setenv('P_TOOLCHAIN', self._profile.toolchain()) s.setenv('P_ARCH', self._profile.arch()) s.setenv('P_NPROCS', '4') # TODO: Get this programmatically ... s.run('. %s && make -C %s -f %s %s' % (envfile, srcdir if os.path.isdir(srcdir) else self.workdir(), self.makefile(), rule))
def destroy(self, name): current = self.current() if name not in self.list(): cookie.logger.abort('unknown target %s' % name) else: mapping = json.load(open('%s/mapping.json' % cookie.layout.cache())) cookie.shell().run('docker volume rm %s' % mapping['targets'][name]['volume']) del mapping['targets'][name] if name == current: remain = [str(x) for x in mapping['targets'].keys()] mapping['current'] = None if len(remain) == 0 else remain[0] json.dump(mapping, open('%s/mapping.json' % cookie.layout.cache(), 'w'))
def extract(self, name, dest): try: s = cookie.shell() if not os.path.isdir(dest): os.makedirs(dest) if name.endswith('.tar.bz2') or name.endswith('.tbz2'): s.run('cd %s && tar -xjf %s' % (dest, self.archive(name))) elif name.endswith('.tar.gz') or name.endswith('.tgz'): s.run('cd %s && tar -xzf %s' % (dest, self.archive(name))) elif name.endswith('.zip'): s.run('cd %s && unzip %s' % (dest, self.archive(name))) elif name.endswith('.xz'): s.run('cd %s && tar -xf %s' % (dest, self.archive(name))) else: raise Exception('unsupported %s format' % name.split('.')[-1]) except Exception, e: raise Exception('could not extract %s: %s' % (name, str(e)))
def fetch(self, url, name): name = url.split('/')[-1] if name is None else name dest = cookie.layout.distfile(name) cookie.logger.info('retrieving %s from %s' % (name, url)) try: if not os.path.isdir(cookie.layout.distfiles()): os.makedirs(cookie.layout.distfiles()) if not os.path.isfile(dest): (status, out, err) = cookie.shell().run('wget -c %s -O %s.t' % (url, dest)) if status != 0: raise Exception( 'failed to download from "%s", got HTTP status %d' % (url, status)) else: os.rename('%s.t' % (dest), '%s' % (dest, )) else: cookie.logger.debug('already downloaded this archive') except Exception, e: os.unlink('%s.t' % (dest)) raise
def remove(self): cookie.logger.info('Removing cookie docker image') cookie.shell().run('docker rmi -f cookie')
def update(self): cookie.logger.info('Creating or updating cookie docker image') cookie.shell().run('docker build -t cookie %s' % cookie.layout.bootstrap())
def create(self, pname, pboard, name=None): now = str(datetime.date.today()) name = '%s-%s' % (pname, now) if name == None else name cookie.logger.info('creating target %s' % name) if name in self.list(): raise Exception('target %s already exists' % name) elif pname not in cookie.profiles.list(): raise Exception('unknown profile %s' % pname) elif pboard not in cookie.profiles.boards(pname): raise Exception('unknown board %s for profile %s', (pboard, pname)) else: cookie.logger.debug('creating target volume') (s, volume, e) = cookie.shell(quiet=True).run('docker volume create') cookie.logger.debug('adding target mapping') mapping_file = '%s/mapping.json' % cookie.layout.cache() mapping = json.load( open(mapping_file)) if os.path.isfile(mapping_file) else { 'targets': {} } mapping['current'] = name mapping['targets'][name] = { 'created_on': now, 'profile': pname, 'board': pboard, 'volume': volume[0] } json.dump(mapping, open(mapping_file, 'w')) try: profile = cookie.profiles.get(pname, pboard) source_file = '%s/%s.config' % (cookie.layout.toolchains(), profile.toolchain()) source_sha1 = cookie.sha1.compute(source_file) target_path = '%s/toolchains' % cookie.layout.cache() target_file = '%s/%s.sha1' % (target_path, profile.toolchain()) target_sha1 = cookie.sha1.load(target_file) cookie.logger.debug('toolchain cache directory is %s' % target_path) cookie.logger.debug('profile toolchain sha1 is: %s' % source_sha1) cookie.logger.debug('prebuilt toolchain sha1 is: %s' % target_sha1) if not os.path.isdir(target_path): cookie.logger.debug('creating missing cache directory') os.makedirs(target_path) if source_sha1 == target_sha1: cookie.logger.debug('reusing packaged toolchain') cookie.docker.run( 'tar -C /opt/target -xJf /opt/cookie/cache/toolchains/%s.tar.xz' % profile.toolchain()) else: cookie.logger.debug('compiling the profile toolchain') cookie.docker.run( 'cp /opt/cookie/toolchains/%s.config /opt/target/.config' % profile.toolchain()) cookie.docker.run('ct-ng -C /opt/target build') cookie.docker.run( 'tar -C /opt/target -cJf /opt/cookie/cache/toolchains/%s.tar.xz toolchain' % profile.toolchain()) cookie.docker.run( 'rm -rf /opt/target/.build /opt/target/build.log /opt/target/.config' ) cookie.sha1.save(source_sha1, target_file) except Exception, e: self.destroy(name) raise e
def mkimage(self): prf = cookie.profiles.get(self.profile(), self.board()) cookie.shell().run('mkimage %s %s' % (prf.image('size'), prf.image('boot')))