wordpress_config_parser 0.9.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 38f90fe48a8714b268601c33bced5f5c6e9b00a4
4
+ data.tar.gz: 06061cd7e28fad3c370c54b878a790fadeddf822
5
+ SHA512:
6
+ metadata.gz: a79084d5859d13b9ad4ae2d6692e981ae78e2624da37d3462406071c3a3e73c7d625055eeaf659569e72cec95cf5fa02fc614567f7caafa743ed8b00e2be32f0
7
+ data.tar.gz: 7c6a9f9e79f98d727954aba1b442dcd40ca23c91eac408f95c87fc4c81086aa8b7fbe3836930aaa018e67c125c04aef4f880245f8a1e953cf528c6c8fe5b93b4
data/README.md CHANGED
@@ -40,10 +40,37 @@ entire shell account), and pushes them to the origin repo:
40
40
 
41
41
  ```ruby
42
42
  #!/usr/bin/env ruby
43
-
44
- require 'wordpress_config_parser'
43
+ #
44
+ # backup-wordpress-db.rb
45
+ #
46
+ # Calls mysqldump to create a backup of a MySQL data base
47
+ # in the form of a .sql file containing the SQL commands
48
+ # required to reconstruct it.
49
+ #
50
+ # Assumptions:
51
+ #
52
+ # 1) The directory containing the output (backup) files is in a git repo.
53
+ #
54
+ # 2) Either you have .ssh keys set up, or you're willing to
55
+ # type in your password for the git push.
56
+ #
57
+ # 3) You're storing your SQL backup files in ~/sql-backups, and
58
+ # that directory already exists.
59
+ #
60
+ # 4) The blogs are all under the $HOME/public_html directory.
61
+ #
62
+ # 5) There are no changes git added when you run this script.
63
+ # Otherwise, those changes will be included in the commit,
64
+ # without any commit message to describe them.
65
+
66
+ require 'wordpress_config_reader'
45
67
  require 'shellwords'
46
68
 
69
+ if ARGV.empty?
70
+ puts "Syntax is backup-wordpress.rb blog_dir_name_1 [...blog_dir_name_n]."
71
+ exit -1
72
+ end
73
+
47
74
 
48
75
  def run_command(command)
49
76
  puts(command)
@@ -53,29 +80,28 @@ end
53
80
 
54
81
  home = ENV['HOME']
55
82
  output_dir = File.join(home, 'sql-backups')
56
- blognames = %w(blog1 blog2)
57
-
83
+ blognames = ARGV
58
84
 
59
85
  blognames.each do |blogname|
60
86
 
61
87
  blog_dir = File.join(home, 'public_html', blogname)
62
- parser = WCParser.new(blog_dir)
63
- time_str = Time.now.strftime("%Y_%m_%d__%H%M%S")
64
- outfilespec = File.join(output_dir, "#{blogname}-db-backup-#{time_str}.sql")
88
+ reader = WCReader.new(blog_dir)
89
+ outfilespec = File.join(output_dir, "#{blogname}-db-backup.sql")
65
90
 
66
- user = Shellwords.escape(parser.db_user)
67
- password = Shellwords.escape(parser.db_password)
68
- host = Shellwords.escape(parser.db_host)
69
- name = Shellwords.escape(parser.db_name)
91
+ user = Shellwords.escape(reader.db_user)
92
+ password = Shellwords.escape(reader.db_password)
93
+ host = Shellwords.escape(reader.db_host)
94
+ name = Shellwords.escape(reader.db_name)
70
95
 
71
96
 
72
- Dir.chdir(home) do # in case you have another .git dir where you are
97
+ Dir.chdir(output_dir) do # make sure we're in the right repo
73
98
  run_command("mysqldump -u#{user} -p#{password} -h#{host} #{name} 2>&1 | tee #{outfilespec}")
74
99
  run_command("git add #{outfilespec} 2>&1")
75
- run_command("git commit -m \"Added #{outfilespec}.\"")
76
100
  end
77
101
  end
78
102
 
103
+
104
+ run_command("git commit -m \"Updated SQL backups for blogs: #{blognames.join(', ')}.\"")
79
105
  run_command("git push -u origin master")
80
106
  ```
