#!/usr/bin/python
#Filename : list_file.py
import ftplib
import os
import re #zheng ze biao da shi
import max #self defined
import download
file_pool = {}
file_host = ''
file_path = ''
#get server and path
try:
try:
f_path = file('config\path')
except:
print "***** Could not open the file of information."
exit(0)
while True:
line = f_path.readline()
if len(line) == 0:
break
else:
if line.startswith('host'):
file_host = line[5:-1]
elif line.startswith('path'):
file_path = line[5:-1]
finally:
f_path.close()
#get program's name and version
try:
try:
f_information = file('config\information')
except:
print "***** Could not open the file of information."
exit(0)
all_line = f_information.readlines()
file_name = ''
file_version = ''
for each_line in all_line:
if each_line.startswith('name'):
file_name = each_line[5:-1]
if each_line.startswith('version'):
file_version = each_line[8:-1]
file_pool[file_name] = file_version
finally:
f_information.close()
#to connect to ftp server
print '***** Begin to connect to host %s ...\n' % file_host
try:
ftp = ftplib.FTP(file_host,'dongjunjun@jan.tech','jakjmax')
except ftplib.error_perm:
print "\n***** Could not connect to FTP server.\n"
ftp.quit()
exit(0)
print '********************************************************************************\n'
print ftp.getwelcome()
print '\n'
print '********************************************************************************\n'
try:
ftp.cwd(file_path)
except ftplib.error_perm:
print 'ERROR:cannot CD to "%s"\n' % file_path
print ' ***** Changed to %s\n' % file_path
#enter directory of file_name
for fn,fv in file_pool.items():
now_dir = ftp.pwd()
try:
ch_dir = now_dir + '/' + fn
ftp.cwd(ch_dir)
except ftplib.error_perm:
print 'ERROR:cannot CD to "%s"\n' % ch_dir
print ' ***** Changed to %s\n' % ch_dir
list = ftp.nlst()
biger = '^>\d'
equal_biger = '^>='
equal = '^\d'
#biger
b = re.match(biger,fv)
if b is not None:
will_get = []
ver = fv[1:]
get_file = fn + '_' + ver
for program in list:
if program > get_file:
will_get.append(program)
load_file = max.comp(will_get)
if os.path.isfile(load_file):
print 'Local machine has exist %s\n' % load_file
try:
ftp.cwd(now_dir)
except ftplib.error_perm:
print 'ERROR:cannot CD to "%s"\n' % now_dir
print ' ***** Changed to %s\n' % now_dir
continue
else:
print '***** Loading,please wait:',load_file
get_dir = ftp.pwd()
target = 'ftp://' + file_host + get_dir + '/' + load_file
download.load_file(target)
#eq
eb = re.match(equal_biger,fv)
if eb is not None:
will_get = []
ver = fv[2:]
get_file = fn + '_' + ver
for program in list:
if program > get_file:
will_get.append(program)
load_file = max.comp(will_get)
if os.path.isfile(load_file):
print 'Local machine has exist %s\n' % load_file
try:
ftp.cwd(now_dir)
except ftplib.error_perm:
print 'ERROR:cannot CD to "%s"\n' % now_dir
print ' ***** Changed to %s\n' % now_dir
continue
else:
print '***** Loading,please wait:',load_file
get_dir = ftp.pwd()
target = 'ftp://' + file_host + get_dir + '/' + load_file
download.load_file(target)
#equal
e = re.match(equal,fv)
if e is not None:
will_get = []
for program in list:
m = re.search(fv,program)
if m is not None:
will_get.append(program)
load_file = max.comp(will_get)
if os.path.isfile(load_file):
print 'Local machine has exist %s\n' % load_file
try:
ftp.cwd(now_dir)
except ftplib.error_perm:
print 'ERROR:cannot CD to "%s"\n' % now_dir
print ' ***** Changed to %s\n' % now_dir
continue
else:
print '***** Loading,please wait:',load_file
get_dir = ftp.pwd()
target = 'ftp://' + file_host + get_dir + '/' + load_file
download.load_file(target)
try:
ftp.cwd(now_dir)
except ftplib.error_perm:
print 'ERROR:cannot CD to "%s"\n' % now_dir
print ' ***** Changed to %s\n' % now_dir
continue
#exit ftp
ftp.quit()
print "***** Exit FTP server:%s\n" % file_host