/[couchdb]/design/design-couch.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /design/design-couch.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (show annotations)
Tue Apr 28 15:06:02 2009 UTC (15 years ago) by dpavlin
File MIME type: text/plain
File size: 3458 byte(s)
first version which can push design document (without _attachments)
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use LWP::UserAgent;
7 use JSON;
8 use Data::Dump qw/dump/;
9 use File::Path qw/mkpath/;
10 use File::Slurp qw//;
11 use File::Find;
12 use HTTP::Request::Common;
13
14 # design-couch.pl
15 #
16 # 04/26/09 21:12:28 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
17
18 my ( $command, $database, $design ) = @ARGV;
19 die "usage: $0 [push|pull] database design\n" unless $database && $design;
20
21 my $ua = LWP::UserAgent->new;
22
23 my $url = "http://llin.lan:5984/$database/_design/$design";
24
25 sub create_path {
26 my $path = shift;
27 if ( $path =~ m{/} ) {
28 my $dir = $path;
29 $dir =~ s{/[^/]+$}{};
30 mkpath $dir if ! -e $dir;
31 #warn "# dir $dir";
32 }
33 }
34 sub write_file {
35 my ( $path, $content ) = @_;
36 $path =~ s{^/+}{};
37 create_path $path;
38 File::Slurp::write_file $path, $content;
39 print "$path ", -s $path, " bytes\n";
40 }
41
42 sub write_attachment {
43 my ( $path ) = @_;
44 my $file = "_attachemnts/$path";
45 create_path $file;
46 $ua->mirror( "$url/$path", $file );
47 print "detached $file ", -s $file, " bytes\n";
48 }
49
50 sub unroll {
51 my ( $tree, $path ) = @_;
52
53 my $ref = ref $tree;
54 if ( $ref eq 'HASH' ) {
55 foreach my $child ( keys %$tree ) {
56 if ( $child eq '_attachments' ) {
57 write_attachment $_ foreach keys %{ $tree->{$child} };
58 } else {
59 unroll( $tree->{$child}, $path ? "$path/$child" : $child );
60 }
61 }
62 } elsif ( $ref ) {
63 warn "UNSUPPORTED $path $ref ", dump( $tree );
64 write_file "$path.json", to_json $tree;
65 } elsif ( $ref eq '' ) {
66
67 if ( $tree =~ m[^\s*(function(.*){.*}|/\*|//|var)]is ) {
68 $path .= '.js';
69 } elsif ( $tree =~ m{<%=.*%>} ) { # couchapp template
70 $path .= '.html';
71 } else {
72 warn "# can't detect type of $path\n";
73 }
74
75 write_file $path, $tree;
76 }
77
78 }
79
80 if ( $command eq 'pull' ) {
81
82 warn "# get $url\n";
83 my $response = $ua->get( $url );
84 die $response->status_line if $response->is_error;
85
86 my $json = $response->decoded_content;
87 write_file "../$database-$design.pull.js", $json;
88
89 unroll( from_json $json, '' );
90
91 } elsif ( $command eq 'push' ) {
92
93 my $json;
94
95 my @attached;
96
97 find({ no_chdir => 1, wanted => sub {
98 my $path = $File::Find::name;
99 return unless -f $path;
100
101 warn "## $path\n";
102
103 $path =~ s{^\./}{};
104
105 if ( $path =~ m{_attachemnts} ) {
106 push @attached, $path;
107 return;
108 }
109
110 my $data = File::Slurp::read_file( $path );
111 $path =~ s[/]['}->{']g;
112 $path =~ s{\.\w+$}{};
113 my $code = "\$json->{'$path'} = \$data;";
114 eval $code;
115 die "ERROR in $code: $@" if $@;
116 # warn "## json = ",dump( $json );
117 }}, '.' );
118
119 warn "# $database/_design/$design = ",dump( $json );
120 write_file "../$database-$design.push.js", to_json $json;
121
122 die "no _id?" unless $json->{_id};
123 delete( $json->{_rev} );
124
125 warn "# put $url\n";
126 my $response = $ua->request( HTTP::Request::Common::PUT($url, 'Content-Type' => 'application/json', Content => to_json $json ) );
127
128 if ( $response->code == 409 ) {
129 warn "## get old $url\n";
130 my $response = $ua->get( $url );
131 die $response->status_line if $response->is_error;
132
133 my $data = from_json $response;
134 $json->{$_} = $data->{$_} foreach ( 'rev', '_id' );
135
136 $response = $ua->request( HTTP::Request::Common::PUT($url, 'Content-Type' => 'application/json', Content => to_json $json ) );
137 die $response->status_line if $response->is_error;
138 warn "push updated $url\n";
139 } else {
140 die $response->status_line if $response->is_error;
141 warn "push new $url\n";
142 }
143
144 warn "FIXME: ignore attachments: ",dump( @attached );
145
146 } else {
147 die "$0: unknown command $command";
148 }
149

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26