winrm-fs 1.3.0 → 1.3.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7898b5512c391163a77d694860db51a5c15c5745031c2b34c0cd181a9bbda37
4
- data.tar.gz: 87864314b0863331944c604d26c0721db568db238da87923b836d80097531dad
3
+ metadata.gz: 22197843455d6d73e34d00c91fd58d4a6201d08f02f61a64d5b971085360aebf
4
+ data.tar.gz: fd9181f9187df12ad0a6812891c29a1e917ccfc9f2d4064448a52d08f0455d05
5
5
  SHA512:
6
- metadata.gz: d468205639de56be5fcf4c4896a823f96760e03e23153b6e09cc9c1bc2779725d84f429a759d67088e622a288a3e002bb528f9e23749cded6416baac38b26614
7
- data.tar.gz: b8787f4e3ae986aa32ae324cd1e6113081b83cb291ba519b027dea7ee67f0c4a0d9d15e50e38222328d94829735156a525c066cab56966c6e92de25754efc5b4
6
+ metadata.gz: ebf3f8d594d00219a5b19f6eb2ba17ae4f466de4d4eb0929b6f61f8f9075473ccdaff127ab3ed10a06280a309cfe3b5d8976a1857970c3f02a22080413690667
7
+ data.tar.gz: a56cf8645bcb803c377f7146a1c5a005496e74b9b7993b6f18f38dd70c2a4de00174c65cd7abc3a0e409b07fa10a10bab7fce145b5dfb6991744f2c720d9a2e0
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  #
3
5
  # Copyright 2014 Shawn Neal <sneal@sneal.net>
4
6
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Copyright 2015 Shawn Neal <sneal@sneal.net>
3
5
  #
@@ -13,9 +15,9 @@
13
15
  # See the License for the specific language governing permissions and
14
16
  # limitations under the License.
15
17
 
16
- require 'winrm'
18
+ require 'winrm' unless defined?(WinRM::Connection)
17
19
  require 'logger'
18
- require 'pathname'
20
+ require 'pathname' unless defined?(Pathname)
19
21
  require_relative 'winrm-fs/exceptions'
20
22
  require_relative 'winrm-fs/file_manager'
21
23
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: false
2
+
1
3
  #
2
4
  # Author:: Fletcher (<fnichol@nichol.ca>)
3
5
  #
@@ -15,11 +17,11 @@
15
17
  # See the License for the specific language governing permissions and
16
18
  # limitations under the License.
17
19
 
18
- require 'benchmark'
19
- require 'csv'
20
- require 'digest'
21
- require 'securerandom'
22
- require 'stringio'
20
+ require 'benchmark' unless defined?(Benchmark)
21
+ require 'csv' unless defined?(CSV)
22
+ require 'digest' unless defined?(Digest)
23
+ require 'securerandom' unless defined?(SecureRandom)
24
+ require 'stringio' unless defined?(StringIO)
23
25
 
24
26
  require 'winrm/exceptions'
25
27
  require 'winrm-fs/core/tmp_zip'
@@ -31,7 +33,7 @@ module WinRM
31
33
  #
32
34
  # @author Fletcher Nichol <fnichol@nichol.ca>
33
35
  class FileTransporterFailed < ::WinRM::WinRMError; end
34
- # rubocop:disable MethodLength, AbcSize, ClassLength
36
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/ClassLength
35
37
 
36
38
  # Exception for the case where upload source contains more than one
37
39
  # StringIO object, or a combination of file/directory paths and StringIO object
@@ -115,6 +117,10 @@ module WinRM
115
117
  [total_size, files]
116
118
  end
117
119
 
120
+ def close
121
+ shell.close
122
+ end
123
+
118
124
  private
119
125
 
120
126
  # @return [String] the Array pack template for Base64 encoding a stream
@@ -179,12 +185,12 @@ module WinRM
179
185
  zip_sha1 = sha1sum(zip_io.path)
180
186
 
181
187
  hash[zip_sha1] = {
182
- 'src' => dir,
188
+ 'src' => dir,
183
189
  'src_zip' => zip_io.path.to_s,
184
- 'zip_io' => zip_io,
185
- 'tmpzip' => "#{TEMP_UPLOAD_DIRECTORY}\\tmpzip-#{zip_sha1}.zip",
186
- 'dst' => "#{remote}\\#{File.basename(dir)}",
187
- 'size' => File.size(zip_io.path)
190
+ 'zip_io' => zip_io,
191
+ 'tmpzip' => "#{TEMP_UPLOAD_DIRECTORY}\\tmpzip-#{zip_sha1}.zip",
192
+ 'dst' => "#{remote}\\#{File.basename(dir)}",
193
+ 'size' => File.size(zip_io.path)
188
194
  }
