<s> '''<STR_LIT>''' <EOL> import sys , os <EOL> import logging <EOL> from apiclient . http import MediaFileUpload <EOL> from oauth2client . anyjson import simplejson <EOL> from abstract_client import MANIFEST , SCRIPT_TYPE <EOL> from da_client import DAClient <EOL> class DAScript ( DAClient ) : <EOL> def __init__ ( self , basedir , folder , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> super ( DAScript , self ) . __init__ ( basedir , ** kwargs ) <EOL> self . folder = folder <EOL> def upload ( self , id , name , create = False ) : <EOL> logger = logging . getLogger ( ) <EOL> foldername = os . path . join ( self . folder , name ) <EOL> logger . info ( '<STR_LIT>' % foldername ) <EOL> manifest_path = os . path . join ( foldername , MANIFEST ) <EOL> mfile = open ( manifest_path , '<STR_LIT:rb>' ) <EOL> data = simplejson . loads ( mfile . read ( ) ) <EOL> mfile . close ( ) <EOL> for i , fileInProject in enumerate ( data [ '<STR_LIT>' ] ) : <EOL> extension = '<STR_LIT>' <EOL> if fileInProject [ '<STR_LIT:type>' ] == '<STR_LIT>' : extension = '<STR_LIT>' <EOL> filename = '<STR_LIT>' % ( fileInProject [ '<STR_LIT:name>' ] , extension ) <EOL> logger . info ( '<STR_LIT>' % ( i , filename ) ) <EOL> f = open ( os . path . join ( foldername , filename ) , '<STR_LIT:rb>' ) <EOL> fileInProject [ '<STR_LIT:source>' ] = f . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> f . close ( ) <EOL> logger . info ( '<STR_LIT>' % MANIFEST ) <EOL> mfile = open ( manifest_path , '<STR_LIT:wb>' ) <EOL> mfile . write ( simplejson . dumps ( data ) ) <EOL> mfile . close ( ) <EOL> mbody = MediaFileUpload ( manifest_path , mimetype = SCRIPT_TYPE , resumable = True ) <EOL> if create : <EOL> body = { '<STR_LIT:title>' : name , '<STR_LIT>' : SCRIPT_TYPE , '<STR_LIT:description>' : name } <EOL> fileobj = self . service . files ( ) . insert ( <EOL> body = body , media_body = mbody ) . execute ( ) <EOL> id = fileobj [ '<STR_LIT:id>' ] <EOL> download_url = fileobj [ '<STR_LIT>' ] [ SCRIPT_TYPE ] <EOL> resp , content = self . service . _http . request ( download_url ) <EOL> if resp . status != <NUM_LIT:200> : raise Exception ( '<STR_LIT>' % resp ) <EOL> logger . info ( '<STR_LIT>' % MANIFEST ) <EOL> mfile = open ( manifest_path , '<STR_LIT:wb>' ) <EOL> mfile . write ( content ) <EOL> mfile . close ( ) <EOL> else : <EOL> body = { '<STR_LIT>' : SCRIPT_TYPE } <EOL> fileobj = self . service . files ( ) . update ( <EOL> fileId = id , body = body , media_body = mbody ) . execute ( ) <EOL> return ( id , fileobj ) <EOL> def download ( self , id ) : <EOL> logger = logging . getLogger ( ) <EOL> fileobj = self . service . files ( ) . get ( fileId = id ) . execute ( ) <EOL> download_url = fileobj [ '<STR_LIT>' ] [ SCRIPT_TYPE ] <EOL> resp , content = self . service . _http . request ( download_url ) <EOL> if resp . status != <NUM_LIT:200> : raise Exception ( '<STR_LIT>' % resp ) <EOL> data = simplejson . loads ( content ) <EOL> foldername = os . path . join ( self . folder , fileobj [ '<STR_LIT:title>' ] ) <EOL> logger . info ( '<STR_LIT>' % foldername ) <EOL> if not os . path . exists ( foldername ) : os . makedirs ( foldername ) <EOL> for the_file in os . listdir ( foldername ) : <EOL> file_path = os . path . join ( foldername , the_file ) <EOL> try : <EOL> if os . path . isfile ( file_path ) : os . unlink ( file_path ) <EOL> except ( Exception , ) , e : <EOL> print e <EOL> manifest_path = os . path . join ( foldername , MANIFEST ) <EOL> logger . info ( '<STR_LIT>' % MANIFEST ) <EOL> mfile = open ( manifest_path , '<STR_LIT:wb>' ) <EOL> mfile . write ( content ) <EOL> mfile . close ( ) <EOL> for i , fileInProject in enumerate ( data [ '<STR_LIT>' ] ) : <EOL> extension = '<STR_LIT>' <EOL> if fileInProject [ '<STR_LIT:type>' ] == '<STR_LIT>' : extension = '<STR_LIT>' <EOL> filename = '<STR_LIT>' % ( fileInProject [ '<STR_LIT:name>' ] , extension ) <EOL> logger . info ( '<STR_LIT>' % ( i , filename ) ) <EOL> f = open ( os . path . join ( foldername , filename ) , '<STR_LIT:wb>' ) <EOL> f . write ( fileInProject [ '<STR_LIT:source>' ] . encode ( '<STR_LIT:utf-8>' ) ) <EOL> f . close ( ) <EOL> return ( id , None ) </s>