81
107
 
@@ -97,7 +123,7 @@ value = parser.db_xyz
97
123
  ```
98
124
 
99
125
  You can also call has_key? (with either string or symbol) to see if it's there
100
- before trying to get it. This function is very fast, and pulls the value into
126
+ before trying to get it. This function pulls the value into
101
127
  the cache if it wasn't already there, so the subsequent access to get the
102
128
  actual value is very fast.
103
129
 
@@ -123,9 +149,6 @@ Or install it yourself as:
123
149
 
124
150
  $ gem install wordpress_config_parser
125
151
 
126
- ## Usage
127
-
128
- TODO: Write usage instructions here
129
152
 
130
153
  ## Contributing
131
154
 
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
- # backup-wordpress-db.rb
3
+ # backup-wordpress-db
4
4
  #
5
5
  # Calls mysqldump to create a backup of a MySQL data base
6
6
  # in the form of a .sql file containing the SQL commands
@@ -22,7 +22,7 @@
22
22
  # Otherwise, those changes will be included in the commit,
23
23
  # without any commit message to describe them.
24
24
 
25
- require 'wordpress_config_reader'
25
+ require 'wordpress_config_parser'
26
26
  require 'shellwords'
27
27
 
28
28
  if ARGV.empty?
@@ -44,13 +44,13 @@ blognames = ARGV
44
44
  blognames.each do |blogname|
45
45
 
46
46
  blog_dir = File.join(home, 'public_html', blogname)
47
- reader = WCReader.new(blog_dir)
47
+ parser = WordpressConfigParser::Parser.new(blog_dir)
48
48
  outfilespec = File.join(output_dir, "#{blogname}-db-backup.sql")
49
49
 
50
- user = Shellwords.escape(reader.db_user)
51
- password = Shellwords.escape(reader.db_password)
52
- host = Shellwords.escape(reader.db_host)
53
- name = Shellwords.escape(reader.db_name)
50
+ user = Shellwords.escape(parser.db_user)
51
+ password = Shellwords.escape(parser.db_password)
52
+ host = Shellwords.escape(parser.db_host)
53
+ name = Shellwords.escape(parser.db_name)
54
54
 
55
55
 
56
56
  Dir.chdir(output_dir) do # make sure we're in the right repo
@@ -1,9 +1,4 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'wordpress_config_parser'))
2
2
 
3
3
  require "wordpress_config_parser/version"
4
-
5
-
6
- module WordpressConfigParser
7
-
8
- require 'wc_parser'
9
- end
4
+ require "wordpress_config_parser/parser"
@@ -0,0 +1,82 @@
1
+ module WordpressConfigParser
2
+ class Parser
3
+
4
+ attr_accessor :lines
5
+
6
+
7
+ def initialize(filespec_or_array)
8
+
9
+ @cache = {}
10
+
11
+ error_message = "WCParser constructor must be passed an array of lines, " +
12
+ "a config filespec, or a directory name that contains a wp-config.php file."
13
+
14
+ @lines = case filespec_or_array
15
+
16
+ when Array
17
+ filespec_or_array
18
+
19
+ when String # can be the config filespec or its directory
20
+ filespec = filespec_or_array
21
+
22
+ if File.directory?(filespec)
23
+ filespec = File.join(filespec, 'wp-config.php')
24
+ end
25
+
26
+ unless File.file?(filespec)
27
+ raise ArgumentError.new("File '#{filespec}' does not exist. #{error_message}")
28
+ end
29
+
30
+ File.readlines(filespec).map(&:chomp)
31
+
32
+ else # error: neither array nor string
33
+ raise ArgumentError.new(error_message)
34
+ end
35
+ end
36
+
37
+
38
+ def get(token)
39
+
40
+ hash_key = token.to_s.downcase.to_sym
41
+
42
+ @cache[hash_key] ||= begin
43
+ config_file_key = token.to_s.upcase
44
+ line = find_def_line(config_file_key)
45
+ line.nil? ? nil : extract_value_from_line(line) # Note: cache will contain any invalid keys requested
46
+ end
47
+ end
48
+
49
+
50
+ def has_key?(key)
51
+ !! get(key)
52
+ end
53
+
54
+
55
+ alias [] get
56
+
57
+ # For missing methods, assume the method_name is the name or symbol
58
+ # of a defined value's key. The key is looked up, and if it exists,
59
+ # its corresponding value is returned; otherwise, super's
60
+ # method_missing is called.
61
+ def method_missing(method_name, *method_args)
62
+ get(method_name) || super.method_missing(method_name, *method_args)
63
+ end
64
+
65
+
66
+ # Gets *last* matching def line's value, or nil if not found.
67
+ def find_def_line(token)
68
+ search_string = "define('#{token}'"
69
+ lines.reverse.detect { |line| line.start_with?(search_string) }
70
+ end
71
+
72
+
73
+ def extract_value_from_line(line)
74
+ # Ex: define('DB_NAME', 'abcdefgh_0001');
75
+ # Splitting by ' will leave value as array[3]
76
+ line.split("'")[3]
77
+ end
78
+
79
+ private :find_def_line, :extract_value_from_line
80
+
81
+ end
82
+ end
@@ -1,3 +1,3 @@
1
1
  module WordpressConfigParser
2
- VERSION = "0.9.0"
2
+ VERSION = "0.11.0"
3
3
  end
@@ -0,0 +1,86 @@
1
+ require_relative '../spec_helper'
2
+ require 'wordpress_config_parser'
3
+ require 'tempfile'
4
+
5
+ module WordpressConfigParser
6
+
7
+ describe Parser do
8
+
9
+
10
+ let(:sample_lines) { %w(abc 789) }
11
+ let(:sample_dirspec) { File.expand_path(File.join(File.dirname(__FILE__), '..', 'resources')) }
12
+ let(:sample_filespec) { File.join(sample_dirspec, 'wp-config.php') }
13
+ let(:sample_parser) { Parser.new(sample_filespec) }
14
+
15
+ it "should initialize correctly when creating with an array of lines" do
16
+ parser = Parser.new(sample_lines)
17
+ expect(parser.lines).to eq(sample_lines)
18
+ end
19
+
20
+ it "parser.lines should be an array when instantiating with a filespec" do
21
+ expect(sample_parser.lines).to be_an(Array)
22
+ end
23
+
24
+ specify "parser.lines should equal the initial array when instantiating with a filespec" do
25
+ parser = Parser.new(sample_filespec)
26
+ expect(parser.lines).to eq(File.readlines(sample_filespec).map(&:chomp))
27
+ end
28
+
29
+ it "should correctly extract the value from a config line" do
30
+ config_line = "define('DB_NAME', 'abcdefgh_0001');"
31
+ expect(sample_parser.send(:extract_value_from_line, config_line)).to eq('abcdefgh_0001')
32
+ end
33
+
34
+ it "should extract the correct (i.e., the last) matching line when > 1 matches are present" do
35
+ line = sample_parser.send(:find_def_line, 'DB_NAME')
36
+ value = sample_parser.send(:extract_value_from_line, line)
37
+ expect(value).to eq('mysite_wrd2')
38
+ end
39
+
40
+ specify "get should get the correct value" do
41
+ expect(sample_parser.get('DB_NAME')).to eq('mysite_wrd2')
42
+ end
43
+
44
+ it "[] operator should get the correct value" do
45
+ expect(sample_parser['DB_NAME']).to eq('mysite_wrd2')
46
+ end
47
+
48
+ it "should correctly generate a new method when receiving a property in lower case" do
49
+ expect(sample_parser.db_name).to eq('mysite_wrd2')
50
+ end
51
+
52
+ it "should correctly create a mysqladmin dump command with parameters from the file" do
53
+ db_name = sample_parser.db_name
54
+ db_password = sample_parser.db_password
55
+ db_hostname = sample_parser.db_host
56
+ db_user = sample_parser.db_user
57
+
58
+ # mysqldump -u#{userid} -p#{password} -h#{hostname} #{database_name} 2>&1 > #{outfilespec}`
59
+ command = "mysqldump -u#{db_user} -p#{db_password} -h#{db_hostname} #{db_name}"
60
+ expected = "mysqldump -umysite_user -pgobbledygook -hlocalhost mysite_wrd2"
61
+ expect(command).to eq(expected)
62
+ end
63
+
64
+ it "should correctly return true for has_key?(:db_name) and has_key?('DB_NAME')" do
65
+ expect(sample_parser.has_key?(:db_name) && sample_parser.has_key?('DB_NAME')).to eq(true)
66
+ end
67
+
68
+ it "should correctly return false for has_key?(:zyx) and has_key?('ZYX')" do
69
+ expect(sample_parser.has_key?(:zyx) || sample_parser.has_key?('ZYX')).to eq(false)
70
+ end
71
+
72
+ it "should throw an ArgumentError if a bad file is provided" do
73
+ expect{ Parser.new('/:!@#$%^&') }.to raise_error(ArgumentError)
74
+ end
75
+
76
+ it "should throw an ArgumentError if a something other than a string or array is provided" do
77
+ expect { Parser.new(123) }.to raise_error(ArgumentError)
78
+ end
79
+
80
+ it "should throw a NoMethodError if a method is called for which there is no key" do
81
+ expect { sample_parser.foo }.to raise_error(NoMethodError)
82
+ end
83
+
84
+ end
85
+ end
86
+
@@ -1,12 +1,13 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/wordpress_config_parser/version', __FILE__)
2
+ require_relative 'lib/wordpress_config_parser/version'
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Keith Bennett"]
6
6
  gem.email = ["keithrbennett@gmail.com"]
