Archive schema, cred def, revocation caches to disk as json.

        :param base_dir: archive base directory
        :return: timestamp (epoch seconds) used as subdirectory
763697
def archive(base_dir: str) -> int:
        

        LOGGER.debug('archive >>> base_dir: %s', base_dir)

        rv = int(time())
        timestamp_dir = join(base_dir, str(rv))
        makedirs(timestamp_dir, exist_ok=True)

        with SCHEMA_CACHE.lock:
            with open(join(timestamp_dir, 'schema'), 'w') as archive:
                print(json.dumps(SCHEMA_CACHE.schemata()), file=archive)

        with CRED_DEF_CACHE.lock:
            with open(join(timestamp_dir, 'cred_def'), 'w') as archive:
                print(json.dumps(CRED_DEF_CACHE), file=archive)

        with REVO_CACHE.lock:
            with open(join(timestamp_dir, 'revocation'), 'w') as archive:
                revo_cache_dict = {}
                for rr_id in REVO_CACHE:
                    revo_cache_dict[rr_id] = {
                        'rev_reg_def': REVO_CACHE[rr_id].rev_reg_def,
                        'rr_delta_frames': [vars(f) for f in REVO_CACHE[rr_id].rr_delta_frames],
                        'rr_state_frames': [vars(f) for f in REVO_CACHE[rr_id].rr_state_frames]
                    }
                print(json.dumps(revo_cache_dict), file=archive)

        LOGGER.debug('archive <<< %s', rv)
        return rv
289997
def _create_json(self):
        
        data_json = {
            "rclass": "local",
            "key": self.name,
            "description": self.description,
            "packageType": self.packageType,
            "notes": "",
            "includesPattern": "**/*",
            "excludesPattern": "",
            "repoLayoutRef": self.repoLayoutRef,
            "dockerApiVersion": self.dockerApiVersion,
            "checksumPolicyType": "client-checksums",
            "handleReleases": True,
            "handleSnapshots": True,
            "maxUniqueSnapshots": 0,
            "snapshotVersionBehavior": "unique",
            "suppressPomConsistencyChecks": True,
            "blackedOut": False,
            "propertySets": [],
            "archiveBrowsingEnabled": self.archiveBrowsingEnabled,
            "yumRootDepth": 0,
        }
        return data_json
60222
def directory(context, data):
    """"""
    with context.http.rehash(data) as result:
        if not result.ok:
            return

        content_hash = data.get('content_hash')
        if content_hash is None:
            context.emit_warning("No content hash in data.")
            return

        path = _get_directory_path(context)
        file_name = data.get('file_name', result.file_name)
        file_name = safe_filename(file_name, default='raw')
        file_name = '%s.%s' % (content_hash, file_name)
        data['_file_name'] = file_name
        file_path = os.path.join(path, file_name)
        if not os.path.exists(file_path):
            shutil.copyfile(result.file_path, file_path)

        context.log.info("Store [directory]: %s", file_name)

        meta_path = os.path.join(path, '%s.json' % content_hash)
        with open(meta_path, 'w') as fh:
            json.dump(data, fh)
807674
def store(bank, key, data, cachedir):
    '''
    
    '''
    base = os.path.join(cachedir, os.path.normpath(bank))
    try:
        os.makedirs(base)
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise SaltCacheError(
                'The cache directory, {0}, could not be created: {1}'.format(
                    base, exc
                )
            )

    outfile = os.path.join(base, '{0}.p'.format(key))
    tmpfh, tmpfname = tempfile.mkstemp(dir=base)
    os.close(tmpfh)
    try:
        with salt.utils.files.fopen(tmpfname, 'w+b') as fh_:
            fh_.write(__context__['serial'].dumps(data))
        # On Windows, os.rename will fail if the destination file exists.
        salt.utils.atomicfile.atomic_rename(tmpfname, outfile)
    except IOError as exc:
        raise SaltCacheError(
            'There was an error writing the cache file, {0}: {1}'.format(
                base, exc
            )
        )
631210
def _get_default_cache_dir(self):
        

        return os.path.join(os.path.dirname(__file__), self._DATA_DIR)
1126953
def _get_cache_file_path(self, cache_dir=None):
        
        if cache_dir is None:
            # Tries to get writable cache dir with fallback to users data dir
            # and temp directory
            cache_dir = self._get_writable_cache_dir()
        else:
            if not os.access(cache_dir, os.W_OK):
                raise CacheFileError("None of cache directories is writable.")

        # get directory for cached file
        return os.path.join(cache_dir, self._CACHE_FILE_NAME)
303788
def last_updated(self):
        '''
        
        '''
        derived_path = self.context.request.url
        timestamp_key = self.timestamp_key_for(derived_path)

        if self.exists(self.timestamp_key_for, derived_path):
            return self.storage.get(timestamp_key)

        return None
547744
def get_all_cached_commit_times(root_folder):
    
    result = []
    location = cache_location(root_folder)

    if os.path.exists(location):
        try:
            result = json.load(open(location))
        except (TypeError, ValueError) as error:
            log.warning("Failed to open gitmit cached commit_times\tlocation=%s\terror=%s", location, error)
        else:
            if type(result) is not list or not all(type(item) is dict for item in result):
                log.warning("Gitmit cached commit_times needs to be a list of dictionaries\tlocation=%s\tgot=%s", location, type(result))
                result = []

    return result
