python 自动下载 moudle

import sys,re,subprocess
import os
from subprocess import CalledProcessError

new_set = set()
ls = set()

def dls():
    temp_set = set()
    a = subprocess.check_output("ls -R", shell=True)
    for line in a.split('\n'):
          str = re.sub(r'.py', r'', line)
          if  str.find('.')<0  and str!='\n' :
               temp_set.add(str)
               print str
    return temp_set
   

def dpip(new_set):
    ls = dls()
    for str in new_set-ls:
         str = str.replace('\n','')
         with open('daicy.py', 'w') as f:
                     f.write('import '+str)
         try:
              a = subprocess.check_call('python daicy.py', shell=True)
              if a!=0:
                  command = 'pip  install  '+str
                  print command
                  subprocess.call(command, shell=True)
         except CalledProcessError, e:
            print 'except:', e
            command = 'pip  install  '+str
            print command
            os.system(command)
         finally:
              pass

a = subprocess.Popen('''find -name '*.py' | xargs grep -h -o -w  -s "^import\s\+\([\_0-9a-zA-Z]\+\)\|^from\s\+\([\_0-9a-zA-Z]\+\)"''', shell=True,stdout=subprocess.PIPE)
for line in a.stdout.readlines():
    line = line.replace('\n','')
    new_set.add(re.sub(r'import\s+|from\s+', r'', line))


dpip(new_set)


posted on 2014-04-29 21:02  分布式编程  阅读(141)  评论(0编辑  收藏  举报

导航