VictoriaFreSh 2020.8.29 → 2021.4.17

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: 62522d43b651b6ba58ed738f1a0e72b2c0f66ca24f8329cb60dc6e6807be782a
4
- data.tar.gz: e4c61f51e78420aac6c6d29090b4fefa627479415671a0a1df919b735277baa9
3
+ metadata.gz: 2c90c85569d4f2c8396f17754e03eb87e35c272b015397983c2afa9af9492ccf
4
+ data.tar.gz: e88f1ac7afb0ab584ed2973fcceec8701f379c0e5e7f0a22e3be07569799c63c
5
5
  SHA512:
6
- metadata.gz: fc5531da173296c23d942ed4690d71dda0b7748beae9fd1923e47c91011f544b1efea697d7ac330930d3d3f40455af1ae846258372223c61cdab918a020c5551
7
- data.tar.gz: 0a43292024c056a5af88b210728fc9345533de397d23078134b21429c61b63812945eaab823bac890c06ed4f60b77d7f9b024d6328a8e58768cd9f60d102e0cd
6
+ metadata.gz: 130b068d432a0199bab6ab093619e4c0e56f31a5437115138a767e719f62083fe07c45abd6cbb5f90661a99122ffa3a8efea57e815c42ccf007ecf7bc5b64e9a
7
+ data.tar.gz: 3c25711cbca993d774d5581f507c7087951304fcb3ae5dfb971ce0cfc93f58b7364c28108e56be3976dffd884d9f7d68dbd9290967fe3293954f03e095bca05c
data/lib/victoriafresh.rb CHANGED
@@ -3,10 +3,34 @@
3
3
  require 'pathname'
4
4
  require 'fileutils'
5
5
  require 'cbor'
6
+ require 'get_process_mem'
6
7
 
7
8
  # require File.dirname(__FILE__)+'/VictoriaFreSh/filemessage_pb.rb'
8
9
 
9
10
  class VictoriaFresh
11
+ attr_accessor :diskFlushSize #累积了这么多的数据就向磁盘写入,以减少内存占用
12
+ attr_accessor :diskFileName #向磁盘写入数据时的文件名或前缀
13
+ attr_accessor :diskMultiFile #向磁盘写入时,是否要写多个文件
14
+ attr_accessor :diskFlush #要提前磁盘写入文件,以节省内存吗?
15
+
16
+ def initialize
17
+ @diskFlush=false #要向磁盘写入文件
18
+ @diskFlushSize=143212 #磁盘分块大小
19
+ @diskFileName='victoriafreshdata.v' #磁盘文件名
20
+ @diskMultiFile=false #磁盘整个文件,不是多个文件
21
+ @diskWriteFileObject={} #向磁盘写入文件的对象
22
+ @contentString="" #内容字符串
23
+ @contentPartArray=[] #内容片段数组
24
+ end
25
+
26
+ def checkMemoryUsage(lineNumber)
27
+ mem= GetProcessMem.new
28
+
29
+ puts("#{lineNumber} , Memory: #{mem.mb}"); #Debug
30
+
31
+ end #def checkMemoryUsage
32
+
33
+
10
34
  def releaseFiles(victoriaFreshPackagedFileString, contentString) #释放目录树
11
35
  # packagedFile=Com::Stupidbeauty::Victoriafresh::FileMessage.decode(victoriaFreshPackagedFileString) #解码文件消息对象。
12
36
  packagedFile=CBOR.decode(victoriaFreshPackagedFileString) #解码
@@ -33,6 +57,13 @@ class VictoriaFresh
33
57
 
34
58
  FileUtils.touch pathToMake, :mtime => timeObject #设置修改时间
35
59
 
60
+ permissionNumber=packagedFile['permission'] #获取权限数字
61
+
62
+ if (permissionNumber.nil?) #不带权限字段
63
+ elsif #带权限字段
64
+ File.chmod(permissionNumber, pathToMake) #设置权限
65
+ end #if (permissionNumber.nil?) #不带权限字段
66
+
36
67
  end #writeFile(pathPrefix, packagedFile, contentString) #写入文件
37
68
 
38
69
  #创建符号链接
@@ -52,7 +83,17 @@ class VictoriaFresh
52
83
 
53
84
  FileUtils.symlink(victoriaFreshData, pathToMake, force: true) #创建符号链接
54
85
 