7
7
  gem.description = %q{Gets values defined in Wordpress wp-config.php file.}
8
- gem.summary = %q{Gets values defined in Wordpress wp-config.php file.}
8
+ gem.summary = gem.description
9
9
  gem.homepage = "https://github.com/keithrbennett/wordpress_config_parser"
10
+ gem.license = "MIT"
10
11
 
11
12
  gem.files = `git ls-files`.split($\)
12
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -14,4 +15,7 @@ Gem::Specification.new do |gem|
14
15
  gem.name = "wordpress_config_parser"
15
16
  gem.require_paths = ["lib"]
16
17
  gem.version = WordpressConfigParser::VERSION
18
+
19
+ gem.add_development_dependency "rspec", '~> 3.0'
20
+
17
21
  end
metadata CHANGED
@@ -1,62 +1,76 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordpress_config_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
5
- prerelease:
4
+ version: 0.11.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Keith Bennett
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-09-21 00:00:00.000000000 Z
13
- dependencies: []
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
14
27
  description: Gets values defined in Wordpress wp-config.php file.
15
28
  email:
16
29
  - keithrbennett@gmail.com
17
- executables: []
30
+ executables:
31
+ - backup-wordpress-db
18
32
  extensions: []
19
33
  extra_rdoc_files: []
