def Cached(cls, cache_dir, *args, **kwargs): """Reuses previously fetched GSUtil, performing the fetch if necessary. Arguments: cache_dir: The toplevel cache dir. *args, **kwargs: Arguments that are passed through to the GSContext() constructor. Returns: An initialized GSContext() object. """ common_path = os.path.join(cache_dir, constants.COMMON_CACHE) tar_cache = cache.TarballCache(common_path) key = (cls.GSUTIL_TAR, ) # The common cache will not be LRU, removing the need to hold a read # lock on the cached gsutil. ref = tar_cache.Lookup(key) if ref.Exists(): logging.debug('Reusing cached gsutil.') else: logging.debug('Fetching gsutil.') with osutils.TempDirContextManager( base_dir=tar_cache.staging_dir) as tempdir: gsutil_tar = os.path.join(tempdir, cls.GSUTIL_TAR) cros_build_lib.RunCurl([cls.GSUTIL_URL, '-o', gsutil_tar], debug_level=logging.DEBUG) ref.SetDefault(gsutil_tar) gsutil_bin = os.path.join(ref.path, 'gsutil', 'gsutil') return cls(*args, gsutil_bin=gsutil_bin, **kwargs)
def _UpdateTarball(self, url, ref): """Worker function to fetch tarballs""" with osutils.TempDirContextManager( base_dir=self.tarball_cache.staging_dir) as tempdir: local_path = os.path.join(tempdir, os.path.basename(url)) self.gs_ctx.Copy(url, tempdir) ref.SetDefault(local_path, lock=True)
def _GetRCFile(self, env, user_rc): """Returns path to dynamically created bashrc file. The bashrc file sets the environment variables contained in |env|, as well as sources the user-editable chrome_sdk.bashrc file in the user's home directory. That rc file is created if it doesn't already exist. Arguments: env: A dictionary of environment variables that will be set by the rc file. user_rc: User-supplied rc file. """ if not os.path.exists(user_rc): osutils.Touch(user_rc, makedirs=True) self._VerifyGoma(user_rc) # We need a temporary rc file to 'wrap' the user configuration file, # because running with '--rcfile' causes bash to ignore bash special # variables passed through subprocess.Popen, such as PS1. So we set them # here. # Having a wrapper rc file will also allow us to inject bash functions into # the environment, not just variables. with osutils.TempDirContextManager() as tempdir: contents = [] for key, value in env.iteritems(): contents.append("export %s='%s'\n" % (key, value)) contents.append('. "%s"\n' % user_rc) rc_file = os.path.join(tempdir, 'rcfile') osutils.WriteFile(rc_file, contents) yield rc_file
def CreatePackages(targets_wanted, output_dir, root='/'): """Create redistributable cross-compiler packages for the specified targets This creates toolchain packages that should be usable in conjunction with a downloaded sysroot (created elsewhere). Tarballs (one per target) will be created in $PWD. Args: targets_wanted: The targets to package up root: The root path to pull all packages/files from """ osutils.SafeMakedirs(output_dir) ldpaths = lddtree.LoadLdpaths(root) targets = ExpandTargets(targets_wanted) with osutils.TempDirContextManager() as tempdir: # We have to split the root generation from the compression stages. This is # because we hardlink in all the files (to avoid overhead of reading/writing # the copies multiple times). But tar gets angry if a file's hardlink count # changes from when it starts reading a file to when it finishes. with parallel.BackgroundTaskRunner(CreatePackagableRoot) as queue: for target in targets: output_target_dir = os.path.join(tempdir, target) queue.put([target, output_target_dir, ldpaths, root]) # Build the tarball. with parallel.BackgroundTaskRunner( cros_build_lib.CreateTarball) as queue: for target in targets: tar_file = os.path.join(output_dir, target + '.tar.xz') queue.put([tar_file, os.path.join(tempdir, target)])
def _Insert(self, key, tarball_path): """Insert a tarball and its extracted contents into the cache.""" with osutils.TempDirContextManager( base_dir=self.staging_dir) as tempdir: extract_path = os.path.join(tempdir, 'extract') os.mkdir(extract_path) Untar(tarball_path, extract_path) DiskCache._Insert(self, key, extract_path)
def main(argv): options = ParseCommandLine(argv) FinishParsing(options) cros_build_lib.AssertInsideChroot() with sudo.SudoKeepAlive(ttyless_sudo=False): with osutils.TempDirContextManager(sudo_rm=True) as tempdir: sysroot = os.path.join(tempdir, SYSROOT) os.mkdir(sysroot) GenerateSysroot(sysroot, options).Perform()
def main(argv): options, args = _ParseCommandLine(argv) _PostParseCheck(options, args) # Set cros_build_lib debug level to hide RunCommand spew. if options.verbose: logging.getLogger().setLevel(logging.DEBUG) else: logging.getLogger().setLevel(logging.INFO) with osutils.TempDirContextManager() as tempdir: staging_dir = options.staging_dir if not staging_dir: staging_dir = os.path.join(tempdir, 'chrome') deploy = DeployChrome(options, tempdir, staging_dir) try: deploy.Perform() except results_lib.StepFailure as ex: raise SystemExit(str(ex).strip())
def _GSCopyMock(_self, path, dest): """Used to simulate a GS Copy operation.""" with osutils.TempDirContextManager() as tempdir: local_path = os.path.join(tempdir, os.path.basename(path)) osutils.Touch(local_path) shutil.move(local_path, dest)
def _TempDirContext(self): return osutils.TempDirContextManager(base_dir=self.staging_dir)