aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2015-06-08 10:20:15 -0500
committerAndy Doan <andy.doan@linaro.org>2015-06-22 14:13:43 -0500
commit980445eb10725b0c5a58ae7a614501699a64f539 (patch)
treea12bd917ad43871c6fae226e87113bd5530e3fe4
parent93570b9cc9f8f7a3f41b2b1d7ddace1b247bae20 (diff)
Split up Artifact class
S3 will introduce a new type of Artifact. So we'll split up the current class into an Artifact and LocalArtifact. We'll try and keep as much logic as possible in the base class and leave the subclasses to be implementation specific. Change-Id: Ifc4251d86378036c383b5f05668b7eddbeb83d16
-rw-r--r--license_protected_downloads/common.py78
-rw-r--r--license_protected_downloads/tests/test_common.py2
2 files changed, 42 insertions, 38 deletions
diff --git a/license_protected_downloads/common.py b/license_protected_downloads/common.py
index 125bd64..e8938ed 100644
--- a/license_protected_downloads/common.py
+++ b/license_protected_downloads/common.py
@@ -228,11 +228,17 @@ def _sizeof_fmt(num):
class Artifact(object):
- def __init__(self, urlbase, file_name, human_readable, local_path):
- self.human_readable = human_readable
- self.file_name = file_name
+ def __init__(self, urlbase, file_name, size, mtime, human_readable):
self.urlbase = urlbase
- self.full_path = os.path.join(local_path, file_name)
+ self.file_name = file_name
+ self.size = size
+ self.mtime = mtime
+ self.human_readable = human_readable
+
+ if human_readable:
+ self.size = _sizeof_fmt(size)
+ mtime = datetime.fromtimestamp(mtime)
+ self.mtime = mtime.strftime('%d-%b-%Y %H:%M')
def url(self):
url = self.urlbase
@@ -246,35 +252,7 @@ class Artifact(object):
return url + self.file_name
def get_type(self):
- if os.path.isdir(self.full_path):
- return 'folder'
- else:
- mtype = mimetypes.guess_type(self.full_path)[0]
- if self.human_readable:
- if mtype is None:
- mtype = 'other'
- elif mtype.split('/')[0] == 'text':
- mtype = 'text'
- return mtype
-
- def get_size(self):
- size = 0
- # ensure the file we are looking at exists (not broken symlink)
- if os.path.exists(self.full_path):
- size = os.path.getsize(self.full_path)
- if self.human_readable:
- size = _sizeof_fmt(size)
- return size
-
- def get_mtime(self):
- mtime = 0
- # ensure the file we are looking at exists (not broken symlink)
- if os.path.exists(self.full_path):
- mtime = os.path.getmtime(self.full_path)
- if self.human_readable:
- mtime = datetime.fromtimestamp(mtime)
- mtime = mtime.strftime('%d-%b-%Y %H:%M')
- return mtime
+ raise NotImplementedError()
def get_listing(self):
if os.path.isdir(self.full_path):
@@ -290,20 +268,46 @@ class Artifact(object):
ll = models.License.objects.all_with_hashes(ldl)
return {
'name': self.file_name,
- 'size': self.get_size(),
- 'type': self.get_type(),
- 'mtime': self.get_mtime(),
+ 'size': self.size,
+ 'mtime': self.mtime,
'license_digest_list': ldl,
'license_list': ll,
+ 'type': self.get_type(),
'url': self.url(),
}
+class LocalArtifact(Artifact):
+ '''An artifact that lives on the local filesystem'''
+ def __init__(self, urlbase, file_name, human_readable, path):
+ self.full_path = os.path.join(path, file_name)
+
+ size = mtime = 0
+ # ensure the file we are looking at exists (not broken symlink)
+ if os.path.exists(self.full_path):
+ size = os.path.getsize(self.full_path)
+ mtime = os.path.getmtime(self.full_path)
+ super(LocalArtifact, self).__init__(
+ urlbase, file_name, size, mtime, human_readable)
+
+ def get_type(self):
+ if os.path.isdir(self.full_path):
+ return 'folder'
+ else:
+ mtype = mimetypes.guess_type(self.full_path)[0]
+ if self.human_readable:
+ if mtype is None:
+ mtype = 'other'
+ elif mtype.split('/')[0] == 'text':
+ mtype = 'text'
+ return mtype
+
+
def dir_list(url, path, human_readable=True):
artifacts = []
if os.path.exists(path):
- artifacts = [Artifact(url, x, human_readable, path)
+ artifacts = [LocalArtifact(url, x, human_readable, path)
for x in os.listdir(path)]
artifacts.sort(_sort_artifacts)
diff --git a/license_protected_downloads/tests/test_common.py b/license_protected_downloads/tests/test_common.py
index f606f25..eec6b3d 100644
--- a/license_protected_downloads/tests/test_common.py
+++ b/license_protected_downloads/tests/test_common.py
@@ -20,7 +20,7 @@ class CommonTests(unittest.TestCase):
(['10', 'foo', '100', 'latest'], ['latest', '10', '100', 'foo']),
]
for files, expected in patterns:
- artifacts = [common.Artifact('', x, True, '') for x in files]
+ artifacts = [common.LocalArtifact('', x, True, '') for x in files]
artifacts.sort(common._sort_artifacts)
self.assertEqual(expected, [x.file_name for x in artifacts])