contentful-importer 0.0.2 → 0.1.0
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 +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +6 -4
- data/bin/contentful-importer +10 -10
- data/contentful_importer.gemspec +2 -2
- data/lib/contentful/importer/cli.rb +15 -0
- data/lib/contentful/importer/configuration.rb +38 -0
- data/lib/{converters → contentful/importer/converters}/content_types_structure_creator.rb +1 -1
- data/lib/{converters → contentful/importer/converters}/contentful_model_to_json.rb +1 -1
- data/lib/contentful/importer/data_organizer.rb +119 -0
- data/lib/contentful/importer/json_schema_validator.rb +66 -0
- data/lib/contentful/importer/migrator.rb +43 -0
- data/lib/contentful/importer/mime_content_type.rb +566 -0
- data/lib/contentful/importer/parallel_importer.rb +432 -0
- data/lib/contentful/importer/version.rb +5 -0
- data/spec/lib/configuration_spec.rb +16 -14
- data/spec/lib/importer/parallel_importer_spec.rb +151 -149
- data/spec/lib/json_schema_validator_spec.rb +50 -48
- data/spec/lib/migrator_spec.rb +86 -82
- data/spec/support/shared_configuration.rb +2 -2
- metadata +13 -12
- data/lib/cli.rb +0 -13
- data/lib/configuration.rb +0 -36
- data/lib/importer/data_organizer.rb +0 -117
- data/lib/importer/mime_content_type.rb +0 -564
- data/lib/importer/parallel_importer.rb +0 -430
- data/lib/json_schema_validator.rb +0 -64
- data/lib/migrator.rb +0 -39
- data/lib/version.rb +0 -3
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contentful-importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Contentful GmbH (Andreas Tiefenthaler)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -229,16 +229,16 @@ files:
|
|
229
229
|
- example_settings/contentful_structure.json
|
230
230
|
- example_settings/files_meaning.txt
|
231
231
|
- example_settings/settings.yml
|
232
|
-
- lib/cli.rb
|
233
|
-
- lib/configuration.rb
|
234
|
-
- lib/converters/content_types_structure_creator.rb
|
235
|
-
- lib/converters/contentful_model_to_json.rb
|
236
|
-
- lib/importer/data_organizer.rb
|
237
|
-
- lib/importer/
|
238
|
-
- lib/importer/
|
239
|
-
- lib/
|
240
|
-
- lib/
|
241
|
-
- lib/version.rb
|
232
|
+
- lib/contentful/importer/cli.rb
|
233
|
+
- lib/contentful/importer/configuration.rb
|
234
|
+
- lib/contentful/importer/converters/content_types_structure_creator.rb
|
235
|
+
- lib/contentful/importer/converters/contentful_model_to_json.rb
|
236
|
+
- lib/contentful/importer/data_organizer.rb
|
237
|
+
- lib/contentful/importer/json_schema_validator.rb
|
238
|
+
- lib/contentful/importer/migrator.rb
|
239
|
+
- lib/contentful/importer/mime_content_type.rb
|
240
|
+
- lib/contentful/importer/parallel_importer.rb
|
241
|
+
- lib/contentful/importer/version.rb
|
242
242
|
- spec/fixtures/import_files/assets/image/image_1.json
|
243
243
|
- spec/fixtures/import_files/assets/image/image_2.json
|
244
244
|
- spec/fixtures/import_files/assets/image/image_3.json
|
@@ -434,3 +434,4 @@ test_files:
|
|
434
434
|
- spec/support/db_rows_json.rb
|
435
435
|
- spec/support/shared_configuration.rb
|
436
436
|
- spec/support/vcr.rb
|
437
|
+
has_rdoc:
|
data/lib/cli.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require_relative 'migrator'
|
2
|
-
require 'yaml'
|
3
|
-
|
4
|
-
module Command
|
5
|
-
class CLI < Escort::ActionCommand::Base
|
6
|
-
|
7
|
-
def execute
|
8
|
-
setting_file = YAML.load_file(global_options[:file])
|
9
|
-
Migrator.new(setting_file).run(command_name, command_options)
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
end
|
data/lib/configuration.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/hash'
|
2
|
-
module Contentful
|
3
|
-
class Configuration
|
4
|
-
attr_reader :space_id,
|
5
|
-
:config,
|
6
|
-
:data_dir,
|
7
|
-
:collections_dir,
|
8
|
-
:entries_dir,
|
9
|
-
:assets_dir,
|
10
|
-
:log_files_dir,
|
11
|
-
:threads_dir,
|
12
|
-
:imported_entries,
|
13
|
-
:published_entries,
|
14
|
-
:published_assets,
|
15
|
-
:space_id
|
16
|
-
|
17
|
-
def initialize(settings)
|
18
|
-
@config = settings
|
19
|
-
validate_required_parameters
|
20
|
-
@data_dir = settings['data_dir']
|
21
|
-
@collections_dir = "#{data_dir}/collections"
|
22
|
-
@entries_dir = "#{data_dir}/entries"
|
23
|
-
@assets_dir = "#{data_dir}/assets"
|
24
|
-
@log_files_dir = "#{data_dir}/logs"
|
25
|
-
@threads_dir = "#{data_dir}/threads"
|
26
|
-
@imported_entries = []
|
27
|
-
@published_entries = []
|
28
|
-
@published_assets = []
|
29
|
-
@space_id = settings['space_id']
|
30
|
-
end
|
31
|
-
|
32
|
-
def validate_required_parameters
|
33
|
-
fail ArgumentError, 'Set PATH to data_dir. Folder where all data will be stored. View README' if config['data_dir'].nil?
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
require 'thread'
|
3
|
-
require_relative 'parallel_importer'
|
4
|
-
|
5
|
-
module Contentful
|
6
|
-
class DataOrganizer
|
7
|
-
|
8
|
-
attr_reader :config, :split_params, :logger
|
9
|
-
|
10
|
-
def initialize(settings)
|
11
|
-
@config = settings
|
12
|
-
@split_params = {object_index: 0, current_thread: 0}
|
13
|
-
@logger = Logger.new(STDOUT)
|
14
|
-
end
|
15
|
-
|
16
|
-
def execute(threads_count)
|
17
|
-
create_threads_subdirectories(threads_count, true)
|
18
|
-
split_entries(threads_count)
|
19
|
-
end
|
20
|
-
|
21
|
-
def split_assets_to_threads(threads_count)
|
22
|
-
create_threads_subdirectories(threads_count, false, {assets: 'assets/'})
|
23
|
-
split_assets(threads_count)
|
24
|
-
end
|
25
|
-
|
26
|
-
def split_entries(threads_count)
|
27
|
-
entries_per_thread_count = total_entries_count / threads_count
|
28
|
-
Dir.glob("#{config.entries_dir}/*") do |dir_path|
|
29
|
-
collection_name = File.basename(dir_path)
|
30
|
-
if has_contentful_structure?(collection_name)
|
31
|
-
content_type_id = content_type_id_from_file(collection_name)
|
32
|
-
process_collection_files(content_type_id, dir_path, entries_per_thread_count, threads_count)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def split_assets(threads_count)
|
38
|
-
asset_per_thread = total_assets_count / threads_count
|
39
|
-
Dir.glob("#{config.assets_dir}/**/*json") do |asset_path|
|
40
|
-
copy_asset(asset_path)
|
41
|
-
split_params[:object_index] += 1
|
42
|
-
count_index_files(asset_per_thread, threads_count)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def process_collection_files(content_type_id, dir_path, entries_per_thread_count, threads_count)
|
47
|
-
logger.info "Processing collection: #{content_type_id}"
|
48
|
-
Dir.glob("#{dir_path}/*.json") do |entry_path|
|
49
|
-
copy_entry(entry_path, split_params[:current_thread], content_type_id)
|
50
|
-
split_params[:object_index] += 1
|
51
|
-
count_index_files(entries_per_thread_count, threads_count)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def count_index_files(objects_per_thread_count, threads_count)
|
56
|
-
if split_params[:object_index] == objects_per_thread_count
|
57
|
-
split_params[:object_index] = 0
|
58
|
-
set_current_thread(threads_count)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def set_current_thread(threads_count)
|
63
|
-
split_params[:current_thread] += 1
|
64
|
-
split_params[:current_thread] = 0 if split_params[:current_thread] == threads_count
|
65
|
-
end
|
66
|
-
|
67
|
-
def has_contentful_structure?(collection_file)
|
68
|
-
File.exist?("#{config.collections_dir}/#{collection_file}.json")
|
69
|
-
end
|
70
|
-
|
71
|
-
def content_type_id_from_file(collection_file)
|
72
|
-
JSON.parse(File.read("#{config.collections_dir}/#{collection_file}.json"))['id']
|
73
|
-
end
|
74
|
-
|
75
|
-
def new_entry_name(content_type_id, entry_path)
|
76
|
-
"#{content_type_id}_#{File.basename(entry_path, '.*').match(/(\d+)/)[0]}.json"
|
77
|
-
end
|
78
|
-
|
79
|
-
def copy_entry(entry_path, current_thread, content_type_id)
|
80
|
-
FileUtils.cp entry_path, "#{config.threads_dir}/#{current_thread}/#{new_entry_name(content_type_id, entry_path)}"
|
81
|
-
end
|
82
|
-
|
83
|
-
def copy_asset(asset_path)
|
84
|
-
FileUtils.cp asset_path, "#{config.threads_dir}/assets/#{split_params[:current_thread]}/#{File.basename(asset_path)}"
|
85
|
-
end
|
86
|
-
|
87
|
-
def create_threads_subdirectories(threads_count, validate, dir = {})
|
88
|
-
validate_collections_files if validate
|
89
|
-
create_directory(config.threads_dir)
|
90
|
-
threads_count.times do |thread_id|
|
91
|
-
create_directory("#{config.threads_dir}/#{dir[:assets]}#{thread_id}")
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def create_directory(path)
|
96
|
-
FileUtils.mkdir_p(path) unless File.directory?(path)
|
97
|
-
end
|
98
|
-
|
99
|
-
def total_entries_count
|
100
|
-
total_number = 0
|
101
|
-
Dir.glob("#{config.entries_dir}/*") do |dir_path|
|
102
|
-
collection_name = File.basename(dir_path)
|
103
|
-
total_number += Dir.glob("#{config.entries_dir}/#{collection_name}/*").count if has_contentful_structure?(collection_name)
|
104
|
-
end
|
105
|
-
total_number
|
106
|
-
end
|
107
|
-
|
108
|
-
def total_assets_count
|
109
|
-
Dir.glob("#{config.assets_dir}/**/*json").count
|
110
|
-
end
|
111
|
-
|
112
|
-
def validate_collections_files
|
113
|
-
fail ArgumentError, "Make sure the #{config.collections_dir} directory exists and the content structure resides within it. View README" unless Dir.exist?(config.collections_dir)
|
114
|
-
fail ArgumentError, 'Collections directory is empty! Create content types JSON files. View README' if Dir.glob("#{config.collections_dir}/*").empty?
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|