commit 7f831f10cbf1228a00634329ad3d2302b8b1941e
Author: Aleix Conchillo Flaque <aleix@member.fsf.org>
Date:   Mon Apr 14 17:48:19 2008 +0000

    Create initial Mercurial perl backend and db update fixes

diff --git a/backend/accounts/sv_groups.in b/backend/accounts/sv_groups.in
index 86e7e4d..64c7fb6 100755
--- a/backend/accounts/sv_groups.in
+++ b/backend/accounts/sv_groups.in
@@ -7,6 +7,8 @@
 #                          Sylvain Beucler <beuc--beuc.net>
 #                          Timothee Besset <ttimo--ttimo.net>
 #
+#  Copyright (C) 2008  Aleix Conchillo Flaque
+#
 # This file is part of Savane.
 # 
 # Savane is free software: you can redistribute it and/or modify
@@ -43,6 +45,7 @@ use Savane::Download;
 use Savane::Cvs;
 use Savane::Svn;
 use Savane::Git;
+use Savane::Mercurial;
 use Getopt::Long;
 use Term::ANSIColor qw(:constants);
 use POSIX qw(strftime);
@@ -64,6 +67,7 @@ my $no_cvs = 0;
 my $no_arch = 0; 
 my $no_svn = 0;
 my $no_git = 0;
+my $no_hg = 0;
 my $no_homepage = 0;
 my $no_download = 0;
 
@@ -72,6 +76,7 @@ my $only_cvs;
 my $only_arch;
 my $only_svn;
 my $only_git;
+my $only_hg;
 my $only_homepage;
 my $only_download;
 
