def create_default_user_config(self): user_config_dir = UserInfo.config_folder() user_config_file = os.path.join(user_config_dir, "config.json") default_config = json.loads(AppConfig.DEFAULT_CONFIG) default_config["maintainer-info"] = UserInfo.maintainer_info() for app in default_config.get("apps", {}).values(): secret_key = base64.encodestring(os.urandom(32)).decode("utf-8") app\ .setdefault("appconfig", {})\ .setdefault("SECRET_KEY", secret_key) #end for try: os.makedirs(user_config_dir, exist_ok=True) with open(user_config_file, "w", encoding="utf-8") as f, \ self._lock_file(f) as f: os.fchmod(f.fileno(), 0o0600) json.dump(default_config, f, ensure_ascii=False, indent=4) except Exception as e: raise BoltError( "failed to store '{}': {}".format(user_config_file, str(e)) ) return default_config
def refresh(self, releases=False, mirrors=False, overwrite_existing=False, **kwargs): items_to_fetch = [] if releases: items_to_fetch.append("releases") if mirrors: items_to_fetch.append("mirrors") for item in items_to_fetch: os.makedirs(UserInfo.config_folder(), exist_ok=True) filename = "{}.json".format(item) src_url = '/'.join([self.base_url, filename]) data = self._fetch_json(src_url) dest_file = os.path.join(UserInfo.config_folder(), filename) # Run the appropriate merge helper. getattr(self, "_merge_{}".format(item))( data, dest_file, overwrite_existing=overwrite_existing)
def load_user_config(self): config = None user_config_file = os.path.join(UserInfo.config_folder(), "config.json") if os.path.exists(user_config_file): with open(user_config_file, "r", encoding="utf-8") as f, \ self._lock_file(f) as f: config = json.load(f) else: config = self.create_default_user_config() return config
def __init__(self, release, arch="amd64", components=None, cache_dir=None, security_enabled=True, updates_enabled=False, keyring=None): self.release = release self.arch = arch if not components: components = ["main", "contrib", "non-free"] self.components = components if not cache_dir: cache_dir = os.path.realpath( os.path.join(UserInfo.cache_dir(), "debian")) self._cache_dir = cache_dir self._keyring = keyring self.sources_list = [ ("debian", "http://ftp.debian.org/debian/dists/{}".format(release)) ] if security_enabled: self.sources_list.append(( "debian-security", "http://security.debian.org/debian-security/dists/{}-security" # noqa: .format(release))) #end if if updates_enabled: self.sources_list.append( ("debian-updates", "http://ftp.debian.org/debian/dists/{}-updates".format( release))) #end if self.source = {} self.binary = {}
def _load_json_file(self, which): result = collections.OrderedDict() json_file = os.path.join(UserInfo.config_folder(), "{}.json".format(which)) if not os.path.exists(json_file): self.refresh(**{which: True}) try: with open(json_file, "r", encoding="utf-8") as f, \ self._lock_file(f) as f: result = json.load(f, object_pairs_hook=collections.OrderedDict) #end with except DistroInfoError: raise except Exception as e: raise DistroInfoError("error loading '{}': {}".format( json_file, str(e))) return result
def __init__(self, filename, cache_dir=None, **kwargs): self.parms = { "build_for": "target", "copy_archives": True, "debug_pkgs": True, "disable_packages": [], "enable_packages": [], "force_local": False, "format": "deb", "ignore_deps": False, "outdir": None, } self.parms.update(kwargs) build_for = self.parms["build_for"] if "machine" in kwargs: machine = kwargs["machine"] elif build_for in ["tools", "cross-tools"]: machine = Platform.tools_machine() else: machine = Platform.target_machine() xml_doc = Specfile(filename).xml_doc self.info = {} if not cache_dir: cache_dir = UserInfo.cache_dir() self._cache_dir = cache_dir # copy maintainer, email, version, revision to package sections for attr_name in ["maintainer", "email", "epoch", "version", "revision"]: xpath = "/control/changelog/release[1]/@%s" % attr_name try: attr_val = xml_doc.xpath(xpath)[0] except IndexError: continue xpath = "/control/*[name() = 'source' or name() = 'package']" for pkg_node in xml_doc.xpath(xpath): pkg_node.attrib[attr_name] = attr_val #end for xpath = "/control/source/@name" source_name = xml_doc.xpath(xpath)[0] xpath = "/control/source/@repo" repo_name = xml_doc.xpath(xpath)[0] xpath = "/control/source/@architecture-independent" try: is_arch_indep = xml_doc.xpath(xpath)[0].lower() except IndexError: is_arch_indep = "false" #end try for pkg_node in xml_doc.xpath("/control/package"): pkg_node.attrib["source"] = source_name pkg_node.attrib["repo"] = repo_name if build_for in ["tools", "cross-tools"]: pkg_node.attrib["architecture"] = "tools" elif is_arch_indep.lower() == "true": pkg_node.attrib["architecture"] = "all" else: pkg_node.attrib["architecture"] = machine #end for xml_doc.xpath("/control/changelog")[0].attrib["source"] = source_name self.defines = { "BOLT_SOURCE_DIR": "sources", "BOLT_BUILD_DIR": "sources", "BOLT_INSTALL_DIR": "install", "BOLT_WORK_DIR": os.getcwd(), "BOLT_BUILD_TYPE": Platform.target_type(), "BOLT_TOOLS_TYPE": Platform.tools_type(), "BOLT_BUILD_FOR": build_for } if build_for == "tools": self.defines["BOLT_HOST_TYPE"] = Platform.tools_type() self.defines["BOLT_TARGET_TYPE"] = Platform.tools_type() self.defines["BOLT_INSTALL_PREFIX"] = "/tools" elif build_for == "cross-tools": self.defines["BOLT_HOST_TYPE"] = Platform.tools_type() self.defines["BOLT_TARGET_TYPE"] = Platform.target_type() self.defines["BOLT_INSTALL_PREFIX"] = "/tools" else: self.defines["BOLT_HOST_TYPE"] = Platform.target_type() self.defines["BOLT_TARGET_TYPE"] = Platform.target_type() self.defines["BOLT_INSTALL_PREFIX"] = "/usr" #end if for node in xml_doc.xpath("/control/defines/def"): self.defines[node.get("name")] = node.get("value", "") # these *must* be absolute paths for s in ["SOURCE", "BUILD", "INSTALL"]: if os.path.isabs(self.defines["BOLT_%s_DIR" % s]): self.defines["BOLT_%s_DIR" % s] = os.path.realpath( self.defines["BOLT_%s_DIR" % s]) else: self.defines["BOLT_%s_DIR" % s] = os.path.realpath( os.sep.join([self.defines["BOLT_WORK_DIR"], self.defines["BOLT_%s_DIR" % s]]) ) #end if #end for self.src_pkg = SourcePackage( xml_doc.xpath("/control/source")[0], build_for=build_for, machine=machine, copy_archives=self.parms["copy_archives"] ) self.src_pkg.basedir = os.path.realpath(os.path.dirname(filename)) if self.parms["enable_packages"]: for p in self.parms["enable_packages"]: if not xml_doc.xpath("/control/package[@name='%s']" % p): raise InvocationError("unknown binary package '%s'." % p) #end for #end if if self.parms["disable_packages"]: for p in self.parms["disable_packages"]: if not xml_doc.xpath("/control/package[@name='%s']" % p): raise InvocationError("unknown binary package '%s'." % p) #end for #end if self.bin_pkgs = [] for node in xml_doc.xpath("/control/package"): pkg = DebianPackage( node, debug_pkgs=self.parms["debug_pkgs"], install_prefix=self.defines["BOLT_INSTALL_PREFIX"], host_type=self.defines["BOLT_HOST_TYPE"], build_for=build_for, machine=machine ) if self.parms["enable_packages"]: if pkg.name not in self.parms["enable_packages"]: continue if self.parms["disable_packages"]: if pkg.name in self.parms["disable_packages"]: continue if not pkg.builds_for(build_for): continue if not pkg.is_supported_on(machine): continue if build_for == "tools": pkg.name = "tools-" + pkg.name elif build_for == "cross-tools": pkg.name = "tools-target-" + pkg.name #end if pkg.basedir = self.defines["BOLT_INSTALL_DIR"] if self.parms.get("outdir"): pkg.output_dir = os.path.realpath(self.parms["outdir"]) self.bin_pkgs.append(pkg) #end for self.changelog = Changelog(xml_doc.xpath('/control/changelog')[0])
def cache_dir(): cache_dir = UserInfo.cache_dir() if not cache_dir: raise BuildBoxError("unable to determine cache directory.") return cache_dir
def homedir(): home = UserInfo.homedir() if not home: raise BuildBoxError("unable to determine user home directory.") return home