summaryrefslogtreecommitdiff
blob: 54c7cbae8ae86af179211c36790bcb75f4be6408 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Copyright 2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: monotone.eclass
# @MAINTAINER:
# Martin Väth <martin@mvath.de>
# @AUTHOR:
# Martin Väth <martin@mvath.de>
# @BLURB: The monotone eclass is written to fetch software sources from monotone repositories
# @DESCRIPTION:
# The monotone eclass provides functions to fetch software sources from
# monotone repositories.

# @ECLASS-VARIABLE: EMTN_STORE_DIR
# @DESCRIPTION:
# monotone sources store directory. Users may override this in /etc/make.conf
: ${EMTN_STORE_DIR:=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}/mtn-src}

# @ECLASS-VARIABLE: EMTN_OFFLINE
# @DESCRIPTION:
# Set this variable to a non-empty value to disable the automatic updating of
# an monotone source tree. This is intended to be set by users.
: ${EMTN_OFFLINE:=${EVCS_OFFLINE}}

# @ECLASS-VARIABLE: EMTN_CMD
# @DESCRIPTION:
# monotone command with argument for database which must be '$db'
: ${EMTN_CMD:=mtn -d \"\$db\"}

# @ECLASS-VARIABLE: EMTN_PULL_CMD
# @DESCRIPTION:
# monotone pull command
: ${EMTN_PULL_CMD:=${EMTN_CMD} pull}

# @ECLASS-VARIABLE: EMTN_INIT_CMD
# @DESCRIPTION:
# monotone init command
: ${EMTN_INIT_CMD:=${EMTN_CMD} db init}

# @ECLASS-VARIABLE: EMTN_CO_CMD
# @DESCRIPTION:
# monotone checkout command
: ${EMTN_CO_CMD:=${EMTN_CMD} co}

# @ECLASS-VARIABLE: EMTN_PRINT_HEADS_CMD
# @DESCRIPTION:
# monotone command to print the revision of the heads
: ${EMTN_PRINT_HEADS_CMD:=${EMTN_CMD} automate heads}

# @ECLASS-VARIABLE: EMTN_DB
# @DESCRIPTION:
# Name of the database file where the local monotone repository is stored.
: ${EMTN_DB:=${PN}.db}

# @ECLASS-VARIABLE: EMTN_REPO_URI
# @DESCRIPTION:
# Name of the external monotone repository, e.g. foo.bar.org
: ${EMTN_REPO_URI:=}

# @ECLASS-VARIABLE: EMTN_GLOB
# @DESCRIPTION:
# Name of the glob for the external repository. Typically '*'
: ${EMTN_GLOB:=*}

# @ECLASS-VARIABLE: EMTN_MODULEPATH
# @DESCRIPTION:
# Name of the module to checkout
: ${EMTN_MODULEPATH:=${PN}}

# @ECLASS-VARIABLE: EMTN_MODULEDIR
# @DESCRIPTION:
# Name where the module should come. Empty means: basename of modulepath.
: ${EMTN_MODULEDIR:=}

# @ECLASS-VARIABLE: EMTN_REVISIONARGS
# @DESCRIPTION:
# Args for revision to checkout, e.g. "-r something"
# The special value "head" means to use the first head.
: ${EMTN_REVISIONARGS=head}

# @ECLASS-VARIABLE: EMTN_DISABLE_DEPENDENCIES
# @DESCRIPTION:
# Set this variable to a non-empty value to disable the automatic inclusion of
# monotone in dependencies.
: ${EMTN_DISABLE_DEPENDENCIES:=}

# @FUNCTION: monotone_fetch
# @USAGE: [repo_uri] [glob] [db]
# @DESCRIPTION:
# Fetch/update ${EMTN_STORE_DIR}/database from external uri (using glob)
# and copy it into ${S}.
# After this function, current working directory is ${S}.
#
# Can take three optional parameters:
#   repo_uri - a repository URI. If empty defaults to EMTN_REPO_URI.
#   glob     - The glob for URI. If empty defaults to EMTN_GLOB.
#   db       - the database filename. If empty defaults to EMTN_DB.
monotone_fetch() {
	local repo_uri glob db db_full
	repo_uri=${1:-${EMTN_REPO_URI}}
	glob=${2:-${EMTN_GLOB}}
	db=${3:-${EMTN_DB}}
	test -d "${EMTN_STORE_DIR}" || (
			addwrite /
			mkdir -p -- "/${EMTN_STORE_DIR}"
	)
	cd -P -- "${EMTN_STORE_DIR}" >/dev/null \
		|| die "cannot cd to ${EMTN_STORE_DIR}"

	if ! test -e "${db}"
	then	(
		addwrite "${PWD}"
		einfo "Initializing new ${db}" && \
		eval "${EMTN_INIT_CMD}" && \
		einfo "Fetching ${db} from remote ${repo_uri}" && \
		eval "${EMTN_PULL_CMD} \"\${repo_uri}\" \"\${glob}\""
	)
	elif [ -z "${EMTN_OFFLINE}" ]
	then	(
		addwrite "${PWD}"
		einfo "Updating ${db} from remote ${repo_uri}"
		eval "${EMTN_PULL_CMD}"
	)
	fi || die "Could not fetch/update ${db}"
	db_full="${EMTN_STORE_DIR}/${db}"
	einfo "Copying database ${db_full} ..."
	test -d "${S}" || mkdir -p -- "${S}" || die "mkdir ${S} failed"
	cd -- "${S}" >/dev/null
	cp -p -- "${db_full}" "${db}" || die "cp ${db_full} ${db} failed"
}

# @FUNCTION: monotone_co
# @USAGE: [db] [modulepath] [moduledir] [revisionargs]
# @DESCRIPTION:
# Unpack monotone sources from the local database.
#
# All parameters are optional:
#   db         - the database filename. If empty defaults to EMTN_DB.
#   modulepath - the name of the module to checkout.
#                If empty defaults to EMTN_MODULEPATH
#   moduledir  - the name of the directory in which the module should come.
#                If empty defaults to EMTN_MODULEDIR
#                If that is also empty defaults to basename of EMTN_MODULEPATH.
#   revisionargs - Args for the revision to checkout. If empty defaults to
#                EMTN_REVISIONARGS.
#                The special value "head" means to use the first head.
monotone_co() {
	local db modulepath moduledir r
	db=${1:-${EMTN_DB}}
	modulepath=${2:-${EMTN_MODULEPATH}}
	moduledir=${3:-${EMTN_MODULEDIR}}
	[ -z "${moduledir}" ] && moduledir=${modulepath##*/}
	if [ ${#} -gt 3 ]
	then	shift 3
	else	eval "set -- ${EMTN_REVISIONARGS}"
	fi
	if [ "${1}" = 'head' ]
	then	if r=`eval "${EMTN_PRINT_HEADS_CMD} \"\${modulepath}\"" \
		| tail -n1` && [ -n "${r}" ]
		then	set -- -r "${r}"
		else	set --
		fi
	fi
	if [ -n "${modulepath}" ]
	then	einfo "Checking out module ${modulepath}"
		eval "${EMTN_CO_CMD} -b \"\${modulepath}\" \"\${@}\" \"\${moduledir}\"" \
			|| die "checkout of ${modulepath} failed"
	else	einfo "Checking out module ${module}"
		eval "${EMTN_CO_CMD} \"\${@}\" \"\${moduledir}\"" \
			|| die "checkout of ${module} failed"
	fi
}

# @FUNCTION: monotone_finish
# @USAGE: [db]
# @DESCRIPTION:
# Call this when all modules are checked out: Removes the local database.
# The optional argument db defaults to EMTN_DB.
monotone_finish() {
	local db
	db=${1:-${EMTN_DB}}
	rm -- "${S}/${db}" || die "cannot remove ${S}/${db}"
}

# @FUNCTION: monotone_src_unpack
# @DESCRIPTION:
# Default src_unpack. Call monotone_fetch, monotone_co, monotone_finish
monotone_src_unpack() {
	monotone_fetch
	monotone_co
	monotone_finish
}

[ -n "${EMTN_DISABLE_DEPENDENCIES}" ] || DEPEND='dev-vcs/monotone'

EXPORT_FUNCTIONS src_unpack