55
- File.lchmod(packagedFile['permission'], pathToMake) #设置权限
86
+ permissionNumber=packagedFile['permission'] #获取权限数字
87
+
88
+ if (permissionNumber.nil?) #不带权限字段
89
+ elsif #带权限字段
90
+ # File.chmod(permissionNumber, pathToMake) #设置权限
91
+ begin #尝试修改链接本身的权限
92
+ File.lchmod(permissionNumber, pathToMake) #设置权限
93
+ rescue NotImplementedError #未实现
94
+ puts 'File.lchmod not implemented' #Debug
95
+ end #begin #尝试修改链接本身的权限
96
+ end #if (permissionNumber.nil?) #不带权限字段
56
97
  end #def makeSymlink(pathPrefix, packagedFile, contentString) #创建符号链接
57
98
 
58
99
  def getTimeObject(packagedFile) #构造时间戳对象
@@ -70,12 +111,10 @@ class VictoriaFresh
70
111
  timeObject=getTimeObject(packagedFile) #构造时间戳对象
71
112
 
72
113
 
73
- puts 'mkdir' #Debug
74
-
75
- # pathToMake=File.join(pathPrefix, packagedFile.name)
114
+ # puts 'mkdir' #Debug
76
115
  pathToMake=File.join(pathPrefix, packagedFile['name'])
77
116
 
78
- puts pathToMake #Debug.
117
+ # puts pathToMake #Debug.
79
118
 
80
119
  if (Dir.exist?(pathToMake)) #目录已经存在
81
120
  else #目录 不存在
@@ -84,29 +123,25 @@ class VictoriaFresh
84
123
 
85
124
 
86
125
  FileUtils.touch pathToMake, :mtime => timeObject #设置修改时间
87
- File.chmod(packagedFile['permission'], pathToMake) #设置权限
126
+
127
+ permissionNumber=packagedFile['permission'] #获取权限数字
128
+
129
+ if (permissionNumber.nil?) #不带权限字段
130
+ elsif #带权限字段
131
+ File.chmod(permissionNumber, pathToMake) #设置权限
132
+ end #if (permissionNumber.nil?) #不带权限字段
88
133
  end #makeDirectory(pathPrefix, packagedFile) #创建目录
89
134
 
90
135
  def releaseFile( pathPrefix, packagedFile, contentString) #释放一个文件
91
- # puts packagedFile.name #Debug
92
- puts packagedFile['name'] #Debug
93
- puts("content stirng length: #{contentString.bytesize}") #Debug
94
- # puts packagedFile.timestamp #Debug
95
- puts packagedFile['timestamp'] #Debug
96
-
97
- # if packagedFile.is_file #是文件,则直接写入文件
98
136
  if packagedFile['is_file'] #是文件,则直接写入文件
99
137
  writeFile(pathPrefix, packagedFile, contentString) #写入文件
100
138
  elsif packagedFile['is_symlink'] #是符号链接,则创建符号链接
101
139
  makeSymlink(pathPrefix, packagedFile, contentString) #创建符号链接
102
- # writeFile(pathPrefix, packagedFile, contentString) #写入文件
103
140
  else #是目录,则创建目录,并递归处理
104
141
  makeDirectory(pathPrefix, packagedFile) #创建目录
105
142
 
106
- # direcotryPathPrefix=pathPrefix + '/' + packagedFile.name #构造针对该目录的路径前缀
107
143
  direcotryPathPrefix=pathPrefix + '/' + packagedFile['name'] #构造针对该目录的路径前缀
108
144
 
109
- # subFiles=packagedFile.sub_files #获取子文件列表。
110
145
  subFiles=packagedFile['sub_files'] #获取子文件列表。
111
146
 
112
147
  subFiles.each do |currentSubFile| #一个个子文件地释放
@@ -116,7 +151,68 @@ class VictoriaFresh
116
151
  end #if packagedFile.is_file #是文件,则直接写入文件
117
152
  end #def releaseFile(packagedFile, contentString) #释放一个文件
118
153
 