569701
def set_dump_directory(base=None, sub_dir=None):
    """"""
    # Set current timestamp
    timestamp = datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H-%M-%S')

    # Clean sub_dir
    if sub_dir and '.' in sub_dir:
        sub_dir = sub_dir.rsplit('.', 1)[0]

    # Create a directory to save fail SQL scripts
    # TODO: Replace with function that recursively creates directories until path exists
    if not os.path.exists(base):
        os.mkdir(base)
    dump_dir = os.path.join(base, sub_dir) if sub_dir else base
    if not os.path.exists(dump_dir):
        os.mkdir(dump_dir)
    dump_dir = os.path.join(dump_dir, timestamp)
    if not os.path.exists(dump_dir):
        os.mkdir(dump_dir)
        return dump_dir
1053549
def get_ratetimestamp(self, base, code):
        """"""
        self.get_latestcurrencyrates(base)
        try:
            return datetime.fromtimestamp(self.rates["timestamp"])
        except KeyError:
            return None
343133
def export(self, timestamp=None, info={}):
        
        tval = tuple(time.localtime()) if timestamp is None else timestamp
        tstamp = time.strftime(self.timestamp_format, tval)

        info = dict(info, timestamp=tstamp)
        export_name = self._format(self.export_name, info)
        files = [((self._format(base, info), ext), val)
                 for ((base, ext), val) in self._files.items()]
        root = os.path.abspath(self.root)
        # Make directory and populate if multiple files and not packed
        if len(self) > 1 and not self.pack:
            self._directory_archive(export_name, files, root)
        elif len(files) == 1:
            self._single_file_archive(export_name, files, root)
        elif self.archive_format == 'zip':
            self._zip_archive(export_name, files, root)
        elif self.archive_format == 'tar':
            self._tar_archive(export_name, files, root)
        if self.flush_archive:
            self._files = OrderedDict()
685951
def save_repo_cache(i):
    

    r=save_json_to_file({'json_file':work['dir_cache_repo_uoa'], 'dict':cache_repo_uoa})
    if r['return']>0: return r

    r=save_json_to_file({'json_file':work['dir_cache_repo_info'], 'dict':cache_repo_info})
    if r['return']>0: return r

    return {'return':0}
1351611
def reap_fileserver_cache_dir(cache_base, find_func):
    '''
    
    '''
    for saltenv in os.listdir(cache_base):
        env_base = os.path.join(cache_base, saltenv)
        for root, dirs, files in salt.utils.path.os_walk(env_base):
            # if we have an empty directory, lets cleanup
            # This will only remove the directory on the second time
            # "_reap_cache" is called (which is intentional)
            if not dirs and not files:
                # only remove if empty directory is older than 60s
                if time.time() - os.path.getctime(root) > 60:
                    os.rmdir(root)
                continue
            # if not, lets check the files in the directory
            for file_ in files:
                file_path = os.path.join(root, file_)
                file_rel_path = os.path.relpath(file_path, env_base)
                try:
                    filename, _, hash_type = file_rel_path.rsplit('.', 2)
                except ValueError:
                    log.warning(
                        'Found invalid hash file [%s] when attempting to reap '
                        'cache directory', file_
                    )
                    continue
                # do we have the file?
                ret = find_func(filename, saltenv=saltenv)
                # if we don't actually have the file, lets clean up the cache
                # object
                if ret['path'] == '':
                    os.unlink(file_path)
428250
def get_stat_json(self, pathobj):
        
        url = '/'.join([pathobj.drive,
                        'api/storage',
                        str(pathobj.relative_to(pathobj.drive)).strip('/')])

        text, code = self.rest_get(url, auth=pathobj.auth, verify=pathobj.verify,
                                   cert=pathobj.cert)
        if code == 404 and "Unable to find item" in text:
            raise OSError(2, "No such file or directory: '%s'" % url)
        if code != 200:
            raise RuntimeError(text)

        return json.loads(text)
265748
def dir(base_dir: str, rr_id: str) -> str:
        

        LOGGER.debug('Tails.dir >>> base_dir: %s, rr_id: %s', base_dir, rr_id)

        if not ok_rev_reg_id(rr_id):
            LOGGER.debug('Tails.dir <!< Bad rev reg id %s', rr_id)
            raise BadIdentifier('Bad rev reg id {}'.format(rr_id))

        rv = join(base_dir, rev_reg_id2cred_def_id(rr_id))
        LOGGER.debug('Tails.dir <<< %s', rv)
        return rv
1147091
def _get_timestamp(dirname_full, remove):
    
    record_filename = os.path.join(dirname_full, RECORD_FILENAME)

    if not os.path.exists(record_filename):
        return None

    mtime = os.stat(record_filename).st_mtime
    mtime_str = datetime.fromtimestamp(mtime)
    print('Found timestamp {}:{}'.format(dirname_full, mtime_str))
    if Settings.record_timestamp and remove:
        OLD_TIMESTAMPS.add(record_filename)
    return mtime
469165
def get_cached_files(url, server_name="", document_root=None):
    
    import glob
    url_info = process_url(url, server_name, document_root, check_security=False)

    # get path to cache directory with basename of file (no extension)
    filedir = os.path.dirname(url_info['requested_file'])
    fileglob = '{0}*{1}'.format(url_info['base_filename'], url_info['ext'])
    return glob.glob(os.path.join(filedir, fileglob))