Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
projet
VPLPP
Commits
e56ed9a9
Commit
e56ed9a9
authored
Nov 04, 2020
by
Guillaume Huard
Browse files
push basique fonctionnel
parent
44b2df48
Changes
1
Hide whitespace changes
Inline
Side-by-side
push.pl
View file @
e56ed9a9
...
@@ -3,51 +3,114 @@ use strict;
...
@@ -3,51 +3,114 @@ use strict;
use
LWP::
UserAgent
;
use
LWP::
UserAgent
;
use
Data::
Dumper
;
use
Data::
Dumper
;
use
File::
Slurp
;
use
File::
Slurp
;
use
Getopt::
Long
qw(:config bundling)
;
use
XML::
LibXML
;
$ENV
{
PERL_LWP_SSL_VERIFY_HOSTNAME
}
=
0
;
$ENV
{
PERL_LWP_SSL_VERIFY_HOSTNAME
}
=
0
;
my
$url
=
'
https://moodle.caseine.org/webservice/rest/server.php
';
my
$url
=
'
https://moodle.caseine.org/webservice/rest/server.php
';
my
$
vplid
=
'
19435
'
;
my
$
debug
=
0
;
my
$
token
=
'
3d610600e046a8669cb39ec4798f8a56
'
;
my
$
vplid
=
$ENV
{'
CASEINE_VPLID
'}
;
my
$
ua
=
LWP::
UserAgent
->
new
(
agent
=>
'
Secret
')
;
my
$
token
=
$ENV
{'
CASEINE_TOKEN
'}
;
sub
help
();
sub
failure
($);
sub
debug
($)
{
print
shift
if
(
$debug
);
}
sub
save
($@)
{
my
$content
=
shift
;
my
$num
=
0
;
failure
"
Missing files
"
unless
scalar
(
@
_
);
foreach
my
$file
(
@
_
)
{
debug
("
Adding
$file
...
\n
");
my
$data
=
read_file
(
$file
);
push
@
{
$content
},
"
files[
$num
][name]
",
$file
,
"
files[
$num
][data]
",
$data
;
$num
++
;
}
}
sub
get_setting
($@)
{
my
$content
=
shift
;
my
$setting
=
shift
;
failure
"
Extraneous arguments
"
unless
not
scalar
(
@
_
);
push
@
{
$content
},
"
settingname
",
$setting
;
}
sub
set_setting
($@)
{
my
$content
=
shift
;
my
$setting
=
shift
;
failure
"
Extraneous arguments
"
unless
not
scalar
(
@
_
);
my
(
$name
,
$value
)
=
split
/=/
,
$setting
;
failure
"
Invalid setting
"
unless
not
defined
(
$value
);
push
@
{
$content
},
"
settingname
",
$name
,
"
settingvalue
",
$value
;
}
my
%handlers
=
(
'
save
'
=>
\
&save
,
'
save_required_files
'
=>
\
&save
,
'
save_corrected_files
'
=>
\
&save
,
'
save_execution_files
'
=>
\
&save
,
'
get_setting
'
=>
\
&get_setting
,
'
set_setting
'
=>
\
&set_setting
);
my
@commands
=
keys
(
%handlers
);
sub
help
()
{
print
<<"END";
usage :
$0 [ --help ] [ --token value ] [ --vplid value ] command [ arguments ]
Pushes data to caseine, where :
- token is the personnal token associated to the user of this script (by default, the value of the CASEINE_TOKEN environment variable)
- vplid is the id of the involved VPL (by default, the value of the CASEINE_VPLID environment variable)
- command is one of : @commands and its arguments are :
- filenames for save commands
- a setting name for get_setting
- an assignment of the form name=value for set_setting
END
exit
(
0
);
}
sub
failure
($)
{
print
"
@_
\n
";
help
;
}
GetOptions
('
help|h|?
'
=>
\
&help
,
'
vplid|v=s
'
=>
\
$vplid
,
'
token|t=s
'
=>
\
$token
,
'
debug|d
'
=>
\
$debug
);
#failure "Missing command" if scalar(@ARGV) == 0;
debug
"
VPLID =
$vplid
, TOKEN =
$token
, ARGS =
@ARGV
\n
";
my
$command
=
shift
@ARGV
;
my
$content
=
[
id
=>
"
$vplid
",
wstoken
=>
"
$token
",
wsfunction
=>
"
mod_vpl_
$command
"
];
failure
"
Unknown command
"
unless
exists
(
$handlers
{
$command
});
&
{
$handlers
{
$command
}}(
$content
,
@ARGV
);
my
$ua
=
LWP::
UserAgent
->
new
(
agent
=>
'
Secret
');
my
$res
=
$ua
->
post
(
$url
,
my
$res
=
$ua
->
post
(
$url
,
Content_Type
=>
'
form-data
',
Content_Type
=>
'
form-data
',
Content
=>
[
Content
=>
$content
moodlewsrestformat
=>
'
json
',
);
id
=>
"
$vplid
",
wstoken
=>
"
$token
",
if
(
exists
(
$res
->
{'
_content
'}))
{
wsfunction
=>
'
mod_vpl_set_setting
',
my
$answer
=
$res
->
{'
_content
'};
settingname
=>
'
name
',
my
$parser
=
XML::
LibXML
->
new
();
settingvalue
=>
'
Essai
',
my
$tree
=
$parser
->
parse_string
(
$answer
);
],
my
$root
=
$tree
->
getDocumentElement
;
);
my
@values
=
$root
->
getElementsByTagName
('
VALUE
');
print
$values
[
0
]
->
getFirstChild
->
getData
,
"
\n
";
# print Dumper($res);
}
else
{
failure
"
Invalid answer from server
";
my
$data
=
read_file
('
./local.sh
');
}
my
$res
=
$ua
->
post
(
$url
,
Content_Type
=>
'
form-data
',
Content
=>
[
moodlewsrestformat
=>
'
json
',
id
=>
"
$vplid
",
wstoken
=>
"
$token
",
wsfunction
=>
'
mod_vpl_save_execution_files
',
"
files[0][name]
"
=>
'
local.sh
',
"
files[0][data]
"
=>
"
$data
",
],
);
# Classic post does not work
# my $data = read_file('./local.sh');
# my $res = $ua->post( $url,
# Content_Type => 'form-data',
# Content => [
# moodlewsrestformat => 'json',
# id => "$vplid",
# wstoken => "$token",
# wsfunction => 'mod_vpl_save_execution_files',
# "files[0]" => [ './local.sh', 'local.sh' ],
# ],
# );
print
Dumper
(
$res
);
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment