22require 'yaml'
33require 'find'
44require 'json'
5+ require 'open3'
56require 'pathname'
67require 'tempfile'
78require 'fileutils'
@@ -21,25 +22,23 @@ def self.execute(directory, _development, env_config, skip_data: false)
2122 end
2223
2324 database_path = MorphCLI . database_path ( directory )
24- if skip_data
25- all_paths . delete ( database_path )
26- database_path = nil
27- end
25+ all_paths . delete ( database_path )
26+ database_path = nil if skip_data
2827
29- size = MorphCLI . get_dir_size ( directory , all_paths )
28+ size = MorphCLI . get_dir_size ( directory , all_paths + [ database_path ] . compact )
3029 puts "Uploading #{ size } #{ " (including #{ database_path } )" if database_path } ..."
3130
3231 file = MorphCLI . create_tar ( directory , all_paths )
3332
34- scraper_output = run ( file , env_config )
33+ scraper_output = run ( file , env_config , directory , database_path )
3534
3635 puts "Scraper didn't output anything, but it ran successfully." unless scraper_output
3736 end
3837
3938 # Uploads the code to the server, streams the run output to the local
4039 # stdout/stderr and returns whether the scraper itself wrote anything
4140 # to stdout or stderr
42- def self . run ( file , env_config )
41+ def self . run ( file , env_config , directory , database_path )
4342 connection = Faraday . new ( url : env_config [ :base_url ] ) do |f |
4443 f . request :multipart
4544 f . response :raise_error
@@ -49,10 +48,18 @@ def self.run(file, env_config)
4948 buffer = +""
5049 scraper_output = false
5150 connection . post ( "/run" ) do |req |
52- req . body = {
51+ body = {
5352 api_key : env_config [ :api_key ] ,
5453 code : Faraday ::Multipart ::FilePart . new ( file , "application/gzip" )
5554 }
55+ if database_path
56+ body [ :database ] = Faraday ::Multipart ::FilePart . new (
57+ File . join ( directory , database_path ) ,
58+ "application/octet-stream" ,
59+ database_path
60+ )
61+ end
62+ req . body = body
5663 # 10 minutes should be "enough for everyone", right?
5764 # Setting :timeout to nil in the config will disable the timeout
5865 # entirely. The Faraday default is 60 seconds.
@@ -72,6 +79,46 @@ def self.run(file, env_config)
7279 scraper_output
7380 end
7481
82+ def self . download ( directory , env_config , scraper )
83+ connection = Faraday . new ( url : env_config [ :base_url ] ) do |f |
84+ f . response :raise_error
85+ f . adapter Faraday . default_adapter
86+ end
87+
88+ # Download to a tempfile in the same directory first so a failed download
89+ # doesn't clobber an existing database
90+ tempfile = Tempfile . new ( [ "morph" , ".sqlite" ] , directory )
91+ tempfile . binmode
92+
93+ begin
94+ connection . get ( "/#{ scraper } /data.sqlite" ) do |req |
95+ req . params [ :key ] = env_config [ :api_key ]
96+ req . options . timeout = env_config . fetch ( :timeout , 600 )
97+ req . options . on_data = proc do |chunk , _overall_received_bytes , env |
98+ tempfile . write ( chunk ) if env . status == 200
99+ end
100+ end
101+
102+ tempfile . close
103+ File . rename ( tempfile . path , File . join ( directory , "data.sqlite" ) )
104+ ensure
105+ tempfile . close unless tempfile . closed?
106+ FileUtils . rm_f ( tempfile . path )
107+ end
108+
109+ size = Filesize . from ( "#{ File . size ( File . join ( directory , 'data.sqlite' ) ) } B" ) . pretty
110+ puts "Saved #{ size } to data.sqlite"
111+ end
112+
113+ # The name of the scraper on morph (owner/name), worked out from the git
114+ # remote of the given directory. Returns nil if it can't be worked out.
115+ def self . scraper_name ( directory )
116+ url , _stderr , status = Open3 . capture3 ( "git" , "-C" , directory , "config" , "--get" , "remote.origin.url" )
117+ return nil unless status . success?
118+
119+ url . strip [ %r{([^/:]+/[^/:]+?)(?:\. git)?\z } , 1 ]
120+ end
121+
75122 # Writes the line to the local stdout/stderr and returns the name of the
76123 # stream it came from
77124 def self . log ( line )
0 commit comments