Return a dict containing the IPFS hash, file name, and size of a file.
494094
def pin_assets(self, file_or_dir_path: Path) -> List[Dict[str, str]]:
        
        if file_or_dir_path.is_dir():
            asset_data = [dummy_ipfs_pin(path) for path in file_or_dir_path.glob("*")]
        elif file_or_dir_path.is_file():
            asset_data = [dummy_ipfs_pin(file_or_dir_path)]
        else:
            raise FileNotFoundError(
                f"{file_or_dir_path} is not a valid file or directory path."
            )
        return asset_data
44432
def _query(self, filename):
        """"""
        #  - Query metadata of one file
        #  - Return a dict with a 'size' key, and a file size value (-1 for not found)
        #  - Retried if an exception is thrown
        log.Info('Querying size of %s' % filename)
        from jottalib.JFS import JFSNotFoundError, JFSIncompleteFile
        remote_path = posixpath.join(self.folder.path, filename)
        try:
            remote_file = self.client.getObject(remote_path)
        except JFSNotFoundError:
            return {'size': -1}
        return {
            'size': remote_file.size,
        }
802005
def _hashfile(self,filename,blocksize=65536):
        """"""
        logger.debug("Hashing file %s"%(filename))
        hasher=hashlib.sha256()
        afile=open(filename,'rb')
        buf=afile.read(blocksize)
        while len(buf) > 0:
            hasher.update(buf)
            buf = afile.read(blocksize)
        return hasher.hexdigest()
733887
def get_file_hexdigest(filename, blocksize=1024*1024*10):
    ''''''

    if hashlib.__name__ == 'hashlib':
        m = hashlib.md5()       # new - 'hashlib' module
    else:
        m = hashlib.new()       # old - 'md5' module - remove once py2.4 gone
    fd = open(filename, 'r')

    while True:
        data = fd.read(blocksize)
        if len(data) == 0:
            break
        m.update(data)

    fd.close()
    return m.hexdigest()
834136
def get_description(self, with_size=True, with_index=True):
        

        desc = ''

        if with_size and self.size:
            desc += '{0} '.format(self.get_formatted_size())

        s = self.info.get('statfstype') or self.info.get('fsdescription') or '-'
        if with_index:
            desc += '{1}:{0}'.format(s, self.index)
        else:
            desc += s

        if self.info.get('label'):
            desc += ' {0}'.format(self.info.get('label'))

        if self.info.get('version'):  # NTFS
            desc += ' [{0}]'.format(self.info.get('version'))

        return desc
136246
def hash_filesystem(filesystem, hashtype='sha1'):
    
    try:
        return dict(filesystem.checksums('/'))
    except RuntimeError:
        results = {}

        logging.warning("Error hashing disk %s contents, iterating over files.",
                        filesystem.disk_path)

        for path in filesystem.nodes('/'):
            try:
                regular = stat.S_ISREG(filesystem.stat(path)['mode'])
            except RuntimeError:
                continue  # unaccessible node

            if regular:
                try:
                    results[path] = filesystem.checksum(path, hashtype=hashtype)
                except RuntimeError:
                    logging.debug("Unable to hash %s.", path)

        return results
643835
def hash(self):
        """"""
        if self._hash is None:
            # Only hash _camera.ini and _para.ini
            fsh = [self.path.with_name(self._mid + "_camera.ini"),
                   self.path.with_name(self._mid + "_para.ini")]
            tohash = [hashfile(f) for f in fsh]
            tohash.append(self.path.name)
            # Hash a maximum of ~1MB of the tdms file
            tohash.append(hashfile(self.path, blocksize=65536, count=20))
            self._hash = hashobj(tohash)
        return self._hash
1103862
def get_from_ipfs_and_checkhash(ipfs_client, ipfs_hash_base58, validate=True):
    
    if validate:
        from snet_cli.resources.proto.unixfs_pb2 import Data
        from snet_cli.resources.proto.merckledag_pb2 import MerkleNode

        # No nice Python library to parse ipfs blocks, so do it ourselves.
        block_data = ipfs_client.block_get(ipfs_hash_base58)
        mn = MerkleNode()
        mn.ParseFromString(block_data)
        unixfs_data = Data()
        unixfs_data.ParseFromString(mn.Data)
        assert unixfs_data.Type == unixfs_data.DataType.Value('File'), "IPFS hash must be a file"
        data = unixfs_data.Data
        
        # multihash has a badly registered base58 codec, overwrite it...
        multihash.CodecReg.register('base58', base58.b58encode, base58.b58decode)
        # create a multihash object from our ipfs hash
        mh = multihash.decode(ipfs_hash_base58.encode('ascii'), 'base58')
        
        # Convenience method lets us directly use a multihash to verify data
        if not mh.verify(block_data):
            raise Exception("IPFS hash mismatch with data")
    else:
        data = ipfs_client.cat(ipfs_hash_base58)
    return data
