/[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

Annotation of /couchdb/design-couch.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26