def process_prog(self, data): now = time() if now - Status.rTime > 30: if sys.DEV_MODE: stat = os.statvfs(".") else: stat = os.statvfs("/var/audio") Status.remaining = (stat.f_bsize * stat.f_bavail) / ( (core.depth / 8) * core.channels * core.rate * core.comp_ratio ) Status.rTime = now updateBTimer() updateTemp() save_config() # core.sync_dir() for msg in data: d = json.loads(msg) d["_t"] = "status" # d['c'] = os.getloadavg() d["r"] = Status.remaining d["s"] = core.port.gotSignal() d["ss"] = core.depth d["sr"] = core.rate # d['bt'] = hrBTimer() d["ct"] = int(getTemp()) # if o: self.send(d) Status.pTime = now
def same_partition(dir1, dir2): """Are both directories on the same partition?""" if "nt" == os.name: return free_space(dir1) == free_space(dir2) stat1 = os.statvfs(dir1) stat2 = os.statvfs(dir2) return stat1.f_blocks == stat2.f_blocks and stat1.f_bfree == stat2.f_bfree
def main(): if len(sys.argv) != 3: print 'Usage: %s -c configfile' % sys.argv[0] sys.exit(2) get_config(sys.argv[2]) fd = flock_exclusive() if not fd: logger('Error: cannot obtain lock, there maybe another time-machine ' 'is running') logger('Backup task aborted!') sys.exit(2) stat_before = os.statvfs(cfg['dest_path']) check_freespace(stat_before) t_start = datetime.now() take_snapshot() snapshots = find_snapshots() smart_remove(snapshots, None, cfg['keep_all'], cfg['keep_one_per_day'], cfg['keep_one_per_week'], cfg['keep_one_per_month']) # report stat_after = os.statvfs(cfg['dest_path']) logger('Filesystem before backup:') print_fs_stat(stat_before) logger('Filesystem after backup:') print_fs_stat(stat_after) t_used = datetime.now() - t_start logger('Backup runtime: %s' % str(t_used).split('.')[0]) flock_release(fd)
def cacheCopy(src, dst, min_free, file_map, no_delete): #Cache a copy of src if possible, removing old files from cache if necessary src_size = os.stat(src).st_size * 2 #Safety factor of 2 to account for file growth if cached copy is modified du = os.statvfs(utilities.fullPath("/scratch/babymaker")) avail = du.f_bsize*du.f_bavail while avail-src_size < min_free: #Keep deleting until there's room if no_delete: return removed_file = removeOldCache(file_map) if not removed_file: return du = os.statvfs(utilities.fullPath("/scratch/babymaker")) avail = du.f_bsize*du.f_bavail print("Caching "+src+" to "+dst+"\n") try: shutil.copy(src, dst) os.chmod(dst, 0775) while not cacheUpToDate(dst, src): now = time.time() os.utime(dst, (now, now)) except: os.remove(dst) utilities.ePrint("Failed to cache "+src+" to "+dst+"\n") raise
def disk_usage(path): """Return disk usage associated with path.""" try: st = os.statvfs(path) except UnicodeEncodeError: if not PY3 and isinstance(path, unicode): # this is a bug with os.statvfs() and unicode on # Python 2, see: # - https://github.com/giampaolo/psutil/issues/416 # - http://bugs.python.org/issue18695 try: path = path.encode(sys.getfilesystemencoding()) except UnicodeEncodeError: pass st = os.statvfs(path) else: raise free = (st.f_bavail * st.f_frsize) total = (st.f_blocks * st.f_frsize) used = (st.f_blocks - st.f_bfree) * st.f_frsize percent = usage_percent(used, total, _round=1) # NB: the percentage is -5% than what shown by df due to # reserved blocks that we are currently not considering: # http://goo.gl/sWGbH return sdiskusage(total, used, free, percent)
def test_statvfs(): if not hasattr(os, 'statvfs'): py.test.skip('posix specific function') try: os.statvfs('.') except OSError, e: py.test.skip("the underlying os.statvfs() failed: %s" % e)
def get_free_space(folder, units="MB"): """ Return folder/drive free space (Free Space, total space, units) :type folder: str :return: A Dict with items: units, bytes_per_unit, folder, free, total :rtype: dict """ u_constants = get_byte_unit_def(units) disk_info = { 'units': units, 'bytes_per_unit': u_constants, 'folder': folder } if platform.system() == 'Windows': free_bytes = ctypes.c_ulonglong(0) total_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, ctypes.pointer(total_bytes), ctypes.pointer(free_bytes)) disk_info['free'] = int(free_bytes.value / u_constants) disk_info['total'] = int(total_bytes.value / u_constants) # return int(free_bytes.value / u_constants), int(total_bytes.value / u_constants), units return disk_info else: disk_info['free'] = int(os.statvfs(folder).f_bfree * os.statvfs(folder).f_bsize / u_constants) disk_info['total'] = int(os.statvfs(folder).f_blocks * os.statvfs(folder).f_bsize / u_constants) # return int(os.statvfs(folder).f_bfree * os.statvfs(folder).f_bsize / u_constants), units return disk_info
def getmeta(self, meta_name, default=NoDefaultMeta): if meta_name == 'free_space': if platform.system() == 'Windows': try: import ctypes free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(self.root_path), None, None, ctypes.pointer(free_bytes)) return free_bytes.value except ImportError: # Fall through to call the base class pass else: stat = os.statvfs(self.root_path) return stat.f_bfree * stat.f_bsize elif meta_name == 'total_space': if platform.system() == 'Windows': try: import ctypes total_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(self.root_path), None, ctypes.pointer(total_bytes), None) return total_bytes.value except ImportError: # Fall through to call the base class pass else: stat = os.statvfs(self.root_path) return stat.f_blocks * stat.f_bsize return super(OSFS, self).getmeta(meta_name, default)
def get_df_data(self, work_directory): """ Retrive raw data from df (transformations are performed via df_list_transformation) """ result = {'data': [], 'xlog': []} # obtain the device names data_dev = self.get_mounted_device(self.get_mount_point(work_directory)) xlog_dev = self.get_mounted_device(self.get_mount_point(work_directory + self.wal_directory)) if data_dev not in self.df_cache: data_vfs = os.statvfs(work_directory) self.df_cache[data_dev] = data_vfs else: data_vfs = self.df_cache[data_dev] if xlog_dev not in self.df_cache: xlog_vfs = os.statvfs(work_directory + self.wal_directory) self.df_cache[xlog_dev] = xlog_vfs else: xlog_vfs = self.df_cache[xlog_dev] result['data'] = (data_dev, data_vfs.f_blocks * (data_vfs.f_bsize / BLOCK_SIZE), data_vfs.f_bavail * (data_vfs.f_bsize / BLOCK_SIZE)) if data_dev != xlog_dev: result['xlog'] = (xlog_dev, xlog_vfs.f_blocks * (xlog_vfs.f_bsize / BLOCK_SIZE), xlog_vfs.f_bavail * (xlog_vfs.f_bsize / BLOCK_SIZE)) else: result['xlog'] = result['data'] return result
def fetch_all_images(path): """ Main executing function Loads all images from database, loads, resizes & frames Maintains df with sizes prints histograms of size data :param path: :return: """ start_disk_space = (os.statvfs(path).f_bavail * os.statvfs(path).f_frsize) / 1024 tweet_list = load_tweet_list() image_stats = pd.DataFrame(columns=('width', 'height', 'pixels')) for tweet in tweet_list: image_stats = fetch_image(tweet['image_url'], tweet['tweet_id'], path, image_stats) if (len(image_stats) % 5000 == 0): disk_avail = (os.statvfs(path).f_bavail * os.statvfs(path).f_frsize) / 1024 avg_img_size = (start_disk_space - disk_avail) / len(image_stats) imgs_to_go = len(tweet_list) - len(image_stats) if disk_avail/imgs_to_go < avg_img_size: input('May run out of space: Press Enter to continue') # Save size histograms plot_histogram(image_stats, 'height') plot_histogram(image_stats, 'width') # Print Stats print('\nAverage original:') print(image_stats.mean()) print('\nStd Dev original:') print(image_stats.std())
def get_set_storage(stats=None, hypervisors=None): storage_settings = get_set_path() if storage_settings: if hypervisors: for hypervisor in hypervisors: free = 0 total = 0 used = 0 G = 1000 ** 3 for path in storage_settings: st = os.statvfs(path) free += (st.f_bavail * st.f_frsize) / G total += (st.f_blocks * st.f_frsize) / G used += (st.f_blocks - st.f_bfree) * st.f_frsize / G hypervisor.local_gb = total hypervisor.local_gb_used = used hypervisor.free_disk_gb = free else: free = 0 total = 0 used = 0 G = 1000 ** 3 for path in storage_settings: st = os.statvfs(path) free += (st.f_bavail * st.f_frsize) / G total += (st.f_blocks * st.f_frsize) / G used += (st.f_blocks - st.f_bfree) * st.f_frsize / G stats.local_gb = total stats.local_gb_used = used stats.free_gb = free
def Drive_Size ( Path ): # os.stat try: ret = os.statvfs( Path )[F_FRSIZE ] * os.statvfs ( Path )[F_BLOCKS] #@UndefinedVariable except: ret = 0 return ret
def install_remote(cls, info, remote_packages): import os from ..tools import log_check_call from subprocess import CalledProcessError try: env = os.environ.copy() env['DEBIAN_FRONTEND'] = 'noninteractive' log_check_call(['chroot', info.root, 'apt-get', 'install', '--no-install-recommends', '--assume-yes'] + map(str, remote_packages), env=env) except CalledProcessError as e: import logging disk_stat = os.statvfs(info.root) root_free_mb = disk_stat.f_bsize * disk_stat.f_bavail / 1024 / 1024 disk_stat = os.statvfs(os.path.join(info.root, 'boot')) boot_free_mb = disk_stat.f_bsize * disk_stat.f_bavail / 1024 / 1024 free_mb = min(root_free_mb, boot_free_mb) if free_mb < 50: msg = ('apt exited with a non-zero status, ' 'this may be because\nthe image volume is ' 'running out of disk space ({free}MB left)').format(free=free_mb) logging.getLogger(__name__).warn(msg) else: if e.returncode == 100: msg = ('apt exited with status code 100. ' 'This can sometimes occur when package retrieval times out or a package extraction failed. ' 'apt might succeed if you try bootstrapping again.') logging.getLogger(__name__).warn(msg) raise
def __init__(self, interval=1.0, path=None): Thread.__init__(self) self.setDaemon(True) if path is None: path='.' try: os.makedirs(path) except OSError: if not os.path.isdir(path): raise try: os.statvfs(path) self.path=path except OSError: logging.exception("monitoring non-existing path '%s', fallback to ~" % path) self.path=os.path.expanduser('~') self.interval=interval self.cpu = 1. self.mem = 1. self.disk= 1. self.last=0 self.stopEvent=Event() self.startEvent=Event()
def showResults(self): resultWidget = self.pageWidget.getWidget(2).ui free_size = os.statvfs('/').f_bavail * os.statvfs('/').f_bsize if not self.planner_successful: self.hideMessage() QMessageBox.critical(self, _("Critical Error"), _("An error ocurred while planning the upgrade procedure, "\ "this is usually caused by faulty Network Connection or "\ "wrong repo address.")) self.pageWidget.prev() return if self.required_diskspace + 200000000 > free_size: self.hideMessage() QMessageBox.critical(self, _("Not enough free space"), _("This upgrade requires at least <b>%s MB</b> free space, "\ "please use another repository or remove some files "\ "to make a space for upgrade operation.") % \ str(self.required_diskspace / (1024 ** 2))) self.pageWidget.prev() return else: resultWidget.widget.show() if self.missing_packages: resultWidget.package_list.clear() resultWidget.package_list.addItems(self.missing_packages) self.log("MISSING PACKAGES FOUND: %s" % ','.join(self.missing_packages), "GUI") self.label_header.setText(_("Check results...")) self.hideMessage()
def dir_fill(path): make_dirs(path) try: f = os.statvfs(path).f_bfree a = os.statvfs(path).f_blocks return float(a - f) / a except: return 1.0
def get_free_space(folder): """ Return folder/drive free space (in bytes)""" if platform.system() == 'Windows': free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes)) return free_bytes.value else: return os.statvfs(folder).f_bfree * os.statvfs(folder).f_frsize
def checkPathStat(pathToCheck): try: startTime = time.time() os.statvfs(pathToCheck) delay = time.time() - startTime return (True, delay) except: return (False, 0)
def monitor(request): status = True data = {} # Check Read/Write filepaths = [ (settings.UPLOAD_DIR, os.R_OK | os.W_OK, 'We want read + write.'), ] if hasattr(settings, 'XPI_TARGETDIR'): filepaths.append((settings.XPI_TARGETDIR, os.R_OK | os.W_OK, 'We want read + write. Should be a shared directory ' 'on multiserver installations')) for sdk in SDK.objects.all(): filepaths.append((sdk.get_source_dir(), os.R_OK, 'We want read on %s' % sdk.version),) filepath_results = [] filepath_status = True for path, perms, notes in filepaths: path_exists = os.path.isdir(path) path_perms = os.access(path, perms) filepath_status = filepath_status and path_exists and path_perms if not filepath_status and status: status = False filepath_results.append((path, path_exists, path_perms, notes)) # free space on XPI_TARGETDIR disk x_path = '%s/' % settings.XPI_TARGETDIR s_path = '%s/' % settings.SDKDIR_PREFIX x = os.statvfs(x_path) s = os.statvfs(s_path) data['free'] = { 'xpi_targetdir %s' % x_path: (x.f_bavail * x.f_frsize) / 1024, 'sdkdir_prefix %s' % s_path: (s.f_bavail * s.f_frsize) / 1024 } data['filepaths'] = filepath_results # Check celery try: data['celery_responses'] = CeleryResponse.objects.all() except: status = False # Check ElasticSearch try: es = get_es() data['es_health'] = es.cluster_health() data['es_health']['version'] = es.collect_info()['server']['version']['number'] if data['es_health']['status'] =='red': status = False log.warning('ElasticSearch cluster health was red.') except Exception, e: status = False log.critical('Failed to connect to ElasticSearch: %s' % e)
def run(self): self.getMetricsFromFile() maxConnections = self.getMaxConnections() if self.mysql_hostname == 'localhost' or self.mysql_hostname == '127.0.0.1': dataDir = self.getDataDir() diskStat = os.statvfs(dataDir) diskFree = float(diskStat.f_frsize * diskStat.f_bavail) / (1024*1024*1024) diskTotal = float(diskStat.f_frsize * diskStat.f_blocks) / (1024*1024*1024) tmpDir = self.getTempDir() tmpDiskStat = os.statvfs(tmpDir) tmpDiskFree = float(tmpDiskStat.f_frsize * tmpDiskStat.f_bavail) / (1024*1024*1024) tmpDiskTotal = float(tmpDiskStat.f_frsize * tmpDiskStat.f_blocks) / (1024*1024*1024) slaveLag = self.getSlaveLag() self.getMetricsTextFromServer() self.saveMetricsToFile() processStates = self.getProcessStateCounts() deltaTime = self.currentTimestamp - self.previousTimestamp prefix = self.getGmetricPrefix() metrics = [] metrics.append(MetricInfo(prefix + "max_connections", maxConnections, MetricInfo.GMETRIC_UINT32, None, self.GMETRIC_GROUP)) if self.mysql_hostname == 'localhost' or self.mysql_hostname == '127.0.0.1': metrics.append(MetricInfo(prefix + "disk_free", round(diskFree,2), MetricInfo.GMETRIC_FLOAT, "GB", self.GMETRIC_GROUP)) metrics.append(MetricInfo(prefix + "disk_total", round(diskTotal,2), MetricInfo.GMETRIC_FLOAT, "GB", self.GMETRIC_GROUP)) metrics.append(MetricInfo(prefix + "tmp_disk_free", round(tmpDiskFree,2), MetricInfo.GMETRIC_FLOAT, "GB", self.GMETRIC_GROUP)) metrics.append(MetricInfo(prefix + "tmp_disk_total", round(tmpDiskTotal,2), MetricInfo.GMETRIC_FLOAT, "GB", self.GMETRIC_GROUP)) if deltaTime > 0: for metricName in self.MYSQL_QUERY_RATE_OPERATIONS: requestRate = self.calculateMetricRate(metricName, deltaTime) if requestRate is not None: metrics.append(MetricInfo(prefix + metricName.lower() + '_qps', requestRate, MetricInfo.GMETRIC_DOUBLE, None, self.GMETRIC_GROUP)) for metricName in self.MYSQL_RATE_OPERATIONS: requestRate = self.calculateMetricRate(metricName, deltaTime) if requestRate is not None: metrics.append(MetricInfo(prefix + metricName.lower() + '_per_sec', requestRate, MetricInfo.GMETRIC_DOUBLE, None, self.GMETRIC_GROUP)) if processStates: for metricName in self.MYSQL_PROCESS_STATES: if metricName in processStates: metrics.append(MetricInfo(prefix + metricName.lower(), processStates[metricName], MetricInfo.GMETRIC_UINT32, None, self.GMETRIC_GROUP)) else: metrics.append(MetricInfo(prefix + metricName.lower(), 0, MetricInfo.GMETRIC_UINT32, None, self.GMETRIC_GROUP)) for metricName in self.MYSQL_STATE_VARIABLES: if metricName in self.currentMetrics: metrics.append(MetricInfo(prefix + metricName.lower(), self.currentMetrics[metricName], MetricInfo.GMETRIC_UINT32, None, self.GMETRIC_GROUP)) metrics.append(MetricInfo(prefix + "query_cache_size", self.getQueryCacheSize(), MetricInfo.GMETRIC_UINT32, "bytes", self.GMETRIC_GROUP)) if slaveLag is not None: metrics.append(MetricInfo(prefix + "slave_lag", slaveLag, MetricInfo.GMETRIC_UINT32, None, self.GMETRIC_GROUP)) self.sendMetrics(metrics)
def get_status(self): s = os.statvfs(self.get_work_directory()) diskfree = s.f_bfree * s.f_bsize diskfreepct = (float(s.f_bfree) / s.f_blocks) * 100 s = os.statvfs(self.get_user_directory()) userdiskfree = s.f_bfree * s.f_bsize userdiskfreepct = (float(s.f_bfree) / s.f_blocks) * 100 queuedsize = 0 queueddone = 0 active_downloads = 0 download_rate = 0 upload_rate = 0 connections = 0 downloads = self.get_downloads() for d in downloads: if d.size: queuedsize += d.size queueddone += d.downloaded if d.active: active_downloads += 1 download_rate += d.downloadrate upload_rate += d.uploadrate connections += d.connections if queuedsize: progress = round((float(queueddone) / queuedsize) * 100, 2) else: progress = 0 interface = get_interface(self.get_option( ('downpour', 'interface'), '0.0.0.0')) if interface == '0.0.0.0': # Load IPs for local host ips = [i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)] ips = filter(lambda ip: ip[:4] != '127.' and ip[:2] != '::', ips) interface = ', '.join(dict(map(lambda i: (i,1), ips)).keys()) hostname = '%s (%s)' % (socket.gethostname(), interface) status = {'host': hostname, 'version': VERSION, 'downloads': len(downloads), 'active_downloads': active_downloads, 'downloadrate': download_rate, 'uploadrate': upload_rate, 'progress': progress, 'diskfree': diskfree, 'diskfreepct': diskfreepct, 'userdiskfree': userdiskfree, 'userdiskfreepct': userdiskfreepct, 'connections': connections, 'paused': self.paused } return status
def executeCMDs(self): progBarVal=0 cnt=len(self.fileDict) self.messages.SetValue("") if self.jobType==0: cmd=["cp", "-rp"] elif self.jobType==1: cmd=["mv"] else: self.EndModal(wx.ID_CANCEL) if DEBUG_FS==True: countup=bnls=0 formatstrg="{0:15} {1:15} {2:15} {3:15} {4:15} {5:5} {6:15}" # nach filename sortiert über "self.fileDict" iterieren for k in OrderedDict(sorted(self.fileDict.items(), key=lambda t: t[1].filename)): v=self.fileDict[k] cmdToExecute=cmd+[k, self.targetFolder] # Kommando zusammensetzen self.messages.AppendText( self.listToString(cmdToExecute)+"\n") self.currentBytes.SetValue( hlp.intToStringWithCommas(v.sizeAdj)) self.objRemain.SetValue( hlp.intToStringWithCommas(cnt)) self.bytesRemain.SetValue( hlp.intToStringWithCommas(self.sumBytesToCopy-progBarVal)) wx.Yield() if DEBUG_FS==True: szv=os.statvfs(self.targetFolder) sv=szv.f_bavail*szv.f_bsize # Dateisystem-Restgröße vor dem copy self.executeSingleCMD(cmdToExecute) if self.abortTransfer==True: # Abbruch-Wunsch weiterreichen break cnt-=1 progBarVal+=v.sizeAdj # Fortschritt abhängig von der aktuellen Dateigröße... self.progBar.SetValue(progBarVal//self.gauge_devisor) # ...darstellen wx.Yield() if DEBUG_FS==True: countup+=1 # Nummer der bearbeiteten Datei szn=os.statvfs(self.targetFolder) sn=szn.f_bavail*szn.f_bsize # Dateisystem-Restgröße nach dem copy delta1=(sv-sn) # bei letztem copy belegte Byte delta2=delta1-v.sizeAdj # Differenz der belegten Byte zur Dateigröße (also der Fehler) bn=os.path.basename(k) # Dateiname der gerade kopierten Datei bnls+=len(bn) # Länge der bisher kopierten Dateinamen print formatstrg.format(sv, sn, delta1, v.sizeAdj, delta2, countup, bn) self.currentBytes.SetValue( "0") self.objRemain.SetValue( "0") self.bytesRemain.SetValue( "0") self.ok.Enable() # Schließen des Dialoges freischalten self.cancel.Disable() # der Cancel-Button wird jetzt nicht mehr gebraucht
def get_free_space(directory): """ Return directory free space (in human readable form) """ if sys.platform in ["win32", "cygwin"]: free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(directory), None, None, ctypes.pointer(free_bytes)) space = free_bytes.value else: space = os.statvfs(directory).f_bfree * os.statvfs(directory).f_frsize return format_size(space)
def statfs(self, path): full_path = self._full_path(path) try: real_path = self.fancy_filenames[full_path] stv = os.statvfs(real_path) except KeyError: stv = os.statvfs(full_path) return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree', 'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag', 'f_frsize', 'f_namemax'))
def compare_free_space(x, y): # statvfs can return a long; comparison functions must return an # int. Hence the checks that occur here. blocks_avail = os.statvfs(y).f_bavail - os.statvfs(x).f_bavail if (blocks_avail > 0): return 1 elif (blocks_avail < 0): return -1 else: return 0
def monitor(request): status = True data = {} filepaths = [ (settings.UPLOAD_DIR, os.R_OK | os.W_OK, 'We want read + write.'), ] if hasattr(settings, 'XPI_TARGETDIR'): filepaths.append((settings.XPI_TARGETDIR, os.R_OK | os.W_OK, 'We want read + write. Should be a shared directory ' 'on multiserver installations')) for sdk in SDK.objects.all(): filepaths.append((sdk.get_source_dir(), os.R_OK, 'We want read on %s' % sdk.version),) filepath_results = [] filepath_status = True for path, perms, notes in filepaths: path_exists = os.path.isdir(path) path_perms = os.access(path, perms) filepath_status = filepath_status and path_exists and path_perms if not filepath_status and status: status = False filepath_results.append((path, path_exists, path_perms, notes)) # free space on XPI_TARGETDIR disk x_path = '%s/' % settings.XPI_TARGETDIR s_path = '%s/' % settings.SDKDIR_PREFIX x = os.statvfs(x_path) s = os.statvfs(s_path) data['free'] = { 'xpi_targetdir %s' % x_path: (x.f_bavail * x.f_frsize) / 1024, 'sdkdir_prefix %s' % s_path: (s.f_bavail * s.f_frsize) / 1024 } data['filepaths'] = filepath_results template = loader.get_template('monitor.html') try: data['celery_responses'] = CeleryResponse.objects.all() except: pass try: data['es_health'] = get_es().cluster_health() except: pass context = RequestContext(request, data) status = 200 if status else 500 return HttpResponse(template.render(context), status=status)
def split_file(file_path, chunk_size): chunk_num = 1 chunks = [] file_name = os.path.basename(file_path) file_size = os.stat(file_path).st_size tempdir = tempfile.mkdtemp(suffix=".rhst") tempdir_statvfs = os.statvfs(tempdir) tempdir_free = tempdir_statvfs.f_bavail * tempdir_statvfs.f_frsize if (tempdir_free < file_size): print _('Not enough space available in /tmp to split %s') % (file_name) while True: line = raw_input(_('Please provide an alternative location to' ' split the file: ')) line = str(line).strip() tempdir = os.path.expanduser(line) try: os.mkdir(tempdir) tempdir_statvfs = os.statvfs(tempdir) tempdir_free = (tempdir_statvfs.f_bavail * tempdir_statvfs.f_frsize) if (tempdir_free < file_size): print _('Not enough space available in %s, %d bytes' ' required') % (tempdir, file_size) else: continue except OSError: print _('Unable to create directory at %s') % (tempdir) in_file = open(file_path) while True: msg = '' shasum = None if _sha256support: shasum = sha256() msg = _('SHA256: %s') else: shasum = sha.new() msg = _('SHA1: %s') data = in_file.read(chunk_size) if not data: break out_filename = os.path.join(tempdir, "%s.%03d" % (file_name, chunk_num)) out_file = open(out_filename, 'w') out_file.write(data) shasum.update(data) chunks.append({'file': out_filename, 'msg': msg % (shasum.hexdigest())}) chunk_num += 1 return chunks
def getText(self): service = self.source.service if service: if self.type == self.free: try: stat = statvfs(service.getPath().replace('Latest Recordings','')) hdd = stat.f_bfree * stat.f_bsize if hdd > 1099511627776: free = float(hdd/1099511627776.0) return '%.2f TB' % free elif hdd > 1073741824: free = float(hdd/1073741824.0) return '%.2f GB' % free elif hdd > 1048576: free = float(hdd/1048576.0) return '%i MB' % free except OSError: return 'N/A' elif self.type == self.size: try: stat = statvfs(service.getPath().replace('Latest Recordings','')) hddsize = stat.f_blocks * stat.f_bsize if hddsize > 1099511627776: locks = float(hddsize/1099511627776.0) return '(%.2f TB)' % locks elif hddsize > 1073741824: locks = float(hddsize/1073741824.0) return '(%.2f GB)' % locks elif hddsize > 1048576: locks = float(hddsize/1048576.0) return '(%i MB)' % locks except OSError: return 'N/A' elif self.type == self.both: try: stat = statvfs(service.getPath().replace('Latest Recordings','')) total = stat.f_blocks * stat.f_bsize free = (stat.f_bavail or stat.f_bfree) * stat.f_bsize if total == 0: total = 1 percentage = free * 100 / total return ('%s / %s (%d%%) ' + _('free')) % (self.bytes2human(free, 1), self.bytes2human(total, 1), percentage) except OSError: return 'N/A' elif self.type == self.path: if "." in str(service.getPath()) or "@" in str(service.getPath()) or "Latest Recordings" in str(service.getPath()): return service.getPath().rsplit('/', 1)[0] else: return service.getPath().replace('/Latest Recordings','') return ""
def getFreeSpace(folder, mulVar): """Return folder/drive free space in bytes if mulVar is 0. Adapted from http://stackoverflow.com/a/2372171 .""" assert mulVar >= 0 if platform.system() == 'Windows': free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes)) retVal = float(free_bytes.value) else: retVal = float(os.statvfs(folder).f_bfree * os.statvfs(folder).f_frsize) return retVal / (1024 ** mulVar)
def get_obj_store_free_space_bytes(): """Return total free space available on the disk on which the object storage resides (in bytes)""" obj_store_path = django.conf.settings.OBJECT_STORE_PATH if platform.system() == 'Windows': free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW( ctypes.c_wchar_p(obj_store_path), None, None, ctypes.pointer(free_bytes) ) return free_bytes.value else: return os.statvfs(obj_store_path).f_bfree * os.statvfs(obj_store_path).f_frsize
def cuckoo_status(): # In order to keep track of the diskspace statistics of the temporary # directory we create a temporary file so we can statvfs() on that. temp_file = Files.temp_put("") paths = dict( binaries=cwd("storage", "binaries"), analyses=cwd("storage", "analyses"), temporary=temp_file, ) diskspace = {} for key, path in paths.items(): if hasattr(os, "statvfs") and os.path.isdir(path): stats = os.statvfs(path) diskspace[key] = dict( free=stats.f_bavail * stats.f_frsize, total=stats.f_blocks * stats.f_frsize, used=(stats.f_blocks - stats.f_bavail) * stats.f_frsize, ) # Now we remove the temporary file and its parent directory. os.unlink(temp_file) # Get the CPU load. if hasattr(os, "getloadavg"): cpuload = os.getloadavg() else: cpuload = [] if os.path.isfile("/proc/meminfo"): values = {} for line in open("/proc/meminfo"): key, value = line.split(":", 1) values[key.strip()] = value.replace("kB", "").strip() if "MemAvailable" in values and "MemTotal" in values: memavail = int(values["MemAvailable"]) memtotal = int(values["MemTotal"]) memory = 100 - 100.0 * memavail / memtotal else: memory = memavail = memtotal = None else: memory = memavail = memtotal = None response = dict( version=version, hostname=socket.gethostname(), machines=dict(total=len(db.list_machines()), available=db.count_machines_available()), tasks=dict(total=db.count_tasks(), pending=db.count_tasks("pending"), running=db.count_tasks("running"), completed=db.count_tasks("completed"), reported=db.count_tasks("reported")), diskspace=diskspace, cpuload=cpuload, memory=memory, memavail=memavail, memtotal=memtotal, ) return jsonify(response)
def updinst_prepare(src, dst, dstfs=None, upgrade=False): files = [ (("boot/bzImage", ), ""), (("img/ro-size", ), "img/"), (("img/rootfs.img", ), "img/"), ] ramdisks = [] fsmods = [] with os.scandir(src + "/rdparts") as it: for e in it: if e.is_file(): if ".cpio" in e.name: s, _ = e.name.split(".", maxsplit=1) fsmods += [(s, e.name)] if e.name.endswith(".img"): ramdisks += [e.name] files += [(("rdparts/" + e.name, ), "rdparts/")] # figure out the destination filesystem; for which fsmod to use if dstfs is None: with open("/proc/mounts") as f: for L in f: M = L.split() if M[1] == dst: dstfs = M[2] if dstfs is None: sys.exit("Unable to determine dst FS type") fsmod = None for fs, fn in fsmods: if fs == dstfs: fsmod = fn if fsmod is None: sys.exit("Missing fsmod .cpio for dst FS") for rd in ramdisks: files += [(("rdparts/" + rd, "rdparts/" + fsmod), "rd/")] updsz = 0 dstsz = 0 overwrites = [] for fl, dstdir in files: dstname = dst + os.path.sep + dstdir + os.path.basename(fl[0]) try: s = os.stat(dstname) dstsz += s.st_blocks / 2 overwrites += [dstname] except FileNotFoundError: pass for f in fl: try: s = os.stat(src + os.path.sep + f) updsz += s.st_blocks / 2 except FileNotFoundError: sys.exit("Missing file: " + f) fss = os.statvfs(dst) fs_space = (fss.f_frsize * fss.f_bavail) / 1024 usemoves = False if overwrites: usemoves = True spacetol = 1024 if (updsz + spacetol) > fs_space: usemoves = False dstwipesz = dstsz if upgrade and overwrites: if not os.path.exists("/.o/Z/ro.cpio") and not os.path.exists( "/ro.sqfs"): for f in overwrites: if "rootfs.img" in f: s = os.stat(f) dstwipesz -= s.st_blocks / 2 nonmovesz = updsz - dstwipesz if (nonmovesz + spacetol) > fs_space: sys.exit("Insufficient target disk space") if upgrade and not overwrites: print( "Warning: none of target files exist - is this an upgrade at all?") dirs = [] if not upgrade: dirs = ["grub", "img", "rd", "rdparts"] if dstfs == "ext4": dirs += ["root"] return { "files": files, "usemoves": usemoves, "overwrites": overwrites, "dirs": dirs, }
def Freespace(dev): statdev = statvfs(dev) space = (statdev.f_bavail * statdev.f_frsize) / 1024 print "[FULL BACKUP] Free space on %s = %i kilobytes" %(dev, space) return space
def __init__(self, lock_file, timeout=-1, max_filename_length=None): max_filename_length = os.statvfs(os.path.dirname(lock_file)).f_namemax super().__init__(lock_file, timeout=timeout, max_filename_length=max_filename_length)
def manager_thread(): # now loop context = zmq.Context() thermal_sock = messaging.pub_sock(context, service_list['thermal'].port) health_sock = messaging.sub_sock(context, service_list['health'].port) version = open( os.path.join(os.path.dirname(os.path.abspath(__file__)), "common", "version.h")).read().split('"')[1] cloudlog.info("manager start %s" % version) cloudlog.info(dict(os.environ)) start_managed_process("logmessaged") start_managed_process("logcatd") start_managed_process("uploader") start_managed_process("ui") if os.getenv("NOBOARD") is None: # *** wait for the board *** wait_for_device() # flash the device if os.getenv("NOPROG") is None: boarddir = os.path.dirname(os.path.abspath(__file__)) + "/../board/" os.system("cd %s && make" % boarddir) start_managed_process("boardd") if os.getenv("STARTALL") is not None: for p in car_started_processes: start_managed_process(p) logger_dead = False count = 0 # set 5 second timeout on health socket # 5x slower than expected health_sock.RCVTIMEO = 5000 while 1: # get health of board, log this in "thermal" td = messaging.recv_sock(health_sock, wait=True) print td # replace thermald msg = read_thermal() # loggerd is gated based on free space statvfs = os.statvfs(ROOT) avail = (statvfs.f_bavail * 1.0) / statvfs.f_blocks # thermal message now also includes free space msg.thermal.freeSpace = avail with open("/sys/class/power_supply/battery/capacity") as f: msg.thermal.batteryPercent = int(f.read()) with open("/sys/class/power_supply/battery/status") as f: msg.thermal.batteryStatus = f.read().strip() thermal_sock.send(msg.to_bytes()) print msg # TODO: add car battery voltage check max_temp = max(msg.thermal.cpu0, msg.thermal.cpu1, msg.thermal.cpu2, msg.thermal.cpu3) / 10.0 # uploader is gated based on the phone temperature if False and max_temp > 85.0: cloudlog.info("over temp: %r", max_temp) kill_managed_process("uploader") elif max_temp < 70.0: start_managed_process("uploader") if avail < 0.05: logger_dead = True # start constellation of processes when the car starts if not os.getenv("STARTALL"): # with 2% left, we killall, otherwise the phone is bricked if td is not None and td.health.started and avail > 0.02: for p in car_started_processes: if p == "loggerd" and logger_dead: kill_managed_process(p) else: # Wait for visiond to get a good camera image if p == "controlsd": time.sleep(2) start_managed_process(p) else: logger_dead = False for p in car_started_processes: kill_managed_process(p) # shutdown if the battery gets lower than 10%, we aren't running, and we are discharging if msg.thermal.batteryPercent < 5 and msg.thermal.batteryStatus == "Discharging": os.system('LD_LIBRARY_PATH="" svc power shutdown') # check the status of all processes, did any of them die? for p in running: cloudlog.debug(" running %s %s" % (p, running[p])) # report to server once per minute if (count % 60) == 0: cloudlog.event("STATUS_PACKET", running=running.keys(), count=count, health=(td.to_dict() if td else None), thermal=msg.to_dict(), version=version) count += 1
def tst_statvfs(mnt_dir): os.statvfs(mnt_dir)
def spaceFinder(self,dir): statistics = os.statvfs(dir) return int(statistics.f_bavail * statistics.f_frsize)
def start(self): """Start scheduler.""" self.initialize() log.info("Waiting for analysis tasks.") # Message queue with threads to transmit exceptions (used as IPC). errors = Queue.Queue() # Command-line overrides the configuration file. if self.maxcount is None: self.maxcount = self.cfg.cuckoo.max_analysis_count # This loop runs forever. while self.running: time.sleep(1) # If not enough free disk space is available, then we print an # error message and wait another round (this check is ignored # when the freespace configuration variable is set to zero). if self.cfg.cuckoo.freespace: # Resolve the full base path to the analysis folder, just in # case somebody decides to make a symbolic link out of it. dir_path = os.path.join(CUCKOO_ROOT, "storage", "analyses") # TODO: Windows support if hasattr(os, "statvfs"): dir_stats = os.statvfs(dir_path) # Calculate the free disk space in megabytes. space_available = dir_stats.f_bavail * dir_stats.f_frsize space_available /= 1024 * 1024 if space_available < self.cfg.cuckoo.freespace: log.error("Not enough free disk space! (Only %d MB!)", space_available) continue # Have we limited the number of concurrently executing machines? if self.cfg.cuckoo.max_machines_count > 0: # Are too many running? if len(machinery.running() ) >= self.cfg.cuckoo.max_machines_count: continue # If no machines are available, it's pointless to fetch for # pending tasks. Loop over. if not machinery.availables(): continue # Exits if max_analysis_count is defined in the configuration # file and has been reached. if self.maxcount and self.total_analysis_count >= self.maxcount: if active_analysis_count <= 0: self.stop() else: # Fetch a pending analysis task. #TODO: this fixes only submissions by --machine, need to add other attributes (tags etc.) for machine in self.db.get_available_machines(): task = self.db.fetch(machine=machine.name) if task: log.debug("Processing task #%s", task.id) self.total_analysis_count += 1 # Initialize and start the analysis manager. analysis = AnalysisManager(task, errors) analysis.start() # Deal with errors. try: raise errors.get(block=False) except Queue.Empty: pass
def statfs(self, a1): full_path = self._full_path(a1) stv = os.statvfs(full_path) return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree', 'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag', 'f_frsize', 'f_namemax'))
def stat(self): if self.mountpoint: return os.statvfs(self.mountpoint) else: raise OSError, "Device %s is not mounted" % self.device
def work(self): mounts = [] matches = [] print "[Trashcan] probing folders" f = open('/proc/mounts', 'r') for line in f.readlines(): parts = line.strip().split() if config.usage.movielist_trashcan_network_clean.getValue( ) and parts[1].startswith('/media/net'): mounts.append(parts[1]) elif not parts[1].startswith('/media/net'): mounts.append(parts[1]) f.close() for mount in mounts: if os.path.isdir(os.path.join(mount, '.Trash')): matches.append(os.path.join(mount, '.Trash')) if os.path.isdir(os.path.join(mount, 'movie/.Trash')): matches.append(os.path.join(mount, 'movie/.Trash')) print "[Trashcan] found following trashcan's:", matches if len(matches): for trashfolder in matches: print "[Trashcan] looking in trashcan", trashfolder trashsize = get_size(trashfolder) diskstat = os.statvfs(trashfolder) free = diskstat.f_bfree * diskstat.f_bsize bytesToRemove = self.reserveBytes - free print "[Trashcan] " + str(trashfolder) + ": Size:", trashsize candidates = [] size = 0 for root, dirs, files in os.walk(trashfolder, topdown=False): for name in files: try: fn = os.path.join(root, name) st = os.stat(fn) if st.st_ctime < self.ctimeLimit: enigma.eBackgroundFileEraser.getInstance( ).erase(fn) bytesToRemove -= st.st_size else: candidates.append( (st.st_ctime, fn, st.st_size)) size += st.st_size except Exception, e: print "[Trashcan] Failed to stat %s:" % name, e # Remove empty directories if possible for name in dirs: try: os.rmdir(os.path.join(root, name)) except: pass candidates.sort() # Now we have a list of ctime, candidates, size. Sorted by ctime (=deletion time) for st_ctime, fn, st_size in candidates: if bytesToRemove < 0: break try: # somtimes the file does not exist, can happen if trashcan is on a network, the main box could also be emptying trash at same time. enigma.eBackgroundFileEraser.getInstance().erase( fn) except: pass bytesToRemove -= st_size size -= st_size print "[Trashcan] " + str( trashfolder) + ": Size now:", size
def f(): import os s = os.statvfs('/data') return 100 * (1 - float(s.f_bfree) / float(s.f_blocks))
def getFreeSpace(): st = os.statvfs(filepath + "/") du = st.f_bavail * st.f_frsize return du
def statvfs(self): """ Perform a statvfs() system call on this path. """ return os.statvfs(self)
def createSetup(self, widget): self.list = [] self.timerJustplayEntry = getConfigListEntry( _('Timer type'), self.timerentry_justplay, _('Chose between record and ZAP.')) self.list.append(self.timerJustplayEntry) self.entryName = getConfigListEntry( _('Name'), self.timerentry_name, _('Set the name the recording will get.')) self.list.append(self.entryName) self.entryDescription = getConfigListEntry( _('Description'), self.timerentry_description, _('Set the description of the recording.')) self.list.append(self.entryDescription) self.timerTypeEntry = getConfigListEntry( _('Repeat type'), self.timerentry_type, _('A repeating timer or just once?')) self.list.append(self.timerTypeEntry) if self.timerentry_type.value == 'once': self.frequencyEntry = None else: self.frequencyEntry = getConfigListEntry( _('Repeats'), self.timerentry_repeated, _('Choose between Daily, Weekly, Weekdays or user defined.')) self.list.append(self.frequencyEntry) self.repeatedbegindateEntry = getConfigListEntry( _('Starting on'), self.timerentry_repeatedbegindate, _('Set the date the timer must start.')) self.list.append(self.repeatedbegindateEntry) if self.timerentry_repeated.value == 'daily': pass if self.timerentry_repeated.value == 'weekdays': pass if self.timerentry_repeated.value == 'weekly': self.list.append( getConfigListEntry(_('Weekday'), self.timerentry_weekday)) if self.timerentry_repeated.value == 'user': self.list.append( getConfigListEntry(_('Monday'), self.timerentry_day[0])) self.list.append( getConfigListEntry(_('Tuesday'), self.timerentry_day[1])) self.list.append( getConfigListEntry(_('Wednesday'), self.timerentry_day[2])) self.list.append( getConfigListEntry(_('Thursday'), self.timerentry_day[3])) self.list.append( getConfigListEntry(_('Friday'), self.timerentry_day[4])) self.list.append( getConfigListEntry(_('Saturday'), self.timerentry_day[5])) self.list.append( getConfigListEntry(_('Sunday'), self.timerentry_day[6])) if self.timerentry_justplay.value != 'zap': self.list.append( getConfigListEntry( _('Rename name and description for new events'), self.timerentry_renamerepeat)) self.entryDate = getConfigListEntry( _('Date'), self.timerentry_date, _('Set the date the timer must start.')) if self.timerentry_type.value == 'once': self.list.append(self.entryDate) self.entryStartTime = getConfigListEntry( _('Start time'), self.timerentry_starttime, _('Set the time the timer must start.')) self.list.append(self.entryStartTime) self.entryShowEndTime = getConfigListEntry( _('Set end time'), self.timerentry_showendtime, _('Set the time the timer must stop.')) if self.timerentry_justplay.value == 'zap': self.list.append(self.entryShowEndTime) self.entryEndTime = getConfigListEntry( _('End time'), self.timerentry_endtime, _('Set the time the timer must stop.')) if self.timerentry_justplay.value != 'zap' or self.timerentry_showendtime.value: self.list.append(self.entryEndTime) self.channelEntry = getConfigListEntry( _('Channel'), self.timerentry_service, _('Set the channel for this timer.')) self.list.append(self.channelEntry) if self.timerentry_showendtime.value and self.timerentry_justplay.value == 'zap': self.list.append( getConfigListEntry( _('After event'), self.timerentry_afterevent, _("What action is required on complettion of the timer? 'Auto' lets the box return to the state it had when the timer started. 'Do nothing', 'Go to standby' and 'Go to deep standby' do ecaxtly that." ))) description = free = '' try: if self.timerentry_justplay.value != 'zap': stat = statvfs(self.timerentry_dirname.value) a = float(stat.f_blocks) * stat.f_bsize / 1024 / 1024 / 1024 b = float(stat.f_bavail) * stat.f_bsize / 1024 / 1024 / 1024 c = 100.0 * b / a free = ('%0.f GB (%0.f %s) ' + _('free diskspace')) % (b, c, '%') description = _('Current location') except: pass self['locationdescription'].setText(description) self['locationfreespace'].setText(free) self.dirname = getConfigListEntry( _('Location'), self.timerentry_dirname, _('Where should the recording be saved?')) self.tagsSet = getConfigListEntry( _('Tags'), self.timerentry_tagsset, _('Choose a tag for easy finding a recording.')) if self.timerentry_justplay.value != 'zap': if config.usage.setup_level.index >= 2: self.list.append(self.dirname) if getPreferredTagEditor(): self.list.append(self.tagsSet) self.list.append( getConfigListEntry( _('After Recording'), self.timerentry_afterevent, _("What action is required on complettion of the timer? 'Auto' lets the box return to the state it had when the timer started. 'Do nothing', 'Go to standby' and 'Go to deep standby' do ecaxtly that." ))) self.list.append( getConfigListEntry( _('Recording type'), self.timerentry_recordingtype, _("Descramble & record ECM' gives the option to descramble afterwards if descrambling on recording failed. 'Don't descramble, record ECM' save a scramble recording that can be descrambled on playback. 'Normal' means descramble the recording and don't record ECM." ))) self[widget].list = self.list self[widget].l.setList(self.list) return
def Freespace(dev): statdev = os.statvfs(dev) space = (statdev.f_bavail * statdev.f_frsize) / 1024 print "[Flash Online] Free space on %s = %i kilobytes" % (dev, space) return space
def get_free_disk_size(dir_path): stat = os.statvfs(dir_path) return stat.f_frsize * stat.f_bavail
def df(path='/'): s = os.statvfs(path) print('{0}% free space ({1} bytes)'.format(s[3] / s[2] * 100, s[3] * s[0]))
def get_free_space(): ret = os.statvfs('./') free_space = ret.f_frsize * ret.f_bfree / 1024 / 1024 / 1024 # 기가바이트 return free_space
def filesystem_get_free_space(path): """ Get Free space of directory """ statvfs = os.statvfs(path) return (statvfs.f_frsize * statvfs.f_bavail)
def getSpace(drive_path): s = os.statvfs(drive_path) freebytes = s[statvfs.F_BSIZE] * s[statvfs.F_BAVAIL] return freebytes
def og_folders(self): if self.keep_original_foldernames is True: url = self.radarr_url + '/api/movie/' + str(self.radarr_id) logger.info('[RADARR] Retrieving existing movie information for %s' % self.radarr_movie) r = requests.get(url, headers=self.radarr_headers) existingdata = r.json() #try updating the path logger.info("[RADARR] OLD_PATH: %s" % existingdata['path']) existingfilename = None try: existingfilename = existingdata['movieFile']['relativePath'] logger.info("[RADARR] OLD_FILENAME: %s" % existingfilename) except: pass #now we check the movieinfo to see what directory we sling it to... if all([self.dir_hd_movies is None, self.dir_sd_movies is None, self.dir_web_movies is None]): destdir = self.radarr_rootdir else: logger.info('[RADARR Now checking movie file for further information as to where to sling the final file.') destdir = self.moviecheck(existingdata) logger.info('[RADARR] Current/Existing Directory: %s' % destdir) newpath = os.path.join(destdir, self.snstat['name']) logger.info('[RADARR] New Directory: %s' % newpath) #makes sure we have enough free space on new location for the move st = os.statvfs(destdir) dst_freesize = st.f_bavail * st.f_frsize src_filesize = 0 for dirpath, dirnames, filenames in os.walk(existingdata['path']): for f in filenames: fp = os.path.join(dirpath, f) src_filesize += os.path.getsize(fp) logger.info('[FREESPACE-CHECK] ' + destdir + ' has ' + str(self.sizeof_fmt(dst_freesize)) + ' free.') logger.info('[FREESPACE-CHECK] ' + self.snstat['name'] + ' will consume ' + str(self.sizeof_fmt(src_filesize)) + '.') if dst_freesize > src_filesize: logger.info('[FREESPACE-CHECK] PASS. Free space available after move: ' + str(self.sizeof_fmt(dst_freesize - src_filesize)) + '.') else: logger.warn('[FREESPACE-CHECK] FAIL. There is not enough free space on the destination to move file.') sys.exit('Not enough free space on destination:' + destdir) #move the dir to the new location (if in same dir will do a rename, otherwise will do a copy, then delete) shutil.move(existingdata['path'], newpath) logger.info("[RADARR] MOVE/RENAME successful to : %s " % newpath) url = self.radarr_url + '/api/command' refreshpayload = {'name': 'refreshmovie', 'movieId': int(self.radarr_id)} logger.info("[RADARR] Refreshing movie to make sure old location could not be located anymore: %s" % refreshpayload) r = requests.post(url, json=refreshpayload, headers=self.radarr_headers) datachk = r.json() check = True while check: url = self.radarr_url + '/api/command/' + str(datachk['id']) logger.info("[RADARR] API Submitting: %s" % url) r = requests.get(url, params=None, headers=self.radarr_headers) dchk = r.json() if dchk['state'] == 'completed': check = False else: logger.info('[RADARR] Refreshing of movie currently running - will recheck in 10s to see if completed') time.sleep(10) url = self.radarr_url + '/api/movie/' + str(self.radarr_id) logger.info('[RADARR] Retrieving existing movie information for %s' % self.radarr_movie) r = requests.get(url, headers=self.radarr_headers) data = r.json() data['path'] = u"" + newpath.decode('utf-8') data['folderName'] = u"" + self.snstat['name'].decode('utf-8') url = self.radarr_url + '/api/movie' #set the new path in the json - assume that the torrent name is ALSO the folder name #could set folder name to file name via an option..possible to-do. logger.info('[RADARR] Updating data for movie: ' + str(data)) r = requests.put(url, json=data, headers=self.radarr_headers) url = self.radarr_url + '/api/command' refreshpayload = {'name': 'refreshmovie', 'movieId': int(self.radarr_id)} logger.info("[RADARR] Refreshing movie to make sure new location is now recognized: %s" % refreshpayload) r = requests.post(url, json=refreshpayload, headers=self.radarr_headers) datachk = r.json() check = True while check: url = self.radarr_url + '/api/command/' + str(datachk['id']) logger.info("[RADARR] API Submitting: %s" % url) r = requests.get(url, params=None, headers=self.radarr_headers) dchk = r.json() if dchk['state'] == 'completed': check = False else: logger.info('[RADARR] Refreshing of movie currently running - will recheck in 10s to see if completed') time.sleep(10) url = self.radarr_url + '/api/movie/' + str(self.radarr_id) logger.info('[RADARR] Retrieving existing movie information for %s' % self.radarr_movie) r = requests.get(url, headers=self.radarr_headers) data = r.json() data['path'] = u"" + newpath.decode('utf-8') data['folderName'] = u"" + self.snstat['name'].decode('utf-8') url = self.radarr_url + '/api/movie' #set the new path in the json - assume that the torrent name is ALSO the folder name #could set folder name to file name via an option..possible to-do. logger.info('[RADARR] Updating data for movie: ' + str(data)) r = requests.put(url, json=data, headers=self.radarr_headers) url = self.radarr_url + '/api/command' refreshpayload = {'name': 'refreshmovie', 'movieId': int(self.radarr_id)} logger.info("[RADARR] Refreshing movie to make sure new location is now recognized: %s" % refreshpayload) r = requests.post(url, json=refreshpayload, headers=self.radarr_headers) datachk = r.json() check = True while check: url = self.radarr_url + '/api/command/' + str(datachk['id']) logger.info("[RADARR] API Submitting: %s" % url) r = requests.get(url, params=None, headers=self.radarr_headers) dchk = r.json() if dchk['state'] == 'completed': logger.info('[RADARR] Successfully updated paths to original foldername for ' + self.radarr_movie) check = False else: logger.info('[RADARR] Refreshing of movie currently running - will recheck in 10s to see if completed') time.sleep(10)
def upload(self, path): """ :param path: :return: """ collect_files = self.collect_files(path) self.main.led.set_led_yellow(1) # Sum up byte size for upload sum_size = 0 for f in collect_files: try: sum_size += os.path.getsize(f) except OSError: # lost file, do not sum it -- lost drive? pass self.main.led.set_led_yellow(0) # check free disk space s = os.statvfs('/local/upload') free = s.f_bavail * s.f_frsize if sum_size > free - 1024 * 1024 * 200: # keep at least 200MB (logs, cache, whatever) self.main.log.write( log.MESSAGE, "[UPLOADER] Not uploading %s -- insufficient " "disk space" % path) return # perform upload self.main.log.write(log.MESSAGE, '[UPLOADER] Uploading... %s' % path) i = 0 for f in collect_files: # remove '/media/usbX/' rel = os.sep.join(f.split(os.sep)[3:]) dest = os.path.join('/local/upload', rel) # check if exists if os.path.isfile(dest): continue # check dir exists dirname = os.path.dirname(dest) if not os.path.isdir(dirname): try: os.makedirs(dirname) except OSError: # TODO: do something pass # perform copy print("COPY: %s -> %s" % (f, dest)) try: shutil.copy(f, dest) except IOError: # TODO: do something pass self.main.led.set_led_yellow(i % 2) i += 1 if not self.main.keep_run: return self.main.led.set_led_yellow(0) # Flash some LED for i in range(5): self.main.led.flash_led(i, 1) time.sleep(2) for i in range(5): self.main.led.flash_led(i, 1) self.main.log.write(log.MESSAGE, '[UPLOADER] Upload done.')
def get_total_disk_size(dir_path): stat = os.statvfs(dir_path) return stat.f_blocks * stat.f_frsize
def stat(self): return statvfs(self.mountpoint)
def get_disk_stats(whichdir, reserved_space=0): """Return disk statistics for the storage disk, in the form of a dict with the following fields. total: total bytes on disk free_for_root: bytes actually free on disk free_for_nonroot: bytes free for "a non-privileged user" [Unix] or the current user [Windows]; might take into account quotas depending on platform used: bytes used on disk avail: bytes available excluding reserved space An AttributeError can occur if the OS has no API to get disk information. An EnvironmentError can occur if the OS call fails. whichdir is a directory on the filesystem in question -- the answer is about the filesystem, not about the directory, so the directory is used only to specify which filesystem. reserved_space is how many bytes to subtract from the answer, so you can pass how many bytes you would like to leave unused on this filesystem as reserved_space. """ if have_GetDiskFreeSpaceExW: # If this is a Windows system and GetDiskFreeSpaceExW is available, use it. # (This might put up an error dialog unless # SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX) has been called, # which we do in allmydata.windows.fixups.initialize().) n_free_for_nonroot = c_ulonglong(0) n_total = c_ulonglong(0) n_free_for_root = c_ulonglong(0) retval = GetDiskFreeSpaceExW(whichdir, byref(n_free_for_nonroot), byref(n_total), byref(n_free_for_root)) if retval == 0: raise OSError( "WinError: %s\n attempting to get disk statistics for %r" % (WinError(get_last_error()), whichdir)) free_for_nonroot = n_free_for_nonroot.value total = n_total.value free_for_root = n_free_for_root.value else: # For Unix-like systems. # <http://docs.python.org/library/os.html#os.statvfs> # <http://opengroup.org/onlinepubs/7990989799/xsh/fstatvfs.html> # <http://opengroup.org/onlinepubs/7990989799/xsh/sysstatvfs.h.html> s = os.statvfs(whichdir) # on my mac laptop: # statvfs(2) is a wrapper around statfs(2). # statvfs.f_frsize = statfs.f_bsize : # "minimum unit of allocation" (statvfs) # "fundamental file system block size" (statfs) # statvfs.f_bsize = statfs.f_iosize = stat.st_blocks : preferred IO size # on an encrypted home directory ("FileVault"), it gets f_blocks # wrong, and s.f_blocks*s.f_frsize is twice the size of my disk, # but s.f_bavail*s.f_frsize is correct total = s.f_frsize * s.f_blocks free_for_root = s.f_frsize * s.f_bfree free_for_nonroot = s.f_frsize * s.f_bavail # valid for all platforms: used = total - free_for_root avail = max(free_for_nonroot - reserved_space, 0) return { 'total': total, 'free_for_root': free_for_root, 'free_for_nonroot': free_for_nonroot, 'used': used, 'avail': avail, }
def free_space(self, path): st = os.statvfs(path) free = st.f_bavail * st.f_frsize return "free " + str(free / (1000 * 1000)) + "MB"
#!/usr/bin/env python from __future__ import division from sys import argv from os.path import join, isdir from os import statvfs, listdir, stat, remove from shutil import rmtree if __name__ == '__main__': checkouts, repos = argv[1:] # Look at checkouts and repos in turn. for dirname in (checkouts, repos): # Calculate free space info = statvfs(dirname) free = info.f_bavail / info.f_blocks # Skip if free space is over 20% if free > .2: continue # Order directory contents by age, oldest-first paths = [join(dirname, name) for name in listdir(dirname)] times = [stat(path).st_mtime for path in paths] infos = zip(times, paths) infos.sort() if dirname == checkouts: # Remove twenty-five checkouts at a time removals = infos[:25]
def bigStorage(minFree, default, *candidates): try: diskstat = os.statvfs(default) free = diskstat.f_bfree * diskstat.f_bsize if (free > minFree) and (free > 2000000): return default except Exception, e: pass mounts = open('/proc/mounts', 'rb').readlines() # format: device mountpoint fstype options # mountpoints = [x.split(' ', 2)[1] for x in mounts] for candidate in candidates: if candidate in mountpoints: try: diskstat = os.statvfs(candidate) free = diskstat.f_bfree * diskstat.f_bsize if free > minFree: return candidate except: pass return default class SettingsMgr: def __init__(self, sections): self.settingsFile = resolveFilename(SCOPE_CURRENT_PLUGIN, "Extensions/EPGImportFilter/settings.conf") self.sections = sections def loadUserSettings(self): #self.sources = {} sources = {}
def partition_freespace(directory): """Return free space of given directory's partition.""" st = os.statvfs(directory) return st[statvfs.F_BSIZE] * st[statvfs.F_BFREE]