1138219
def get_metadata(self, filename):
        ''''''
        dest = self.path(filename)
        with open(dest, 'rb', buffering=0) as f:
            checksum = 'sha1:{0}'.format(sha1(f))
        return {
            'checksum': checksum,
            'size': os.path.getsize(dest),
            'mime': files.mime(filename),
            'modified': datetime.fromtimestamp(os.path.getmtime(dest)),
        }
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)
228975
def get_file_report(self, this_hash):
        
        params = {'api_key': self.api_key, 'hash': this_hash}

        try:
            response_info = requests.get(self.base + 'file/mwsinfo', params=params)
            response_additional = requests.get(self.base + 'file/addinfo', params=params)
        except requests.RequestException as e:
            return dict(error=e.message)

        ri = _return_response_and_status_code(response_info)
        ra = _return_response_and_status_code(response_additional)

        if ri['response_code'] == '1' and ra['response_code'] == '1':  # both ok
            both = ri['results'].copy()
            both.update(ra['results'])
            response = dict(results=both, response_code=1)
        elif ri['response_code'] == '1' and ra['response_code'] == '0':  # advance non exists but standard ok
            response = ri
        elif ri['response_code'] == '2':  # main is still loading
            response = dict(results={}, response_code=2)
        else:  # error generic
            response = ri
        return response
628500
def get_configuration_dict(self, secret_attrs=False):
        """"""
        rd = {'name': self.name,
              'path': self.path,
              'git_dir': self.git_dir,
              'assumed_doc_version': self.assumed_doc_version,
              'doc_dir': self.doc_dir,
              'git_ssh': self.git_ssh, }
        if secret_attrs:
            rd['pkey'] = self.pkey
        with self._index_lock:
            si = self._doc_index
        r = _invert_dict_list_val(si)
        key_list = list(r.keys())
        rd['number of documents'] = len(key_list)
        key_list.sort()
        m = []
        for k in key_list:
            v = r[k]
            fp = k[2]
            assert fp.startswith(self.doc_dir)
            rp = fp[len(self.doc_dir) + 1:]
            m.append({'keys': v, 'relpath': rp})
        rd['documents'] = m
        return rd
1128515
def get_file_hash(self, *algorithms: str):
        '''
        
        '''
        from .hashs import hashfile_hexdigest
        return hashfile_hexdigest(self._path, algorithms)
196925
def file_names(self) -> Iterable[str]:
        
        if self.is_sharded():
            yield from ShardedFile(self.filename_spec).get_filenames()
        elif self.filename_spec:
            yield self.filename_spec
944863
def fileinfo(path):
    '''
    
    '''
    cmd = 'mfsfileinfo ' + path
    ret = {}
    chunknum = ''
    out = __salt__['cmd.run_all'](cmd, python_shell=False)

    output = out['stdout'].splitlines()
    for line in output:
        if not line:
            continue
        if '/' in line:
            comps = line.split('/')

            chunknum = comps[0].strip().split(':')
            meta = comps[1].strip().split(' ')

            chunk = chunknum[0].replace('chunk ', '')
            loc = chunknum[1].strip()
            id_ = meta[0].replace('(id:', '')
            ver = meta[1].replace(')', '').replace('ver:', '')

            ret[chunknum[0]] = {
                'chunk': chunk,
                'loc': loc,
                'id': id_,
                'ver': ver,
            }
        if 'copy' in line:
            copyinfo = line.strip().split(':')
            ret[chunknum[0]][copyinfo[0]] = {
                'copy': copyinfo[0].replace('copy ', ''),
                'ip': copyinfo[1].strip(),
                'port': copyinfo[2],
            }
    return ret
303250
def json(self):
        """"""
        output = {}
        for filename in self.data:
            output[filename] = []

            for secret_hash in self.data[filename]:
                tmp = self.data[filename][secret_hash].json()
                del tmp['filename']  # Because filename will map to the secrets

                output[filename].append(tmp)

        return output
347165
def _get_sizes_checksums(checksums_path):
  """"""
  checksums = {}
  for line in _read_file(checksums_path).split('\n'):
    if not line:
      continue
    # URL might have spaces inside, but size and checksum will not.
    url, size, checksum = line.rsplit(' ', 2)
    checksums[url] = (int(size), checksum)
  return checksums