#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-06-11 09:35:49
# @Author : Yaheng Wang (m13262578991@163.com)
# @Link : http://www.wy2160640.github.io
# @Version : $Id$
import os
import sys
from ftplib import FTP
class FTPSync(object):
def __init__(self):
self.conn = FTP('ftp.hapmap.org')
self.conn.login()
self.conn.cwd('hapmap/frequencies/')
os.chdir('/home/ftp.hapmap.org/hapmap/frequencies/')
def get_dirs_files(self):
dir_res = []
self.conn.dir('.', dir_res.append)
files = [f.split(None, 8)[-1] for f in dir_res if f.startswith('-')]
dirs = [f.split(None, 8)[-1] for f in dir_res if f.startswith('d')]
return files, dirs
def walk(self, next_dir):
sys.stderr.write('Walking to %s\n'%next_dir)
self.conn.cwd(next_dir)
try:
os.mkdir(next_dir)
except OSError:
pass
os.chdir(next_dir)
ftp_curr_dir = self.conn.pwd()
local_curr_dir = os.getcwd()
files, dirs = self.get_dirs_files()
sys.stdout.write("FILES: %s"%files)
sys.stdout.write("DIRS: %s"%dirs)
for f in files:
sys.stdout.write("%s : %s"%(next_dir, f))
sys.stdout.write("download : %s"%os.path.abspath(f))
outf = open(f, "wb")
try:
self.conn.retrbinary("RETR %s"%f, outf.write)
finally:
outf.close()
for d in dirs:
os.chdir(local_curr_dir)
self.conn.cwd(ftp_curr_dir)
self.walk(d)
def run(self):
self.walk('.')
def main():
f = FTPSync()
f.run()
if __name__ == '__main__':
main()