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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
| use warnings;
use strict;
use File::ChangeNotify;
use Switch;
use File::Basename;
use Text::CSV;
use Win32;
use Win32::Daemon;
$| = 1;
my $watcher =
File::ChangeNotify->instantiate_watcher
( directories => [ 'C:\infoG\Create-Windows-service' ],
filter => qr/\.csv$/
);
main();
use constant SERVICE_NAME => 'Four01';
use constant SERVICE_DESC => 'dossier Four01';
sub main
{
# Get command line argument - if none passed, use empty string
my $opt = shift (@ARGV) || "";
# Check command line argument
if ($opt =~ /^(-i|--install)$/i)
{
install_service(SERVICE_NAME, SERVICE_DESC);
}
elsif ($opt =~ /^(-r|--remove)$/i)
{
remove_service(SERVICE_NAME);
}
elsif ($opt =~ /^(--run)$/i)
{
# Redirect STDOUT and STDERR to a log file
# Derive the name of the file from the name of the program
# The log file will be in the scripts directory, with extension .log
my ($cwd,$bn,$ext) =
( Win32::GetFullPathName($0) =~ /^(.*\\)(.*)\.(.*)$/ ) [0..2] ;
my $log = $cwd . $bn . ".log";
# Redirect STDOUT and STDERR to log file
open(STDOUT, ">> $log") or die "Couldn't open $log for appending: $!\n";
open(STDERR, ">&STDOUT");
# Autoflush, no buffering
$|=1;
# Register the events which the service responds to
Win32::Daemon::RegisterCallbacks( {
start => \&Callback_Start,
running => \&Callback_Running,
stop => \&Callback_Stop,
pause => \&Callback_Pause,
continue => \&Callback_Continue,
} );
my %Context = (
last_state => SERVICE_STOPPED,
start_time => time(),
);
# Start the service passing in a context and indicating to callback
# using the "Running" event every 2000 milliseconds (2 seconds).
# NOTE: the StartService method with in 'callback mode' will block, in other
# words it won't return until the service has stopped, but the callbacks below
# will respond to the various events - START, STOP, PAUSE etc...
Win32::Daemon::StartService( \%Context, 2000 );
# Here the service has stopped
close STDERR; close STDOUT;
}
else
{
print "No valid options passed - nothing done\n";
}
}
sub Callback_Running
{
my( $Event, $Context ) = @_;
# Note that here you want to check that the state
# is indeed SERVICE_RUNNING. Even though the Running
# callback is called it could have done so before
# calling the "Start" callback.
if( SERVICE_RUNNING == Win32::Daemon::State() )
{
# ... process your main stuff here...
# ... note that here there is no need to
# change the state
# For now just print hello to the STDOUT, which goes to the log file
print "Hello! Start demon\n";
$Context->{last_state} = SERVICE_RUNNING;
Win32::Daemon::State( SERVICE_RUNNING );
while ( my @events = $watcher->wait_for_events() ) {
print "new event\n";
for my $event ( @events ) {
my $path_url = $event->path();
my $ev = $event->type();
print $path_url, ' - ', $ev, "\n";
print $path_url, ' - ', $ev, "\n";
my($filename, $dirs, $suffix) = fileparse($path_url);
print "filename: ", $filename, ' - ',"dirs: ", $dirs, ' - ', "suffix: ", $suffix, "\n";
my $file_out = "create.html";
#"create", "modify", "delete", or "unknown".
switch($ev){
case "create" {
print "info: create\n";
my ($ref_tab) = parce_csv($dirs, $filename, $path_url);
my (@tableau) = @{$ref_tab};
#print "@$_\n" for @array;
print "tableau: --->","\n";
print "@$_\n" for @tableau;
create_htm($dirs, $file_out, $ref_tab);
}
case "modify" {
print "info: modify\n";
my ($ref_tab) = parce_csv($dirs, $filename);
my (@tableau) = @{$ref_tab};
#print "@$_\n" for @array;
print "tableau: --->","\n";
print "@$_\n" for @tableau;
create_htm($dirs, $file_out, $ref_tab);
}
case "delete" { print "info: delete\n" }
case "unknown" { print "info: unknown\n" }
else { print "info: autre\n" }
}
}
}
}
}
sub Callback_Start
{
my( $Event, $Context ) = @_;
# Initialization code
# ...do whatever you need to do to start...
print "Starting...\n";
$Context->{last_state} = SERVICE_RUNNING;
Win32::Daemon::State( SERVICE_RUNNING );
}
sub Callback_Pause
{
my( $Event, $Context ) = @_;
print "Pausing...\n";
$Context->{last_state} = SERVICE_PAUSED;
Win32::Daemon::State( SERVICE_PAUSED );
}
sub Callback_Continue
{
my( $Event, $Context ) = @_;
print "Continuing...\n";
$Context->{last_state} = SERVICE_RUNNING;
Win32::Daemon::State( SERVICE_RUNNING );
}
sub Callback_Stop
{
my( $Event, $Context ) = @_;
print "Stopping...\n";
$watcher->reset();
$Context->{last_state} = SERVICE_STOPPED;
Win32::Daemon::State( SERVICE_STOPPED );
# We need to notify the Daemon that we want to stop callbacks and the service.
Win32::Daemon::StopService();
}
sub install_service
{
my ($srv_name, $srv_desc) = @_;
my ($path, $parameters);
# Get the program's full filename, break it down into constituent parts
my $fn = Win32::GetFullPathName($0);
my ($cwd,$bn,$ext) = ( $fn =~ /^(.*\\)(.*)\.(.*)$/ ) [0..2] ;
# Determine service's path to executable based on file extension
if ($ext eq "pl")
{
# Source perl script - invoke perl interpreter
$path = "\"$^X\"";
# Parameters include extra @INC directories and perl script
# @INC directories must not end in \ otherwise perl hangs
my $inc = ($cwd =~ /^(.*?)[\\]?$/) [0];
# The command includes the --run switch needed in main()
$parameters = "-I " . "\"$inc\"" . " \"$fn\" --run";
}
elsif ($ext eq "exe")
{
# Compiled perl script - invoke the compiled script
$path = "\"$fn\"";
$parameters = "";
}
else
{
# Invalid file type?
die "Can not install service for $fn,
file extension $ext not supported\n";
}
# Populate the service configuration hash
# The hash is required by Win32::Daemon::CreateService
my %srv_config = (
name => $srv_name,
display => $srv_name,
path => $path,
description => $srv_desc,
parameters => $parameters,
service_type => SERVICE_WIN32_OWN_PROCESS,
start_type => SERVICE_AUTO_START,
);
# Install the service
if( Win32::Daemon::CreateService( \%srv_config ) )
{
print "Service installed successfully\n";
}
else
{
print "Failed to install service\n";
}
}
sub remove_service
{
my ($srv_name, $hostname) = @_;
$hostname ||= Win32::NodeName();
if ( Win32::Daemon::DeleteService ( $srv_name ) )
{
print "Service uninstalled successfully\n";
}
else
{
print "Failed to uninstall service\n";
}
}
sub parce_csv {
my($folder_file, $my_file, $my_url) = @_;
my (@tableau2D);
print "sub parce_csv: ", ' - ',"folder_file: ", $folder_file, ' - ', "my_file: ", $my_file, "\n";
print "sub parce_csv: ", ' - ',"my_url: ", $my_url, "\n";
my $csv = Text::CSV->new({ sep_char => ';' });
my $file = $my_url or die "Need to get CSV file on the command line\n";
my $iter_ligne = 0;
open(my $data, '<:utf8', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
chomp $line;
if ($csv->parse($line)) {
my @fields = $csv->fields();
$tableau2D[$iter_ligne] = [ @fields ];
#$sum += $fields[2];
my $arrSize = @fields;
my $i = 0;
for my $field ( @fields ) {
if ($i==($arrSize-1)) {
print $field, "\n";
}
else {
print $field, " - ";
}
$i++;
}
} else {
warn "Line could not be parsed: $line\n";
}
$iter_ligne++;
print "iter_ligne : ","$iter_ligne\n";
}
#print "$sum\n";
print "tableau int2D: --->","\n";
print "@$_\n" for @tableau2D;
return \@tableau2D;
}
sub create_htm {
my(@args) = @_;
my $cmdString = $args[0];
my $file=$args[1];
my $ref = $args[2];
my @tab = @$ref;
print "tableau create_htm: --->","\n";
print "@$_\n" for @tab;
chdir($cmdString)|| die "Error: could not '$cmdString'";
if (-e $file) #if the file exists
{
print "re-ecriture HLM\n";
}
open(my $FILE, ">$file") or die "Cannot open $file: $!"; ## >> means +append to the end of file.
my $re = $file;
print $FILE '<!DOCTYPE html>';
print $FILE '<html>';
print $FILE '<head>';
print $FILE ' <meta name="robots" content="noindex,nofollow">';
print $FILE ' <title>HTML auto</title>';
print $FILE '<script type="text/javascript">';
print $FILE 'var refreshTime= 2*2;';
#rafréchissement
print $FILE ' var foo = document.write(\'<meta http-equiv="refresh" content="\'+refreshTime+\';url=',$re,' ">\');';
print $FILE '</script>';
print $FILE '</head>';
print $FILE '<body>';
print $FILE '<h1>My First Heading</h1>';
print $FILE '<p>My first paragraph.</p>';
print $FILE '<table style="width:100%">';
foreach my $row (@tab) {
print $FILE '<tr>';
foreach my $element (@$row) {
print $FILE '<td>';
print $FILE $element;
print $FILE '</td>';
}
print $FILE '</tr>';
}
print $FILE '</table>';
print $FILE '</body>';
print $FILE '</html>';
close($FILE);
print "Your html has been created";
} |
Partager