<commit_before>#!/usr/bin/perl

#############################################################################
# This script will set up a test version of the extension in the profiles   #
# listed in .profileDirs file (one per line). This means that manifests,    #
# components and preferences are copied to the profile (and must be         #
# copied again on changes) while current directory is used for the chrome.  #
# If you set nglayout.debug.disable_xul_cache preference to true the        #
# changes in current directory will be available in the application without #
# restart. Also, tests (if any) will be available under                     #
# chrome://mochikit/content/harness-???.xul.                                #
#                                                                           #
# Example of .profileDirs contents:                                         #
#                                                                           #
#  c:\Documents and Setting\<user>\Application Data\Mozilla\Firefox\Profiles\<seed>.default
#  c:\Documents and Setting\<user>\Application Data\Songbird1\Profiles\<seed>.default
#                                                                           #
#############################################################################

use strict;
use warnings;
use Cwd;

my $version = readFile("version");
$version =~ s/[^\w\.]//gs;

opendir(local* LOCALES, "chrome/locale") || die "Could not read directory chrome/locales";
my @locales = grep {!/[^\w\-]/} readdir(LOCALES);
@locales = sort {$a eq "en-US" ? -1 : ($b eq "en-US" ? 1 : $a cmp $b)} @locales; 
closedir(LOCALES);

my @files = ();
my $installManifest = fixupFile(readFile("install.rdf"));
push @files, ["install.rdf", $installManifest];

my $cleanManifest = $installManifest;
$cleanManifest =~ s/<(\w+:)?targetApplication>.*?<\/\1targetApplication>//gs;
$cleanManifest =~ s/<(\w+:)?requires>.*?<\/\1requires>//gs;

die "Failed to extract extension name from install manifest" unless $cleanManifest =~ /<(\w+:)?name>([^<>]+)<\/\1name>/;
my $name = $2;

die "Failed to extract extension ID from install manifest" unless $cleanManifest =~ /<(\w+:)?id>([^<>]+)<\/\1id>/;
my $id = $2;

my $chromeManifest = fixupFile(readFile("chrome.manifest"));
my $baseURL = cwd;
$baseURL =~ s/\\/\//g;
$baseURL = "file:///$baseURL";
$chromeManifest =~ s~jar:chrome/\w+\.jar!~$baseURL/chrome~g;
$chromeManifest =~ s~^\s*resource\s+\S+\s+~$&$baseURL/~gmi;
$chromeManifest =~ s~^content ~content   mochikit $baseURL/chrome/content/mochitest/\n$&~m if -d "chrome/content/mochitest";

push @files, ["chrome.manifest", $chromeManifest];