20
34
  files:
21
- - .gitignore
35
+ - ".gitignore"
22
36
  - Gemfile
23
37
  - LICENSE
24
38
  - README.md
25
39
  - Rakefile
40
+ - bin/backup-wordpress-db
26
41
  - lib/wordpress_config_parser.rb
42
+ - lib/wordpress_config_parser/parser.rb
27
43
  - lib/wordpress_config_parser/version.rb
28
- - lib/wordpress_config_parser/wc_parser.rb
29
44
  - make-gem.sh
30
- - sample_scripts/backup-wordpress-db.rb
31
45
  - spec/resources/wp-config.php
32
46
  - spec/spec_helper.rb
33
- - spec/wordpress_config_reader/wc_reader_spec.rb
47
+ - spec/wordpress_config_parser/parser_spec.rb
34
48
  - wordpress_config_parser.gemspec
35
49
  homepage: https://github.com/keithrbennett/wordpress_config_parser
36
- licenses: []
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
37
53
  post_install_message:
38
54
  rdoc_options: []
39
55
  require_paths:
40
56
  - lib
41
57
  required_ruby_version: !ruby/object:Gem::Requirement
42
- none: false
43
58
  requirements:
44
- - - ! '>='
59
+ - - ">="
45
60
  - !ruby/object:Gem::Version
