<s> from __future__ import print_function <EOL> import itertools <EOL> import os <EOL> import sqlite3 <EOL> from hashlib import md5 <EOL> from six . moves import urllib <EOL> from swift . common . utils import hash_path , storage_directory , Timestamp <EOL> from swift . common . ring import Ring <EOL> from swift . common . request_helpers import is_sys_meta , is_user_meta , strip_sys_meta_prefix , strip_user_meta_prefix <EOL> from swift . account . backend import AccountBroker , DATADIR as ABDATADIR <EOL> from swift . container . backend import ContainerBroker , DATADIR as CBDATADIR <EOL> from swift . obj . diskfile import get_data_dir , read_metadata , DATADIR_BASE , extract_policy <EOL> from swift . common . storage_policy import POLICIES <EOL> class InfoSystemExit ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def print_ring_locations ( ring , datadir , account , container = None , obj = None , <EOL> tpart = None , all_nodes = False , policy_index = None ) : <EOL> """<STR_LIT>""" <EOL> if not ring : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not datadir : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if tpart is None and not account : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not account and ( container or obj ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if obj and not container : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if obj : <EOL> target = '<STR_LIT>' % ( account , container , obj ) <EOL> elif container : <EOL> target = '<STR_LIT>' % ( account , container ) <EOL> else : <EOL> target = '<STR_LIT:%s>' % ( account ) <EOL> if tpart : <EOL> part = int ( tpart ) <EOL> else : <EOL> part = ring . get_part ( account , container , obj ) <EOL> primary_nodes = ring . get_part_nodes ( part ) <EOL> handoff_nodes = ring . get_more_nodes ( part ) <EOL> if not all_nodes : <EOL> handoff_nodes = itertools . islice ( handoff_nodes , len ( primary_nodes ) ) <EOL> handoff_nodes = list ( handoff_nodes ) <EOL> if account and not tpart : <EOL> path_hash = hash_path ( account , container , obj ) <EOL> else : <EOL> path_hash = None <EOL> print ( '<STR_LIT>' % part ) <EOL> print ( '<STR_LIT>' % path_hash ) <EOL> for node in primary_nodes : <EOL> print ( '<STR_LIT>' % ( node [ '<STR_LIT>' ] , node [ '<STR_LIT:port>' ] , <EOL> node [ '<STR_LIT>' ] ) ) <EOL> for node in handoff_nodes : <EOL> print ( '<STR_LIT>' % ( <EOL> node [ '<STR_LIT>' ] , node [ '<STR_LIT:port>' ] , node [ '<STR_LIT>' ] ) ) <EOL> print ( "<STR_LIT:\n>" ) <EOL> for node in primary_nodes : <EOL> cmd = '<STR_LIT>' % ( node [ '<STR_LIT>' ] , node [ '<STR_LIT:port>' ] , node [ '<STR_LIT>' ] , part , <EOL> urllib . parse . quote ( target ) ) <EOL> if policy_index is not None : <EOL> cmd += '<STR_LIT>' % ( '<STR_LIT>' , <EOL> policy_index ) <EOL> print ( cmd ) <EOL> for node in handoff_nodes : <EOL> cmd = '<STR_LIT>' % ( node [ '<STR_LIT>' ] , node [ '<STR_LIT:port>' ] , node [ '<STR_LIT>' ] , part , <EOL> urllib . parse . quote ( target ) ) <EOL> if policy_index is not None : <EOL> cmd += '<STR_LIT>' % ( '<STR_LIT>' , <EOL> policy_index ) <EOL> cmd += '<STR_LIT>' <EOL> print ( cmd ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if path_hash : <EOL> for node in primary_nodes : <EOL> print ( '<STR_LIT>' % <EOL> ( node [ '<STR_LIT>' ] , node [ '<STR_LIT>' ] , <EOL> storage_directory ( datadir , part , path_hash ) ) ) <EOL> for node in handoff_nodes : <EOL> print ( '<STR_LIT>' % <EOL> ( node [ '<STR_LIT>' ] , node [ '<STR_LIT>' ] , <EOL> storage_directory ( datadir , part , path_hash ) ) ) <EOL> else : <EOL> for node in primary_nodes : <EOL> print ( '<STR_LIT>' % <EOL> ( node [ '<STR_LIT>' ] , node [ '<STR_LIT>' ] , datadir , part ) ) <EOL> for node in handoff_nodes : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( node [ '<STR_LIT>' ] , node [ '<STR_LIT>' ] , datadir , part ) ) <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def print_db_info_metadata ( db_type , info , metadata ) : <EOL> """<STR_LIT>""" <EOL> if info is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if db_type not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> try : <EOL> account = info [ '<STR_LIT>' ] <EOL> container = None <EOL> if db_type == '<STR_LIT>' : <EOL> container = info [ '<STR_LIT>' ] <EOL> path = '<STR_LIT>' % ( account , container ) <EOL> else : <EOL> path = '<STR_LIT>' % account <EOL> print ( '<STR_LIT>' % path ) <EOL> print ( '<STR_LIT>' % account ) <EOL> if db_type == '<STR_LIT>' : <EOL> print ( '<STR_LIT>' % container ) <EOL> path_hash = hash_path ( account , container ) <EOL> if db_type == '<STR_LIT>' : <EOL> print ( '<STR_LIT>' % path_hash ) <EOL> else : <EOL> print ( '<STR_LIT>' % path_hash ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % <EOL> ( Timestamp ( info [ '<STR_LIT>' ] ) . isoformat , <EOL> info [ '<STR_LIT>' ] ) ) <EOL> print ( '<STR_LIT>' % <EOL> ( Timestamp ( info [ '<STR_LIT>' ] ) . isoformat , <EOL> info [ '<STR_LIT>' ] ) ) <EOL> print ( '<STR_LIT>' % <EOL> ( Timestamp ( info [ '<STR_LIT>' ] ) . isoformat , <EOL> info [ '<STR_LIT>' ] ) ) <EOL> print ( '<STR_LIT>' % <EOL> ( Timestamp ( info [ '<STR_LIT>' ] ) . isoformat , <EOL> info [ '<STR_LIT>' ] ) ) <EOL> if db_type == '<STR_LIT>' : <EOL> print ( '<STR_LIT>' % info [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' % info [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' % info [ '<STR_LIT>' ] ) <EOL> if db_type == '<STR_LIT>' : <EOL> try : <EOL> policy_name = POLICIES [ info [ '<STR_LIT>' ] ] . name <EOL> except KeyError : <EOL> policy_name = '<STR_LIT>' <EOL> print ( '<STR_LIT>' % ( <EOL> policy_name , info [ '<STR_LIT>' ] ) ) <EOL> print ( '<STR_LIT>' % <EOL> ( Timestamp ( info [ '<STR_LIT>' ] ) . isoformat , <EOL> info [ '<STR_LIT>' ] ) ) <EOL> print ( '<STR_LIT>' % <EOL> ( Timestamp ( info [ '<STR_LIT>' ] ) . isoformat , <EOL> info [ '<STR_LIT>' ] ) ) <EOL> print ( '<STR_LIT>' % <EOL> info [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' % info [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' % info [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' % info [ '<STR_LIT:id>' ] ) <EOL> except KeyError as e : <EOL> raise ValueError ( '<STR_LIT>' % e ) <EOL> meta_prefix = '<STR_LIT>' + db_type + '<STR_LIT:_>' <EOL> for key , value in info . items ( ) : <EOL> if key . lower ( ) . startswith ( meta_prefix ) : <EOL> title = key . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) . title ( ) <EOL> print ( '<STR_LIT>' % ( title , value ) ) <EOL> user_metadata = { } <EOL> sys_metadata = { } <EOL> for key , ( value , timestamp ) in metadata . items ( ) : <EOL> if is_user_meta ( db_type , key ) : <EOL> user_metadata [ strip_user_meta_prefix ( db_type , key ) ] = value <EOL> elif is_sys_meta ( db_type , key ) : <EOL> sys_metadata [ strip_sys_meta_prefix ( db_type , key ) ] = value <EOL> else : <EOL> title = key . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) . title ( ) <EOL> print ( '<STR_LIT>' % ( title , value ) ) <EOL> if sys_metadata : <EOL> print ( '<STR_LIT>' % sys_metadata ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> if user_metadata : <EOL> print ( '<STR_LIT>' % user_metadata ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> def print_obj_metadata ( metadata ) : <EOL> """<STR_LIT>""" <EOL> user_metadata = { } <EOL> sys_metadata = { } <EOL> other_metadata = { } <EOL> if not metadata : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> path = metadata . pop ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> content_type = metadata . pop ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> ts = Timestamp ( metadata . pop ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> account = container = obj = obj_hash = None <EOL> if path : <EOL> try : <EOL> account , container , obj = path . split ( '<STR_LIT:/>' , <NUM_LIT:3> ) [ <NUM_LIT:1> : ] <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' % path ) <EOL> else : <EOL> obj_hash = hash_path ( account , container , obj ) <EOL> print ( '<STR_LIT>' % path ) <EOL> print ( '<STR_LIT>' % account ) <EOL> print ( '<STR_LIT>' % container ) <EOL> print ( '<STR_LIT>' % obj ) <EOL> print ( '<STR_LIT>' % obj_hash ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> if content_type : <EOL> print ( '<STR_LIT>' % content_type ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> if ts : <EOL> print ( '<STR_LIT>' % ( ts . isoformat , ts . internal ) ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> for key , value in metadata . items ( ) : <EOL> if is_user_meta ( '<STR_LIT>' , key ) : <EOL> user_metadata [ key ] = value <EOL> elif is_sys_meta ( '<STR_LIT>' , key ) : <EOL> sys_metadata [ key ] = value <EOL> else : <EOL> other_metadata [ key ] = value <EOL> def print_metadata ( title , items ) : <EOL> print ( title ) <EOL> if items : <EOL> for meta_key in sorted ( items ) : <EOL> print ( '<STR_LIT>' % ( meta_key , items [ meta_key ] ) ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> print_metadata ( '<STR_LIT>' , sys_metadata ) <EOL> print_metadata ( '<STR_LIT>' , user_metadata ) <EOL> print_metadata ( '<STR_LIT>' , other_metadata ) <EOL> def print_info ( db_type , db_file , swift_dir = '<STR_LIT>' , stale_reads_ok = False ) : <EOL> if db_type not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" ) <EOL> raise InfoSystemExit ( ) <EOL> if not os . path . exists ( db_file ) or not db_file . endswith ( '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" ) <EOL> raise InfoSystemExit ( ) <EOL> if not db_file . startswith ( ( '<STR_LIT:/>' , '<STR_LIT>' ) ) : <EOL> db_file = '<STR_LIT>' + db_file <EOL> if db_type == '<STR_LIT>' : <EOL> broker = AccountBroker ( db_file , stale_reads_ok = stale_reads_ok ) <EOL> datadir = ABDATADIR <EOL> else : <EOL> broker = ContainerBroker ( db_file , stale_reads_ok = stale_reads_ok ) <EOL> datadir = CBDATADIR <EOL> try : <EOL> info = broker . get_info ( ) <EOL> except sqlite3 . OperationalError as err : <EOL> if '<STR_LIT>' in str ( err ) : <EOL> print ( "<STR_LIT>" <EOL> % ( db_type , db_file ) ) <EOL> raise InfoSystemExit ( ) <EOL> raise <EOL> account = info [ '<STR_LIT>' ] <EOL> container = info [ '<STR_LIT>' ] if db_type == '<STR_LIT>' else None <EOL> print_db_info_metadata ( db_type , info , broker . metadata ) <EOL> try : <EOL> ring = Ring ( swift_dir , ring_name = db_type ) <EOL> except Exception : <EOL> ring = None <EOL> else : <EOL> print_ring_locations ( ring , datadir , account , container ) <EOL> def print_obj ( datafile , check_etag = True , swift_dir = '<STR_LIT>' , <EOL> policy_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( datafile ) : <EOL> print ( "<STR_LIT>" ) <EOL> raise InfoSystemExit ( ) <EOL> if not datafile . startswith ( ( '<STR_LIT:/>' , '<STR_LIT>' ) ) : <EOL> datafile = '<STR_LIT>' + datafile <EOL> policy_index = None <EOL> ring = None <EOL> datadir = DATADIR_BASE <EOL> fullpath = os . path . abspath ( datafile ) <EOL> policy_index = int ( extract_policy ( fullpath ) or POLICIES . legacy ) <EOL> try : <EOL> if policy_index : <EOL> datadir += '<STR_LIT:->' + str ( policy_index ) <EOL> ring = Ring ( swift_dir , ring_name = '<STR_LIT>' + str ( policy_index ) ) <EOL> elif policy_index == <NUM_LIT:0> : <EOL> ring = Ring ( swift_dir , ring_name = '<STR_LIT:object>' ) <EOL> except IOError : <EOL> pass <EOL> if policy_name : <EOL> policy = POLICIES . get_by_name ( policy_name ) <EOL> if policy : <EOL> policy_index_for_name = policy . idx <EOL> if ( policy_index is not None and <EOL> policy_index_for_name is not None and <EOL> policy_index != policy_index_for_name ) : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> if not ring and policy_index_for_name : <EOL> ring = POLICIES . get_object_ring ( policy_index_for_name , <EOL> swift_dir ) <EOL> datadir = get_data_dir ( policy_index_for_name ) <EOL> with open ( datafile , '<STR_LIT:rb>' ) as fp : <EOL> try : <EOL> metadata = read_metadata ( fp ) <EOL> except EOFError : <EOL> print ( "<STR_LIT>" ) <EOL> raise InfoSystemExit ( ) <EOL> etag = metadata . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> length = metadata . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> path = metadata . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> print_obj_metadata ( metadata ) <EOL> file_len = None <EOL> if check_etag : <EOL> h = md5 ( ) <EOL> file_len = <NUM_LIT:0> <EOL> while True : <EOL> data = fp . read ( <NUM_LIT:64> * <NUM_LIT> ) <EOL> if not data : <EOL> break <EOL> h . update ( data ) <EOL> file_len += len ( data ) <EOL> h = h . hexdigest ( ) <EOL> if etag : <EOL> if h == etag : <EOL> print ( '<STR_LIT>' % etag ) <EOL> else : <EOL> print ( "<STR_LIT>" % <EOL> ( etag , h ) ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> print ( '<STR_LIT>' % etag ) <EOL> file_len = os . fstat ( fp . fileno ( ) ) . st_size <EOL> if length : <EOL> if file_len == int ( length ) : <EOL> print ( '<STR_LIT>' % length ) <EOL> else : <EOL> print ( "<STR_LIT>" <EOL> % ( length , file_len ) ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> account , container , obj = path . split ( '<STR_LIT:/>' , <NUM_LIT:3> ) [ <NUM_LIT:1> : ] <EOL> if ring : <EOL> print_ring_locations ( ring , datadir , account , container , obj , <EOL> policy_index = policy_index ) <EOL> def print_item_locations ( ring , ring_name = None , account = None , container = None , <EOL> obj = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> policy_name = kwargs . get ( '<STR_LIT>' , None ) <EOL> part = kwargs . get ( '<STR_LIT>' , None ) <EOL> all_nodes = kwargs . get ( '<STR_LIT:all>' , False ) <EOL> swift_dir = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ring and policy_name : <EOL> policy = POLICIES . get_by_name ( policy_name ) <EOL> if policy : <EOL> if ring_name != policy . ring_name : <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> print ( '<STR_LIT>' % policy_name ) <EOL> policy_index = None <EOL> if ring is None and ( obj or part ) : <EOL> if not policy_name : <EOL> print ( '<STR_LIT>' ) <EOL> raise InfoSystemExit ( ) <EOL> policy = POLICIES . get_by_name ( policy_name ) <EOL> if not policy : <EOL> print ( '<STR_LIT>' % policy_name ) <EOL> raise InfoSystemExit ( ) <EOL> policy_index = int ( policy ) <EOL> ring = POLICIES . get_object_ring ( policy_index , swift_dir ) <EOL> ring_name = ( POLICIES . get_by_name ( policy_name ) ) . ring_name <EOL> if account is None and ( container is not None or obj is not None ) : <EOL> print ( '<STR_LIT>' ) <EOL> raise InfoSystemExit ( ) <EOL> if container is None and obj is not None : <EOL> print ( '<STR_LIT>' ) <EOL> raise InfoSystemExit ( ) <EOL> if account is None and part is None : <EOL> print ( '<STR_LIT>' ) <EOL> raise InfoSystemExit ( ) <EOL> loc = '<STR_LIT>' <EOL> if part and ring_name : <EOL> if '<STR_LIT:->' in ring_name and ring_name . startswith ( '<STR_LIT:object>' ) : <EOL> loc = '<STR_LIT>' + ring_name . split ( '<STR_LIT:->' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> else : <EOL> loc = ring_name + '<STR_LIT:s>' <EOL> if account and container and obj : <EOL> loc = '<STR_LIT>' <EOL> if '<STR_LIT:->' in ring_name and ring_name . startswith ( '<STR_LIT:object>' ) : <EOL> policy_index = int ( ring_name . rsplit ( '<STR_LIT:->' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] ) <EOL> loc = '<STR_LIT>' % policy_index <EOL> if account and container and not obj : <EOL> loc = '<STR_LIT>' <EOL> if not any ( [ ring , ring_name ] ) : <EOL> ring = Ring ( swift_dir , ring_name = '<STR_LIT>' ) <EOL> else : <EOL> if ring_name != '<STR_LIT>' : <EOL> print ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> if account and not container and not obj : <EOL> loc = '<STR_LIT>' <EOL> if not any ( [ ring , ring_name ] ) : <EOL> ring = Ring ( swift_dir , ring_name = '<STR_LIT>' ) <EOL> else : <EOL> if ring_name != '<STR_LIT>' : <EOL> print ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % account ) <EOL> print ( '<STR_LIT>' % container ) <EOL> print ( '<STR_LIT>' % obj ) <EOL> print_ring_locations ( ring , loc , account , container , obj , part , all_nodes , <EOL> policy_index = policy_index ) </s>
