pinport 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.md +21 -0
- data/README.md +50 -0
- data/bin/pinport +142 -0
- data/lib/pinport/core_ext/hash.rb +15 -0
- data/lib/pinport/error.rb +30 -0
- data/lib/pinport/generators/generator.rb +11 -0
- data/lib/pinport/generators/templates/config/config.yml +41 -0
- data/lib/pinport/parser.rb +74 -0
- data/lib/pinport/version.rb +3 -0
- data/lib/pinport.rb +177 -0
- data/pinport.gemspec +27 -0
- metadata +141 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 4e6decec98406cbf6572de83a023f6d746965bfb
|
|
4
|
+
data.tar.gz: 61d9b68ac81489cdde13e3fed979daa1f6457e37
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e28234ce6aacc3642a7dc037cc71c7c61723c6a572dee7325c00a1fef2e182f60d7b999c3dc99874be702b4c3e72c1d2fd3a78b978ef9ceede92dd3bf5f9f16a
|
|
7
|
+
data.tar.gz: 7770d071f68f284580dd1fe4ff9729fdafd3e1c70aba9829f323672859540b53c590f802f5a28c8399e4e304530538017a4c6e84928ed757671c210ace6348b5
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
###The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 Francis San Juan http://github.com/ftsanjuan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# pinport
|
|
2
|
+
|
|
3
|
+
Pinport is a PIN importing command line utility written in Ruby.
|
|
4
|
+
|
|
5
|
+
Currently supports importing to a MySQL database.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
``` sh
|
|
10
|
+
gem install pinport
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Configuration
|
|
15
|
+
|
|
16
|
+
A `config.yml` file specifying details for your MySQL database is required. Pinport uses the file
|
|
17
|
+
|
|
18
|
+
### Generating a config.yml file
|
|
19
|
+
|
|
20
|
+
Use `pinport generate` to generate the required `config.yml` file in the current directory.
|
|
21
|
+
|
|
22
|
+
Pinport uses the `mysql2` gem to establish the connection to the database. For available database connection options, refer to: https://github.com/brianmario/mysql2#connection-options.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Importing a single .txt file:
|
|
28
|
+
|
|
29
|
+
``` sh
|
|
30
|
+
pinport import FILE
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
FILE should contain one line per item to be imported.
|
|
34
|
+
|
|
35
|
+
### Importing a folder of .txt files:
|
|
36
|
+
``` sh
|
|
37
|
+
pinport import FOLDER
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
## Development
|
|
42
|
+
Clone this repository using `git clone`.
|
|
43
|
+
|
|
44
|
+
Navigate to the directory of cloned repository and run `rake` to compile and install the gem from
|
|
45
|
+
source.
|
|
46
|
+
|
|
47
|
+
To do the tasks separately:
|
|
48
|
+
|
|
49
|
+
- `rake build` to compile the gem
|
|
50
|
+
- `rake install` to install the gem.
|
data/bin/pinport
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'thor'
|
|
4
|
+
require 'pinport'
|
|
5
|
+
|
|
6
|
+
class Cli < Thor
|
|
7
|
+
include Thor::Actions
|
|
8
|
+
package_name "Pinport"
|
|
9
|
+
|
|
10
|
+
# Imports a file contaning pins into the database
|
|
11
|
+
desc "import FILE", "Imports pins from FILE"
|
|
12
|
+
method_option :table, :default => nil, :aliases => '-t'
|
|
13
|
+
method_option :column, :default => nil, :aliases => '-c'
|
|
14
|
+
method_option :filter, :default => nil, :aliases => '-f'
|
|
15
|
+
method_option :fix_newlines, :type => :boolean, :default => true, :aliases => '-newlines'
|
|
16
|
+
method_option :config_file, :default => 'config.yml', :aliases => '-config'
|
|
17
|
+
def import(file)
|
|
18
|
+
table = options[:table]
|
|
19
|
+
column = options[:column]
|
|
20
|
+
filter = options[:filter]
|
|
21
|
+
fix_newlines = options[:fix_newlines]
|
|
22
|
+
config_file = options[:config_file]
|
|
23
|
+
|
|
24
|
+
begin
|
|
25
|
+
test = File.open(file)
|
|
26
|
+
rescue => e
|
|
27
|
+
puts e.message
|
|
28
|
+
exit 1
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
begin
|
|
32
|
+
check_config config_file
|
|
33
|
+
rescue => e
|
|
34
|
+
puts e.message
|
|
35
|
+
else
|
|
36
|
+
# Otherwise... attempt to import pins from FILE into database
|
|
37
|
+
puts "Attempting import using following settings:"
|
|
38
|
+
puts "PINs source: #{file}"
|
|
39
|
+
puts "Target Database: #{Pinport.database_name}"
|
|
40
|
+
puts "Table: #{Pinport.table_name}"
|
|
41
|
+
puts "Column: #{Pinport.column_name}"
|
|
42
|
+
if yes? "Continue with import?"
|
|
43
|
+
Pinport.import_file(file, table, column, filter, fix_newlines)
|
|
44
|
+
say "pinport import complete.", :green
|
|
45
|
+
else
|
|
46
|
+
say "Import cancelled.", :red
|
|
47
|
+
exit 1
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Imports a folder of files containing pins into the database
|
|
53
|
+
desc "import_folder FOLDER", "Imports pins from files in a FOLDER"
|
|
54
|
+
method_option :extension, :default => 'txt', :aliases => '-ext'
|
|
55
|
+
method_option :table, :default => nil, :aliases => '-t'
|
|
56
|
+
method_option :column, :default => nil, :aliases => '-c'
|
|
57
|
+
method_option :filter, :default => nil, :aliases => '-f'
|
|
58
|
+
method_option :fix_newlines, :type => :boolean, :default => true, :aliases => '-newlines'
|
|
59
|
+
method_option :config_file, :default => 'config.yml', :aliases => '-config'
|
|
60
|
+
def import_folder(folder)
|
|
61
|
+
extension = options[:extension]
|
|
62
|
+
table = options[:table]
|
|
63
|
+
column = options[:column]
|
|
64
|
+
filter = options[:filter]
|
|
65
|
+
fix_newlines = options[:fix_newlines]
|
|
66
|
+
config_file = options[:config_file]
|
|
67
|
+
|
|
68
|
+
begin
|
|
69
|
+
check_config config_file
|
|
70
|
+
rescue => e
|
|
71
|
+
puts e.message
|
|
72
|
+
else
|
|
73
|
+
# Otherwise... attempt to import pins from FILE into database
|
|
74
|
+
puts "Attempting import using following settings:"
|
|
75
|
+
file_targets = Dir.glob("#{folder}/*.#{extension}")
|
|
76
|
+
puts "Found: #{file_targets.count} .#{extension} files in #{folder}:"
|
|
77
|
+
file_targets.each do |f|
|
|
78
|
+
puts " #{f}"
|
|
79
|
+
end
|
|
80
|
+
puts "Target:"
|
|
81
|
+
puts " Database: #{Pinport.database_name}"
|
|
82
|
+
puts " Table: #{Pinport.table_name}"
|
|
83
|
+
puts " Column: #{Pinport.column_name}"
|
|
84
|
+
|
|
85
|
+
if yes? "Continue with import?"
|
|
86
|
+
Pinport.import_folder(folder, extension, table, column, filter, fix_newlines)
|
|
87
|
+
say "pinport import_folder complete.", :green
|
|
88
|
+
else
|
|
89
|
+
say "Import cancelled.", :red
|
|
90
|
+
exit 1
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Generates a base config file
|
|
96
|
+
desc "generate", "Generates a base config.yml file in the current directory"
|
|
97
|
+
method_option :output, :default => 'config.yml', :aliases => '-o'
|
|
98
|
+
def generate(output='config.yml')
|
|
99
|
+
output = "#{Dir.pwd}/#{options[:output]}"
|
|
100
|
+
say "Generating a config file in: #{output}"
|
|
101
|
+
Pinport::Generator.generate_config(output)
|
|
102
|
+
say "pinport generate complete.", :green
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Verifies if the supplied config file is valid
|
|
106
|
+
desc "check_config CONFIG_FILE", "Checks pinport's configuration and initializes it"
|
|
107
|
+
def check_config(config_file)
|
|
108
|
+
# Prompts user to generate a default config.yml file if it doesn't exist
|
|
109
|
+
if !File.exists?("#{Dir.pwd}/config.yml")
|
|
110
|
+
if yes? "No config file found in current directory, generate one?"
|
|
111
|
+
# invoke generate with default parameters
|
|
112
|
+
invoke :generate, ["config.yml"]
|
|
113
|
+
else
|
|
114
|
+
raise "Config file not found."
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Prompts user to use the default config file
|
|
119
|
+
if config_file.empty?
|
|
120
|
+
if yes? "Config file not specified, use default config.yml?"
|
|
121
|
+
config_file = 'config.yml'
|
|
122
|
+
else
|
|
123
|
+
raise "No config file specified used."
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Convert config file path to be absolute
|
|
128
|
+
config_file = File.absolute_path(config_file)
|
|
129
|
+
|
|
130
|
+
# Attempt to initialize the module
|
|
131
|
+
begin
|
|
132
|
+
# Check if module can successfully use the supplied config_file
|
|
133
|
+
Pinport.initialize(config_file)
|
|
134
|
+
rescue => e
|
|
135
|
+
puts e.message
|
|
136
|
+
say "Unable to use #{config_file} to configure Pinport.", :red
|
|
137
|
+
say "Verify that it is a valid YAML file, or try running 'pinport generate' to create a new config file.", :red
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
Cli.start(ARGV)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class Hash
|
|
2
|
+
# Returns a hash containing symbolized keys
|
|
3
|
+
#
|
|
4
|
+
# @param hash [Hash]
|
|
5
|
+
# @return [Hash] The same hash with symbolized keys
|
|
6
|
+
def symbolize_keys
|
|
7
|
+
return nil unless hash
|
|
8
|
+
result = {}
|
|
9
|
+
each_key do |key|
|
|
10
|
+
result[key.to_sym] = self[key]
|
|
11
|
+
end
|
|
12
|
+
result
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module Pinport
|
|
2
|
+
class Error < StandardError
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
class DatabaseConnectionError < Error
|
|
6
|
+
def message
|
|
7
|
+
@msg = "An error occurred while establishing database connection.\n"
|
|
8
|
+
@msg += "Please verify your database connection information."
|
|
9
|
+
@msg
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class InvalidFileError < SystemCallError
|
|
14
|
+
def message
|
|
15
|
+
"An invalid file or directory was specified. Please try again."
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class UnexpectedArgumentError < ArgumentError
|
|
20
|
+
def message
|
|
21
|
+
"Encountered an unexpected argument."
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class DirectoryArgumentError < UnexpectedArgumentError
|
|
26
|
+
def message
|
|
27
|
+
"A directory was supplied as an argument to a method expecting a file argument."
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Settings used to connect to the database
|
|
2
|
+
#
|
|
3
|
+
# refer to mysql2 gem documentation for additional connection options
|
|
4
|
+
# https://github.com/brianmario/mysql2#connection-options
|
|
5
|
+
#
|
|
6
|
+
database:
|
|
7
|
+
host: localhost
|
|
8
|
+
username:
|
|
9
|
+
password:
|
|
10
|
+
database:
|
|
11
|
+
socket:
|
|
12
|
+
|
|
13
|
+
# Database schema settings
|
|
14
|
+
#
|
|
15
|
+
# table:
|
|
16
|
+
# the table name to import pins into
|
|
17
|
+
# column:
|
|
18
|
+
# the name of the column (field) to insert pins into
|
|
19
|
+
schema:
|
|
20
|
+
table: pins
|
|
21
|
+
column: pin
|
|
22
|
+
|
|
23
|
+
# Terminal output (notification) settings
|
|
24
|
+
#
|
|
25
|
+
# import:
|
|
26
|
+
# every:
|
|
27
|
+
# prints a confirmation message every nth time a pin is imported
|
|
28
|
+
# reduce number to increase import performance
|
|
29
|
+
#
|
|
30
|
+
notifications:
|
|
31
|
+
import:
|
|
32
|
+
every: 20000
|
|
33
|
+
|
|
34
|
+
# Various debugging settings
|
|
35
|
+
#
|
|
36
|
+
# errors:
|
|
37
|
+
# verbose:
|
|
38
|
+
# if set to true, will display error backtraces
|
|
39
|
+
debug:
|
|
40
|
+
errors:
|
|
41
|
+
verbose: false
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module Pinport
|
|
2
|
+
class Parser
|
|
3
|
+
require 'tempfile'
|
|
4
|
+
require 'shellwords'
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
# Fixes linebreaks on files and returns result
|
|
8
|
+
# @param file [String] path to the file to be processed by dos2unix
|
|
9
|
+
# @param output_path [String] path to file where output should be returned, default: same as file
|
|
10
|
+
# @return [String] path to file that was processed
|
|
11
|
+
def dos2unix(file, output_path = nil)
|
|
12
|
+
puts "Fixing newlines for #{file} ..."
|
|
13
|
+
if File.exists?(file)
|
|
14
|
+
input = File.open(file)
|
|
15
|
+
output = Tempfile.new("/tmp")
|
|
16
|
+
regex = /\r\n/
|
|
17
|
+
newstring = "\n"
|
|
18
|
+
while (line = input.gets) do
|
|
19
|
+
output_line = line.gsub(regex, newstring)
|
|
20
|
+
output << output_line
|
|
21
|
+
end
|
|
22
|
+
input.close
|
|
23
|
+
output.close
|
|
24
|
+
output_path = file unless output_path != nil
|
|
25
|
+
system("mv #{Shellwords.shellescape(output.path)} #{Shellwords.shellescape(output_path)}")
|
|
26
|
+
puts "Parser: Corrected newlines."
|
|
27
|
+
return output_path
|
|
28
|
+
# return "Newlines were successfully fixed for #{file}"
|
|
29
|
+
else
|
|
30
|
+
puts "Specified file does not exist."
|
|
31
|
+
return nil
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Strips characters to be filtered and returns result
|
|
36
|
+
# @param file [String] path to the file to be processed
|
|
37
|
+
# @param chars [String] a string of characters to be filtered out
|
|
38
|
+
# @param output_path [String] path to file where output should be returned, defaults to same path as `file` parameter
|
|
39
|
+
# @return [String] path to file that was processed
|
|
40
|
+
def strip_chars(file, chars, output_path = nil)
|
|
41
|
+
puts "Filtering: #{file} ..."
|
|
42
|
+
if File.exists?(file)
|
|
43
|
+
input = File.open(file)
|
|
44
|
+
output = Tempfile.new("/tmp")
|
|
45
|
+
while (line = input.gets) do
|
|
46
|
+
output_line = line.gsub!(chars, '')
|
|
47
|
+
output << output_line
|
|
48
|
+
end
|
|
49
|
+
input.close
|
|
50
|
+
output.close
|
|
51
|
+
output_path = file unless output_path != nil
|
|
52
|
+
system("mv #{Shellwords.shellescape(output.path)} #{Shellwords.shellescape(output_path)}")
|
|
53
|
+
puts "Parser: Filtered #{chars}."
|
|
54
|
+
return output_path
|
|
55
|
+
else
|
|
56
|
+
puts "Specified file does not exist."
|
|
57
|
+
return nil
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Runs `sort` command to sort contents of a text file. Returns true if successfully outputted to a file; otherwise prints the contents of the result
|
|
62
|
+
# @param input [String] path to the file to be sorted
|
|
63
|
+
# @param output_path [String] the path to
|
|
64
|
+
def sort_file_contents(input, output_path = nil)
|
|
65
|
+
puts "Sorting: #{input} ..."
|
|
66
|
+
# Shellescape arguments to prevent errors
|
|
67
|
+
system("sort #{Shellwords.shellescape(input)} -o #{Shellwords.shellescape(output_path)}") unless output_path == nil
|
|
68
|
+
return true
|
|
69
|
+
# otherwise: print the result
|
|
70
|
+
`sort #{input}`
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
data/lib/pinport.rb
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
|
2
|
+
|
|
3
|
+
require 'mysql2'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
require 'sequel'
|
|
6
|
+
require 'fileutils'
|
|
7
|
+
require 'pinport/parser'
|
|
8
|
+
require 'pinport/error'
|
|
9
|
+
require 'pinport/core_ext/hash'
|
|
10
|
+
require 'pinport/generators/generator'
|
|
11
|
+
|
|
12
|
+
module Pinport
|
|
13
|
+
@db = nil
|
|
14
|
+
@settings = nil
|
|
15
|
+
@settings_file = nil
|
|
16
|
+
@notifications = nil
|
|
17
|
+
@verbose_errors = nil
|
|
18
|
+
|
|
19
|
+
def self.initialize(file)
|
|
20
|
+
@db = db_connection(self.settings['database'])
|
|
21
|
+
@verbose_errors = self.settings['debug']['errors']['verbose']
|
|
22
|
+
self.settings_file = file
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.db
|
|
26
|
+
@db ||= db_connection(self.settings['database'])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.settings
|
|
30
|
+
@settings ||= YAML.load_file(self.settings_file)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.settings=(new_settings)
|
|
34
|
+
@settings = new_settings
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.schema
|
|
38
|
+
@settings['schema'] ||= self.settings['schema']
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.database_name
|
|
42
|
+
@settings['database']['database'] ||= self.settings['database']['database']
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.table_name
|
|
46
|
+
@settings['schema']['table'] ||= self.settings['schema']['table']
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.column_name
|
|
50
|
+
@settings['schema']['column'] ||= self.settings['schema']['column']
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Loads a config file to use with pinport
|
|
54
|
+
# @param file [String] the config file to load
|
|
55
|
+
# @return [Hash] Parsed settings as a hash
|
|
56
|
+
def self.load_config_file(file)
|
|
57
|
+
if File.exists?(file)
|
|
58
|
+
puts "Loading config file: #{file}"
|
|
59
|
+
return YAML.load_file(file)
|
|
60
|
+
else
|
|
61
|
+
puts "Could not find config file: #{file}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Updates settings_file and settings vars if specified
|
|
66
|
+
# config file can be found
|
|
67
|
+
# @param file [String] the config file to be loaded for settings
|
|
68
|
+
def self.settings_file=(file)
|
|
69
|
+
if File.exists?(file)
|
|
70
|
+
@settings_file = file
|
|
71
|
+
self.settings = load_config_file(@settings_file)
|
|
72
|
+
return true
|
|
73
|
+
else
|
|
74
|
+
raise "Specified settings file could assigned."
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.settings_file
|
|
79
|
+
@settings_file ||= 'config.yml'
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.root
|
|
83
|
+
File.expand_path '../..', __FILE__
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def self.notifications
|
|
87
|
+
@notifications ||= self.settings['notifications']
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Establishes a connection to the database
|
|
91
|
+
# @param db_config [String] path to a YAML file specifying database connection details, refer to mysql2 connection options for available settings (https://github.com/brianmario/mysql2#connection-options).
|
|
92
|
+
# @return [Sequel::Mysql2::Database]
|
|
93
|
+
def self.db_connection(db_config)
|
|
94
|
+
db_config = db_config.symbolize_keys
|
|
95
|
+
db_config[:test] = true
|
|
96
|
+
begin
|
|
97
|
+
Sequel.mysql2(db_config)
|
|
98
|
+
rescue => e
|
|
99
|
+
puts e.message
|
|
100
|
+
if @verbose_errors
|
|
101
|
+
puts "Trace:\n"
|
|
102
|
+
puts e.backtrace
|
|
103
|
+
end
|
|
104
|
+
exit 1
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Imports pins into database
|
|
109
|
+
#
|
|
110
|
+
# @param file [String] path to the file to be imported
|
|
111
|
+
# @param table [String] the table name that pins should be imported into
|
|
112
|
+
# @param column [String] the column name pins should be inserted into, default: "pin"
|
|
113
|
+
# @param filter [String] a string of characters to filter out from each pin
|
|
114
|
+
# @param fix_newlines [Boolean] specifies whether to fix newlines (in case of cross-OS incompatibilities), default: true
|
|
115
|
+
def self.import_file(file, table = nil, column = nil, filter = nil, fix_newlines = true)
|
|
116
|
+
# convert supplied file path to absolute
|
|
117
|
+
file_original = "#{file}"
|
|
118
|
+
file = File.absolute_path("#{file}")
|
|
119
|
+
if !File.exists?(file)
|
|
120
|
+
raise Pinport::InvalidFileError
|
|
121
|
+
elsif File.directory?(file)
|
|
122
|
+
raise Pinport::DirectoryArgumentError
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# escape filename as precaution
|
|
126
|
+
# file = Shellwords.shellescape(file)
|
|
127
|
+
source = File.open(file)
|
|
128
|
+
source_dirname = File.dirname(file)
|
|
129
|
+
source_basename = File.basename(file, '.txt')
|
|
130
|
+
cleaned_filename = "#{source_dirname}/#{source_basename}-cleaned.txt"
|
|
131
|
+
sorted_filename = "#{source_dirname}/#{source_basename}-sorted.txt"
|
|
132
|
+
|
|
133
|
+
# clean up txt files
|
|
134
|
+
cleaned = Pinport::Parser.dos2unix(file) unless fix_newlines == false
|
|
135
|
+
cleaned = Pinport::Parser.strip_chars(cleaned, filter, cleaned_filename) unless filter == nil
|
|
136
|
+
|
|
137
|
+
# perform the sort, if successful...
|
|
138
|
+
if Pinport::Parser.sort_file_contents(cleaned, sorted_filename)
|
|
139
|
+
|
|
140
|
+
# build a Sequel dataset and insert pins into it
|
|
141
|
+
# retrieve table/column names from schema if not specified
|
|
142
|
+
puts "Importing..."
|
|
143
|
+
table = self.table_name if table == nil
|
|
144
|
+
column = self.column_name if column == nil
|
|
145
|
+
pins = self.db[table.to_sym]
|
|
146
|
+
|
|
147
|
+
# open the sorted file and insert each pin line-by-line
|
|
148
|
+
count = 0
|
|
149
|
+
start_time = Time.now
|
|
150
|
+
File.open(sorted_filename).each_line do |line|
|
|
151
|
+
pins.insert(column.to_sym => line.chomp)
|
|
152
|
+
count += 1
|
|
153
|
+
puts "Imported #{count} pins." if (count % self.notifications['import']['every'] == 0)
|
|
154
|
+
end
|
|
155
|
+
puts "Finished importing #{count} pins into #{table}."
|
|
156
|
+
|
|
157
|
+
# remove any temporary files that were created
|
|
158
|
+
File.delete(cleaned_filename) if File.exists?(cleaned_filename)
|
|
159
|
+
File.delete(sorted_filename) if File.exists?(sorted_filename)
|
|
160
|
+
else
|
|
161
|
+
puts "Error encountered while sorting pins in file."
|
|
162
|
+
return false
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Imports pins from folder containing pin files
|
|
167
|
+
#
|
|
168
|
+
# Accepts the same parameters as `import_file` along with the following:
|
|
169
|
+
# @param extension [String] file extension of files to be imported, default: 'txt'
|
|
170
|
+
def self.import_folder(folder, extension = "txt", table = nil, column = nil, filter = nil, fix_newlines = true)
|
|
171
|
+
folder = File.absolute_path(folder)
|
|
172
|
+
Dir.glob("#{folder}/*.#{extension}").each do |file|
|
|
173
|
+
file = File.absolute_path("#{file}")
|
|
174
|
+
import_file(file, table, column, filter, fix_newlines)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
data/pinport.gemspec
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require File.expand_path('lib/pinport/version')
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = 'pinport'
|
|
5
|
+
s.version = Pinport::VERSION
|
|
6
|
+
s.date = '2013-04-29'
|
|
7
|
+
s.summary = %q{A PIN importing tool}
|
|
8
|
+
s.description = %q{Pinport is a command-line tool for importing a large list of PINs (Personal Identification Numbers) into a MySQL Database}
|
|
9
|
+
s.authors = ['Francis San Juan']
|
|
10
|
+
s.email = 'francis.sanjuan@gmail.com'
|
|
11
|
+
s.license = 'MIT'
|
|
12
|
+
s.licenses = ['MIT']
|
|
13
|
+
s.homepage = 'https://github.com/ftsanjuan/pinport'
|
|
14
|
+
s.require_paths = ['lib']
|
|
15
|
+
s.files = %w(README.md LICENSE.md README.md pinport.gemspec)
|
|
16
|
+
s.files += Dir.glob("lib/**/*.rb")
|
|
17
|
+
s.files += Dir.glob("lib/**/generators/templates/**/*")
|
|
18
|
+
s.executables << 'pinport'
|
|
19
|
+
|
|
20
|
+
s.add_runtime_dependency 'bundler', '~> 1.0'
|
|
21
|
+
s.add_runtime_dependency 'mysql2', '~> 0.3.11'
|
|
22
|
+
s.add_runtime_dependency 'sequel', '~> 3.46.0'
|
|
23
|
+
|
|
24
|
+
s.add_development_dependency 'rake', '~> 10.0.4'
|
|
25
|
+
s.add_development_dependency 'yard', '~> 0.8.6.1'
|
|
26
|
+
s.add_development_dependency 'thor', '~> 0.18.1'
|
|
27
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pinport
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Francis San Juan
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-04-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: mysql2
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 0.3.11
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 0.3.11
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: sequel
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ~>
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.46.0
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ~>
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 3.46.0
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ~>
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 10.0.4
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ~>
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 10.0.4
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: yard
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ~>
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 0.8.6.1
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ~>
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 0.8.6.1
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: thor
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ~>
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 0.18.1
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ~>
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 0.18.1
|
|
97
|
+
description: Pinport is a command-line tool for importing a large list of PINs (Personal
|
|
98
|
+
Identification Numbers) into a MySQL Database
|
|
99
|
+
email: francis.sanjuan@gmail.com
|
|
100
|
+
executables:
|
|
101
|
+
- pinport
|
|
102
|
+
extensions: []
|
|
103
|
+
extra_rdoc_files: []
|
|
104
|
+
files:
|
|
105
|
+
- README.md
|
|
106
|
+
- LICENSE.md
|
|
107
|
+
- pinport.gemspec
|
|
108
|
+
- lib/pinport/core_ext/hash.rb
|
|
109
|
+
- lib/pinport/error.rb
|
|
110
|
+
- lib/pinport/generators/generator.rb
|
|
111
|
+
- lib/pinport/parser.rb
|
|
112
|
+
- lib/pinport/version.rb
|
|
113
|
+
- lib/pinport.rb
|
|
114
|
+
- lib/pinport/generators/templates/config/config.yml
|
|
115
|
+
- bin/pinport
|
|
116
|
+
homepage: https://github.com/ftsanjuan/pinport
|
|
117
|
+
licenses:
|
|
118
|
+
- MIT
|
|
119
|
+
metadata: {}
|
|
120
|
+
post_install_message:
|
|
121
|
+
rdoc_options: []
|
|
122
|
+
require_paths:
|
|
123
|
+
- lib
|
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - '>='
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '0'
|
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
|
+
requirements:
|
|
131
|
+
- - '>='
|
|
132
|
+
- !ruby/object:Gem::Version
|
|
133
|
+
version: '0'
|
|
134
|
+
requirements: []
|
|
135
|
+
rubyforge_project:
|
|
136
|
+
rubygems_version: 2.0.0
|
|
137
|
+
signing_key:
|
|
138
|
+
specification_version: 4
|
|
139
|
+
summary: A PIN importing tool
|
|
140
|
+
test_files: []
|
|
141
|
+
has_rdoc:
|