/[pxelator]/couchdb/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 /couchdb/design-couch.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 341 - (show annotations)
Sat Aug 29 14:43:32 2009 UTC (10 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 4214 byte(s)
create directory for new design on pull

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 use MIME::Base64;
14
15 use lib qw(lib ../lib);
16 use Media::Type::Simple;
17
18
19 # design-couch.pl
20 #
21 # 04/26/09 21:12:28 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
22
23 my ( $command, $database, $design ) = @ARGV;
24 die "usage: $0 [push|pull] database design\n" unless $database && $design;
25
26 if ( ! -e $design && $command eq 'pull') {
27 warn "# create new design $design\n";
28 mkdir $design;
29 }
30
31 chdir $design || die "can't find $design: $!";
32
33 my $ua = LWP::UserAgent->new;
34
35 my $url = "http://localhost:5984/$database/_design/$design";
36
37 sub create_path {
38 my $path = shift;
39 if ( $path =~ m{/} ) {
40 my $dir = $path;
41 $dir =~ s{/[^/]+$}{};
42 mkpath $dir if ! -e $dir;
43 #warn "# dir $dir";
44 }
45 }
46 sub write_file {
47 my ( $path, $content ) = @_;
48 $path =~ s{^/+}{};
49 create_path $path;
50 File::Slurp::write_file $path, $content;
51 print "$path ", -s $path, " bytes created\n";
52 }
53
54 sub write_attachment {
55 my ( $path ) = @_;
56 my $file = "_attachemnts/$path";
57 create_path $file;
58 $ua->mirror( "$url/$path", $file );
59 print "detached $file ", -s $file, " bytes\n";
60 }
61
62
63 sub unroll {
64 my ( $tree, $path ) = @_;
65
66 my $ref = ref $tree;
67 if ( $ref eq 'HASH' ) {
68 foreach my $child ( keys %$tree ) {
69 if ( $child eq '_attachments' ) {
70 write_attachment $_ foreach keys %{ $tree->{$child} };
71 } else {
72 unroll( $tree->{$child}, $path ? "$path/$child" : $child );
73 }
74 }
75 } elsif ( $ref ) {
76 warn "UNSUPPORTED $path $ref ", dump( $tree );
77 write_file "$path.json", to_json $tree;
78 } elsif ( $ref eq '' ) {
79
80 if ( $tree =~ m[^\s*(function(.*){.*}|/\*|//|var)]is ) {
81 $path .= '.js';
82 } elsif ( $tree =~ m{<%=.*%>} ) { # couchapp template
83 $path .= '.html';
84 } else {
85 warn "# can't detect type of $path\n";
86 }
87
88 write_file $path, $tree;
89 }
90
91 }
92
93 if ( $command eq 'pull' ) {
94
95 warn "# get $url\n";
96 my $response = $ua->get( $url );
97 die $response->status_line if $response->is_error;
98
99 my $json = $response->decoded_content;
100 write_file "../$database-$design.pull.js", $json;
101
102 unroll( from_json $json, '' );
103
104 } elsif ( $command eq 'push' ) {
105
106 my $json;
107
108 find({ no_chdir => 1, wanted => sub {
109 my $path = $File::Find::name;
110 return unless -f $path;
111 return if $path =~ m{/\.svn};
112
113 warn "## $path\n";
114
115 $path =~ s{^\./}{};
116
117 if ( $path =~ m{_attachemnts/(.+)} ) {
118
119 my $filename = $1;
120 my $content_type = 'text/plain';
121 $content_type = type_from_ext($1) if $filename =~ m{\.(\w+)$};
122
123 my $data = File::Slurp::read_file( $path );
124 $data = encode_base64( $data );
125 # XXX inline attachments must be single line
126 # XXX http://wiki.apache.org/couchdb/HTTP_Document_API
127 $data =~ s/[\n\r]+//gs;
128 $json->{_attachments}->{ $filename } = {
129 content_type => $content_type,
130 data => $data,
131 };
132 return;
133 }
134
135 my $data = File::Slurp::read_file( $path );
136 $path =~ s[/]['}->{']g;
137 $path =~ s{\.\w+$}{};
138 my $code = "\$json->{'$path'} = \$data;";
139 eval $code;
140 die "ERROR in $code: $@" if $@;
141 # warn "## json = ",dump( $json );
142 }}, '.' );
143
144 if ( ! defined $json->{_id} ) {
145 warn "creating _id for document\n";
146 $json->{_id} = $$ . '-' . time();
147 }
148 delete( $json->{_rev} ) && warn "removing _rev from document\n";
149
150 print "push $database/_design/$design\n";
151 write_file "../$database-$design.push.js", to_json $json;
152
153 warn "# put $url\n";
154 my $response = $ua->request(
155 HTTP::Request::Common::PUT(
156 $url,
157 'Content-Type' => 'application/json',
158 Content => to_json $json,
159 )
160 );
161
162 if ( $response->code == 409 ) {
163 warn "## update $url\n";
164 my $response = $ua->get( $url );
165 die $response->status_line if $response->is_error;
166
167 my $data = from_json $response->decoded_content;
168 $json->{$_} = $data->{$_} foreach ( '_rev', '_id' );
169
170 $response = $ua->request( HTTP::Request::Common::PUT($url, 'Content-Type' => 'application/json', Content => to_json $json ) );
171 die $response->status_line if $response->is_error;
172 warn "push updated $url\n";
173 } else {
174 die $response->status_line if $response->is_error;
175 warn "push new $url\n";
176 }
177
178 } else {
179 die "$0: unknown command $command";
180 }
181

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26