@@ -92,6 +97,7 @@ eval {
 			 "no-cvs" => \$no_cvs,
 			 "no-svn" => \$no_svn,
 			 "no-git" => \$no_git,
+			 "no-hg" => \$no_hg,
 			 "no-arch" => \$no_arch,
 			 "no-homepage" => \$no_homepage,
 			 "no-download" => \$no_download,
@@ -99,6 +105,7 @@ eval {
 			 "only-cvs" => \$only_cvs,
 			 "only-svn" => \$only_svn,
 			 "only-git" => \$only_git,
+			 "only-hg" => \$only_hg,
 			 "only-arch" => \$only_arch,
 			 "only-homepage" => \$only_homepage,
 			 "only-download" => \$only_download,
@@ -123,6 +130,7 @@ Normally, sv_users should run just after.
       --no-arch                Do not update arch trees
       --no-svn                 Do not update svn trees
       --no-git                 Do not update git trees
+      --no-hg                  Do not update mercurial trees
       --no-homepage            Do not update homepage dirs
       --no-download            Do not update download dirs
 
@@ -131,6 +139,7 @@ Normally, sv_users should run just after.
       --only-arch              Only update arch trees
       --only-svn               Only update svn trees
       --only-git               Only update git trees
+      --only-hg                Only update mercurial trees
       --only-homepage          Only update homepage dirs
       --only-download          Only update download dirs
 
@@ -150,6 +159,7 @@ if ($only_etc ||
     $only_arch ||
     $only_svn ||
     $only_git ||
+    $only_hg ||
     $only_homepage ||
     $only_download) {
  
@@ -159,6 +169,7 @@ if ($only_etc ||
     $no_arch = 1;
     $no_svn = 1;
     $no_git = 1;
+    $no_hg = 1;
     $no_homepage = 1;
     $no_download = 1;
 
@@ -167,6 +178,7 @@ if ($only_etc ||
     $no_arch = 0 if $only_arch;
     $no_svn = 0 if $only_svn;
     $no_git = 0 if $only_git;
+    $no_hg = 0 if $only_hg;
     $no_homepage = 0 if $only_homepage;
     $no_download = 0 if $only_download;
 }
@@ -204,11 +216,11 @@ AcquireReplicationLock($lockfile);
 my %db_group_type;
 foreach my $line (GetDB("group_type", 
 			0,
-			"name,type_id,dir_type_homepage,dir_type_cvs,dir_type_arch,dir_type_svn,dir_type_git,dir_type_download,dir_homepage,dir_cvs,dir_arch,dir_svn,dir_git,dir_download,can_use_homepage,can_use_cvs,can_use_arch,can_use_svn,can_use_git,can_use_download")) {
+			"name,type_id,dir_type_homepage,dir_type_cvs,dir_type_arch,dir_type_svn,dir_type_git,dir_type_hg,dir_type_download,dir_homepage,dir_cvs,dir_arch,dir_svn,dir_git,dir_hg,dir_download,can_use_homepage,can_use_cvs,can_use_arch,can_use_svn,can_use_git,can_use_hg,can_use_download")) {
     chomp($line);
-    my ($name, $id, $dir_type_homepage, $dir_type_cvs, $dir_type_arch, $dir_type_svn, $dir_type_git, $dir_type_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_download, $can_use_homepage, $can_use_cvs, $can_use_arch, $can_use_svn, $can_use_git, $can_use_download) = split(",", $line);
+    my ($name, $id, $dir_type_homepage, $dir_type_cvs, $dir_type_arch, $dir_type_svn, $dir_type_git, $dir_type_hg, $dir_type_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_hg, $dir_download, $can_use_homepage, $can_use_cvs, $can_use_arch, $can_use_svn, $can_use_git, $can_use_hg, $can_use_download) = split(",", $line);
     print "DBG db: get $line from database\n" if $debug;
-    $db_group_type{$id} = [ ($name, $id, $dir_type_homepage, $dir_type_cvs, $dir_type_arch, $dir_type_svn, $dir_type_git, $dir_type_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_download, $can_use_homepage, $can_use_cvs, $can_use_arch, $can_use_svn, $can_use_git, $can_use_download) ];
+    $db_group_type{$id} = [ ($name, $id, $dir_type_homepage, $dir_type_cvs, $dir_type_arch, $dir_type_svn, $dir_type_git, $dir_type_hg, $dir_type_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_hg, $dir_download, $can_use_homepage, $can_use_cvs, $can_use_arch, $can_use_svn, $can_use_git, $can_use_hg, $can_use_download) ];
 }
 
 # db_groups:
@@ -226,11 +238,11 @@ my %db_groups;
 my @db_groups;
 foreach my $line (GetDB("groups", 
 			"status='A'",
-			"unix_group_name,type,is_public,use_homepage,use_cvs,use_arch,use_svn,use_git,use_download,dir_homepage,dir_cvs,dir_arch,dir_svn,dir_git,dir_download")) {
+			"unix_group_name,type,is_public,use_homepage,use_cvs,use_arch,use_svn,use_git,use_hg,use_download,dir_homepage,dir_cvs,dir_arch,dir_svn,dir_git,dir_hg,dir_download")) {
     chomp($line);
-    my ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_download) = split(",", $line);
+    my ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_hg, $use_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_hg, $dir_download) = split(",", $line);
     print "DBG db: get group $line from database\n" if $debug;
-    $db_groups{$name} = [ ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_download) ];
+    $db_groups{$name} = [ ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_hg, $use_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_hg, $dir_download) ];
     push(@db_groups, $name);
 }
 
@@ -305,7 +317,7 @@ for ("svusers", "anoncvs") {
 
 foreach my $group (@only_in_db){
     
-    my ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_download) = @{$db_groups{$group}};
+    my ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_hg, $use_download) = @{$db_groups{$group}};
     
     print "DBG create: $name and web$name\n" if $debug;
     
