<s> """<STR_LIT>""" <EOL> import collections <EOL> import stat <EOL> import struct <EOL> import sys <EOL> import time <EOL> from openhtf . plugs . usb import adb_message <EOL> from openhtf . plugs . usb import usb_exceptions <EOL> DEFAULT_PUSH_MODE = stat . S_IFREG | stat . S_IRWXU | stat . S_IRWXG | stat . S_IRWXO <EOL> MAX_PUSH_DATA_BYTES = <NUM_LIT:64> * <NUM_LIT> <EOL> DeviceFileStat = collections . namedtuple ( '<STR_LIT>' , [ <EOL> '<STR_LIT:filename>' , '<STR_LIT>' , '<STR_LIT:size>' , '<STR_LIT>' ] ) <EOL> def _make_message_type ( name , attributes , has_data = True ) : <EOL> """<STR_LIT>""" <EOL> def AssertCommandIs ( self , command ) : <EOL> """<STR_LIT>""" <EOL> if self . command != command : <EOL> raise usb_exceptions . AdbProtocolError ( <EOL> '<STR_LIT>' , command , self ) <EOL> return type ( name , ( collections . namedtuple ( name , attributes ) , ) , <EOL> { <EOL> '<STR_LIT>' : AssertCommandIs , <EOL> '<STR_LIT>' : has_data , <EOL> '<STR_LIT>' : '<STR_LIT>' % len ( attributes . split ( ) ) , <EOL> } ) <EOL> class FilesyncService ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def __del__ ( self ) : <EOL> self . Close ( ) <EOL> def Close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stream . Close ( ) <EOL> def Stat ( self , filename , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> transport = StatFilesyncTransport ( self . stream ) <EOL> transport . WriteData ( '<STR_LIT>' , filename , timeout ) <EOL> stat_msg = transport . ReadMessage ( timeout ) <EOL> stat_msg . AssertCommandIs ( '<STR_LIT>' ) <EOL> return DeviceFileStat ( filename , stat_msg . mode , stat_msg . size , stat_msg . time ) <EOL> def List ( self , path , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> transport = DentFilesyncTransport ( self . stream ) <EOL> transport . WriteData ( '<STR_LIT>' , path , timeout ) <EOL> return ( DeviceFileStat ( dent_msg . name , dent_msg . mode , <EOL> dent_msg . size , dent_msg . time ) for dent_msg in <EOL> transport . ReadUntilDone ( '<STR_LIT>' , timeout ) ) <EOL> def Recv ( self , filename , dest_file , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> transport = DataFilesyncTransport ( self . stream ) <EOL> transport . WriteData ( '<STR_LIT>' , filename , timeout ) <EOL> for data_msg in transport . ReadUntilDone ( '<STR_LIT>' , timeout ) : <EOL> dest_file . write ( data_msg . data ) <EOL> def _CheckForFailMessage ( self , transport , exc_info , timeout ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> transport . ReadMessage ( timeout ) <EOL> except usb_exceptions . CommonUsbError : <EOL> if sys . exc_info ( ) [ <NUM_LIT:0> ] is usb_exceptions . AdbRemoteError : <EOL> raise <EOL> raise exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] <EOL> def Send ( self , src_file , filename , st_mode = DEFAULT_PUSH_MODE , mtime = None , <EOL> timeout = None ) : <EOL> """<STR_LIT>""" <EOL> transport = DataFilesyncTransport ( self . stream ) <EOL> transport . WriteData ( '<STR_LIT>' , '<STR_LIT>' % ( filename , st_mode ) , timeout ) <EOL> try : <EOL> while True : <EOL> data = src_file . read ( MAX_PUSH_DATA_BYTES ) <EOL> if not data : <EOL> break <EOL> transport . WriteData ( '<STR_LIT>' , data , timeout ) <EOL> mtime = mtime or int ( time . time ( ) ) <EOL> transport . WriteMessage ( <EOL> FilesyncMessageTypes . DoneMessage ( '<STR_LIT>' , mtime ) , timeout ) <EOL> except usb_exceptions . AdbStreamClosedError : <EOL> self . _CheckForFailMessage ( transport , sys . exc_info ( ) , timeout ) <EOL> data_msg = transport . ReadMessage ( timeout ) <EOL> data_msg . AssertCommandIs ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def UsingConnection ( cls , connection , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return cls ( connection . OpenStream ( '<STR_LIT>' , timeout ) ) <EOL> class AbstractFilesyncTransport ( object ) : <EOL> """<STR_LIT>""" <EOL> CMD_TO_WIRE , WIRE_TO_CMD = adb_message . make_wire_commands ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> def __init__ ( self , stream ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( self , '<STR_LIT>' ) , '<STR_LIT>' <EOL> assert hasattr ( self , '<STR_LIT>' ) , '<STR_LIT>' <EOL> self . stream = stream <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . stream , <EOL> id ( self ) , <EOL> self . RECV_MSG_TYPE . __name__ ) <EOL> __repr__ = __str__ <EOL> def WriteData ( self , command , data , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> self . WriteMessage ( FilesyncMessageTypes . DataMessage ( command , data ) , timeout ) <EOL> def WriteMessage ( self , msg , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> replace_dict = { '<STR_LIT>' : self . CMD_TO_WIRE [ msg . command ] } <EOL> if msg . has_data : <EOL> data = msg [ - <NUM_LIT:1> ] <EOL> replace_dict [ msg . _fields [ - <NUM_LIT:1> ] ] = len ( data ) <EOL> self . stream . Write ( struct . pack ( msg . struct_format , <EOL> * msg . _replace ( ** replace_dict ) ) , timeout ) <EOL> if msg . has_data : <EOL> self . stream . Write ( data , timeout ) <EOL> def ReadUntilDone ( self , command , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> message = self . ReadMessage ( timeout ) <EOL> while message . command != '<STR_LIT>' : <EOL> message . AssertCommandIs ( command ) <EOL> yield message <EOL> message = self . ReadMessage ( timeout ) <EOL> def ReadMessage ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> raw_data = self . stream . Read ( <EOL> struct . calcsize ( self . RECV_MSG_TYPE . struct_format ) , timeout ) <EOL> try : <EOL> raw_message = struct . unpack ( self . RECV_MSG_TYPE . struct_format , raw_data ) <EOL> except struct . error : <EOL> raise usb_exceptions . AdbProtocolError ( <EOL> '<STR_LIT>' , self , <EOL> self . RECV_MSG_TYPE . struct_format , raw_data ) <EOL> if raw_message [ <NUM_LIT:0> ] not in self . WIRE_TO_CMD : <EOL> raise usb_exceptions . AdbProtocolError ( <EOL> '<STR_LIT>' , raw_message ) <EOL> raw_message = ( self . WIRE_TO_CMD [ raw_message [ <NUM_LIT:0> ] ] , ) + raw_message [ <NUM_LIT:1> : ] <EOL> if self . RECV_MSG_TYPE . has_data and raw_message [ - <NUM_LIT:1> ] : <EOL> data_len = raw_message [ - <NUM_LIT:1> ] <EOL> raw_message = raw_message [ : - <NUM_LIT:1> ] + ( self . stream . Read ( data_len , timeout ) , ) <EOL> if raw_message [ <NUM_LIT:0> ] not in self . VALID_RESPONSES : <EOL> raise usb_exceptions . AdbProtocolError ( <EOL> '<STR_LIT>' , raw_message [ <NUM_LIT:0> ] , self ) <EOL> if raw_message [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> raise usb_exceptions . AdbRemoteError ( <EOL> '<STR_LIT>' , raw_message ) <EOL> return self . RECV_MSG_TYPE ( * raw_message ) <EOL> class FilesyncMessageTypes ( object ) : <EOL> """<STR_LIT>""" <EOL> DoneMessage = _make_message_type ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> has_data = False ) <EOL> StatMessage = _make_message_type ( '<STR_LIT>' , '<STR_LIT>' , <EOL> has_data = False ) <EOL> DentMessage = _make_message_type ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> DataMessage = _make_message_type ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class StatFilesyncTransport ( AbstractFilesyncTransport ) : <EOL> """<STR_LIT>""" <EOL> RECV_MSG_TYPE = FilesyncMessageTypes . StatMessage <EOL> VALID_RESPONSES = ( '<STR_LIT>' , ) <EOL> class DentFilesyncTransport ( AbstractFilesyncTransport ) : <EOL> """<STR_LIT>""" <EOL> RECV_MSG_TYPE = FilesyncMessageTypes . DentMessage <EOL> VALID_RESPONSES = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class DataFilesyncTransport ( AbstractFilesyncTransport ) : <EOL> """<STR_LIT>""" <EOL> RECV_MSG_TYPE = FilesyncMessageTypes . DataMessage <EOL> VALID_RESPONSES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) </s>
