ALT Linux Team development discussions
 help / color / mirror / Atom feed
From: Alexey Tourbin <at@altlinux.ru>
To: ALT Linux Team development discussions <devel@lists.altlinux.org>
Subject: [devel] #! shebang fixer
Date: Mon, 10 Sep 2007 03:48:27 +0400
Message-ID: <20070909234827.GZ6051@solemn.turbinal> (raw)
In-Reply-To: <9713cfcc0709091627x508b9094y4cda6be9cd04a47d@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3163 bytes --]

On Mon, Sep 10, 2007 at 02:27:11AM +0300, Led wrote:
> > Этот пакет содержит некоторое количество tcl скриптов.
> > $ rpmfile /ALT/Sisyphus/files/noarch/RPMS/TclTutor-2.0-alt0.2.noarch.rpm |grep tcl
> > /usr/bin/TclTutor.tcl   100755  Bourne shell script text executable
> > /usr/bin/tcltutor       120777  symbolic link to `TclTutor.tcl'
> > /usr/share/TclTutor/htmllib.tcl 100644  Bourne shell script text executable
> > /usr/share/TclTutor/scaler.tcl  100644  ASCII English text
> > $
> >
> > Эти скрипты, по-видимому, не требуют никаких tcl пакетов.
> 
> Там вот такое:
> $ head -3 /usr/bin/TclTutor.tcl
> #!/bin/sh
> # \
> exec wish "$0" "$@"
> 
> Наветное, имеет смысл заменить на явное
> #!/usr/bin/wish
> ?

Здесь есть два подхода.

Во-первых, можно написать shebang fixer.  То есть чтобы на стадии fixup,
перед поиском зависимостей, совершенно автоматически выправлять все
кривые шебанги.  Но этот фиксер будет эвристическим, и если он
облажается, то это будет пшик и автору фиксера дадут подзатыльник.
Подумайте: на самом деле придётся писать full-fledged шелл-парсер.
Ведь "$0" "$@" это простейший вариант, а любая менее тривиальная
манипуляция с аргументами может ввести фиксер в глубокий ступор.
В лучшем случае он просто ничего не зафиксит.  А в худшем случае
зафиксит так что мало не покажется.

Кроме того, есть такие случаи, которые зафиксить принципиально нельзя,
и которые, вместе с тем, имеют свой глубокий смысл.

$ head -3 /usr/lib/rpm/tcl.req
#!/bin/sh
# -*- tcl -*- \
exec ${RPM_TCLSH:-/usr/bin/tclsh} "$0" "$@"
$

Поэтому я решил, что можно реализовать менее навязчивые варианты.
В частности, в новом rpm-build уже реализовано распознавание
/usr/bin/env для всех типичных случаев.  В частности,
релевантная часть анализатора шебанга (shebang.req) выглядит так:

	local f="$1" line=; shift
	line=$(sed -n '1s|^#![[:space:]]*/|/|p' "$f")
	[ -n "$line" ] || return 0
	set -- $line
	case "$#,$1" in
		2,/usr/bin/env)
			FindPackage "$f" "$1" "$2" ;;
		*)
			FindPackage "$f" "$1" ;;
	esac

То есть скрыть зависимость на интерпретатор через /usr/bin/env
в типичном случае (если всего один аргумент у env) уже нельзя.

Теперь только остаётся придумать, что делать с re-exec'ом.
В принципе, подход тоже есть, он реализован в shell.req:

	if ! reqs="$($sh --rpm-requires "$f")"; then
		# sh --rpm-requires failed, and stderr is already there.
		# We are almost dead.  The last chance to escape is to see
		# if the shell is used only to re-exec another interpreter, e.g.
		#	exec tclsh "$0" "$@"
		if line1=$(egrep -m1 -v '^[[:space:]]*(#|$)' "$f"); then
			set -- $line1
			if [ $# -gt 1 ] && [ "$1" = exec ]; then
				Info "$f is $2 script!"
				FindPackage "$f" "$2"
				return 0
			fi
		fi
		Fatal "$f: $sh --rpm-requires failed"
	fi

Проблема тут в том, что этот код нельзя сделать вполне универсальным,
то есть реализовать на его основе диспетчерезацию на другой тип
зависимостей.  Но можно просто вставить аналогичный код в tcl.req.files.
Кажется, это единственный язык, который в котором любят делать
шебанг-хак через re-exec в первой значащей строчке.

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

  reply	other threads:[~2007-09-09 23:48 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-09 18:19 [devel] rpm-build-4.0.4-alt78 rebuild status Alexey Tourbin
2007-09-09 19:45 ` [devel] rpm-build-4.0.4-alt78 beehive_status Alexey Tourbin
2007-09-09 19:54   ` Slava Semushin
2007-09-09 19:57     ` Slava Semushin
2007-09-09 20:15       ` Alexey Tourbin
2007-09-09 20:12     ` Alexey Tourbin
2007-09-09 20:16       ` Led
2007-09-09 22:30         ` Alexey Tourbin
2007-09-09 20:39       ` Alexey I. Froloff
2007-09-09 23:05         ` Alexey Tourbin
2007-09-09 22:28   ` [devel] rpm-build-4.0.4-alt78 tcl dependencies Alexey Tourbin
2007-09-09 23:27     ` Led
2007-09-09 23:48       ` Alexey Tourbin [this message]
2007-09-10  8:48       ` [devel] tcl.req.files: re-exec check implemented Alexey Tourbin
2007-09-10 10:46     ` [devel] rpm-build-4.0.4-alt78 tcl dependencies Sergey Bolshakov
2007-09-10 20:46       ` Alexey Tourbin
2007-09-10 21:52       ` [devel] rpm-build-4.0.4-alt78 -- remove rpm-build-tcl? Alexey Tourbin
2007-09-11  9:49         ` Sergey Bolshakov
2007-09-11 14:59           ` Alexey Tourbin
2007-09-11 15:35             ` Led
2007-09-10 22:13       ` [devel] rpm-build-4.0.4-alt78 rpm-build-tcl vs BuildRequires(pre) Alexey Tourbin
2007-09-11  9:30         ` Sergey Bolshakov
2007-09-11 15:49           ` [devel] link tcl extensions with libtcl Alexey Tourbin
2007-09-10 20:39     ` [devel] rpm-build-4.0.4-alt78 tcl SCRIPTS DISABLED Alexey Tourbin
2007-09-10 22:38     ` [devel] rpm-build-4.0.4-alt78 tcl dependencies Michael Shigorin
2007-09-10 10:13   ` [devel] rpm-build-4.0.4-alt78 beehive_status Sergey Bolshakov
2007-09-10 22:28   ` [devel] rpm-build-tcl Requires += tcl? Michael Shigorin
2007-09-11 16:13   ` [devel] Zope-2.7.7-alt1 Alexey Tourbin
2007-09-10 22:25 ` [devel] rpm-build-4.0.4-alt78 rebuild status Michael Shigorin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070909234827.GZ6051@solemn.turbinal \
    --to=at@altlinux.ru \
    --cc=devel@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux Team development discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
		devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
	public-inbox-index devel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git