def fetch_and_archive(service, email, archive_path, mid_list): logger.info( 'fetch_and_archive started. email: %s, archive_path: %s, mid_list: %d message(s)' % (email, archive_path, len(mid_list)) ) if path_isabs(archive_path): output_dir = realpath(archive_path) else: output_dir = realpath(expanduser(path_join(getcwd(), archive_path))) count = 0 error = 0 for mid in mid_list: file_name = path_join(output_dir, ('%x.gz' % mid)) message = fetch_mail(service, email, mid) if not message: error += 1 continue with gzip_open(file_name, 'wb') as f: f.write(urlsafe_b64decode(message['raw'])) logger.debug('Message id %x gzipped to %s.' % (mid, file_name)) count += 1 logger.info('fetch_and_archive completed. Total %d item(s) saved. Error %d item(s).' % (count, error))
def get_absolute_path(path): if path_isabs(path): abs_dir = realpath(path) else: abs_dir = realpath(expanduser(path_join(getcwd(), path))) return abs_dir
def _pkg_prepare_file(self, src, dst, substitution=False): if path_isabs(dst): # This function should not attempt doing dangerous things # and absolute file destination path is not expected anyway # (copying to destination paths is handled by ``install'') raise DistutilsSetupError \ ("Unexpected attempt to copy file %s to absolute" " location %s" % (src, dst)) if substitution: if DEBUG: print (DBGPFX + "\tSubstituting strings while copying %s -> %s" % (src, dst)) if self.dry_run: return try: with open(src, "r") as fr: with open(dst, "w") as fw: content = fr.read() for key in filter( lambda k: not k.startswith("_") and k not in self.boolean_options, self.pkg_params.iterkeys() ): if DEBUG: print (DBGPFX + "\tReplace %s -> %s" % ('@' + key.upper() + '@', self.pkg_params[key])) content = content.replace( '@' + key.upper() + '@', self.pkg_params[key] ) fw.write(content) copystat(src, dst) except IOError, e: raise DistutilsSetupError(str(e))
def get_archive(mid, archive_path): if path_isabs(archive_path): archive_dir = realpath(archive_path) else: archive_dir = realpath(expanduser(path_join(getcwd(), archive_path))) path = path_join(archive_dir, '%x.gz' % mid) with gzip_open(path, 'rb') as f: mime = f.read() logger.debug('Archive \'%s\' extracted successfully. %d bytes' % (path, len(mime))) return mime
def which(name, single='', *paths, **redefine_check): """Mimic `which' UNIX utility Both `single` and `paths` denotes paths to be tried for `name` lookup, which includes a decision from the perspective of a filter function given by `check` keyword argument (default: a test if the file exists at all). What is special about `single` is that it can be defined as either plain one-path string, PATH-like-separated list of paths, or an iterable. It is decomposed into a list of paths and then `paths` items appended. Apparently, when `name` is provided as an absolute path, only the `check` (i.e., no lookup) phase is performed. If nothing matching the criteria is found, `None` is returned, nominal path as per `name` (and `check` for that matter) otherwise. Empty string instructs code to use environment's PATH instead. """ check, expand_path = redefine_check.pop('check', path_isfile), True if path_isabs(name): where, expand_path = [''], False else: where = list(single.split(pathsep) if not tuplist(single) else single) where.extend(paths) where.reverse() while where: p = where.pop() if not p: if expand_path: where.extend(reversed(environ.get('PATH', '').split(pathsep))) expand_path = False continue elif where: break # degenerated case: multiple '' p = path_join(abspath(p), name) if check(p): return p else: return None
def _pkg_prepare_file(self, src, dst, substitution=False): if path_isabs(dst): # This function should not attempt doing dangerous things # and absolute file destination path is not expected anyway # (copying to destination paths is handled by ``install'') raise DistutilsSetupError \ ("Unexpected attempt to copy file %s to absolute" " location %s" % (src, dst)) if substitution: if DEBUG: print( DBGPFX + "\tSubstituting strings while copying %s -> %s" % (src, dst)) if self.dry_run: return try: with open(src, "r") as fr: with open(dst, "w") as fw: content = fr.read() for key in filter( lambda k: not k.startswith("_") and k not in self.boolean_options, self.pkg_params.iterkeys()): if DEBUG: print( DBGPFX + "\tReplace %s -> %s" % ('@' + key.upper() + '@', self.pkg_params[key])) content = content.replace( '@' + key.upper() + '@', self.pkg_params[key]) fw.write(content) copystat(src, dst) except IOError, e: raise DistutilsSetupError(str(e))