119
- def checkOnce(directoryPath,startIndex=0) #打包一个目录树。
154
+ #考虑是否要向磁盘先输出内容
155
+ def assessDiskFlush(layer, isFinalPart=false)
156
+ # contentString=contentStringInput #要处理的内容字符串
157
+
158
+ # if (layer==0) #是最外层
159
+ while (@contentString.length >= @diskFlushSize) #还有内容要写入
160
+ contentToWrite=@contentString[0, @diskFlushSize] #取出开头的一段
161
+
162
+
163
+
164
+ @contentString=@contentString[@diskFlushSize, @contentString.length-@diskFlushSize] #留下剩余的部分
165
+
166
+ if (@diskFlush) #要向磁盘写入内容
167
+
168
+ @diskWriteFileObject.syswrite(contentToWrite) #写入内容
169
+
170
+ @diskWriteFileObject.flush #写入磁盘
171
+
172
+ else #不向磁盘写入内容
173
+ @contentPartArray << contentToWrite #记录到数组中
174
+
175
+ end #if (@diskFlush) #要向磁盘写入内容
176
+ end #while (contentString.length >= @diskFlushSize) #还有内容要写入
177
+
178
+ if (isFinalPart) #是最后一部分
179
+ contentToWrite=@contentString #要写入的内容
180
+
181
+ # @contentPartArray << contentToWrite #记录到数组中
182
+
183
+ @contentString="" #字符串清空
184
+
185
+ if (@diskFlush) #要向磁盘写入内容
186
+
187
+ @diskWriteFileObject.syswrite(contentToWrite) #写入内容
188
+
189
+ @diskWriteFileObject.close #关闭文件
190
+ else #不向磁盘写入内容
191
+ @contentPartArray << contentToWrite #记录到数组中
192
+
193
+
194
+ end #if (@diskFlush) #要向磁盘写入内容
195
+
196
+ end #if (isFinalPart) #是最后一部分
197
+ # end #if (layer==0) #是最外层
198
+
199
+ # end #if (@diskFlush) #要向磁盘写入内容
200
+
201
+ return @contentString #返回内容
202
+ end #contentString= assessDiskFlush(contentString) #考虑是否要向磁盘先输出内容
203
+
204
+ def checkOnce(directoryPath, startIndex=0, layer=0) #打包一个目录树。
205
+ if (@diskFlush) #要向磁盘写入文件
206
+
207
+ if (layer==0) #最外层
208
+ if (@diskMultiFile) #要写多个文件
209
+ else #不写多个文件
210
+ @diskWriteFileObject=File.new(@diskFileName, 'wb') #打开文件
211
+ end #if (@diskMultiFile) #要写多个文件
212
+ end #if (layer==0) #最外层
213
+
214
+ end #if (@diskFlush) #要向磁盘写入文件s
215
+
120
216
  packagedFile={} #创建文件消息对象。
121
217
 
122
218
  packagedFile['sub_files'] = [] #加入到子文件列表中。
@@ -147,57 +243,81 @@ class VictoriaFresh
147
243
 
148
244
  packagedFile['permission']=(File.stat(directoryPath).mode & 07777 ) #设置权限信息
149
245
  rescue Errno::ENOENT
150
-
246
+ rescue Errno::EACCES #权限受限
151
247
  end #begin #读取时间戳
152
-
248
+
153
249
  if (isFile) #是文件,不用再列出其子文件了。
154
250
  packagedFile['file_length']=directoryPathName.size #记录文件的内容长度。
155
251
 
156
252
  #读取文件内容:
157
253
  fileToReadContent=File.new(directoryPath,"rb") #创建文件。
158
- contentString=fileToReadContent.read #全部读取。
254
+ @contentString= @contentString + fileToReadContent.read #全部读取。
159
255
 
160
- # puts("encoding: #{contentString.encoding}") #Debug.
256
+ assessDiskFlush(layer) #考虑是否要向磁盘先输出内容
161
257
  elsif (isSymLink) #是符号链接
162
- # puts("sub file: #{directoryPathName}, class: #{directoryPathName.class}, symlink: #{directoryPathName.symlink?}, expand_path: #{directoryPathName.expand_path}, file?: #{directoryPathName.file?}, read link: #{directoryPathName.readlink}") #Debug.
163
-
164
258
  linkTarget=directoryPathName.readlink #获取链接目标
165
259
 
166
260
  # 待续,设置内容长度。符号链接字符串的长度
167
261
  packagedFile['file_length']=linkTarget.to_s.bytesize #记录文件的内容长度。
168
-
262
+
169
263
  #读取文件内容:
170
264
  # fileToReadContent=File.new(directoryPath,"rb") #创建文件。
171
- contentString=StringIO.new(linkTarget.to_s).binmode.read #全部读取。
172
- # contentString=linkTarget.to_s.encode('UTF-8') #全部读取。
173
- puts("encoding: #{contentString.encoding}") #Debug.
174
- puts("content string: #{contentString}") #Debug
265
+ @contentString= @contentString + StringIO.new(linkTarget.to_s).binmode.read #全部读取。
266
+
267
+ assessDiskFlush(layer) #考虑是否要向磁盘先输出内容
268
+
175
269
  else #是目录。
176
- contentString="" #容纳内容的字符串。
270
+ # contentString="" #容纳内容的字符串。
177
271
  subFileStartIndex=startIndex #子文件的起始位置,以此目录的起始位置为基准。
178
272
 
179
- # packagedFile.file_length=0 #本目录的内容长度。
180
273
  packagedFile['file_length']=0 #本目录的内容长度。
181
274
 
182
275
  directoryPathName.each_child do |subFile| #一个个文件地处理。
183
276
  # puts("sub file: #{subFile}, class: #{subFile.class}, symlink: #{subFile.symlink?}, expand_path: #{subFile.expand_path}, file?: #{subFile.file?}") #Debug.
277
+ # checkMemoryUsage(221)
184
278
  realPath=subFile.expand_path #获取绝对路径。
185
279
 
186
- packagedSubFile,subFileContent=checkOnce(realPath,subFileStartIndex) #打包这个子文件。
280
+ packagedSubFile,subFileContent=checkOnce(realPath,subFileStartIndex, layer+1) #打包这个子文件。
187
281
 
188
282
  packagedFile['sub_files'] << packagedSubFile #加入到子文件列表中。
189
283
 
190
- # puts("sub file content: #{subFileContent}") #Debug
284
+ # puts("sub file content: #{subFileContent}, nil?: #{subFileContent.nil?}" ) #Debug
191
285
 
192
- contentString = contentString + subFileContent #串接文件内容。
286
+ # puts(" content: #{contentString}, nil?: #{contentString.nil? }") #Debug
287
+
288
+ # contentString = contentString + subFileContent #串接文件内容。
289
+
290
+ assessDiskFlush(layer) #考虑是否要向磁盘先输出内容
291
+
193
292
 
194
293
  subFileStartIndex+=packagedSubFile['file_length'] #记录打包的子文件的长度,更新下一个要打包的子文件的起始位置。
195
294
 
295
+ # puts("237, content string length: #{contentString.length}") #Debug
296
+
196
297
  packagedFile['file_length']+=packagedSubFile['file_length'] #随着子文件的打包而更新本目录的总长度。
197
298
  end #directoryPathName.each_child do |subFile| #一个个文件地处理。
198
299
  end #if (isFile) #是文件,不用再列出其子文件了。
199
300
 
200
- return packagedFile,contentString #返回打包之后的对象。和文件内容字节数组。
301
+ # puts("300, contentString: #{contentString}, nil?: #{contentString.nil?}, direcotry path: #{directoryPath}, layer: #{layer}") #Debug
302
+
303
+
304
+ # puts("302, contentString: #{contentString}, nil?: #{contentString.nil?}, direcotry path: #{directoryPath}, layer: #{layer}") #Debug
305
+
306
+ contentToResult="" #要返回的内容
307
+
308
+ if (layer==0) #是最外层
309
+ assessDiskFlush(layer, true) #考虑是否要向磁盘先输出内容
310
+
311
+ if (@diskFlush) #要向磁盘写入缓存内容
312
+ else #不向磁盘写入缓存内容
313
+ contentToResult=@contentPartArray.join #重新合并成字符串
314
+
315
+ end #if (@diskFlush) #要向磁盘写入缓存内容
316
+
317
+ end #if (layer==0) #是最外层
318
+
319
+
320
+ return packagedFile, contentToResult #返回打包之后的对象。和文件内容字节数组。
201
321
  end #def downloadOne #下载一个视频。
202
322
  end
203
323
 
@@ -9,6 +9,8 @@ else #指定了命令行参数。
9
9
 
10
10
  $clipDownloader=VictoriaFresh.new #创建下载器。
11
11
 
12
+ $clipDownloader.diskFlush=false #不向磁盘写入缓存
13
+
12
14
  victoriaFresh,victoriaFreshData=$clipDownloader.checkOnce($rootPath) #打包该目录树。
13
15
 
14
16
  #利用protobuf打包成字节数组:
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: VictoriaFreSh
3
3
  version: !ruby/object:Gem::Version
4
- version: 2020.8.29
4
+ version: 2021.4.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hxcan Cai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-29 00:00:00.000000000 Z
11
+ date: 2021-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cbor