src/extras/fileserver/fctool
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 #!/usr/bin/env python
       
     2 #
       
     3 # A simple command line client for the simple file transfer server
       
     4 # for Series 60 Python environment.
       
     5 #     
       
     6 # Copyright (c) 2005 Nokia Corporation
       
     7 #
       
     8 # Licensed under the Apache License, Version 2.0 (the "License");
       
     9 # you may not use this file except in compliance with the License.
       
    10 # You may obtain a copy of the License at
       
    11 #
       
    12 #     http://www.apache.org/licenses/LICENSE-2.0
       
    13 #
       
    14 # Unless required by applicable law or agreed to in writing, software
       
    15 # distributed under the License is distributed on an "AS IS" BASIS,
       
    16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    17 # See the License for the specific language governing permissions and
       
    18 # limitations under the License.
       
    19 
       
    20 import sys
       
    21 import os
       
    22 import fileclient
       
    23 import binascii
       
    24 
       
    25 color=1
       
    26 
       
    27 if color:
       
    28     colormap={'red': '\033[31;1m',
       
    29               'green': '\033[32;1m',
       
    30               'blue': '\033[34;1m',
       
    31               'yellow': '\033[33;1m',
       
    32               'cyan': '\033[36;1m',
       
    33               'magenta': '\033[35;1m',
       
    34               'white': '\033[37;1m',
       
    35               }
       
    36     coloroff='\033[0m'
       
    37     def colorize(color,text):
       
    38         return colormap[color]+text+coloroff
       
    39 else:
       
    40     def colorize(color,text):
       
    41         return text
       
    42     
       
    43 def cmd_download(client,remotefile,localfile=None):
       
    44     client.download(remotefile,localfile)
       
    45     print "Downloaded file from "+remotefile+"."
       
    46 
       
    47 def cmd_upload(client,remotefile,localfile=None):
       
    48     client.upload(remotefile,localfile)
       
    49     print "Uploaded file to "+remotefile+"."
       
    50 
       
    51 def cmd_get(client,remotefile):
       
    52     print "File contents: "+client.get(remotefile)
       
    53 
       
    54 def cmd_put(client,remotefile,content):
       
    55     client.put(remotefile,content)
       
    56     print "Data uploaded to file "+remotefile
       
    57 
       
    58 def cmd_eval(client,expression):
       
    59     print "Result: "+repr(client.eval(expression))
       
    60 
       
    61 def cmd_exec(client,*expression):
       
    62     client.exec_(' '.join(expression))
       
    63 
       
    64 def cmd_killserver(client):
       
    65     client.killserver()
       
    66     print "Sent quit command to server."
       
    67 
       
    68 def syncfiles(client,files):
       
    69     """Update the given files on the remote host to the same
       
    70     versions as on the local filesystem. The argument files is
       
    71     a list of (localfile,remotefile) tuples."""
       
    72     print "Obtaining remote checksums..."
       
    73     #client.exec_('import sync')
       
    74     (localfiles,remotefiles)=zip(*files)
       
    75     remotesums=client.eval('file_checksums('+repr(remotefiles)+')')
       
    76     print "Remote checksums: "+repr(remotesums)
       
    77     localsums=map(file_checksum,localfiles)
       
    78     for ((localfile,remotefile),localsum) in zip(files,localsums):
       
    79         if localsum != remotesums[remotefile]:
       
    80             print colorize('green',"Checksum mismatch for file "+remotefile+" - uploading...")
       
    81             client.upload(remotefile,localfile)
       
    82         else:
       
    83             print colorize('white',"Checksum of "+remotefile+" matches - not uploading.")
       
    84 
       
    85 def cmd_syncdir(client,remotedir,localdir):
       
    86     files=filter(lambda x: os.path.isfile(os.path.join(localdir,x)),os.listdir(localdir))
       
    87     print "Synchronizing files: "+' '.join(files)
       
    88     filelist=[(os.path.join(localdir,x),
       
    89                os.path.join(remotedir,x)) for x in files]
       
    90     syncfiles(client,filelist)
       
    91 
       
    92 def cmd_syncfiles(client,remotedir,*localfiles):
       
    93     localfiles=filter(os.path.isfile,localfiles)
       
    94     files=[]
       
    95     for k in localfiles:
       
    96         files.append((k,os.path.join(remotedir,os.path.basename(k))))
       
    97     syncfiles(client,files)
       
    98 def cmd_ping(client):
       
    99     print "Probing server..."
       
   100     result=client.eval('2+2')
       
   101     if result==4:
       
   102         print "The server is alive and well."
       
   103     else:
       
   104         print "The server is down."
       
   105 def file_checksum(filename):
       
   106     f=open(filename,'rb')
       
   107     checksum = binascii.crc32(f.read())
       
   108     f.close()
       
   109     return checksum
       
   110 
       
   111 if len(sys.argv)<2:
       
   112     print "Usage: fctool [-v] command [args]"
       
   113     sys.exit()
       
   114 
       
   115 if sys.argv[1]=='-v':
       
   116     verbose=1
       
   117     del sys.argv[1]
       
   118 else:
       
   119     verbose=0
       
   120 
       
   121 cmd=sys.argv[1]
       
   122 args=sys.argv[2:]
       
   123 
       
   124 try:
       
   125     cmdfunc=globals()['cmd_'+cmd]
       
   126 except:
       
   127     raise "Unknown command "+cmd
       
   128 
       
   129 #print "Executing command: "+cmd+str(tuple(args))
       
   130 
       
   131 client=fileclient.connect('/dev/ttyS4',verbose)
       
   132 
       
   133 cmdfunc(client,*args)