189
195
  end
190
196
 
@@ -197,9 +203,9 @@ module WinRM
197
203
  def add_file_hash!(hash, local, remote)
198
204
  logger.debug "creating hash for file #{remote}"
199
205
  hash[sha1sum(local)] = {
200
- 'src' => local,
201
- 'dst' => remote,
202
- 'size' => local.is_a?(StringIO) ? local.size : File.size(local)
206
+ 'src' => local,
207
+ 'dst' => remote,
208
+ 'size' => local.is_a?(StringIO) ? local.size : File.size(local)
203
209
  }
204
210
  end
205
211
 
@@ -347,6 +353,7 @@ module WinRM
347
353
  path = false
348
354
  locals.each do |local|
349
355
  raise UploadSourceError if string_io
356
+
350
357
  if local.is_a?(StringIO)
351
358
  string_io = true
352
359
  else
@@ -448,8 +455,9 @@ module WinRM
448
455
  read_size = ((max_encoded_write - dest.length) / 4) * 3
449
456
  chunk = 1
450
457
  bytes = 0
458
+ # Do not freeze this string
451
459
  buffer = ''
452
- shell.run(<<-EOS
460
+ shell.run(<<-PS
453
461
  $to = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("#{dest}")
454
462
  $parent = Split-Path $to
455
463
  if(!(Test-path $parent)) { mkdir $parent | Out-Null }
@@ -468,7 +476,7 @@ module WinRM
468
476
  # ClearScriptBlockCache to clear it.
469
477
  $bindingFlags= [Reflection.BindingFlags] "NonPublic,Static"
470
478
  $method = [scriptblock].GetMethod("ClearScriptBlockCache", $bindingFlags)
471
- EOS
479
+ PS
472
480
  )
473
481
 
474
482
  while input_io.read(read_size, buffer)
@@ -485,11 +493,11 @@ module WinRM
485
493
  end
486
494
 
487
495
  def stream_command(encoded_bytes)
488
- <<-EOS
496
+ <<-PS
489
497
  if($method) { $method.Invoke($Null, $Null) }
490
498
  $bytes=[Convert]::FromBase64String('#{encoded_bytes}')
491
499
  $fileStream.Write($bytes, 0, $bytes.length)
492
- EOS
500
+ PS
493
501
  end
494
502
 
495
503
  # Uploads a local file.
@@ -559,7 +567,7 @@ module WinRM
559
567
  size / 3 * 4
560
568
  end
561
569
  end
562
- # rubocop:enable MethodLength, AbcSize, ClassLength
570
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/ClassLength
563
571
  end
564
572
  end
565
573
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Author:: Fletcher (<fnichol@nichol.ca>)
3
5
  #
@@ -16,9 +18,9 @@
16
18
  # limitations under the License.
17
19
 
18
20
  require 'delegate'
19
- require 'pathname'
20
- require 'tempfile'
21
- require 'zip'
21
+ require 'pathname' unless defined?(Pathname)
22
+ require 'tempfile' unless defined?(Tempfile)
23
+ require 'zip' unless defined?(Zip)
22
24
 
23
25
  module WinRM
24
26
  module FS
@@ -103,6 +105,7 @@ module WinRM
103
105
  def clean_dirname(dir)
104
106
  paths = Pathname.glob(dir)
105
107
  raise "Expected Pathname.glob(dir) to return only dir, got #{paths}" if paths.length != 1
108
+
106
109
  paths.first
107
110
  end
108
111
 
@@ -110,7 +113,7 @@ module WinRM
110
113
  # directory, excluding directories
111
114
  # @api private
112
115
  def entries
113
- Pathname.glob(dir.join('**/*')).delete_if(&:directory?).sort
116
+ Pathname.glob(dir.join('**/.*')).push(*Pathname.glob(dir.join('**/*'))).delete_if(&:directory?).sort
114
117
  end
115
118
 
116
119
  # (see Logging.log_subject)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Copyright 2015 Shawn Neal <sneal@sneal.net>
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Copyright 2015 Shawn Neal <sneal@sneal.net>
3
5
  #
@@ -13,7 +15,7 @@
13
15
  # See the License for the specific language governing permissions and
14
16
  # limitations under the License.
15
17
 
16
- require 'winrm'
18
+ require 'winrm' unless defined?(WinRM::Connection)
17
19
  require_relative 'scripts/scripts'
18
20
  require_relative 'core/file_transporter'
19
21
 
@@ -35,7 +37,7 @@ module WinRM
35
37
  def checksum(path, digest = 'SHA1')
36
38
  @logger.debug("checksum with #{digest}: #{path}")
37
39
  script = WinRM::FS::Scripts.render('checksum', path: path, digest: digest)
38
- @connection.shell(:powershell) { |e| e.run(script).stdout.chomp }
40
+ ps_run(script).exitcode == 0
39
41
  end
40
42
 
41
43
  # Create the specifed directory recursively
@@ -44,7 +46,7 @@ module WinRM
44
46
  def create_dir(path)
45
47
  @logger.debug("create_dir: #{path}")
46
48
  script = WinRM::FS::Scripts.render('create_dir', path: path)
47
- @connection.shell(:powershell) { |e| e.run(script).exitcode == 0 }
49
+ ps_run(script).exitcode == 0
48
50
  end
49
51
 
50
52
  # Deletes the file or directory at the specified path
@@ -53,22 +55,50 @@ module WinRM
53
55
  def delete(path)
54
56
  @logger.debug("deleting: #{path}")
55
57
  script = WinRM::FS::Scripts.render('delete', path: path)
56
- @connection.shell(:powershell) { |e| e.run(script).exitcode == 0 }
58
+ ps_run(script).exitcode == 0
57
59
  end
58
60
 
59
61
  # Downloads the specified remote file to the specified local path
60
62
  # @param [String] The full path on the remote machine
61
63
  # @param [String] The full path to write the file to locally
62
- def download(remote_path, local_path, first = true)
63
- @logger.debug("downloading: #{remote_path} -> #{local_path}")
64
- script = WinRM::FS::Scripts.render('download', path: remote_path)
65
- output = @connection.shell(:powershell) { |e| e.run(script) }
66
- contents = output.stdout.gsub('\n\r', '')
67
- return false if output.exitcode != 0
68
- download_dir(remote_path, local_path, first) if contents.empty?
69
- IO.binwrite(local_path, Base64.decode64(contents)) unless contents.empty?
64
+ # rubocop:disable Metrics/MethodLength
65
+ def download(remote_path, local_path, chunk_size = 1024 * 1024, first = true)
66
+ @logger.debug("downloading: #{remote_path} -> #{local_path} #{chunk_size}")
67
+ index = 0
68
+ output = _output_from_file(remote_path, chunk_size, index)
69
+ return download_dir(remote_path, local_path, chunk_size, first) if output.exitcode == 2
70
+
71
+ return false if output.exitcode >= 1
72
+
73
+ File.open(local_path, 'wb') do |fd|
74
+ out = _write_file(fd, output)
75
+ index += out.length
76
+ until out.empty?
77
+ output = _output_from_file(remote_path, chunk_size, index)
78
+ return false if output.exitcode >= 1
79
+
80
+ out = _write_file(fd, output)
81
+ index += out.length
82
+ end
83
+ end
70
84
  true
71
85
  end
86
+ # rubocop:enable Metrics/MethodLength
87
+
88
+ def _output_from_file(remote_path, chunk_size, index)
89
+ script = WinRM::FS::Scripts.render('download', path: remote_path, chunk_size: chunk_size, index: index)
90
+ ps_run(script)
91
+ end
92
+
93
+ def _write_file(tofd, output)
94
+ contents = output.stdout.gsub('\n\r', '')
95
+ out = Base64.decode64(contents)
96
+ out = out[0, out.length - 1] if out.end_with? "\x00"
97
+ return out if out.empty?
98
+
99
+ tofd.write(out)
100
+ out
101
+ end
72
102
 
73
103
  # Checks to see if the given path exists on the target file system.
74
104
  # @param [String] The full path to the directory or file
@@ -76,7 +106,7 @@ module WinRM
76
106
  def exists?(path)
77
107
  @logger.debug("exists?: #{path}")
78
108
  script = WinRM::FS::Scripts.render('exists', path: path)
79
- @connection.shell(:powershell) { |e| e.run(script).exitcode == 0 }
109
+ ps_run(script).exitcode == 0
80
110
  end
81
111
 
82
112
  # Gets the current user's TEMP directory on the remote system, for example
@@ -84,7 +114,7 @@ module WinRM
84
114
  # @return [String] Full path to the temp directory
85
115
  def temp_dir
86
116
  @temp_dir ||= begin
87
- (@connection.shell(:powershell) { |e| e.run('$env:TEMP') }).stdout.chomp.tr('\\', '/')
117
+ ps_run('$env:TEMP').stdout.chomp.tr('\\', '/')
88
118
  end
89
119
  end
90
120
 
@@ -109,18 +139,31 @@ module WinRM
109
139
  def upload(local_path, remote_path, &block)
110
140
  @connection.shell(:powershell) do |shell|
111
141
  file_transporter ||= WinRM::FS::Core::FileTransporter.new(shell)
112
- file_transporter.upload(local_path, remote_path, &block)[0]
142
+ begin
143
+ file_transporter.upload(local_path, remote_path, &block)[0]
144
+ ensure
145
+ file_transporter.close
146
+ end
113
147
  end
114
148
  end
115
149
 
116
150
  private
117
151
 
118
- def download_dir(remote_path, local_path, first)
152
+ def ps_run(cmd)
153
+ shell = @connection.shell(:powershell)
154
+ begin
155
+ shell.run(cmd)
156
+ ensure
157
+ shell.close
158
+ end
159
+ end
160
+
161
+ def download_dir(remote_path, local_path, chunk_size, first)
119
162
  local_path = File.join(local_path, File.basename(remote_path.to_s)) if first
120
163
  FileUtils.mkdir_p(local_path) unless File.directory?(local_path)
121
164
  command = "Get-ChildItem #{remote_path} | Select-Object Name"
122
165
  @connection.shell(:powershell) { |e| e.run(command) }.stdout.strip.split(/\n/).drop(2).each do |file|
123
- download(File.join(remote_path.to_s, file.strip), File.join(local_path, file.strip), first = false)
166
+ download(File.join(remote_path.to_s, file.strip), File.join(local_path, file.strip), chunk_size, false)
124
167
  end
125
168
  end
126
169
  end
@@ -1,11 +1,17 @@
1
1
  $p = $ExecutionContext.SessionState.Path
2
2
  $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
- If (Test-Path $path -PathType Leaf) {
4
- $bytes = [System.convert]::ToBase64String([System.IO.File]::ReadAllBytes($path))
3
+ $index = <%= index %>
4
+ $chunkSize = <%= chunk_size %>
5
+ if (Test-Path $path -PathType Leaf) {
6
+ $file = [System.IO.File]::OpenRead($path)
7
+ $seekedTo = $file.Seek($index, [System.IO.SeekOrigin]::Begin)
8
+ $chunk = New-Object byte[] $chunkSize
9
+ $bytesRead = $file.Read($chunk, 0, $chunkSize)
10
+ $bytes = [System.convert]::ToBase64String($chunk[0..$bytesRead])
5
11
  Write-Host $bytes
6
12
  exit 0
7
13
  }
8
14
  ElseIf (Test-Path $path -PathType Container) {
9
- exit 0
15
+ exit 2
10
16
  }
11
17
  exit 1
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #
2
4
  # Copyright 2015 Shawn Neal <sneal@sneal.net>
3
5
  #
@@ -13,18 +15,32 @@
13
15
  # See the License for the specific language governing permissions and
14
16
  # limitations under the License.
15
17
 
16
- require 'erubis'
18
+ require 'erubi'
17
19
 
18
20
  module WinRM
19
21
  module FS
20
22
  # PS1 scripts
21
23
  module Scripts
24
+ # rubocop:disable Metrics/MethodLength
22
25
  def self.render(template, context)
26
+ # rubocop:enable Metrics/MethodLength
23
27
  template_path = File.expand_path(
24
28
  "#{File.dirname(__FILE__)}/#{template}.ps1.erb"
25
29
  )
26
30
  template = File.read(template_path)
27
- Erubis::Eruby.new(template).result(context)
31
+ case context
32
+ when Hash
33
+ b = binding
34
+ locals = context.collect { |k, _| "#{k} = context[#{k.inspect}]; " }
35
+ b.eval(locals.join)
36
+ when Binding
37
+ b = context
38
+ when NilClass
39
+ b = binding
40
+ else
41
+ raise ArgumentError
42
+ end
43
+ b.eval(Erubi::Engine.new(template).src)
28
44
  end
29
45
  end
30
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winrm-fs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Neal
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-08-31 00:00:00.000000000 Z
12
+ date: 2020-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: erubis
15
+ name: erubi
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '2.7'
20
+ version: '1.8'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '2.7'
27
+ version: '1.8'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: logging
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -51,14 +51,14 @@ dependencies:
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.1'
54
+ version: '2.0'
55
55
  type: :runtime
56
56
  prerelease: false
57
57
  version_requirements: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.1'
61
+ version: '2.0'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: winrm
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -91,16 +91,22 @@ dependencies:
91
91
  name: rake
92
92
  requirement: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '10.3'
97
+ - - "<"
98
+ - !ruby/object:Gem::Version
99
+ version: '13'
97
100
  type: :development
98
101
  prerelease: false
99
102
  version_requirements: !ruby/object:Gem::Requirement
100
103
  requirements:
101
- - - "~>"
104
+ - - ">="
102
105
  - !ruby/object:Gem::Version
103
106
  version: '10.3'
107
+ - - "<"
108
+ - !ruby/object:Gem::Version
109
+ version: '13'
104
110
  - !ruby/object:Gem::Dependency
105
111
  name: rspec
106
112
  requirement: !ruby/object:Gem::Requirement
@@ -121,14 +127,14 @@ dependencies:
121
127
  requirements:
122
128
  - - "~>"
123
129
  - !ruby/object:Gem::Version
124
- version: '0.51'
130
+ version: 0.68.0
125
131
  type: :development
126
132
  prerelease: false
127
133
  version_requirements: !ruby/object:Gem::Requirement
128
134
  requirements:
129
135
  - - "~>"
130
136
  - !ruby/object:Gem::Version
131
- version: '0.51'
137
+ version: 0.68.0
132
138
  description: " Ruby library for file system operations via Windows Remote Management\n"
133
139
  email:
134
140
  - sneal@sneal.net
@@ -140,20 +146,9 @@ extra_rdoc_files:
140
146
  - README.md
141
147
  - LICENSE
142
148
  files:
143
- - ".gitignore"
144
- - ".rspec"
145
- - ".rubocop.yml"
146
- - ".rubocop_todo.yml"
147
- - ".travis.yml"
148
- - Gemfile
149
149
  - LICENSE
150
150
  - README.md
151
- - Rakefile
152
- - VERSION
153
- - Vagrantfile
154
- - appveyor.yml
155
151
  - bin/rwinrmcp
156
- - changelog.md
157
152
  - lib/winrm-fs.rb
158
153
  - lib/winrm-fs/core/file_transporter.rb
159
154
  - lib/winrm-fs/core/tmp_zip.rb
@@ -167,15 +162,9 @@ files:
167
162
  - lib/winrm-fs/scripts/exists.ps1.erb
168
163
  - lib/winrm-fs/scripts/extract_files.ps1.erb
169
164
  - lib/winrm-fs/scripts/scripts.rb
170
- - spec/config-example.yml
171
- - spec/integration/file_manager_spec.rb
172
- - spec/integration/tmp_zip_spec.rb
173
- - spec/matchers.rb
174
- - spec/spec_helper.rb
175
- - spec/unit/tmp_zip_spec.rb
176
- - winrm-fs.gemspec
177
165
  homepage: http://github.com/WinRb/winrm-fs
178
- licenses: []
166
+ licenses:
167
+ - Apache-2.0
179
168
  metadata: {}
180
169
  post_install_message:
181
170
  rdoc_options:
@@ -189,15 +178,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
189
178
  requirements:
190
179
  - - ">="
191
180
  - !ruby/object:Gem::Version
192
- version: 2.2.0
181
+ version: 2.4.0
193
182
  required_rubygems_version: !ruby/object:Gem::Requirement
194
183
  requirements:
195
184
  - - ">="
196
185
  - !ruby/object:Gem::Version
197
186
  version: '0'
198
187
  requirements: []
199
- rubyforge_project:
200
- rubygems_version: 2.7.6
188
+ rubygems_version: 3.1.2
201
189
  signing_key:
202
190
  specification_version: 4
203
191
  summary: WinRM File System