-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmonitor.py
81 lines (60 loc) · 2.49 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import argparse
import logging
import ConfigParser
import os
from node import Node
from server import Server, ClientData
from client import Client
logger = logging.getLogger('syncIt')
def setup_logging(log_filename):
handler = logging.FileHandler(log_filename)
# handler = logging.StreamHandler()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
print 'Logging started on file %s' % log_filename
def get_watch_dirs(config, user_name):
watch_dirs = []
for key, value in config.items('syncit.dirs'):
dir = os.path.expanduser(value.strip())
my_dir = Node.get_dest_path(dir, user_name)
watch_dirs.append(my_dir)
logger.debug("watched dirs %s", watch_dirs)
return watch_dirs
def get_clients(config):
clients = []
for key, value in config.items('syncit.clients'):
words = value.split(',')
client_uname, client_ip, client_port = [word.strip() for word in words]
clients.append(ClientData(client_uname, client_ip, int(client_port)))
return clients
def get_server_tuple(config):
server_uname, server_ip, server_port = config.get('syncit.server', 'server', 1).split(',')
return (server_uname, server_ip, server_port)
def main():
#use argparse to get role, ip, port and user name
parser = argparse.ArgumentParser(
description="""PySyncIt""",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'-ip', help='Specify the ip address of this machine', required=True)
parser.add_argument(
'-port', help='Specify the port of this machine to run rpc server', required=True)
parser.add_argument(
'-uname', help='Specify the user name of this machine', required=True)
parser.add_argument(
'-role', help='Specify the role of this machine - client or server', required=True)
args = parser.parse_args()
#start logging
setup_logging("syncit.log.%s-%s" % (args.ip, args.port));
logger = logging.getLogger('syncIt')
#Read config file
config = ConfigParser.ConfigParser()
logger.info("Using config file: syncit.cfg")
config.read('syncit.cfg')
if (args.role == 'server'):
node = Server(args.role, args.ip, int(args.port), args.uname, get_watch_dirs(config, args.uname), get_clients(config))
else:
node = Client(args.role, args.ip, int(args.port), args.uname, get_watch_dirs(config, args.uname), get_server_tuple(config))
node.activate()
if __name__ == "__main__":
main()