Open some closed files if they may have new lines.

        Opening more files may require us to close some of the already open
        files.
333631
def open_closed_files(self):
        
        if not self.can_open_more_files:
            # If we can't open any more files. Close all of the files.
            self.close_all_files()

        files_with_no_updates = []
        while len(self.closed_file_infos) > 0:
            if (len(self.open_file_infos) >=
                    ray_constants.LOG_MONITOR_MAX_OPEN_FILES):
                self.can_open_more_files = False
                break

            file_info = self.closed_file_infos.pop(0)
            assert file_info.file_handle is None
            # Get the file size to see if it has gotten bigger since we last
            # opened it.
            try:
                file_size = os.path.getsize(file_info.filename)
            except (IOError, OSError) as e:
                # Catch "file not found" errors.
                if e.errno == errno.ENOENT:
                    logger.warning("Warning: The file {} was not "
                                   "found.".format(file_info.filename))
                    self.log_filenames.remove(file_info.filename)
                    continue
                raise e

            # If some new lines have been added to this file, try to reopen the
            # file.
            if file_size > file_info.size_when_last_opened:
                try:
                    f = open(file_info.filename, "r")
                except (IOError, OSError) as e:
                    if e.errno == errno.ENOENT:
                        logger.warning("Warning: The file {} was not "
                                       "found.".format(file_info.filename))
                        self.log_filenames.remove(file_info.filename)
                        continue
                    else:
                        raise e

                f.seek(file_info.file_position)
                file_info.filesize_when_last_opened = file_size
                file_info.file_handle = f
                self.open_file_infos.append(file_info)
            else:
                files_with_no_updates.append(file_info)

        # Add the files with no changes back to the list of closed files.
        self.closed_file_infos += files_with_no_updates
524667
public void closeFiles() {
        filesAreOpened = false;
        for (HeaderIndexFile<Data> file : files) {
            if (file != null) {
                file.close();
            }
        }
    }
450882
def next_line(self):
        """"""
        self.line = next(self.lines)  # Will raise StopIteration when there are no more lines
        self.values = self.line.split()
146742
def close_files(subseqs):
        """"""
        print('            . close_files')
        lines = Lines()
        lines.add(1, 'cpdef inline close_files(self):')
        for seq in subseqs:
            lines.add(2, 'if self._%s_diskflag:' % seq.name)
            lines.add(3, 'fclose(self._%s_file)' % seq.name)
        return lines
280580
private void checkFileReopening(FileObject fileObject, boolean forWriting) throws FilerException {
        if (isInFileObjectHistory(fileObject, forWriting)) {
            if (lint)
                log.warning("proc.file.reopening", fileObject.getName());
            throw new FilerException("Attempt to reopen a file for path " + fileObject.getName());
        }
        if (forWriting)
            fileObjectHistory.add(fileObject);
    }
820098
def load_files(self, cursor, target, files, options):
        ""
        for fname in files:
            self.load_file(cursor, target, fname, options)
577309
def has_open_file(self, file_object):
        
        return (file_object in [wrappers[0].get_object()
                                for wrappers in self.open_files if wrappers])
572069
def _open_target_files(self):
        '''
        
        '''
        # Validate the target files listed in target_files
        for tfile in self.files:
            # Ignore directories.
            if not self.subclass == io.FileIO or not os.path.isdir(tfile):
                # Make sure we can open the target files
                try:
                    fp = self.open_file(tfile)
                    fp.close()
                    self.target_files.append(tfile)
                except KeyboardInterrupt as e:
                    raise e
                except Exception as e:
                    self.error(description="Cannot open file %s (CWD: %s) : %s" % (tfile, os.getcwd(), str(e)))
1287151
def open_get_line(filename, limit=-1, **kwargs):
    
    allowed_keys_for_get_line = {'sep', 'pw_filter', 'errors'}
    for k in list(kwargs.keys()):
        if k not in allowed_keys_for_get_line:
            del kwargs[k]
    print("After filtering: {}".format(kwargs))
    with open_(filename, 'rt') as f:
        for w, c in get_line(f, limit, **kwargs):
            yield w, c
