17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

16<br />

Découverte de Python<br />

PREMIÈRE PARTIE<br />

patches_done = os.listdir(os.curdir)<br />

patches_todo = []<br />

_result = StringIO()<br />

# fonctions de récupération des flux ftp<br />

def callback(line):<br />

_result.write(line)<br />

def callbacktext(line):<br />

_result.write('%s\n' % line)<br />

def readresults(text=False):<br />

content = _result.getvalue()<br />

_result.buf = ''<br />

return content<br />

# code principal<br />

ftp = FTP('localhost')<br />

ftp.login('root', 'motdepasse')<br />

try:<br />

ftp.cwd(os.name)<br />

ftp.dir(callbacktext)<br />

patches = readresults().split('\n')<br />

# tous les fichiers téléchargés sont binaires<br />

ftp.voidcmd('TYPE I')<br />

for patch in patches:<br />

line = patch.split()<br />

if len(line) == 0:<br />

continue<br />

filename = line[-1]<br />

if filename in patches_done:<br />

continues<br />

ftp.retrbinary('RETR %s' % filename, callback)<br />

with open(filename, 'w') as file_:<br />

file_.write(readresults())<br />

os.chmod(filename, 467) # 467 dec => 111 010 011 bin => rwx-w--wx<br />

patch_file = os.path.join(os.curdir, filename)<br />

patches_todo.append(patch_file)<br />

finally:<br />

ftp.close()<br />

for patch in patches_todo:<br />

# le patch est auto-exécutable<br />

logging.info('application du patch %s...' % patch)<br />

log = os.popen(patch)<br />

logging.info('\n'.join(log))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!