def __exit__(self, exc_type, exc_value, traceback): for source, dest in self.iterbinds(): dest = os.path.join(self.rootfs, dest) if stat.S_ISREG(os.stat(source).st_mode): os.remove(dest) else: util.print_and(subprocess.check_call, ['umount', dest]) return False
def get_apt_version(): """ Execute ``apt-get --version`` and parse the result to get a list of integers used to identify the apt version number. """ version_output = util.print_and(subprocess.check_output, ['apt-get', '--version']).strip() firstline = version_output.splitlines()[0] version_number = firstline.split()[1] return [int(part) for part in version_number.split('.')]
def __enter__(self): for source, dest in self.iterbinds(): dest = os.path.join(self.rootfs, dest) if stat.S_ISREG(os.stat(source).st_mode): shutil.copy2(source, dest) elif os.path.isdir(source): if not os.path.exists(dest): os.makedirs(dest) util.print_and(subprocess.check_call, ['mount', '-o', 'bind', source, dest]) else: pardir = os.path.dirname(dest) if not os.path.exists(pardir): logging.debug('mkdir %s', pardir) os.makedirs(pardir) with open(dest, 'w') as _: pass util.print_and(subprocess.check_call, ['mount', '-o', 'bind', source, dest])
def check_output(self, cmd, *args, **kwargs): self._update_kwargs(kwargs) return util.print_and(subprocess.check_output, self.cmd + cmd, *args, **kwargs)
def call(self, cmd, *args, **kwargs): self._update_kwargs(kwargs) return util.print_and(subprocess.call, self.cmd + cmd, *args, **kwargs)
def popen(self, cmd, *args, **kwargs): self._update_kwargs(kwargs) return util.print_and(subprocess.Popen, self.cmd + cmd, *args, **kwargs)
def create_rootfs(config): try: os.makedirs(config.rootfs) except OSError: if not os.path.isdir(config.rootfs): raise initialize_rootfs(config.rootfs, config.apt_sources) apt_cmd = get_apt_command(config.architecture, config.rootfs, config.apt_http_proxy) if config.is_enabled('apt-update'): util.print_and(subprocess.check_call, apt_cmd + ['update']) else: logging.info('skipping apt-update') if config.is_enabled('apt-download'): default_packages = get_default_packages(config.rootfs, config.apt_include_essential, config.apt_include_priorities) if config.pip_packages: default_packages.append('python-pip') apt_package_list = list(sorted(set(config.apt_packages + default_packages))) apt_version = get_apt_version() if apt_version >= [1, 2, 24]: util.print_and(subprocess.check_call, apt_cmd + ['-y', '--allow-downgrades', '--allow-remove-essential', '--allow-change-held-packages', '--allow-unauthenticated', 'install'] + apt_package_list) else: util.print_and(subprocess.check_call, apt_cmd + ['-y', '--force-yes', 'install'] + apt_package_list) else: logging.info('skipping apt-download') if config.is_enabled('size-report'): deblist = get_apt_archives_plus(config.rootfs, config.external_debs) report = get_apt_report(deblist) if config.apt_size_report is not None: parent_dir = os.path.dirname(config.apt_size_report) try: os.makedirs(parent_dir) except OSError: pass with open(config.apt_size_report, 'w') as reportfile: json.dump(report, reportfile, indent=2, separators=(',', ': ')) else: logging.info('skipping size-report') if config.is_enabled('dpkg-extract'): unpack_archives(config.rootfs, deblist) else: logging.info('skipping dpkg-extract') if config.is_enabled('tweak-fs'): logging.info('tweaking new filesystem') tweak_new_filesystem(config.rootfs) else: logging.info('skipping tweakfs') if config.is_enabled('dpkg-configure') and config.chroot_app is not None: chroot_app = config.chroot_app(config.rootfs, config.binds, config.qemu_binary, config.pip_wheelhouse) logging.info('applying user quirks') config.user_quirks(chroot_app) with chroot_app: logging.info('Executing dpkg --configure') configure_dpkgs(chroot_app, config.rootfs, config.dpkg_configure_retry_count) if config.pip_packages: logging.info('Installing pip packages') install_pip_packages(chroot_app, config.rootfs, config.pip_packages) else: logging.info('skipping dpkg configure step') if config.is_enabled('apt-clean'): # TODO(josh): document what this actually cleans up util.print_and(subprocess.check_call, apt_cmd + ['clean']) for cleanme in ['var/cache/apt/archives', 'var/lib/apt/lists']: cache_dir = os.path.join(config.rootfs, cleanme) for filename in os.listdir(cache_dir): fullpath = os.path.join(cache_dir, filename) if os.path.isdir(fullpath): continue os.remove(fullpath)