46
61
  version: '0'
47
62
  required_rubygems_version: !ruby/object:Gem::Requirement
48
- none: false
49
63
  requirements:
50
- - - ! '>='
64
+ - - ">="
51
65
  - !ruby/object:Gem::Version
52
66
  version: '0'
53
67
  requirements: []
54
68
  rubyforge_project:
55
- rubygems_version: 1.8.21
69
+ rubygems_version: 2.5.1
56
70
  signing_key:
57
- specification_version: 3
71
+ specification_version: 4
58
72
  summary: Gets values defined in Wordpress wp-config.php file.
59
73
  test_files:
60
74
  - spec/resources/wp-config.php
61
75
  - spec/spec_helper.rb
62
- - spec/wordpress_config_reader/wc_reader_spec.rb
76
+ - spec/wordpress_config_parser/parser_spec.rb
@@ -1,95 +0,0 @@
1
- class WCParser
2
-
3
- attr_accessor :lines
4
-
5
- def initialize(filespec_or_array)
6
-
7
- error_message = "WCParser constructor must be passed an array of lines, " +
8
- "a config filespec, or a directory name that contains a wp-config.php file."
9
-
10
- case filespec_or_array
11
-
12
- when Array
13
- @lines = filespec_or_array
14
-
15
- when String
16
- filespec = filespec_or_array
17
- unless File.exists?(filespec)
18
- raise ArgumentError.new("File/directory '#{filespec}' does not exist. #{error_message}")
19
- end
20
-
21
- if File.directory?(filespec)
22
- filespec = File.join(filespec, 'wp-config.php')
23
- unless File.exists?(filespec)
24
- raise ArgumentError.new("File '#{filespec}' does not exist. #{error_message}")
25
- end
26
- end
27
-
28
- @lines = File.readlines(filespec).map(&:chomp)
29
-
30
- else
31
- raise ArgumentError.new(error_message)
32
- end
33
-
34
- @cache = {}
35
- end
36
-
37
- # Ex: define('DB_NAME', 'abcdefgh_0001');
38
- def find_def_line(token)
39
- search_string = "define('#{token}'"
40
- matches = lines.select { |line| line[0...search_string.size] == search_string }
41
- matches.empty? ? nil : matches.last # last one wins
42
- end
43
-
44
-
45
- def extract_value_from_line(line)
46
- # Ex: define('DB_NAME', 'abcdefgh_0001');
47
- # Splitting by ' will leave value as array[3]
48
- line.split("'")[3]
49
- end
50
-
51
- def get(token)
52
-
53
- hash_key = token.to_s.downcase.to_sym
54
-
55
- if @cache.has_key?(hash_key)
56
- value = @cache[hash_key]
57
- else
58
- config_file_key = token.to_s.upcase
59
- line = find_def_line(config_file_key)
60
- value = (line.nil? ? nil : extract_value_from_line(line))
61
- @cache[hash_key] = value # Note: cache will contain any invalid keys requested
62
- end
63
- value
64
- end
65
-
66
-
67
- def has_key?(key)
68
- !! get(key)
69
- end
70
-
71
-
72
- alias [] get
73
-
74
- # For missing methods, assume the method_name is the name or symbol
75
- # of a defined value's key. If the key exists in the config file,
76
- # a method is created and that value returned. Otherwise, super's
77
- # method_missing will be called.
78
- def method_missing(method_name, *method_args)
79
-
80
- value = get(method_name)
81
-
82
- if value.nil?
83
- super.method_missing(method_name, *method_args)
84
- else
85
- instance_eval("""
86
- def #{method_name.to_s.downcase}
87
- get('#{method_name.to_s.upcase}')
88
- end
89
- """)
90
- end
91
-
92
- value
93
- end
94
-
95
- end
@@ -1,82 +0,0 @@
1
- require_relative '../spec_helper'
2
- require 'wc_parser'
3
- require 'tempfile'
4
-
5
- describe WCParser do
6
-
7
-
8
- let(:sample_lines) { %w(abc 789) }
9
- let(:sample_dirspec) { File.expand_path(File.join(File.dirname(__FILE__), '..', 'resources')) }
10
- let(:sample_filespec) { File.join(sample_dirspec, 'wp-config.php') }
11
- let(:sample_parser) { WCParser.new(sample_filespec) }
12
-
13
- it "should initialize correctly when calling create_with_lines" do
14
- parser = WCParser.new(sample_lines)
15
- parser.lines.should == sample_lines
16
- end
17
-
18
- it "parser.lines should be an array when instantiating with a filespec" do
19
- sample_parser.lines.should be_a Array
20
- end
21
-
22
- it "parser.lines should equal the initial array when calling create_with_filespec" do
23
- parser = WCParser.new(sample_filespec)
24
- parser.lines.should == File.readlines(sample_filespec).map(&:chomp)
25
- end
26
-
27
- it "should correctly extract the value from a config line" do
28
- config_line = "define('DB_NAME', 'abcdefgh_0001');"
29
- sample_parser.extract_value_from_line(config_line).should == 'abcdefgh_0001'
30
- end
31
-
32
- it "should extract the correct line when > 1 matches are present" do
33
- line = sample_parser.find_def_line('DB_NAME')
34
- value = sample_parser.extract_value_from_line(line)
35
- value.should == 'mysite_wrd2'
36
- end
37
-
38
- it "(get) should get the correct value" do
39
- sample_parser.get('DB_NAME').should == 'mysite_wrd2'
40
- end
41
-
42
- it "[] operator should get the correct value" do
43
- sample_parser['DB_NAME'].should == 'mysite_wrd2'
44
- end
45
-
46
- it "should correctly generate a new method when receiving a property in lower case" do
47
- sample_parser.db_name.should == 'mysite_wrd2'
48
- end
49
-
50
- it "should correctly create a mysqladmin dump command with parameters from the file" do
51
- db_name = sample_parser.db_name
52
- db_password = sample_parser.db_password
53
- db_hostname = sample_parser.db_host
54
- db_user = sample_parser.db_user
55
-
56
- # mysqldump -u#{userid} -p#{password} -h#{hostname} #{database_name} 2>&1 > #{outfilespec}`
57
- command = "mysqldump -u#{db_user} -p#{db_password} -h#{db_hostname} #{db_name}"
58
- expected = "mysqldump -umysite_user -pgobbledygook -hlocalhost mysite_wrd2"
59
- command.should == expected
60
- end
61
-
62
- it "should correctly return true for has_key?(:db_name) and has_key?('DB_NAME')" do
63
- (sample_parser.has_key?(:db_name) && sample_parser.has_key?('DB_NAME')).should be_true
64
- end
65
-
66
- it "should correctly return false for has_key?(:zyx) and has_key?('ZYX')" do
67
- (sample_parser.has_key?(:zyx) || sample_parser.has_key?('ZYX')).should be_false
68
- end
69
-
70
- it "should throw an ArgumentError if a bad file is provided" do
71
- lambda { WCParser.new('/:!@#$%^&') }.should raise_error(ArgumentError)
72
- end
73
-
74
- it "should throw an ArgumentError if a something other than a string or array is provided" do
75
- lambda { WCParser.new(123) }.should raise_error(ArgumentError)
76
- end
77
-
78
- it "should throw a NoMethodError if a method is called for which there is no key" do
79
- lambda { sample_parser.foo }.should raise_error(NoMethodError)
80
- end
81
-
82
- end