Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/portupgrade
Path: blob/master/lib/pkgtools/pkgmisc.rb
102 views
1
# vim: set sts=2 sw=2 ts=8 et:
2
#
3
# Copyright (c) 2001-2004 Akinori MUSHA <[email protected]>
4
# Copyright (c) 2006-2008 Sergey Matveychuk <[email protected]>
5
# Copyright (c) 2009-2012 Stanislav Sedov <[email protected]>
6
#
7
# All rights reserved.
8
#
9
# Redistribution and use in source and binary forms, with or without
10
# modification, are permitted provided that the following conditions
11
# are met:
12
# 1. Redistributions of source code must retain the above copyright
13
# notice, this list of conditions and the following disclaimer.
14
# 2. Redistributions in binary form must reproduce the above copyright
15
# notice, this list of conditions and the following disclaimer in the
16
# documentation and/or other materials provided with the distribution.
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
# SUCH DAMAGE.
29
#
30
31
class Array
32
def qindex(item)
33
lower = -1
34
upper = size()
35
while lower + 1 != upper
36
mid = (lower + upper) / 2
37
38
cmp = self[mid] <=> item
39
40
cmp.zero? and return mid
41
42
if cmp < 0
43
lower = mid
44
else
45
upper = mid
46
end
47
end
48
49
nil
50
end
51
52
alias qinclude? qindex
53
end
54
55
def shellwords(line)
56
unless line.kind_of?(String)
57
raise ArgumentError, "Argument must be String class object."
58
end
59
line = line.sub(/\A\s+/, '')
60
words = []
61
while line != ''
62
field = ''
63
while true
64
if line.sub!(/\A"(([^"\\]|\\.)*)"/, '') then #"
65
snippet = $1
66
snippet.gsub!(/\\(.)/, '\1')
67
elsif line =~ /\A"/ then #"
68
raise ArgumentError, "Unmatched double quote: #{line}"
69
elsif line.sub!(/\A'([^']*)'/, '') then #'
70
snippet = $1
71
elsif line =~ /\A'/ then #'
72
raise ArgumentError, "Unmatched single quote: #{line}"
73
elsif line.sub!(/\A\\(.)/, '') then
74
snippet = $1
75
elsif line.sub!(/\A([^\s\\'"]+)/, '') then #'
76
snippet = $1
77
else
78
line.sub!(/\A\s+/, '')
79
break
80
end
81
field.concat(snippet)
82
end
83
words.push(field)
84
end
85
words
86
end
87
88
def shelljoin(*args)
89
args.collect { |arg|
90
if /[*?{}\[\]<>()~&|\\$;\'\`\s]/ =~ arg
91
'"' + arg.gsub(/([$\\\"\`])/, "\\\\\\1") + '"'
92
else
93
arg
94
end
95
}.join(' ')
96
end
97
98
def init_tmpdir
99
if ! $tmpdir.nil? && $tmpdir != "" then
100
return
101
end
102
maintmpdir = ENV['PKG_TMPDIR'] || ENV['TMPDIR'] || '/var/tmp'
103
if !FileTest.directory?(maintmpdir)
104
raise "Temporary directory #{maintmpdir} does not exist"
105
end
106
107
cmdline = shelljoin("/usr/bin/mktemp", "-d", maintmpdir + "/portupgradeXXXXXXXX")
108
pipe = IO.popen(cmdline)
109
tmpdir = pipe.gets
110
pipe.close
111
if $? != 0 || tmpdir.nil? || tmpdir.length == 0
112
raise "Could not create temporary directory in #{maintmpdir}"
113
end
114
tmpdir.chomp!
115
116
at_exit {
117
begin
118
xsystem("rm -r #{tmpdir}")
119
rescue
120
warning_message "Could not clean up temporary directory: " + $!
121
end
122
}
123
$tmpdir=tmpdir
124
end
125
126