9 def _wait_files_creation(file_list
):
10 # Etablish a list of directory and subfiles
12 for dirname
, filename
in [os
.path
.split(f
) for f
in file_list
]:
13 directories
.setdefault(dirname
, dict())
14 directories
[dirname
][filename
] = False
16 def all_files_exists():
17 return all([all(files
.values()) for files
in directories
.values()])
21 # Watch every directories where the file are
22 watchdescriptors
= dict()
23 for dirname
in directories
.keys():
24 wd
= inotifyx
.add_watch(fd
,
26 inotifyx
.IN_CREATE | inotifyx
.IN_DELETE
)
27 watchdescriptors
[wd
] = dirname
29 # Set to True the file wich exists
30 for dirname
, filename
in [os
.path
.split(f
) for f
in file_list
]:
31 directories
[dirname
][filename
] = os
.path
.exists(os
.path
.join(dirname
,
33 # Let's wait for every file creation
34 while not all_files_exists():
35 events_list
= inotifyx
.get_events(fd
)
36 for event
in events_list
:
37 dirname
= watchdescriptors
[event
.wd
]
38 if event
.name
in directories
[dirname
]:
39 # One of watched file was created or deleted
40 if event
.mask
& inotifyx
.IN_DELETE
:
41 directories
[dirname
][event
.name
] = False
43 directories
[dirname
][event
.name
] = True
49 """Portable execution with process replacement"""
50 # XXX: Kept for backward compatibility
51 generic_exec([args
, None, None])
53 def execute_wait(args
):
54 """Execution but after all files in args[1] exists"""
55 # XXX: Kept for backward compatibility
56 generic_exec([args
[0], args
[1], None])
63 """Portable execution with process replacement and environment manipulation"""
64 # XXX: Kept for backward compatibility
65 generic_exec([args
[0], None, args
[1]])
67 def executee_wait(args
):
68 """Portable execution with process replacement and environment manipulation"""
69 # XXX: Kept for backward compatibility
72 def generic_exec(args
):
73 exec_list
= list(args
[0])
75 environment_overriding
= args
[2]
77 exec_env
= os
.environ
.copy()
78 if environment_overriding
is not None:
79 exec_env
.update(environment_overriding
)
81 if file_list
is not None:
82 _wait_files_creation(file_list
)
84 os
.execve(exec_list
[0], exec_list
+ sys
.argv
[1:], exec_env
)
86 def sig_handler(signal
, frame
):
87 print 'Received signal %r, killing children and exiting' % signal
88 if child_pg
is not None:
89 os
.killpg(child_pg
, signal
.SIGHUP
)
90 os
.killpg(child_pg
, signal
.SIGTERM
)
93 signal
.signal(signal
.SIGINT
, sig_handler
)
94 signal
.signal(signal
.SIGQUIT
, sig_handler
)
95 signal
.signal(signal
.SIGTERM
, sig_handler
)
98 def execute_with_signal_translation(args
):
99 """Run process as children and translate from SIGTERM to another signal"""
100 child
= subprocess
.Popen(args
, close_fds
=True, preexec_fn
=os
.setsid
)
103 print 'Process %r started' % args
107 os
.killpg(child_pg
, signal
.SIGHUP
)
108 os
.killpg(child_pg
, signal
.SIGTERM
)