slpkg/slpkg/file_size.py

49 lines
1.5 KiB
Python
Raw Normal View History

2014-08-13 02:39:41 +03:00
#!/usr/bin/python
# -*- coding: utf-8 -*
2014-08-27 07:15:40 +03:00
# file_size.py file is part of slpkg.
2014-08-13 07:28:04 +03:00
# Copyright 2014-2017 Dimitris Zlatanidis <d.zlatanidis@gmail.com>
2014-08-13 07:28:04 +03:00
# All rights reserved.
2014-12-19 06:57:56 +02:00
# Slpkg is a user-friendly package manager for Slackware installations
2014-08-13 07:28:04 +03:00
# https://github.com/dslackw/slpkg
2014-08-27 07:15:40 +03:00
# Slpkg is free software: you can redistribute it and/or modify
2014-08-13 07:28:04 +03:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-05-28 06:07:13 +03:00
2014-08-13 02:39:41 +03:00
import os
import urllib2
2014-10-06 22:44:44 +03:00
class FileSize(object):
2015-08-05 06:58:28 +03:00
"""Check local or remote file size
"""
2014-10-06 22:44:44 +03:00
def __init__(self, registry):
self.registry = registry
2014-10-14 06:01:38 +03:00
2014-10-06 22:44:44 +03:00
def server(self):
2015-09-27 12:11:01 +03:00
"""Returns the size of remote files
2015-06-05 14:42:52 +03:00
"""
2014-10-06 22:44:44 +03:00
try:
tar = urllib2.urlopen(self.registry)
meta = tar.info()
return int(meta.getheaders("Content-Length")[0])
except (urllib2.URLError, IndexError):
2015-06-05 14:42:52 +03:00
return " "
2014-10-06 22:44:44 +03:00
def local(self):
2015-09-27 12:11:01 +03:00
"""Returns the size of local files
2015-06-05 14:42:52 +03:00
"""
2014-10-06 22:44:44 +03:00
return os.path.getsize(self.registry)