my @dirs;
if (open(local *DIRS, ".profileDirs"))
{
  @dirs = map {s/[\r\n]//g;$_} <DIRS>;
  close(DIRS);
}
unless (@dirs)
{
  print STDERR <<EOT;
This script requires a file with the name .profileDirs to be in the current
directory. Please create this file and specify the directories of the profiles
where you want to install a test version of $name, one per line.
For example:

  c:\\Documents and Setting\\<user>\\Application Data\\Mozilla\\Firefox\\Profiles\\<seed>.default
  c:\\Documents and Setting\\<user>\\Application Data\\Songbird1\\Profiles\\<seed>.default
EOT
  exit 1;
}

foreach my $file (<components/*.js>, <defaults/preferences/*.js>)
{
  push @files, [$file, fixupFile(readFile($file))];
}

foreach my $dir (@dirs)
{
  unless (-e $dir)
  {
    warn "Directory '$dir' not found, skipping";
    next;
  }
  unless (-e "$dir/extensions")
  {
    warn "Directory '$dir/extensions' not found, skipping";
    next;
  }

  my $baseDir = "$dir/extensions/$id";
  rm_rec($baseDir);

  mkdir($baseDir);

  foreach my $file (@files)
  {
    my ($filename, $content) = @$file;

    my @parentDirs = split(/\//, $filename);
    pop @parentDirs;
    my $parentDir = $baseDir;
    foreach (@parentDirs)
    {
      $parentDir .= "/" . $_;
      mkdir($parentDir);
    }

    writeFile("$baseDir/$filename", $content);
  }
}

sub readFile
{
  my $file = shift;

  open(local *FILE, "<", $file) || die "Could not read file '$file'";
  binmode(FILE);
  local $/;
  my $result = <FILE>;
  close(FILE);

  return $result;
}

sub writeFile
{
  my ($file, $contents) = @_;

  open(local *FILE, ">", $file) || die "Could not write file '$file'";
  binmode(FILE);
  print FILE $contents;
  close(FILE);
}

sub fixupFile
{
  my $str = shift;

  $str =~ s/{{VERSION}}/$version/g;
  $str =~ s/^.*{{LOCALE}}.*$/
    my @result = ();
    my $template = $&;
    foreach my $locale (@locales)
    {
      push(@result, $template);
      $result[-1] =~ s~{{LOCALE}}~$locale~g;
    }
    join("\n", @result);
  /mge;

  return $str;
}
<commit_msg>Making setupTestEnvironment.pl handle {{BUILD}} tag correctly<commit_after>#!/usr/bin/perl

#############################################################################
# This script will set up a test version of the extension in the profiles   #
# listed in .profileDirs file (one per line). This means that manifests,    #
# components and preferences are copied to the profile (and must be         #
# copied again on changes) while current directory is used for the chrome.  #
# If you set nglayout.debug.disable_xul_cache preference to true the        #
# changes in current directory will be available in the application without #
# restart. Also, tests (if any) will be available under                     #
# chrome://mochikit/content/harness-???.xul.                                #
#                                                                           #
# Example of .profileDirs contents:                                         #
#                                                                           #
#  c:\Documents and Setting\<user>\Application Data\Mozilla\Firefox\Profiles\<seed>.default
#  c:\Documents and Setting\<user>\Application Data\Songbird1\Profiles\<seed>.default
#                                                                           #
#############################################################################

use strict;
use warnings;
use Cwd;

my $version = readFile("version");
$version =~ s/[^\w\.]//gs;

opendir(local* LOCALES, "chrome/locale") || die "Could not read directory chrome/locales";
my @locales = grep {!/[^\w\-]/} readdir(LOCALES);
@locales = sort {$a eq "en-US" ? -1 : ($b eq "en-US" ? 1 : $a cmp $b)} @locales; 
closedir(LOCALES);

my @files = ();
my $installManifest = fixupFile(readFile("install.rdf"));
push @files, ["install.rdf", $installManifest];

my $cleanManifest = $installManifest;
$cleanManifest =~ s/<(\w+:)?targetApplication>.*?<\/\1targetApplication>//gs;
$cleanManifest =~ s/<(\w+:)?requires>.*?<\/\1requires>//gs;

die "Failed to extract extension name from install manifest" unless $cleanManifest =~ /<(\w+:)?name>([^<>]+)<\/\1name>/;
my $name = $2;

die "Failed to extract extension ID from install manifest" unless $cleanManifest =~ /<(\w+:)?id>([^<>]+)<\/\1id>/;
my $id = $2;

my $chromeManifest = fixupFile(readFile("chrome.manifest"));
my $baseURL = cwd;
$baseURL =~ s/\\/\//g;
$baseURL = "file:///$baseURL";
$chromeManifest =~ s~jar:chrome/\w+\.jar!~$baseURL/chrome~g;
$chromeManifest =~ s~^\s*resource\s+\S+\s+~$&$baseURL/~gmi;
$chromeManifest =~ s~^content ~content   mochikit $baseURL/chrome/content/mochitest/\n$&~m if -d "chrome/content/mochitest";

push @files, ["chrome.manifest", $chromeManifest];

my @dirs;
if (open(local *DIRS, ".profileDirs"))
{
  @dirs = map {s/[\r\n]//g;$_} <DIRS>;
  close(DIRS);
}
unless (@dirs)
{
  print STDERR <<EOT;
This script requires a file with the name .profileDirs to be in the current
directory. Please create this file and specify the directories of the profiles
where you want to install a test version of $name, one per line.
For example:

  c:\\Documents and Setting\\<user>\\Application Data\\Mozilla\\Firefox\\Profiles\\<seed>.default
  c:\\Documents and Setting\\<user>\\Application Data\\Songbird1\\Profiles\\<seed>.default
EOT
  exit 1;
}

foreach my $file (<components/*.js>, <defaults/preferences/*.js>)
{
  push @files, [$file, fixupFile(readFile($file))];
}

foreach my $dir (@dirs)
{
  unless (-e $dir)
  {
    warn "Directory '$dir' not found, skipping";
    next;
  }
  unless (-e "$dir/extensions")
  {
    warn "Directory '$dir/extensions' not found, skipping";
    next;
  }

  my $baseDir = "$dir/extensions/$id";
  rm_rec($baseDir);

  mkdir($baseDir);

  foreach my $file (@files)
  {
    my ($filename, $content) = @$file;

    my @parentDirs = split(/\//, $filename);
    pop @parentDirs;
    my $parentDir = $baseDir;
    foreach (@parentDirs)
    {
      $parentDir .= "/" . $_;
      mkdir($parentDir);
    }

    writeFile("$baseDir/$filename", $content);
  }
}

sub readFile
{
  my $file = shift;

  open(local *FILE, "<", $file) || die "Could not read file '$file'";
  binmode(FILE);
  local $/;
  my $result = <FILE>;
  close(FILE);

  return $result;
}

sub writeFile
{
  my ($file, $contents) = @_;

  open(local *FILE, ">", $file) || die "Could not write file '$file'";
  binmode(FILE);
  print FILE $contents;
  close(FILE);
}

sub fixupFile
{
  my $str = shift;

  $str =~ s/{{VERSION}}/$version/g;
  $str =~ s/{{BUILD}}//g;
  $str =~ s/^.*{{LOCALE}}.*$/
    my @result = ();
    my $template = $&;
    foreach my $locale (@locales)
    {
      push(@result, $template);
      $result[-1] =~ s~{{LOCALE}}~$locale~g;
    }
    join("\n", @result);
  /mge;

  return $str;
}

{'fraction_non_alphanumeric': 0.20178230426479948, 'fraction_numerical': 0.002546148949713558, 'mean_word_length': 2.8682806729585555, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4788191', 'n_tokens_mistral': 3101, 'n_tokens_neox': 3013, 'n_words': 803}