def __init__(self, url, destdir="/tmp", destfile=None): if not isinstance(url, inary.uri.URI): url = inary.uri.URI(url) if ctx.config.get_option("authinfo"): url.set_auth_info(ctx.config.get_option("authinfo")) self.url = url self.destdir = destdir self.destfile = destfile self.progress = None self.try_number = 0 self.fetcher = None self.handler = None # spoof user-agent self.useragent = ( ctx.config.values.general.fetcher_useragent or 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' ) self.archive_file = os.path.join(destdir, destfile or url.filename()) self.partial_file = os.path.join( self.destdir, self.url.filename()) + ctx.const.partial_suffix util.ensure_dirs(self.destdir)
def unpack_file_cond(self, pred, target_dir, archive_root=''): """Unpack/Extract files according to predicate function pred: filename -> bool unpacks stuff into target_dir and only extracts files from archive_root, treating it as the archive root""" zip_obj = self.zip_obj for info in zip_obj.infolist(): if pred(info.filename): # check if condition holds # below code removes that, so we find it here is_dir = info.filename.endswith('/') # calculate output file name if archive_root == '': outpath = info.filename else: # change archive_root if util.subpath(archive_root, info.filename): outpath = util.removepathprefix( archive_root, info.filename) else: continue # don't extract if not under ofile = os.path.join(target_dir, outpath) if is_dir: # this is a directory if not os.path.isdir(ofile): os.makedirs(ofile) perm = info.external_attr perm &= 0xFFFF0000 perm >>= 16 perm |= 0x00000100 os.chmod(ofile, perm) continue # check that output dir is present util.ensure_dirs(os.path.dirname(ofile)) # remove output file we might be overwriting. # (also check for islink? for broken symlinks...) if os.path.isfile(ofile) or os.path.islink(ofile): os.remove(ofile) if info.external_attr == self.symmagic: if os.path.isdir(ofile): # A rare case, the file used to be a dir, # now it is a symlink! shutil.rmtree(ofile) target = zip_obj.read(info.filename) os.symlink(target, ofile) else: perm = info.external_attr perm &= 0x08FF0000 perm >>= 16 perm |= 0x00000100 info.filename = outpath zip_obj.extract(info, target_dir) os.chmod(ofile, perm)
def extract_file_synced(self, path, outdir): """Extract file with path to outdir""" data = self.impl.read_file(path) fpath = util.join_path(outdir, path) util.ensure_dirs(os.path.dirname(fpath)) with open(fpath, "wb") as f: f.write(data.encode("utf-8")) f.flush() os.fsync(f.fileno())
def subdir(self, path): subdir = util.join_path(self.dest_dir(), path) # If the directory does not exist, try to create it. try: util.ensure_dirs(subdir) except OSError: pass return subdir
def __init__(self, url, destdir="/tmp", destfile=None): if not isinstance(url, inary.uri.URI): url = inary.uri.URI(url) if ctx.config.get_option("authinfo"): url.set_auth_info(ctx.config.get_option("authinfo")) self.url = url self.destdir = destdir self.destfile = destfile self.progress = None self.archive_file = os.path.join(destdir, destfile or url.filename()) self.partial_file = os.path.join( self.destdir, self.url.filename()) + ctx.const.partial_suffix util.ensure_dirs(self.destdir)
def read_uri_of_repo(self, uri, repo=None, force=False): """Read PSPEC file""" if repo: tmpdir = os.path.join(ctx.config.index_dir(), repo) else: tmpdir = os.path.join(ctx.config.tmp_dir(), 'index') util.clean_dir(tmpdir) util.ensure_dirs(tmpdir) # write uri urlfile = open(util.join_path(tmpdir, 'uri'), 'w') urlfile.write(uri) # uri urlfile.close() self.read_uri(uri, tmpdir, force) if not repo: repo = self.distribution.name() # and what do we do with it? move it to index dir properly newtmpdir = os.path.join(ctx.config.index_dir(), repo) util.clean_dir(newtmpdir) # replace newtmpdir shutil.move(tmpdir, newtmpdir)