def __init__(self, box_path): self._path = os.path.abspath(box_path) try: box_info = load_info( os.path.join(self.path, 'bpt_meta', 'box_info')) except (OSError, IOError): raise UserError('Invalid box: impossible to read box_info') try: self._id = box_info['id'] self._platform = box_info['platform'] except KeyError, exc: raise UserError('Invalid box_info: missing "%s"', str(exc))
def autobuild(box, filename, configure_options='', keep_temp=False, stdout=None): if not os.path.exists(filename): raise UserError('File %s does not exist' % filename) if os.path.isdir(filename): source_dir = os.path.abspath(filename) autobuild_dir(box, source_dir, os.path.basename(source_dir), configure_options) else: build_dir = mkdtemp() try: basename, unpack_cmd = guess_unpack_cmd(filename) log.info('Unpacking the file...') check_call(unpack_cmd, cwd=build_dir) unpacked_files = [ os.path.join(build_dir, f) for f in os.listdir(build_dir) ] unpacked_dirs = [d for d in unpacked_files if os.path.isdir(d)] if len(unpacked_dirs) != 1: raise UnsupportedPath('Could not find source directory') source_dir, = unpacked_dirs autobuild_dir(box, source_dir, basename, configure_options, stdout) finally: if keep_temp: log.info('Not deleting temporary files in directory %s', build_dir) else: rmtree(build_dir)
class Box(object): '''\ Represents a package box in the filesystem. The constructor expects an existing box. A new one can be created with the class method create() ''' def __init__(self, box_path): self._path = os.path.abspath(box_path) try: box_info = load_info( os.path.join(self.path, 'bpt_meta', 'box_info')) except (OSError, IOError): raise UserError('Invalid box: impossible to read box_info') try: self._id = box_info['id'] self._platform = box_info['platform'] except KeyError, exc: raise UserError('Invalid box_info: missing "%s"', str(exc)) # Ensure that /tmp/boxes exists try: os.makedirs('/tmp/boxes') os.chmod('/tmp/boxes', 0777) except OSError, exc: if exc.errno != 17: raise UserError('Error creating /tmp/boxes')
def _run(self, config, cmd_options, cmd_args): self._require_args(cmd_args, 1) require_box(config) try: for tarball in cmd_args: autobuild_(config.box, tarball, cmd_options.configure_options, cmd_options.keep) except UnsupportedPath, exc: raise UserError(str(exc))
def build(self, box, name_suffix='', stdout=None): appname = self.get_var('BPT_APP_NAME') version = self.get_var('BPT_APP_VERSION') # Check last box the sourcedir was built in bpt_status_file = os.path.join(self.path, '.bpt_status') if os.path.exists(bpt_status_file): bpt_status = load_info(bpt_status_file) last_box_id = bpt_status.get('last_box_id', box.box_id) if last_box_id != box.box_id: log.info( 'Intermediate files built for another package box. Cleaning first.' ) self.clean() # Write the current box_id store_info(bpt_status_file, dict(last_box_id=box.box_id)) if not box.check_platform(): raise UserError( 'Current platform is different from box\'s platform') pkg_name = '%s-%s%s' % (appname, version, name_suffix) pkg = box.create_package(pkg_name, app_name=appname, app_version=version, enabled=False) log.info('Building application %s, in sourcedir %s', appname, self._sourcedir) # Build sh_line = ('source "%s";' % BASE_SH_SCRIPT + 'cd "%s";' % self._sourcedir + 'export BPT_PKG_PREFIX="%s";' % pkg.path + 'export BPT_CPU_COUNT="%d";' % cpu_count() + 'source bpt-rules;' + 'bpt_download;' + 'bpt_unpack;' + 'bpt_build;') retcode = call(['bash', '-e', box.env_script, sh_line], stdout=stdout) if retcode != 0: raise UserError('FATAL: build script exited with status %s' % retcode) box.enable_package(pkg) return pkg
def __init__(self, pkgdir): # XXX(ot): better exceptions? metadata version handling? self._path = pkgdir self._name = os.path.basename(pkgdir) self._dict = load_info(_pkg_info_file(pkgdir)) # Sanity check for k in ['app_name', 'app_version', 'enabled']: if k not in self._dict: raise UserError('Invalid package')
def create(cls, dest_path): '''Creates a directory dest_path and initialize a package box in it. Returns the initialized box. >>> box_path = mktemp() >>> box = Box.create(box_path) >>> box.path == box_path True ''' # Safety checks if os.path.exists(dest_path): raise UserError('Destination already exists') try: os.makedirs(dest_path) for directory in STANDARD_DIRS: os.makedirs(os.path.join(dest_path, directory)) except OSError, exc: raise UserError( 'Impossible to create destination directories: "%s"' % str(exc))
def require_box(config): '''Raise an exception if config.box is None''' if config.box is None: raise UserError('No box given')
try: os.makedirs('/tmp/boxes') os.chmod('/tmp/boxes', 0777) except OSError, exc: if exc.errno != 17: raise UserError('Error creating /tmp/boxes') # Ensure that the virtual path symlink is existing and points # to the correct location try: os.symlink(self.path, self.virtual_path) except OSError, exc: if exc.errno == 17 and \ not os.path.samefile(self.path, self.virtual_path): raise UserError( 'Virtual path symlink %s exists but ' 'does not point to this box. Please remove ' 'it manually', self.virtual_path) def __repr__(self): return "Box('%s')" % self.path @property def box_id(self): '''Unique id of the box, used to build the virtual path''' return self._id @property def path(self): '''Actual path of the box''' return self._path
def __init__(self, sourcedir): self._sourcedir = sourcedir self._rulesfile = os.path.join(sourcedir, 'bpt-rules') if not os.path.exists(self._rulesfile): raise UserError('Invalid sourcedir')