本文整理汇总了Python中vsc.utils.cache.FileCache类的典型用法代码示例。如果您正苦于以下问题:Python FileCache类的具体用法?Python FileCache怎么用?Python FileCache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: notify_exceeding_items
def notify_exceeding_items(gpfs, storage, filesystem, exceeding_items, target, dry_run=False):
"""Send out notification to the fileset owners.
- if the fileset belongs to a VO: the VO moderator
- if the fileset belongs to a project: the project moderator
- if the fileset belongs to a user: the user
The information is cached. The mail is sent in the following cases:
- the excession is new
- the excession occurred more than 7 days ago and stayed in the cache. In this case, the cache is updated as
to avoid sending outdated mails repeatedly.
"""
cache_path = os.path.join(gpfs.list_filesystems()[filesystem]['defaultMountPoint'], ".quota_%s_cache.json.gz" % (target))
cache = FileCache(cache_path, True) # we retain the old data
logger.info("Processing %d exceeding items" % (len(exceeding_items)))
for (item, quota) in exceeding_items:
updated = cache.update(item, quota, QUOTA_NOTIFICATION_CACHE_THRESHOLD)
logger.info("Storage %s: cache entry for %s was updated: %s" % (storage, item, updated))
if updated:
notify(storage, item, quota, dry_run)
if not dry_run:
cache.close()
else:
logger.info("Dry run: not saving the updated cache")
示例2: print_vo_quota
def print_vo_quota(opts, storage, vos, now):
"""
Print the quota for the VO fileset.
"""
print "\nVO quota:"
for storage_name in [s for s in opts.options.storage if s != 'VSC_HOME']: # No VOs on VSC_HOME atm
mount_point = storage[storage_name].login_mount_point
path_template = storage.path_templates[storage_name]['vo']
path = os.path.join(mount_point, path_template[0], path_template[1](vos[0]), ".quota_fileset.json.gz")
cache = FileCache(path, True)
try:
(timestamp, quota) = cache.load('quota')
except TypeError:
logger.debug("Cannot load data from %s" % (path,))
print "%s: WARNING: No VO quota information found" % (storage_name,)
continue
if now - timestamp > opts.options.threshold:
print "%s: WARNING: no recent VO quota information (age of data is %d minutes)" % (storage_name,
(now-timestamp)/60)
else:
for (fileset, qi) in quota.quota_map.items():
pp = quota_pretty_print(storage_name, fileset, qi, opts.options.fileset_prefixes)
if pp:
print pp
示例3: cache
def cache(self, nagios_exit, nagios_message):
"""Store the result in the cache file with a timestamp.
@type nagios_exit: one of NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRTITCAL or NAGIOS_EXIT_UNKNOWN
@type nagios_message: string
@param nagios_exit: a valid nagios exit code.
@param nagios_message: the message to print out when the actual check runs.
"""
try:
nagios_cache = FileCache(self.filename)
nagios_cache.update('nagios', (nagios_exit, nagios_message), 0) # always update
nagios_cache.close()
self.log.info("Wrote nagios check cache file %s at about %s" % (self.filename, time.ctime(time.time())))
except (IOError, OSError):
# raising an error is ok, since we usually do this as the very last thing in the script
self.log.raiseException("Cannot save to the nagios gzipped JSON file (%s)" % (self.filename))
try:
p = pwd.getpwnam(self.nagios_username)
if self.world_readable:
os.chmod(self.filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH)
else:
os.chmod(self.filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
# only change owner/group when run as root
if os.geteuid() == 0:
os.chown(self.filename, p.pw_uid, p.pw_gid)
else:
self.log.warn("Not running as root: Cannot chown the nagios check file %s to %s" %
(self.filename, self.nagios_username))
except OSError:
self.log.raiseException("Cannot chown the nagios check file %s to the nagios user" % (self.filename))
return True
示例4: print_user_quota
def print_user_quota(opts, storage, user_name, now):
"""
Print the quota for the user, i.e., USR quota in all filesets the user has access to.
"""
print "User quota:"
for storage_name in opts.options.storage:
mount_point = storage[storage_name].login_mount_point
path_template = storage.path_templates[storage_name]['user']
path = os.path.join(mount_point, path_template[0], path_template[1](user_name), ".quota_user.json.gz")
cache = FileCache(path, True)
try:
(timestamp, quota) = cache.load('quota')
except TypeError:
logger.debug("Cannot load data from %s" % (path,))
print "%s: WARNING: No quota information found" % (storage_name,)
continue
if now - timestamp > opts.options.threshold:
print "%s: WARNING: no recent quota information (age of data is %d minutes)" % (storage_name,
(now-timestamp)/60)
else:
for (fileset, qi) in quota.quota_map.items():
pp = quota_pretty_print(storage_name, fileset, qi, opts.options.fileset_prefixes)
if pp:
print pp
示例5: main
def main():
options = {
'storage': ('the VSC filesystems that are checked by this script', None, 'extend', []),
'threshold': ('allowed the time difference between the cached quota and the time of running', None, 'store',
DEFAULT_ALLOWED_TIME_THRESHOLD),
}
opts = simple_option(options, config_files='/etc/quota_information.conf')
storage = VscStorage()
user_name = getpwuid(os.getuid())[0]
now = time.time()
for storage_name in opts.options.storage:
mount_point = storage[storage_name].login_mount_point
path_template = storage.path_templates[storage_name]['user']
path = os.path.join(mount_point, path_template[0], path_template(user_name))
cache = FileCache(path)
(timestamp, quota) = cache.load('quota')
if now - timestamp > opts.options.threshold:
print "%s: WARNING: no recent quota information (age of data is %d minutes)" % (storage_name,
(now-timestamp)/60)
else:
for (fileset, qi) in quota.quota_map.items():
print "%s: used %d MiB (%d%%) quota %d MiB in fileset %d" % (storage_name,
quota)
if __name__ == '__main__':
main()
示例6: cache
def cache(self, nagios_exit, nagios_message):
"""Store the result in the cache file with a timestamp.
@type nagios_exit: one of NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRTITCAL or NAGIOS_EXIT_UNKNOWN
@type nagios_message: string
@param nagios_exit: a valid nagios exit code.
@param nagios_message: the message to print out when the actual check runs.
"""
try:
nagios_cache = FileCache(self.filename)
nagios_cache.update(0, (nagios_exit, nagios_message), 0) # always update
nagios_cache.close()
self.log.info("Wrote nagios check cache file %s at about %s" % (self.filename, time.ctime(time.time())))
except:
# raising an error is ok, since we usually do this as the very last thing in the script
self.log.raiseException("Cannot save to the nagios pickled file (%s)" % (self.filename))
try:
p = pwd.getpwnam(self.nagios_username)
os.chmod(self.filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
os.chown(self.filename, p.pw_uid, p.pw_gid)
except:
self.log.raiseException("Cannot chown the nagios check file %s to the nagios user" % (self.filename))
return True
示例7: store_on_gpfs
def store_on_gpfs(user_name, path, key, information, gpfs, login_mount_point, gpfs_mount_point, filename,
dry_run=False):
"""
Store the given information in a cache file that resides in a user's directory.
@type user_name: string
@type path: string, representing a directory
@type key: string, name for the kind of information we are going to store in the cache
@type showq_information: a recursive dict structure
@type gpfs: GpfsOperations instance
@type login_mount_point: path representing the mount point of the storage location on the login nodes
@type gpfs_mount_point: path representing the mount point of the storage location when GPFS mounted
@type dry_run: boolean
"""
if user_name and user_name.startswith('vsc4'):
logger.debug("Storing %s information for user %s" % (key, user_name,))
logger.debug("information: %s" % (information,))
logger.debug("path for storing information would be %s" % (path,))
# FIXME: We need some better way to address this
# Right now, we replace the nfs mount prefix which the symlink points to
# with the gpfs mount point. this is a workaround until we resolve the
# symlink problem once we take new default scratch into production
if gpfs.is_symlink(path):
target = os.path.realpath(path)
logger.debug("path is a symlink, target is %s" % (target,))
logger.debug("login_mount_point is %s" % (login_mount_point,))
if target.startswith(login_mount_point):
new_path = target.replace(login_mount_point, gpfs_mount_point, 1)
logger.info("Found a symlinked path %s to the nfs mount point %s. Replaced with %s" %
(path, login_mount_point, gpfs_mount_point))
else:
logger.warning("Unable to store quota information for %s on %s; symlink cannot be resolved properly"
% (user_name, path))
else:
new_path = path
path_stat = os.stat(new_path)
filename = os.path.join(new_path, filename)
if dry_run:
logger.info("Dry run: would update cache for at %s with %s" % (new_path, "%s" % (information,)))
logger.info("Dry run: would chmod 640 %s" % (filename,))
logger.info("Dry run: would chown %s to %s %s" % (filename, path_stat.st_uid, path_stat.st_gid))
else:
cache = FileCache(filename, False) # data need not be retained
cache.update(key=key, data=information, threshold=0)
cache.close()
gpfs.ignorerealpathmismatch = True
gpfs.chmod(0o640, filename)
gpfs.chown(path_stat.st_uid, path_stat.st_uid, filename)
gpfs.ignorerealpathmismatch = False
logger.info("Stored user %s %s information at %s" % (user_name, key, filename))
示例8: read_timestamp
def read_timestamp(filename):
"""Read the stored timestamp value from a pickled file.
@returns: string representing a timestamp in the proper LDAP time format
"""
cache = FileCache(filename)
(_, timestamp) = cache.load('timestamp')
return timestamp
示例9: test_save_and_load
def test_save_and_load(self):
"""Check if the loaded data is the same as the saved data."""
# test with random data
data, threshold = get_rand_data()
# create a tempfilename
(handle, filename) = tempfile.mkstemp()
os.unlink(filename)
os.close(handle)
cache = FileCache(filename)
for (key, value) in data.items():
cache.update(key, value, threshold)
cache.close()
now = time.time()
new_cache = FileCache(filename)
for key in data.keys():
info = cache.load(key)
self.assertTrue(info is not None)
(ts, value) = info
self.assertTrue(value == data[key])
self.assertTrue(ts <= now)
new_cache.close()
os.unlink(filename)
示例10: test_wirte_donefile
def test_wirte_donefile(self):
""" Test the writing of the values to a cache file when done"""
donefile = "/tmp/done"
values = {
'completed' : 50,
'failed' : 5,
'unfinished' : 0
}
zkclient = RsyncSource('dummy', session='new', netcat=True, rsyncpath='/path/dummy', rsyncdepth=2, done_file=donefile)
zkclient.write_donefile(values)
cache_file = FileCache(donefile)
(timestamp, stats) = cache_file.load('stats')
self.assertEqual(values, stats)
示例11: write_timestamp
def write_timestamp(filename, timestamp):
"""Write the given timestamp to a pickled file.
@type timestamp: datetime.datetime timestamp
"""
if timestamp.tzinfo is None:
# add local timezoneinfo
timestamp = timestamp.replace(tzinfo=Local)
cache = FileCache(filename)
cache.update(0, timestamp, 0)
cache.close()
示例12: read_timestamp
def read_timestamp(filename):
"""Read the stored timestamp value from a pickled file.
@returns: string representing a timestamp in the proper LDAP time format
"""
cache = FileCache(filename)
(_, timestamp) = cache.load(0)
if not timestamp is None and timestamp.tzinfo is None:
# add local timezoneinfo
timestamp = timestamp.replace(tzinfo=Local)
return timestamp
示例13: read_cache
def read_cache(path):
"""
Unpickle the file and fill in the resulting datastructure.
"""
try:
cache = FileCache(path)
except Exception:
print "Failed to load checkjob information from %s" % (path,)
res = cache.load('checkjob')
if res[0] < (time.time() - MAXIMAL_AGE):
print "The data in the checkjob cache may be outdated. Please contact your admin to look into this."
return res[1] # CheckjobInfo
示例14: write_timestamp
def write_timestamp(filename, timestamp):
"""Write the given timestamp to a pickled file.
@type timestamp: datetime.datetime timestamp
"""
if isinstance(timestamp, datetime.datetime) and timestamp.tzinfo is None:
# add local timezoneinfo
timestamp_ = timestamp.replace(tzinfo=Local)
(_, timestamp_) = convert_timestamp(timestamp)
else:
timestamp_ = timestamp
cache = FileCache(filename)
cache.update('timestamp', timestamp_, 0)
cache.close()
示例15: test_contents
def test_contents(self, data, threshold):
"""Check that the contents of the cache is what is expected prior to closing it."""
# create a tempfilename
(handle, filename) = tempfile.mkstemp(dir='/tmp')
os.unlink(filename)
cache = FileCache(filename)
for (key, value) in data.items():
cache.update(key, value, threshold)
now = time.time()
for key in data.keys():
info = cache.load(key)
self.assertFalse(info is None)
(ts, value) = info
self.assertTrue(value == data[key])
self.assertTrue(ts <= now)