64021
def close_open_fds(keep_files=[]):
    
    keep_fds = set()
    for file in keep_files:
        if isinstance(file, int):
            keep_fds.add(file)
        else:
            try:
                keep_fds.add(file.fileno())
            except Exception:
                pass

    for fd in os.listdir("/proc/self/fd"):
        fd = int(fd)
        if fd not in keep_fds:
            try:
                os.close(fd)
            except OSError:
                # irrelevant and expected
                # (the fd that was used by os.listdir() of course always fails)
                pass
79127
private void doFindFiles() throws IOException {
    Set<FileSystem> allFs = new HashSet<FileSystem>();
    Set<Path> filesToFix = new HashSet<Path>();      // files that are yet to be fixed

    // collect all unique filesystems in all policies.
    for (PolicyInfo pinfo: all) {
      FileSystem fs = getFs(pinfo.getConf(), pinfo.getSrcPath());
      if (fs != null) {
        allFs.add(fs);
      }
      for (PathInfo d:pinfo.getDestPaths()) {
        fs = getFs(pinfo.getConf(), d.rpath);
        if (fs != null) {
          allFs.add(fs);
        }
      }
    }

    // make a RPC to all relevant namenodes to find corrupt files
    for (FileSystem fs:allFs) {
      if (!running) break;
      List<Path> corruptFiles = null;

      corruptFiles = getCorruptFilesFromNamenode(fs);

      // if we are not already fixing this one, then put it in the list
      // of files that need fixing.
      for (Path p : corruptFiles) {
        if (filesBeingFixed.add(p)) {
           filesToFix.add(p);
        }
      }
    }

    if (!filesToFix.isEmpty()) {
      LOG.info("Found " + filesToFix.size() + " corrupt files.");
    }

    for (Path path: filesToFix) {
      if (!running) break;
      try {
        fixFile(path);
      } catch (IOException ie) {
        LOG.error("Error while processing " + path +
          ": " + StringUtils.stringifyException(ie));
        // For certain kinds of errors, it might be good if we remove
        // this file from filesBeingFixed, so that the file-fix gets
        // attemted in the immediate next iteration. For example, if
        // we get a network Exception, we can retry immediately. On 
        // the other hand, if we get a file length mismatch exception
        // then no amount of retry will fix it, so it is better to 
        // retry less frequently.
      }
    }
  }
951854
def reopen(self, file_obj):
        """"""
        file_obj.open('U')
        if sys.version_info[0] <= 2:
            return file_obj
        else:
            return codecs.getreader('utf-8')(file_obj)
801676
def writefile(openedfile, newcontents):
    """"""
    openedfile.seek(0)
    openedfile.truncate()
    openedfile.write(newcontents)
1153953
def spitOutLines(lines, file, expand=False):
    """
    file = _normalizeToFile(file, mode="w", expand=expand)
    try:     file.writelines(lines)
    finally: file.close()
1334524
protected function openFile()
    {
        ini_set('auto_detect_line_endings', 1);
        $this->fileHandle = fopen($this->filename, 'r');

        if ($this->providedHeaderRow) {
            $this->headerRow = $this->remapHeader($this->providedHeaderRow);
        }
    }
681398
def open_file(self, file):
        
        files = self.get_recent_files()
        try:
            files.remove(file)
        except ValueError:
            pass
        files.insert(0, file)
        # discard old files
        del files[self.max_recent_files:]
        self.set_value('list', files)
        self.updated.emit()
1085031
def extend(self, patterns):
        """"""
        assert patterns is not None
        if isinstance(patterns, Pattern):
            self.append(patterns)
            return

        if isinstance(patterns, PatternSet):
            patterns = patterns.patterns

        assert all(isinstance(pat, Pattern) for pat in patterns)
        self.patterns.extend(patterns)
        self._all_files = None