@@ -344,8 +356,8 @@ foreach my $group (@db_groups) {
 
     } else {
 	
-	my ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_download, $group_dir_homepage, $group_dir_cvs, $group_dir_arch, $group_dir_svn, $group_dir_git, $group_dir_download) = @{$db_groups{$group}};
-	my ($type_name, $type_id, $dir_type_homepage, $dir_type_cvs, $dir_type_arch, $dir_type_svn, $dir_type_git, $dir_type_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_download, $can_use_homepage, $can_use_cvs, $can_use_arch, $can_use_svn, $can_use_git, $can_use_download) = @{$db_group_type{$type}};
+	my ($name, $type, $is_public, $use_homepage, $use_cvs, $use_arch, $use_svn, $use_git, $use_hg, $use_download, $group_dir_homepage, $group_dir_cvs, $group_dir_arch, $group_dir_svn, $group_dir_git, $group_dir_hg, $group_dir_download) = @{$db_groups{$group}};
+	my ($type_name, $type_id, $dir_type_homepage, $dir_type_cvs, $dir_type_arch, $dir_type_svn, $dir_type_git, $dir_type_hg, $dir_type_download, $dir_homepage, $dir_cvs, $dir_arch, $dir_svn, $dir_git, $dir_hg, $dir_download, $can_use_homepage, $can_use_cvs, $can_use_arch, $can_use_svn, $can_use_git, $can_use_hg, $can_use_download) = @{$db_group_type{$type}};
         
 	# If a use in the database is empty, set it to the group type can_use
 	$use_homepage = $can_use_homepage if $use_homepage eq '';
@@ -353,6 +365,7 @@ foreach my $group (@db_groups) {
 	$use_arch = $can_use_arch if $use_arch eq '';
 	$use_svn = $can_use_svn if $use_svn eq '';
 	$use_git = $can_use_git if $use_git eq '';
+	$use_hg = $can_use_hg if $use_hg eq '';
 	$use_download = $can_use_download if $use_download eq '';
 	
 	# If a group_dir in the database is not empty, set reset the default
@@ -362,9 +375,10 @@ foreach my $group (@db_groups) {
 	$dir_arch = $group_dir_arch if $group_dir_arch;
 	$dir_svn = $group_dir_svn if $group_dir_svn;
 	$dir_git = $group_dir_git if $group_dir_git;
+	$dir_hg = $group_dir_hg if $group_dir_hg;
 	$dir_download = $group_dir_download if $group_dir_download;
 	
-	print "DBG update: $name, type:$type, public:$is_public, homepage:$use_homepage, cvs:$use_cvs, arch:$use_arch, svn:$use_svn, git:$use_git, download:$use_download\n\t $dir_homepage $dir_cvs $dir_download\n" if $debug;
+	print "DBG update: $name, type:$type, public:$is_public, homepage:$use_homepage, cvs:$use_cvs, arch:$use_arch, svn:$use_svn, git:$use_git, hg:$use_hg, download:$use_download\n\t $dir_homepage $dir_cvs $dir_download\n" if $debug;
 	
 	my $madesomething;
 	
@@ -488,6 +502,15 @@ foreach my $group (@db_groups) {
 		    $madesomething .= GitMakeArea($name,$dir_git,$is_public);
 		}
 	    }
+
+	    # Create the Mercurial area, by relying on the library,
+	    # that should make the appropriate decision (ie if no
+	    # %PROJECT string is provided, nothing should be done)
+	    if ($use_hg && !$no_hg) {
+		if ($dir_type_hg eq "basichg") {
+		    $madesomething .= HgMakeArea($name,$dir_hg,$is_public);
+		}
+	    }
 	}
 	
 	print LOG strftime "[$script] %c ---- update $name ($madesomething built)\n", localtime if $madesomething;
