summaryrefslogtreecommitdiff
path: root/perl5/Local/Homedir.pm
blob: 7d8e2850e4c2456d0356893e2927badf869ff520 (plain)
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
package Local::Homedir;

# homedir management functions
#
# Copyright (C) 2019 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.



# This code must be as portable as possible.  Only core perl modules.

use strict;
use warnings;
use autodie;

use Cwd qw(getcwd realpath);
use File::Find;
use File::Spec::Functions;
use Exporter 'import';
use File::Temp qw(tempfile);

our @EXPORT = qw( normalise_mrconfig src_register_all src_cleanup );

our %debian_source_repos;

sub normalise_mrconfig {
    my $master = $ENV{HOME} . "/src/dotfiles/.mrconfig.in";
    my $target = $ENV{HOME} . "/.mrconfig";

    unlink $target if -e $target and not -f $target;

    my %master_blocks = blocks_from_file($master);
    my %target_blocks = -f $target ? blocks_from_file($target) : ();

    my $warning = "DO NOT EDIT THIS BLOCK; automatically updated from";

    # filter out any DO NOT EDIT comments because if these blocks were
    # removed from ~/.mrconfig.in but the repos are still present on
    # this machine, those lines would be misleading
    $target_blocks{$_} =~ s/^# ($warning|  $master)\n//mg
      for keys %target_blocks;

    for (keys %master_blocks) {
        $target_blocks{$_} = "# $warning\n#   $master\n" . join "\n",
          grep !/^\s*#|^\s*$/, split "\n", $master_blocks{$_};
    }

    open my $fh, '>', $target;
    print $fh "# -*- mode: conf -*-\n";

    # any DEFAULT has to come first to have effect on the proceding
    # blocks
    say_block($fh, "DEFAULT", delete $target_blocks{DEFAULT})
      if $target_blocks{DEFAULT};
    say_block($fh, $_, $target_blocks{$_}) for keys %target_blocks;

    return 0;
}

sub blocks_from_file ($) {
    my $file = shift;

    my %blocks;
    my $current_block;
    open my $fh, '<', $file;
    while (<$fh>) {
        if (/^\[(.+)\]$/) {
            $current_block = $1;
        } elsif (defined $current_block) {
            $blocks{$current_block} .= $_;
        }
    }
    # drop trailing newlines from the text of each block
    { local $/ = ''; chomp $blocks{$_} foreach keys %blocks }
    return %blocks;
}

sub src_register_all {
    chdir;
    my @known_repos;
    foreach my $f (".mrconfig", "src/dotfiles/lib-src/mr/config") {
	open my $fh, "<", $f;
	while (<$fh>) {
	    if (/^\[(src\/.+)\]$/) {
		push @known_repos, $ENV{HOME}."/$1";
	    }
	}
    }
    find({wanted => sub {
              return unless is_repo($_);
	      my $oldpwd = getcwd;
              chdir $_;
              my $register_out = `mr -c $ENV{HOME}/.mrconfig register 2>&1`;
              unless ($? == 0) {
                  print STDERR "mr register: $File::Find::name\n";
                  print STDERR $register_out."\n";
                  die "src_register_all mr register attempt failed";
              }
              chdir $oldpwd;
          }, preprocess => sub {
              my $cwd = getcwd();
              # once we've found a repo, don't search inside it for more repos
              return () if is_repo($cwd);
              my @entries;
              # don't process repos mr already knows about
              foreach my $entry (@_) {
                  my $entry_path = $cwd."/$entry";
                  push @entries, $entry
                    unless grep /\A$entry_path\z/, @known_repos;
              }
              return @entries;
          }}, "src");
}

sub src_cleanup {
    return unless eval "use Dpkg::Changelog::Parse; use Dpkg::Version; 1";

    find({
            wanted => sub {
                my $dir = $_;
                my $ch = catfile($dir, "debian", "changelog");

                # if no changelog on the current branch, see if there is
                # one on another relevant branch (this covers ~/src/p5-*)
                unless (-f $ch) {
                    return unless is_repo($dir);
                    my @branches
                      = grep
                      m#^refs/(?:heads/debian$|(?:heads|remotes/dgit)/dgit/)#,
`git -C $dir for-each-ref --format='%(refname)' refs/heads/ refs/remotes/dgit/`;
                    chomp @branches;
                    my $branch;
                    for (@branches) {
                        $branch = $_, last
                          if grep m#debian/changelog#,
                          `git -C $dir ls-tree $_ debian/changelog`;
                    }
                    return unless $branch;
                    (my $fh, $ch) = tempfile UNLINK => 1;
                    open my $ph, "-|", "git", "-C", $dir, "cat-file", "blob",
                      "$branch:debian/changelog";
                    print $fh $_ for <$ph>;
                    close $fh;
                }

                my $changelog_entry = changelog_parse(file => $ch);
                $debian_source_repos{ $changelog_entry->{source} }
                  = catfile getcwd, $dir;
            },
            preprocess => sub {
                # skip .git (to avoid .git/dgit/unpack dirs), and once
                # we've found a source package, don't search inside
                # for more source packages
                -f catfile("debian", "changelog")
                  ? ()
                  : grep { $_ ne ".git" } @_
            }
        },
        "$ENV{HOME}/src"
    );
    while (my ($source, $dir) = each %debian_source_repos) {
        # binary package names may not be source package names, so
        # have to handle those separately
        unlink glob catfile $dir, "..", "*.deb";
        my $prefix = catfile $dir, "..", $source . "_";
        unlink glob $prefix . "*" . $_
          for ".dsc", ".diff.gz", ".upload", ".inmulti", ".changes",
          ".build", ".buildinfo", ".debian.tar.*", "[0-9~].tar.*";
        # ^ last one is native package tarballs but not orig.tars,
        # which are handled separately

        # we keep the two most recent orig.tar
        my @origs = sort {
            $a =~ /_([^_]+)\.orig\.tar/;
            my $ver_a = Dpkg::Version->new("$1");
            $b =~ /_([^_]+)\.orig\.tar/;
            my $ver_b = Dpkg::Version->new("$1");
            version_compare($ver_b, $ver_a);
        } grep !/\.asc\z/, map realpath($_), glob "$prefix*.orig.tar.*";
        if (@origs > 2) {
            shift @origs;
            shift @origs;
            for (@origs) {
                unlink $_;
                unlink "$_.asc" if -e "$_.asc";
            }
        }
    }

    # could also run `clean-patch-queues -y` here
}

sub say_block (*$$) {
    my ($fh, $block, $text) = @_;

    print $fh "\n[$block]\n";
    print $fh $text;
    print $fh "\n";
}

sub is_repo { -e "$_[0]/.git" or -d "$_[0]/.hg" }

1;