pastiepacker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 1.0.0 2008-04-03
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Dr Nic Williams
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ bin/pastiepacker
7
+ config/hoe.rb
8
+ config/requirements.rb
9
+ lib/pastiepacker.rb
10
+ lib/pastiepacker/command.rb
11
+ lib/pastiepacker/conversion.rb
12
+ lib/pastiepacker/fetch.rb
13
+ lib/pastiepacker/header.rb
14
+ lib/pastiepacker/io.rb
15
+ lib/pastiepacker/options.rb
16
+ lib/pastiepacker/upload.rb
17
+ lib/pastiepacker/version.rb
18
+ lib/ruby-ext/hash.rb
19
+ script/console
20
+ script/destroy
21
+ script/generate
22
+ script/txt2html
23
+ setup.rb
24
+ tasks/deployment.rake
25
+ tasks/environment.rake
26
+ tasks/website.rake
27
+ test/fixtures/private_pastie.html
28
+ test/fixtures/sample_app/History.txt
29
+ test/fixtures/sample_app/README.txt
30
+ test/fixtures/sample_app/lib/myapp.rb
31
+ test/test_app.rb
32
+ test/test_conversion.rb
33
+ test/test_helper.rb
34
+ website/index.html
35
+ website/index.txt
36
+ website/javascripts/rounded_corners_lite.inc.js
37
+ website/stylesheets/screen.css
38
+ website/template.html.erb
@@ -0,0 +1,79 @@
1
+ = pastiepacker
2
+
3
+ * http://pastiepacker.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ Prepare to pack or unpack piles of files with the pastiepacker.
8
+
9
+ == SYNOPSIS:
10
+
11
+ To pack a folder: +pastiepacker+
12
+
13
+ Packing options:
14
+
15
+ -f, --format=FORMAT Possess pasties with a particular persona
16
+ Supported formats:
17
+ c++, css, diff, html_rails, html,
18
+ javascript, php, plain_text, python,
19
+ ruby, ruby_on_rails, sql, shell-unix-generic
20
+ Default: ruby
21
+ -m, --message=MESSAGE Promotional passage for your pastie
22
+ -p, --private Posted pasties are private
23
+ Ignored for unpacking
24
+ -s, --stdout Prints packed pasties instead of posting
25
+ -H, --no-header Prevents placing pastiepacker promotion in pasties
26
+ That is, no 'about:' section is added to the top of pasties
27
+ -h, --help Show this help message.
28
+
29
+ To only pack a selection of files ending with *txt* you can pass a list of file names via STDIN:
30
+
31
+ find * | grep "txt$" | pastiepacker
32
+
33
+ It outputs the url of the prepared pastie, so you can pipe it to xargs:
34
+
35
+ pastiepacker | xargs open
36
+
37
+ To unpack a packed pastie: pastiepacker http://pastie.caboo.se/175183
38
+ - This unpacks the files into a subfolder 175138/
39
+ To unpack a private pastie: pastiepacker http://pastie.caboo.se/private/5hwfheniddqmyasmfcxaw
40
+ - This unpacks the files into a subfolder 5hwfheniddqmyasmfcxaw/
41
+
42
+
43
+ == REQUIREMENTS:
44
+
45
+ * ruby
46
+ * rubygems
47
+
48
+ To test if you have these, the following should work:
49
+
50
+ gem env
51
+
52
+ == INSTALL:
53
+
54
+ * sudo gem install pastiepacker
55
+
56
+ == LICENSE:
57
+
58
+ (The MIT License)
59
+
60
+ Copyright (c) 2008 Dr Nic Williams
61
+
62
+ Permission is hereby granted, free of charge, to any person obtaining
63
+ a copy of this software and associated documentation files (the
64
+ "Software"), to deal in the Software without restriction, including
65
+ without limitation the rights to use, copy, modify, merge, publish,
66
+ distribute, sublicense, and/or sell copies of the Software, and to
67
+ permit persons to whom the Software is furnished to do so, subject to
68
+ the following conditions:
69
+
70
+ The above copyright notice and this permission notice shall be
71
+ included in all copies or substantial portions of the Software.
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
74
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
75
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
76
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
77
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
78
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
79
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created on 2008-4-3.
4
+ # Copyright (c) 2008. All rights reserved.
5
+
6
+ require File.dirname(__FILE__) + "/../lib/pastiepacker"
7
+ output = PastiePacker.run(ARGV)
8
+ puts output if output
@@ -0,0 +1,83 @@
1
+ require 'pastiepacker/version'
2
+
3
+ AUTHOR = 'Dr Nic Williams' # can also be an array of Authors
4
+ EMAIL = "drnicwilliams@gmail.com"
5
+ DESCRIPTION = <<-EOS
6
+ Prepare to pack or unpack piles of files with the pastiepacker.
7
+
8
+ To pack a folder: #{File.basename($0)}
9
+ To pack some files ending with "txt": find * | grep "txt$" | #{File.basename($0)}
10
+ - It outputs the url of the prepared pastie, so you can pipe it to xargs:
11
+ - pastiepacker | xargs open
12
+
13
+ To unpack a packed pastie: #{File.basename($0)} http://pastie.caboo.se/175183
14
+ - This unpacks the files into a subfolder 175138/
15
+ EOS
16
+ GEM_NAME = 'pastiepacker' # what ppl will type to install your gem
17
+ RUBYFORGE_PROJECT = 'pastiepacker' # The unix name for your project
18
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
19
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
20
+
21
+ @config_file = "~/.rubyforge/user-config.yml"
22
+ @config = nil
23
+ RUBYFORGE_USERNAME = "unknown"
24
+ def rubyforge_username
25
+ unless @config
26
+ begin
27
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
28
+ rescue
29
+ puts <<-EOS
30
+ ERROR: No rubyforge config file found: #{@config_file}
31
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
32
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
33
+ EOS
34
+ exit
35
+ end
36
+ end
37
+ RUBYFORGE_USERNAME.replace @config["username"]
38
+ end
39
+
40
+
41
+ REV = nil
42
+ # UNCOMMENT IF REQUIRED:
43
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
44
+ VERS = PastiePacker::VERSION::STRING + (REV ? ".#{REV}" : "")
45
+ RDOC_OPTS = ['--quiet', '--title', 'pastiepacker documentation',
46
+ "--opname", "index.html",
47
+ "--line-numbers",
48
+ "--main", "README",
49
+ "--inline-source"]
50
+
51
+ class Hoe
52
+ def extra_deps
53
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
54
+ @extra_deps
55
+ end
56
+ end
57
+
58
+ # Generate all the Rake tasks
59
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
60
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
61
+ p.developer(AUTHOR, EMAIL)
62
+ p.description = DESCRIPTION
63
+ p.summary = DESCRIPTION
64
+ p.url = HOMEPATH
65
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
66
+ p.test_globs = ["test/**/test_*.rb"]
67
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
68
+
69
+ # == Optional
70
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
71
+ p.extra_deps = [
72
+ ['shared-mime-info', '>=0.1'],
73
+ ['hpricot', '>=0.6']
74
+ ] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
75
+
76
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
77
+
78
+ end
79
+
80
+ CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
81
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
82
+ $hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
83
+ $hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require "fileutils"
5
+ require 'net/http'
6
+ require 'timeout'
7
+ require 'cgi'
8
+ require 'optparse'
9
+ require "open-uri"
10
+
11
+ begin
12
+ require 'rubygems'
13
+ rescue LoadError
14
+ end
15
+
16
+ require 'shared-mime-info' # shared-mime-info rubygem
17
+ require 'hpricot'
18
+
19
+ require "ruby-ext/hash"
20
+ require "pastiepacker/io"
21
+ require "pastiepacker/options"
22
+ require "pastiepacker/conversion"
23
+ require "pastiepacker/upload"
24
+ require "pastiepacker/fetch"
25
+ require "pastiepacker/header"
26
+ require "pastiepacker/command"
@@ -0,0 +1,50 @@
1
+ class PastiePacker
2
+ attr_accessor :contents
3
+
4
+ def self.run(args = [])
5
+ packer = PastiePacker.new
6
+ packer.parse_options(args)
7
+ if !(files = packer.input_lines).empty?
8
+ packer.do_pack_files(files)
9
+ elsif args.empty?
10
+ packer.do_pack
11
+ else
12
+ packer.do_unpack(packer.args || [])
13
+ end
14
+ end
15
+
16
+ def do_pack
17
+ self.contents = self.path_to_string(FileUtils.pwd)
18
+ if contents && !contents.empty?
19
+ add_header(File.basename(FileUtils.pwd)) unless no_header?
20
+ do_paste
21
+ end
22
+ end
23
+
24
+ def do_pack_files(files)
25
+ self.contents = self.files_to_string(files)
26
+ if contents && !contents.empty?
27
+ add_header unless no_header?
28
+ do_paste
29
+ end
30
+ end
31
+
32
+ def do_unpack(pastie_urls)
33
+ pastie_urls.each do |raw_url|
34
+ url = process_url(raw_url)
35
+ unique_subfolder = url.match(/[\/=]([^\/=]*)$/)[1]
36
+ self.contents = fetch_pastie(url)
37
+ unpack(File.join(FileUtils.pwd, unique_subfolder))
38
+ end
39
+ nil # so nothing is printed as a result
40
+ end
41
+
42
+ def process_url(raw_url)
43
+ if raw_url =~ /private\/(.*)$/
44
+ doc = Hpricot(fetch_pastie(raw_url))
45
+ (doc/"a.utility").find { |a| a.inner_html == 'View' }.get_attribute("href")
46
+ else
47
+ raw_url.gsub(/\.txt$/,'')
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,53 @@
1
+ class PastiePacker
2
+ def path_to_string(path)
3
+ output = ""
4
+ FileUtils.cd path do
5
+ output = files_to_string Dir['**/*'].sort
6
+ end
7
+ output
8
+ end
9
+
10
+ def files_to_string(files)
11
+ readme = files.find { |f| f =~ /^readme/i }
12
+ files = [readme] + (files - [readme]) if readme
13
+ output = files.inject([]) do |mem, file|
14
+ file = file.strip
15
+ if File.file?(file)
16
+ mem << "## #{file}"
17
+ if ascii? file
18
+ mem << File.open(file, 'r').read
19
+ else
20
+ end
21
+ mem << ""
22
+ end
23
+ mem
24
+ end.join("\n")
25
+ output
26
+ end
27
+
28
+ def unpack(target_path)
29
+ FileUtils.rm_rf "#{target_path}/*" # destroys previously unpacked pastie with same id
30
+ FileUtils.mkdir_p target_path
31
+ FileUtils.cd target_path do
32
+ files_contents = contents.split(/^## /)[1..-1] # ignore first ""
33
+ files_contents.each do |file_contents|
34
+ file_name, contents = file_contents.match(/([^\n]+)\n(.*)/m)[1,2]
35
+ if file_name =~ /about:/
36
+ # ignoring header
37
+ else
38
+ contents = contents.gsub(/\n\Z/,'')
39
+ FileUtils.mkdir_p File.dirname(file_name)
40
+ File.open(file_name, "w") do |f|
41
+ f << contents
42
+ end
43
+ end
44
+ end
45
+ end
46
+ target_path
47
+ end
48
+
49
+ protected
50
+ def ascii?(file)
51
+ MIME.check(file) == 'text/plain'
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ class PastiePacker
2
+ def fetch_pastie(url)
3
+ txt_url = "#{url}.txt"
4
+ # REMOVE: Net::HTTP.get(URI.parse(txt_url))
5
+ open(txt_url)
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ class PastiePacker
2
+ def add_header(title=nil)
3
+ self.contents = header(title) + self.contents
4
+ end
5
+
6
+ protected
7
+ def header(title=nil)
8
+ message = extra_message ? "#{extra_message}\n\n" : ""
9
+ if title
10
+ <<-EOS
11
+ ## about:#{title}
12
+ #{message}Files for #{title} uploaded by pastiepacker.
13
+ To unpack files see http://pastiepacker.rubyforge.org
14
+
15
+ EOS
16
+ else
17
+ <<-EOS
18
+ ## about:
19
+ #{message}Files uploaded by pastiepacker.
20
+ To unpack files see http://pastiepacker.rubyforge.org
21
+
22
+ EOS
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ class PastiePacker
2
+ def input_stream
3
+ $stdin
4
+ end
5
+
6
+ def output_stream
7
+ $stdout
8
+ end
9
+
10
+ def input_lines
11
+ if !input_stream.tty?
12
+ input_stream.readlines
13
+ else
14
+ []
15
+ end
16
+ end
17
+ end