|
| 1 | +import calendar |
| 2 | +import os |
| 3 | +import shutil |
| 4 | + |
| 5 | +from .tartifact_store import TartifactStore |
| 6 | + |
| 7 | +class LocalArtifactStore(TartifactStore): |
| 8 | + def __init__(self, config, |
| 9 | + bucket_name=None, |
| 10 | + verbose=10, |
| 11 | + measure_timestamp_diff=False, |
| 12 | + compression=None): |
| 13 | + |
| 14 | + if compression is None: |
| 15 | + compression = config.get('compression') |
| 16 | + |
| 17 | + self.endpoint = config.get('endpoint', '~') |
| 18 | + self.store_root = os.path.realpath(os.path.expanduser(self.endpoint)) |
| 19 | + if not os.path.exists(self.store_root) \ |
| 20 | + or not os.path.isdir(self.store_root): |
| 21 | + raise ValueError() |
| 22 | + |
| 23 | + self.bucket = bucket_name |
| 24 | + if self.bucket is None: |
| 25 | + self.bucket = config.get('bucket') |
| 26 | + self.store_root = os.path.join(self.store_root, self.bucket) |
| 27 | + self._ensure_path_dirs_exist(self.store_root) |
| 28 | + |
| 29 | + super(LocalArtifactStore, self).__init__( |
| 30 | + measure_timestamp_diff, |
| 31 | + compression=compression, |
| 32 | + verbose=verbose) |
| 33 | + |
| 34 | + def _ensure_path_dirs_exist(self, path): |
| 35 | + dirs = os.path.dirname(path) |
| 36 | + os.makedirs(dirs, mode = 0o777, exist_ok = True) |
| 37 | + |
| 38 | + def _upload_file(self, key, local_path): |
| 39 | + target_path = os.path.join(self.store_root, key) |
| 40 | + self._ensure_path_dirs_exist(target_path) |
| 41 | + shutil.copyfile(local_path, target_path) |
| 42 | + |
| 43 | + def _download_file(self, key, local_path, bucket=None): |
| 44 | + source_path = os.path.join(self.store_root, key) |
| 45 | + self._ensure_path_dirs_exist(local_path) |
| 46 | + shutil.copyfile(source_path, local_path) |
| 47 | + |
| 48 | + def _delete_file(self, key): |
| 49 | + os.remove(os.path.join(self.store_root, key)) |
| 50 | + |
| 51 | + def _get_file_url(self, key, method='GET'): |
| 52 | + return str(os.path.join(self.store_root, key)) |
| 53 | + |
| 54 | + def _get_file_post(self, key): |
| 55 | + return str(os.path.join(self.store_root, key)) |
| 56 | + |
| 57 | + def _get_file_timestamp(self, key): |
| 58 | + return None |
| 59 | + |
| 60 | + def get_qualified_location(self, key): |
| 61 | + return 'file:/' + self.store_root + '/' + key |
| 62 | + |
| 63 | + def get_bucket(self): |
| 64 | + return self.bucket |
0 commit comments