2
juin
2013
[Vala] « add-apt-repository » pour debian 7
juin
2013
Un article de Rachel
Pas de commentaires
voici un code Vala, remplaçant la commande « add-apt-repository » sous debian :
using Xml;
using Soup;
namespace IO
{
public errordomain Error
{
Null
}
public static void download_file(string uri, string dest_file) throws IO.Error
{
if(dest_file == null)throw new IO.Error.Null("name of destination cannot be null");
uint8[] data = download_data(uri);
if(data.length < 1)throw new IO.Error.Null("data length is too short or null");
FileUtils.set_data(dest_file,data);
}
public static uint8[] download_data(string uri){
var session = new SessionSync();
var message = new Message("GET",uri);
session.send_message(message);
return message.response_body.data;
}
public static string download_string(string uri){
return (string)download_data(uri);
}
}
public static void main(string[] args){
if(args[1].index_of("ppa:") == -1)return;
string[] t = args[1].split("/");
string maintener = t[0].split(":")[1];
string dir = t[1];
t[1] = "+archive/"+t[1];
t[0] = t[0].replace("ppa:","http://launchpad.net/~");
string ppa = string.joinv("/",t);
IO.download_file(ppa,".temp.html");
Html.ParserCtxt ctx = new Html.ParserCtxt();
ctx.use_options(Html.ParserOption.NOERROR | Html.ParserOption.NOBLANKS | Html.ParserOption.NOWARNING | Html.ParserOption.NONET);
Html.Doc *doc = ctx.read_file(".temp.html");
XPath.Context c = new XPath.Context(doc);
XPath.Object *o = c.eval("//div[@class='yui3-editable_text-text']");
XPath.NodeSet *nodeset = o->nodesetval;
stdout.printf("%s\n",nodeset->item(0)->get_content());
string s = "deb http://ppa.launchpad.net/"+maintener+"/"+dir+"/ubuntu quantal main\n"+
"deb-src http://ppa.launchpad.net/"+maintener+"/"+dir+"/ubuntu quantal main";
FileUtils.set_contents("ppa-"+maintener+"-"+dir+".list",s);
stdout.printf("add this ppa ? [y/n] : ");
string? option = stdin.read_line ();
if(option.down() == "y"){
Process.spawn_command_line_async("sudo mv ppa-"+maintener+"-"+dir+".list /etc/apt/sources.list.d/ppa-"+maintener+"-"+dir+".list");
string k; string l;
stdout.printf("\n");
Process.spawn_command_line_sync("sudo apt-get update",out l, out k);
k = k.split(" NO_PUBKEY ")[1];
Process.spawn_command_line_sync("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys "+k,out k, out l);
stdout.printf("key acquired !\n");
}
}
using Soup;
namespace IO
{
public errordomain Error
{
Null
}
public static void download_file(string uri, string dest_file) throws IO.Error
{
if(dest_file == null)throw new IO.Error.Null("name of destination cannot be null");
uint8[] data = download_data(uri);
if(data.length < 1)throw new IO.Error.Null("data length is too short or null");
FileUtils.set_data(dest_file,data);
}
public static uint8[] download_data(string uri){
var session = new SessionSync();
var message = new Message("GET",uri);
session.send_message(message);
return message.response_body.data;
}
public static string download_string(string uri){
return (string)download_data(uri);
}
}
public static void main(string[] args){
if(args[1].index_of("ppa:") == -1)return;
string[] t = args[1].split("/");
string maintener = t[0].split(":")[1];
string dir = t[1];
t[1] = "+archive/"+t[1];
t[0] = t[0].replace("ppa:","http://launchpad.net/~");
string ppa = string.joinv("/",t);
IO.download_file(ppa,".temp.html");
Html.ParserCtxt ctx = new Html.ParserCtxt();
ctx.use_options(Html.ParserOption.NOERROR | Html.ParserOption.NOBLANKS | Html.ParserOption.NOWARNING | Html.ParserOption.NONET);
Html.Doc *doc = ctx.read_file(".temp.html");
XPath.Context c = new XPath.Context(doc);
XPath.Object *o = c.eval("//div[@class='yui3-editable_text-text']");
XPath.NodeSet *nodeset = o->nodesetval;
stdout.printf("%s\n",nodeset->item(0)->get_content());
string s = "deb http://ppa.launchpad.net/"+maintener+"/"+dir+"/ubuntu quantal main\n"+
"deb-src http://ppa.launchpad.net/"+maintener+"/"+dir+"/ubuntu quantal main";
FileUtils.set_contents("ppa-"+maintener+"-"+dir+".list",s);
stdout.printf("add this ppa ? [y/n] : ");
string? option = stdin.read_line ();
if(option.down() == "y"){
Process.spawn_command_line_async("sudo mv ppa-"+maintener+"-"+dir+".list /etc/apt/sources.list.d/ppa-"+maintener+"-"+dir+".list");
string k; string l;
stdout.printf("\n");
Process.spawn_command_line_sync("sudo apt-get update",out l, out k);
k = k.split(" NO_PUBKEY ")[1];
Process.spawn_command_line_sync("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys "+k,out k, out l);
stdout.printf("key acquired !\n");
}
}