From f231b801ccb87fa2ffe8bdc47f442455860cb0f9 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Thu, 30 Jun 2016 02:48:45 +0300 Subject: [PATCH] brag: Replace dangerous uses of `is` operator It is an implementation detail that `is` happens to behave the same as `==` for low integers Signed-off-by: Oleh Prypin --- src/brag/client/ceph-brag | 26 +++++++++---------- src/brag/server/ceph_brag/controllers/root.py | 6 ++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/brag/client/ceph-brag b/src/brag/client/ceph-brag index 006f076c5c5..86f8ca8c8b8 100755 --- a/src/brag/client/ceph-brag +++ b/src/brag/client/ceph-brag @@ -211,12 +211,12 @@ def run_command(cmd): def get_uuid(): (rc,uid,e) = run_command(['ceph', 'config-key', 'get', CLUSTER_UUID_NAME]) - if rc is not 0: + if rc: #uuid is not yet set. uid = str(uuid.uuid4()) (rc, o, e) = run_command(['ceph', 'config-key', 'put', CLUSTER_UUID_NAME, uid]) - if rc is not 0: + if rc: raise RuntimeError("\'ceph config-key put\' failed -" + e) return uid @@ -239,7 +239,7 @@ def bytes_pretty_to_raw(byte_count, byte_scale): def get_nums(): (rc, o, e) = run_command(['ceph', '-s', '-f', 'json']) - if rc is not 0: + if rc: raise RuntimeError("\'ceph -s\' failed - " + e) oj = json.loads(o) @@ -256,7 +256,7 @@ def get_nums(): num_bytes_total = pgmap['bytes_total'] (rc, o, e) = run_command(['ceph', 'pg', 'dump', 'pools', '-f', 'json-pretty']) - if rc is not 0: + if rc: raise RuntimeError("\'ceph pg dump pools\' failed - " + e) pools = json.loads(o) @@ -277,7 +277,7 @@ def get_nums(): def get_crush_types(): (rc, o, e) = run_command(['ceph', 'osd', 'crush', 'dump']) - if rc is not 0: + if rc: raise RuntimeError("\'ceph osd crush dump\' failed - " + e) crush_dump = json.loads(o) @@ -306,7 +306,7 @@ def get_crush_types(): def get_osd_dump_info(): (rc, o, e) = run_command(['ceph', 'osd', 'dump', '-f', 'json']) - if rc is not 0: + if rc: raise RuntimeError("\'ceph osd dump\' failed - " + e) pool_meta = [] @@ -332,8 +332,8 @@ def get_sysinfo(max_osds): incr = lambda a,k: 1 if k not in a else a[k]+1 while count < max_osds: (rc, o, e) = run_command(['ceph', 'osd', 'metadata', str(count)]) - if rc is 0: - if osd_metadata_available is False: + if rc == 0: + if not osd_metadata_available: osd_metadata_available = True jmeta = json.loads(o) @@ -365,7 +365,7 @@ def get_sysinfo(max_osds): count = count + 1 sysinfo = {} - if osd_metadata_available is False: + if not osd_metadata_available: print >> sys.stderr, "'ceph osd metadata' is not available at all" return sysinfo @@ -387,7 +387,7 @@ def get_sysinfo(max_osds): def get_ownership_info(): (rc, o, e) = run_command(['ceph', 'config-key', 'get', CLUSTER_OWNERSHIP_NAME]) - if rc is not 0: + if rc: return {} return ast.literal_eval(o) @@ -478,7 +478,7 @@ def publish(): if verbose: print "PUT " + str(url) + " : " + str(data) req = requests.put(url, data=data) - if req.status_code is not 201: + if req.status_code != 201: print >> sys.stderr, "Failed to publish, server responded with code " + str(req.status_code) print >> sys.stderr, req.text return 1 @@ -507,7 +507,7 @@ def unpublish(): params = {'uuid':uuid} req = requests.delete(url, params=params) - if req.status_code is not 200: + if req.status_code != 200: print >> sys.stderr, "Failed to unpublish, server responsed with code " + str(req.status_code) return 1 @@ -518,7 +518,7 @@ def main(): global verbose verbose = True sys.argv.pop(1) - if len(sys.argv) is 1: + if len(sys.argv) == 1: print output_json()[0] return 0 if sys.argv[1] == 'update-metadata': diff --git a/src/brag/server/ceph_brag/controllers/root.py b/src/brag/server/ceph_brag/controllers/root.py index a52f7f97115..6ca46c8c081 100644 --- a/src/brag/server/ceph_brag/controllers/root.py +++ b/src/brag/server/ceph_brag/controllers/root.py @@ -11,13 +11,13 @@ class RootController(RestController): @expose('json') def get(self, *args, **kwargs): - if len(args) is 0: + if len(args) == 0: #return the list of uuids try: result = db.get_uuids() except Exception as e: return self.fail(500, msg="Internal Server Error") - elif len(args) is 1 or len(args) is 2 and args[1] == '': + elif len(args) == 1 or len(args) == 2 and args[1] == '': #/uuid try: result = db.get_versions(args[0]) @@ -26,7 +26,7 @@ class RootController(RestController): if result is None: return self.fail(400, msg="Invalid UUID") - elif len(args) is 2 or len(args) is 3 and args[2] == '': + elif len(args) == 2 or len(args) == 3 and args[2] == '': #/uuid/version_number try: result = db.get_brag(args[0], args[1])