Look up a file DataObject by name, uuid, and/or md5.
436596
def _get_file_by_value(cls, value):
        
        # Ignore any FileResource with no DataObject. This is a typical state
        # for a deleted file that has not yet been cleaned up.
        queryset = FileResource.objects.exclude(data_object__isnull=True)
        matches = FileResource.filter_by_name_or_id_or_tag_or_hash(
            value, queryset=queryset)
        if matches.count() == 0:
            raise ValidationError(
                'No file found that matches value "%s"' % value)
        elif matches.count() > 1:
            match_id_list = ['%s@%s' % (match.filename, match.get_uuid())
                             for match in matches]
            match_id_string = ('", "'.join(match_id_list))
            raise ValidationError(
                'Multiple files were found matching value "%s": "%s". '\
                'Use a more precise identifier to select just one file.' % (
                    value, match_id_string))
        return matches.first().data_object
643962
def FindFileByName(self, file_name):
    

    try:
      return self._file_descriptors[file_name]
    except KeyError:
      pass

    try:
      file_proto = self._internal_db.FindFileByName(file_name)
    except KeyError as error:
      if self._descriptor_db:
        file_proto = self._descriptor_db.FindFileByName(file_name)
      else:
        raise error
    if not file_proto:
      raise KeyError('Cannot find a file named %s' % file_name)
    return self._ConvertFileProtoToFileDescriptor(file_proto)
296065
def _md5_of_file(sub_string):
    
    md5 = hashlib.md5()
    file_path = sub_string
    if not os.path.exists(file_path):
        file_path = os.path.join(os.environ['CAFE_DATA_DIR_PATH'], file_path)
    if not os.path.exists(file_path):
        file_path = file_path.replace(' ', '_')
    assert (os.path.exists(file_path)), "File %s doesn't exist" % file_path
    with open(file_path, 'rb') as f:
        for chunk in iter(lambda: f.read(8192), b''):
            md5.update(chunk)
    return md5.hexdigest()
64723
def load_datafile(name, search_path, codecs=get_codecs(), **kwargs):
    
    return munge.load_datafile(name, search_path, codecs, **kwargs)
1208413
def file_md5(self, resource):
        """"""
        warnings.warn(
            "file_md5 is deprecated; use resource_md5 instead",
            DeprecationWarning, stacklevel=2)
        return self.resource_md5(resource)
21765
def gather_file_data(name):
    
    res = {'name': name}
    try:
        res['mtime'] = osp.getmtime(name)
        res['size'] = osp.getsize(name)
    except OSError:
        pass
    return res
958391
def ParseFileHash(hash_obj, result):
    """"""
    if hash_obj.HasField("md5"):
      result.hash_md5 = str(hash_obj.md5)

    if hash_obj.HasField("sha1"):
      result.hash_sha1 = str(hash_obj.sha1)

    if hash_obj.HasField("sha256"):
      result.hash_sha256 = str(hash_obj.sha256)

    if hash_obj.HasField("pecoff_md5"):
      result.pecoff_hash_md5 = str(hash_obj.pecoff_md5)

    if hash_obj.HasField("pecoff_sha1"):
      result.pecoff_hash_sha1 = str(hash_obj.pecoff_sha1)

    if hash_obj.HasField("signed_data"):
      StatEntryToExportedFileConverter.ParseSignedData(hash_obj.signed_data[0],
                                                       result)
206753
def get_hash(fName, readSize, dire=pDir()):
    
    if not fileExists(fName, dire):
        return -1
    readSize = readSize * 1024 # bytes to be read
    fName = os.path.join(dire, fName) # name coupled with path
    with open(fName, 'rb') as f:
        size = os.path.getsize(fName)
        if size < readSize * 2:
            return -1
        data = f.read(readSize)
        f.seek(-readSize, os.SEEK_END)
        data += f.read(readSize)
    return md5(data).hexdigest()
240214
def check_md5(filename, stored_md5):
    
    computed_md5 = _get_file_md5(filename)
    if stored_md5 != computed_md5:
        print ("MD5 checksum of filename", filename,
               "failed. Expected MD5 was", stored_md5,
               "but computed MD5 was", computed_md5, '\n',
               "Please check if the data has been downloaded correctly or if the upstream data has changed.")
661626
def _get_md5(name, path):
    '''
    
    '''
    output = run_stdout(name,
                        'md5sum {0}'.format(pipes.quote(path)),
                        ignore_retcode=True)
    try:
        return output.split()[0]
    except IndexError:
        # Destination file does not exist or could not be accessed
        return None
464770
def calculate_md5(fileobject, size=2**16):
    """"""
    fileobject.seek(0)
    md5 = hashlib.md5()
    for data in iter(lambda: fileobject.read(size), b''):
        if not data: break
        if isinstance(data, six.text_type):
            data = data.encode('utf-8') # md5 needs a byte string
        md5.update(data)
    fileobject.seek(0) # rewind read head
    return md5.hexdigest()
999827
def _getFilename(self, fileNumber):
        
        cur = self._connection.cursor()
        cur.execute('SELECT name FROM files WHERE id = ?', (fileNumber,))
        row = cur.fetchone()
        if row is None:
            return None
        else:
            return row[0]
1109325
def _find(self, id_):
        
        cur = self._connection.cursor()
        cur.execute(
            'SELECT fileNumber, offset FROM sequences WHERE id = ?', (id_,))
        row = cur.fetchone()
        if row is None:
            return None
        else:
            return self._getFilename(row[0]), row[1]
262427
def find_datafile(name, search_path=('.'), codecs=get_codecs()):
    
    rv = []

    if isinstance(search_path, basestring):
        search_path = (search_path,)

    #print "search path ", str(search_path)

    ext = os.path.splitext(name)[1][1:]

    cls = get_codec(ext)
    if cls:
        for each in search_path:
            fq_filename = os.path.join(each, name)
            if os.path.exists(fq_filename):
                rv.append((cls, fq_filename))

    for exts, obj in list(codecs.items()):
        for ext in exts:
            filename = "%s.%s" % (name, ext)
            for each in search_path:
                fq_filename = os.path.join(each, filename)
                if os.path.exists(fq_filename):
                    rv.append((obj, fq_filename))

    return rv
318994
def uuid(dataset_uri):
    """"""
    dataset = dtoolcore.DataSet.from_uri(dataset_uri)
    click.secho(dataset.uuid)
1051677
def get_filehandle(self):
        """"""
        if os.path.exists(self.filename):
            self.filehandle = SD(self.filename, SDC.READ)
            logger.debug("Loading dataset {}".format(self.filename))
        else:
            raise IOError("Path {} does not exist.".format(self.filename))
1320331
def file(filename):
        
        m = hashlib.md5()
        with open(filename, 'rb') as f:
            m.update(f.read())
            result = m.hexdigest()
            return result