From 83025ff38dfea3d4e3f27dc045128a7aa4ba11e4 Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Fri, 24 Jul 2015 11:09:10 -0700 Subject: [PATCH] Use right most occurrence object dir in diskfile.extract_policy I like using the rightmost one more; it's basically /operator-defined/mountpoint/objects/part/suffix/hash/ts.data, so I don't see any opportunity for other things named "objects" to creep in on the right of the real objects-N dir; but I could see some admin using /srv/object-storage/ or something -- Torgomatic The Wise Change-Id: I0a63a3e02df091a5ee2e110a345183012e357a2f --- swift/cli/info.py | 7 +------ swift/obj/diskfile.py | 4 ++-- test/unit/cli/test_info.py | 3 +-- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/swift/cli/info.py b/swift/cli/info.py index dc4df6e2a7..eed4dea164 100644 --- a/swift/cli/info.py +++ b/swift/cli/info.py @@ -366,12 +366,7 @@ def print_obj(datafile, check_etag=True, swift_dir='/etc/swift', # try to extract policy index from datafile disk path fullpath = os.path.abspath(datafile) - try: - # obj_path should be device-relative path of an object - obj_path = fullpath[fullpath.rindex('objects'):] - except ValueError: - obj_path = fullpath - policy_index = int(extract_policy(obj_path) or POLICIES.legacy) + policy_index = int(extract_policy(fullpath) or POLICIES.legacy) try: if policy_index: diff --git a/swift/obj/diskfile.py b/swift/obj/diskfile.py index 2519e1dd5d..62f0187ba3 100644 --- a/swift/obj/diskfile.py +++ b/swift/obj/diskfile.py @@ -175,11 +175,11 @@ def extract_policy(obj_path): objects-5/179/485dc017205a81df3af616d917c90179/1401811134.873649.data - :param obj_path: device-relative path of an object + :param obj_path: device-relative path of an object, or the full path :returns: a :class:`~swift.common.storage_policy.BaseStoragePolicy` or None """ try: - obj_portion = obj_path[obj_path.index(DATADIR_BASE):] + obj_portion = obj_path[obj_path.rindex(DATADIR_BASE):] obj_dirname = obj_portion[:obj_portion.index('/')] except Exception: return None diff --git a/test/unit/cli/test_info.py b/test/unit/cli/test_info.py index 856a5681a9..c76b2ea6f1 100644 --- a/test/unit/cli/test_info.py +++ b/test/unit/cli/test_info.py @@ -399,9 +399,8 @@ class TestPrintObjFullMeta(TestCliInfoBase): os.chdir(hash_dir) with mock.patch('sys.stdout', out): print_obj(file_name, swift_dir=self.testdir) + finally: os.chdir(cwd) - except OSError: # Failure case of os.chdir - self.fail("Unexpected exception raised") self.assertTrue('X-Backend-Storage-Policy-Index: 1' in out.getvalue()) def test_print_obj_meta_and_ts_files(self):