File: Dir.pm

package info (click to toggle)
libvcs-perl 0.14-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 264 kB
  • ctags: 282
  • sloc: perl: 1,083; makefile: 567
file content (205 lines) | stat: -rw-r--r-- 5,330 bytes parent folder | download | duplicates (2)
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
package VCS::Dir;

use VCS::File;

my $PREFIX = 'VCS';

sub new {
    my $container_classtype = shift;
    $container_classtype =~ s#^$PREFIX##;
    my ($hostname, $impl_class, $path, $query) = VCS->parse_url(@_);
    VCS->class_load($impl_class);
    my $this_class = "$impl_class$container_classtype";
    return $this_class->new(@_);
}

# assumes no query string
sub init {
    my($class, $url) = @_;
    my ($hostname, $impl_class, $path, $query) = VCS->parse_url($url);
    if (substr($path, -1, 1) ne '/') {
        $path .= '/';
        $url .= '/';
    }
    my $self = {};
    $self->{HOSTNAME} = $hostname;
    $self->{IMPL_CLASS} = $impl_class;
    $self->{PATH} = $path;
    $self->{URL} = $url;
    bless $self, $class;
    return $self;
}

sub url {
    my $self = shift;
    $self->{URL};
}

sub content {
}

sub path {
    my $self = shift;
    $self->{PATH};
}

sub tags {
  my $self = shift;
  my $rh = {}; # result hash
  my @files = $self->recursive_read_dir();

  my $url;

  foreach my $file (@files) {
    my $vcsfile = eval { VCS::File->new('vcs://'.$self->{HOSTNAME}.'/'.$self->{IMPL_CLASS}.'/'.$file) } or next;
    my $file_tag_information = $vcsfile->tags();
    foreach my $filetag (keys(%$file_tag_information)) {
      $rh->{$filetag}->{$file} = $file_tag_information->{$filetag};
    }
  }

  return $rh;

}


sub recursive_read_dir {
  my $self = shift;
  my ($dir) = @_;
  $dir ||= $self->path(); # let it take path if its not been
                          # defined, i'm not really sure about this,
                          # to be honest the whole things need an
                          # an overhaul in the way it works,
                          # but for now i'm just happy to get
                          # my work done. - Greg
  $dir.='/' unless (substr($dir,-1,1) eq '/');
  my @files;
  opendir(DIR,$dir);
  my @contents = grep { (!/^\.\.?$/) } readdir(DIR);
  @contents = grep { (!/,v$/) } @contents; # RCS files, shouldn't matter if they are RCS/*,v or just *,v
  @contents = grep { (!/^CVS$/) } @contents;

  closedir(DIR);
  foreach my $content (@contents) {
    if (-d $dir.$content) {
      push(@files,($self->recursive_read_dir($dir.$content)));
    } else {
      push(@files,$dir.$content);
    }
  }
  return @files;
}

sub read_dir {
    my ($self, $dir) = @_;
    local *DIR;
    opendir DIR, $dir;
    my @d = grep { (!/^\.\.?$/) } readdir DIR;
    closedir DIR;
#warn "d: @d\n";
    @d;
}

1;

__END__

=head1 NAME

VCS::Dir - module for access to a VCS directory

=head1 SYNOPSIS

    use VCS;
    my $d = VCS::Dir->new($url);
    print $d->url . "\n";
    foreach my $x ($d->content) {
        print "\t" . $x->url . "\t" . ref($x) . "\n";
    }

=head1 DESCRIPTION

C<VCS::Dir> abstracts access to a directory under version control.

=head1 METHODS

Methods marked with a "*" are not yet finalised/implemented.

=head2 VCS::Dir-E<gt>create_new($url) *

C<$url> is a file-container URL.  Creates data as
appropriate to convince the VCS that there is a file-container, and
returns an object of class C<VCS::Dir>, or throws an exception if it
fails. This is a pure virtual method, which must be over-ridden, and
cannot be called directly in this class (a C<die> will result).

=head2 VCS::Dir-E<gt>introduce($name, $create_class) *

C<$name> is a file or directory name, absolute or relative.
C<$create_class> is either C<File> or C<Dir>, and implementation
classes are expected to use something similar to this code, to call the
appropriate create_new:

    sub introduce {
        my ($class, $name, $create_class) = @_;
        my $call_class = $class;
        $call_class =~ s/[^:]+$/$create_class/;
        return $call_class->create_new($name);
    }

This is a pure virtual method, which must be over-ridden, and cannot be
called directly in this class (a C<die> will result).

=head2 VCS::Dir-E<gt>new($url)

C<$url> is a file-container URL.  Returns an object of class
C<VCS::Dir>, or throws an exception if it fails. Normally, an override of
this method will call C<VCS::Dir-E<gt>init($url)> to make an object,
and then add to it as appropriate.

=head2 VCS::Dir-E<gt>init($url)

C<$url> is a file-container URL.  Returns an object of class
C<VCS::Dir>. This method calls C<VCS-E<gt>parse_url> to make sense of
the URL.

=head2 $dir-E<gt>tags

* THIS METHOD WORKS RECURSIVELY ON THE DIRECTORY AT HAND *

Returns all the tags inside a directory and a little bit more
information. The actual datstructure is a hash of hashes. The first
level hash is a hash keyed on tag names, in other words it lists as
its keys every single tag name in or below a directory. Each of
these tag names point to another hash with has filenames as keys
and version numbers as values.

=head2 $dir-E<gt>url

Returns the C<$url> argument to C<new>.

=head2 $dir-E<gt>content

Returns a list of objects, either of class C<VCS::Dir> or
C<VCS::File>, corresponding to files and directories within this
directory.

=head2 $dir-E<gt>path

Returns the absolute path of the directory.

=head2 $dir-E<gt>read_dir($dir)

Returns the contents of the given filesystem directory. This is intended
as a utility method for subclasses.

=head1 SEE ALSO

L<VCS>.

=head1 COPYRIGHT

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut