I'm beginner for python. Here is my first python demo code. I'm going to re factor this code step by step. any help would be very helpful to learn about python.
Code description
Batch program to xcopy from host PC to remote destnation with multi-processing. Kepler, Python-3.x are my environment.
full code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# based on Carnival http://ask.python.kr/users/6970/carnival/
import os
import os.path
import csv
import re
from multiprocessing import Process, Lock
class Server:
def __init__(self, addr, path):
self.addr = addr
self.path = path
def distribute_file(server_list, pathname):
print("Read from {}".format(pathname))
with open(pathname, 'rb') as inFile:
buffer = inFile.read()
for server in server_list:
remotepath = "//%s/%s/%s" % (server.addr, server.path, pathname)
print ("Write to {}".format(remotepath))
with open(remotepath, 'wb') as outFile:
outFile.write(buffer)
def multi_distribute_file(lo, server, dirpath, filename, path):
lo.acquire()
pathname = os.path.join(dirpath, filename)
print("Read from {}".format(pathname))
with open(pathname, 'rb') as inFile:
buffer = inFile.read()
if(path == ''):
remotepath = "//%s/%s/%s" % (server.addr, server.path, filename)
else:
remotepath = "//%s/%s/%s/%s" % (server.addr, server.path, path, filename)
print ("Write to {}".format(remotepath))
with open(remotepath, 'wb') as outFile:
outFile.write(buffer)
lo.release()
def make_dir(server_list, path):
for server in server_list:
dirpath = "//%s/%s/%s" % (server.addr, server.path, path)
d = os.path.dirname(dirpath)
if not os.path.exists(d):
os.makedirs(d)
print ("Make dir {}".format(d))
else:
print ("dir {} already exists.".format(d))
if not os.path.exists(dirpath):
os.makedirs(dirpath)
print ("Make dir {}".format(dirpath))
else:
print ("dir {} already exists.".format(dirpath))
def dist_files(server_list, subdir):
for dirpath, dirnames, filenames in os.walk(subdir):
l = re.findall(r"[\w']+",subdir)
m = re.findall(r"[\w']+",dirpath)
path = ''
if(l != m):
path = m[len(m)-1]
make_dir(server_list, path)
for filename in filenames:
for server in server_list:
lock = Lock()
Process(target=multi_distribute_file,args=(lock, server, dirpath, filename, path)).start()
def get_server_list(filename):
mydictionary = []
csvFile = csv.reader(open(filename, "r"))
for row in csvFile:
mydictionary.append(Server(row[0], row[1]))
print("{}, {}".format(row[0], row[1]))
return mydictionary
if __name__ == '__main__':
server_list = get_server_list('client_list.csv')
dist_files(server_list, 'C:\\Users\\Public\\Test')
client_list.csv
- 192.168.10.100, Test
- 192.168.10.101, Test
- 192.168.10.102, Test