diff --git a/lib/Savane/Mercurial.pm b/lib/Savane/Mercurial.pm
new file mode 100644
index 0000000..4e91913
--- /dev/null
+++ b/lib/Savane/Mercurial.pm
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+# Copyright (C) 2008  Aleix Conchillo Flaque
+# 
+# This file is part of Savane.
+# 
+# Savane is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+# 
+# Savane is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Init Mercurial repository.
+
+use strict;
+use warnings;
+
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT = qw(HgMakeArea);
+our $version = 1;
+
+sub HgMakeArea {
+    my ($name,$dir_hg) = @_;
+    my $warning = '';
+
+    # %PROJECT is not mandatory, but if it is missing, it may well be 
+    # a major misconfiguration.
+    # It should only happen if a directory has been set for a specific 
+    # project.
+    unless ($dir_hg =~ s/\%PROJECT/$name/) {
+	$warning = " (The string \%PROJECT was not found, there may be a group type serious misconfiguration)";
+    }
+
+    unless (-e $dir_hg) {
+	# Layout: /srv/hg/sources/project_name
+        #         /srv/hg/sources/project_name/other_module
+	
+	# Create a repository
+	my $old_umask = umask(0002);
+
+        # Initialise Mercurial repository
+	system('hg', 'init', $dir_hg);
+	
+	system('chmod', 'g+s', $dir_hg);
+	system('chgrp', '-R', $name, $dir_hg);
+
+	# Create folder for subrepositories (need to code multi-repo support first)
+	# TODO: precise directory location
+	#system('mkdir', '-m', '2775', ".../$name/");
+	#system('chown', "root:$name", ".../$name/");
+
+	# Clean-up environment
+	umask($old_umask);
+
+	return ' '.$dir_hg.$warning;	
+    }
+    return 0;
+}
diff --git a/update/clean-up/git.sql b/update/clean-up/git.sql
index 9ff70f9..18562e0 100644
--- a/update/clean-up/git.sql
+++ b/update/clean-up/git.sql
@@ -7,7 +7,7 @@ ALTER TABLE `group_type` ADD `can_use_git` INT( 1 ) DEFAULT '0' NOT NULL AFTER `
 ALTER TABLE `group_type` ADD `is_menu_configurable_git` INT( 1 ) DEFAULT '0' NOT NULL AFTER `is_menu_configurable_svn_viewcvs` ,
  ADD `is_menu_configurable_git_viewcvs` INT( 1 ) DEFAULT '0' NOT NULL AFTER `is_menu_configurable_git`;
 
-ALTER TABLE `group_type` ADD `dir_type_git` VARCHAR( 255 ) DEFAULT 'basicdirectory' NOT NULL AFTER `dir_type_svn`;
+ALTER TABLE `group_type` ADD `dir_type_git` VARCHAR( 255 ) DEFAULT 'basicgit' NOT NULL AFTER `dir_type_svn`;
 
 ALTER TABLE `group_type` ADD `dir_git` VARCHAR( 255 ) DEFAULT '/' NOT NULL AFTER `dir_svn`;
 
diff --git a/update/clean-up/mercurial.sql b/update/clean-up/mercurial.sql
index 54f4e7c..fe07134 100644
--- a/update/clean-up/mercurial.sql
+++ b/update/clean-up/mercurial.sql
@@ -7,7 +7,7 @@ ALTER TABLE `group_type` ADD `can_use_hg` INT( 1 ) DEFAULT '0' NOT NULL AFTER `c
 ALTER TABLE `group_type` ADD `is_menu_configurable_hg` INT( 1 ) DEFAULT '0' NOT NULL AFTER `is_menu_configurable_git_viewcvs` ,
  ADD `is_menu_configurable_hg_viewcvs` INT( 1 ) DEFAULT '0' NOT NULL AFTER `is_menu_configurable_hg`;
 
-ALTER TABLE `group_type` ADD `dir_type_hg` VARCHAR( 255 ) DEFAULT 'basicdirectory' NOT NULL AFTER `dir_type_git`;
+ALTER TABLE `group_type` ADD `dir_type_hg` VARCHAR( 255 ) DEFAULT 'basichg' NOT NULL AFTER `dir_type_git`;
 
 ALTER TABLE `group_type` ADD `dir_hg` VARCHAR( 255 ) DEFAULT '/' NOT NULL AFTER `dir_git`;
 
