-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.sh
executable file
·171 lines (129 loc) · 3.15 KB
/
import.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/sh
# Copyright (C) 2022-2023 by Yuri Victorovich. All rights reserved.
##
## import.sh is a script that
## creates the PortsDB SQLite database
## and adds all ports from the ports tree
## specified in the PORTSDIR environment
## variable.
##
##
## Arguments:
## - DB: (optional) database file to be created (default: ports.sqlite)
## - SQL_FILE: (optional) file to write the SQL dump from which the database can be recreated
##
## Environment variables:
## - PORTSDIR: (optional) ports tree to build the database for (default: /usr/ports)
## - SUBDIR: (optional, DEBUG) choose a subdir in the ports tree, generally produces a broken DB with foreign key violations
##
##
## set strict mode
##
STRICT="set -euo pipefail"
$STRICT
##
## find CODEBASE
##
SCRIPT=$(readlink -f "$0")
CODEBASE=$(dirname "$SCRIPT")
##
## include function libraries and read parameters
##
. $CODEBASE/include/functions.sh
. $CODEBASE/include/functions-import.sh
. $CODEBASE/include/functions-sql.sh
. $CODEBASE/params.sh
##
## read arguments and set defaults
##
DB=${1-} # write the SQLite DB (do not write DB when empty)
SQL_FILE=${2-} # save SQL statements into this file, if set
SQL_FILE_ARG="${SQL_FILE}"
##
## read env variables
##
PORTSDIR=${PORTSDIR:-/usr/ports} # default value
SUBDIR=${SUBDIR-}
##
## global variables
##
PORTSDIR_EFFECTIVE=""
PERFORM_ACTION_WRITE_DB=no
PERFORM_ACTION_WRITE_SQL=no
##
## check dependencies
##
check_dependencies || fail ""
##
## usage
##
usage() {
$STRICT
fail "Usage: $0 <db.sqlite> <file.sql> [{sync|async}]"
}
##
## set defaults
##
if [ -z "$DB" -a -z "$SQL_FILE" ]; then
# no DB or SQL file is supplied, default to ports.sqlite
DB="ports.sqlite"
fi
##
## what do we do
##
[ -n "$DB" ] && PERFORM_ACTION_WRITE_DB=yes
[ -n "$SQL_FILE_ARG" ] && PERFORM_ACTION_WRITE_FILE=yes
##
## check arguments and required enviroment values
##
if ! is_ports_tree_directory $PORTSDIR; then
perror "error: the PORTSDIR environment variable should point to a valid ports tree"
usage
fi
if [ -n "$SUBDIR" ] && ! [ -f "$PORTSDIR/$SUBDIR/Makefile" ]; then
echo "error: the SUBDIR environment variable is expected to point to a valid subdirectory in the ports tree"
usage
fi
# create the file for SQL statements that will be written after traversing the ports tree
if [ -z "$SQL_FILE" ]; then
# generate temporary SQL file if not provided by the user
SQL_FILE=$(mktemp -t ports.sql)
fi
##
## adjust values
##
PORTSDIR=$(make_file_path_global $PORTSDIR)
if [ -n "$DB" ]; then
DB=$(make_file_path_global "$DB")
fi
if [ -n "$SQL_FILE" ]; then
SQL_FILE=$(make_file_path_global "$SQL_FILE")
fi
##
## save arguments and other values in environment
##
export DB
export SQL_FILE
export CODEBASE
export PORTSDIR
##
## MAIN
##
# announcement
announcement "starting to import"
# explain
explain_action
# initialize
initialize
# traverse
PORTSDIR_EFFECTIVE=$(effective_ports_tree $PORTSDIR)
import $PORTSDIR_EFFECTIVE
# save Git revision of the ports tree
write_ports_tree_revision $PORTSDIR "imported ports tree revision $(ports_tree_get_current_revision $PORTSDIR)"
# finalize
finalize
# remove temp file
delete_temp_files
# status report
status_report
exit 0