<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="http://forum.ubuntu-fr.org/extern.php?action=feed&amp;tid=1159611&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Forum Ubuntu-fr.org / Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
		<link>http://forum.ubuntu-fr.org/viewtopic.php?id=1159611</link>
		<description><![CDATA[Les sujets les plus récents dans Comment fonctionnent les pipes et les commandes shell avec Python.]]></description>
		<lastBuildDate>Fri, 11 Jan 2013 14:19:04 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12169651#p12169651</link>
			<description><![CDATA[<p>Voici la nouvelle version du code:</p><div class="codebox"><pre><code>#!/usr/bin/env python

# Add for 4 spaces at each line of the current selection
# store the result in the clipboard

import subprocess
import tempfile

sel = subprocess.check_output(&quot;xsel&quot;)
end_new_line = sel.endswith(&quot;\n&quot;)
sel = sel.replace(&quot;\n&quot;, &quot;\n    &quot;, sel.count(&quot;\n&quot;) - int(end_new_line))
sel = &quot;    &quot; + sel
with tempfile.TemporaryFile() as tmp_file:
    tmp_file.write(sel)
    tmp_file.flush()
    tmp_file.seek(0)
    subprocess.call([&quot;xsel&quot;, &quot;-b&quot;, &quot;-i&quot;], stdin=tmp_file)</code></pre></div><p>Ça fonctionne toujours mais si j&#039;enlève l&#039;option &quot;-i&quot;, ça ne fonctionne plus.</p>]]></description>
			<author><![CDATA[dummy@example.com (Pierre Thibault)]]></author>
			<pubDate>Fri, 11 Jan 2013 14:19:04 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12169651#p12169651</guid>
		</item>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12166891#p12166891</link>
			<description><![CDATA[<p>ton code me donne une idée:</p><p>&#160; - essaye laisser le mode par défaut (lecture+ecriture et binaire) sur ton tempfile<br />&#160; - essaye d&#039;appeler .flush() sur ton file descripteur histoire d&#039;être sûr d&#039;avoir tout est écrit avant de lancer ton process</p><p>avec ça ça devrait mieux marcher, tu confirmes ?</p><div class="quotebox"><blockquote><div><p>OK, ça expliquerait les choses.</p></div></blockquote></div><p>En fait non... le code source est très clair: <em>xsel</em> va lire l&#039;entrée standard ssi <strong>isatty(0)</strong> renvoie <strong>True</strong>, et après un rapide test chez moi ce n&#039;est pas le cas dans le cas d&#039;un appel depuis <strong>subprocess.call()</strong> avec l&#039;option stdin settée à un fichier.<br />D&#039;où l&#039;idée qu&#039;il n&#039;y arrive pas parce que le fichier n&#039;est pas prêt.</p>]]></description>
			<author><![CDATA[dummy@example.com (aurelien.noce)]]></author>
			<pubDate>Fri, 11 Jan 2013 09:37:06 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12166891#p12166891</guid>
		</item>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12163981#p12163981</link>
			<description><![CDATA[<div class="quotebox"><cite>aurelien.noce a écrit&#160;:</cite><blockquote><div><p>En effet c&#039;est bizarre ton histoire, et surtout je n&#039;ai pas le bug chez moi... c&#039;est quoi ta version de python/xsel ?</p></div></blockquote></div><div class="codebox"><pre><code>&gt; python --version &amp;&amp; xsel --version
Python 2.7.3
xsel version 1.2.0 by Conrad Parker &lt;conrad@vergenet.net&gt;</code></pre></div><div class="quotebox"><cite>aurelien.noce a écrit&#160;:</cite><blockquote><div><p>En même temps, l&#039;auteur lui-même précise que son algo de defaulting est assez limite (src) et qu&#039;il vaut mieux utiliser les options explicites comme le -i ici.</p></div></blockquote></div><p>OK, ça expliquerait les choses.</p><div class="quotebox"><cite>aurelien.noce a écrit&#160;:</cite><blockquote><div><p>Mais bon je suis curieux de voir comprendre le pourquoi du comment. Au hasard, est-tu sûr que ton tmp_file ne vaut pas justement sys.stdin ?</p></div></blockquote></div><p>Non, je ne crois pas. Voici le code Python de mon petit script:</p><div class="codebox"><pre><code>#!/usr/bin/env python

# Add for 4 spaces at each line of the current selection
# store the result in the clipboard

import subprocess
import tempfile

sel = subprocess.check_output(&quot;xsel&quot;)
end_new_line = sel.endswith(&quot;\n&quot;)
sel = sel.replace(&quot;\n&quot;, &quot;\n    &quot;, sel.count(&quot;\n&quot;) - int(end_new_line))
sel = &quot;    &quot; + sel
with tempfile.TemporaryFile(mode=&#039;w+&#039;) as tmp_file:
    tmp_file.write(sel)
    tmp_file.seek(0)
    subprocess.call([&quot;xsel&quot;, &quot;-b&quot;, &quot;-i&quot;], stdin=tmp_file)</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (Pierre Thibault)]]></author>
			<pubDate>Fri, 11 Jan 2013 00:04:58 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12163981#p12163981</guid>
		</item>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12160011#p12160011</link>
			<description><![CDATA[<p>En effet c&#039;est bizarre ton histoire, et surtout je n&#039;ai pas le bug chez moi... c&#039;est quoi ta version de python/xsel ?</p><p>En même temps, l&#039;auteur lui-même précise que son algo de defaulting est assez limite (<a href="https://github.com/kfish/xsel/blob/master/xsel.c#L1979">src</a>) et qu&#039;il vaut mieux utiliser les options explicites comme le <strong>-i</strong> ici.</p><p>Mais bon je suis curieux de voir comprendre le pourquoi du comment. Au hasard, est-tu sûr que ton <strong>tmp_file</strong> ne vaut pas justement <strong>sys.stdin</strong> ?</p>]]></description>
			<author><![CDATA[dummy@example.com (aurelien.noce)]]></author>
			<pubDate>Thu, 10 Jan 2013 18:43:08 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12160011#p12160011</guid>
		</item>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12119291#p12119291</link>
			<description><![CDATA[<p>OK. C&#039;est ce que je vois. Donc on peut détecter si une entrée ou une sortie est un terminal. Je ne savais pas cela.</p><p>Cela ne me semble pas être en accord avec la doc qui dit:</p><div class="quotebox"><blockquote><div><p>Otherwise, the current selection is output if standard output is not a terminal (tty), and the selection is set from standard input if standard input is not a terminal (tty).</p></div></blockquote></div><p>Ça dit que ça lit l&#039;entrée standard donc je ne vois pas pourquoi je dois employer l&#039;option -i.</p>]]></description>
			<author><![CDATA[dummy@example.com (Pierre Thibault)]]></author>
			<pubDate>Mon, 07 Jan 2013 12:26:54 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12119291#p12119291</guid>
		</item>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12117141#p12117141</link>
			<description><![CDATA[<p>Bonjour,<br />Comme l&#039;indique l&#039;extrait de <strong>man xsel</strong> (Maisondouf #2), l&#039;entrée standard est traitée différemment si c&#039;est un terminal.<br />Ces trois commandes vont bien lire l&#039;entrée standard :</p><div class="codebox"><pre><code>cat tmp_file | xsel
xsel &lt;tmp_file
xset -i        # Lecture sur le terminal</code></pre></div><p>mais pas celle-ci :</p><div class="codebox"><pre><code>xsel</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (pingouinux)]]></author>
			<pubDate>Mon, 07 Jan 2013 07:16:10 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12117141#p12117141</guid>
		</item>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12116601#p12116601</link>
			<description><![CDATA[<div class="quotebox"><cite>Maisondouf a écrit&#160;:</cite><blockquote><div><p>Alors effectivement depuis Python, il n&#039;y pas de notion de pipe au sens shell, c&#039;est le call python qui &quot;déverse&quot; le contenu du fichier dans l&#039;entrée de la commande.<br />Comme si sous shell, on avait fait:</p><div class="codebox"><pre><code>cat tmp_file | xsel</code></pre></div></div></blockquote></div><p>Je ne comprends pas. Si je fais la commande précédente dans le terminal, je n&#039;ai pas besoin de l&#039;option -i comme en Python. Est-ce que ça veut dire qu&#039;il y a une erreur dans la documentation de la commande xsel?</p>]]></description>
			<author><![CDATA[dummy@example.com (Pierre Thibault)]]></author>
			<pubDate>Mon, 07 Jan 2013 02:31:37 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12116601#p12116601</guid>
		</item>
		<item>
			<title><![CDATA[Réponse à&#160;:  Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12116451#p12116451</link>
			<description><![CDATA[<p>parce que le shell, c&#039;est pas du python.<br />Chacun a sa syntaxe et son fonctionnement distinct.</p><div class="quotebox"><cite>Man xsel a écrit&#160;:</cite><blockquote><div><p>By default, this program outputs the selection without modification if both standard input and standard output are terminals (ttys). <br />Otherwise, the current selection is output if standard output is not a terminal (tty), and the selection is set from standard input if standard input is not a terminal (tty).</p></div></blockquote></div><p>Alors effectivement depuis Python, il n&#039;y pas de notion de pipe au sens shell, c&#039;est le call python qui &quot;déverse&quot; le contenu du fichier dans l&#039;entrée de la commande.<br />Comme si sous shell, on avait fait:</p><div class="codebox"><pre><code>cat tmp_file | xsel</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (Maisondouf)]]></author>
			<pubDate>Mon, 07 Jan 2013 01:33:40 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12116451#p12116451</guid>
		</item>
		<item>
			<title><![CDATA[Comment fonctionnent les pipes et les commandes shell avec Python]]></title>
			<link>http://forum.ubuntu-fr.org/viewtopic.php?pid=12114471#p12114471</link>
			<description><![CDATA[<p>Bonjour,</p><p>J&#039;ai de la difficulté à comprendre comment les redirections fonctionnent avec les commandes shell et Python. Par exemple, si dans un shell bash je tape:</p><div class="codebox"><pre><code>aa | bb</code></pre></div><p>Si je tape la commande précédente, cela veut dire que la sortie de la commande &#039;aa&#039;, stdout, sera l&#039;entrée pour la commande &#039;bb&#039;, stdin, n&#039;est-ce pas?</p><p>Mon problème se trouve à cette ligne de code en Python :</p><div class="codebox"><pre><code>subprocess.call([&quot;xsel&quot;, &quot;-b&quot;, &quot;-i&quot;], stdin=tmp_file)</code></pre></div><p>L&#039;entrée pour la commande &#039;xsel&#039; se trouve avec le fichier tmp_file. Cette commande fonctionne très bien. Ce que je ne comprends pas est que j&#039;ai dû employer l&#039;option &#039;-i&#039; qui est définie dans la doc de xsel de la façon suivante :</p><div class="codebox"><pre><code> -i, --input           Read standard input into the selection</code></pre></div><p>Ma question est pourquoi dois-je employer cette option?</p><p>Par exemple, si je tape dans le shell bash :</p><p>echo xx | xsel</p><p>xsel lit son information depuis l&#039;entrée standard qui est alimentée par la commande &#039;echo&#039;, n&#039;est-ce pas? Pourquoi n&#039;ai-je pas à employer l&#039;option -i comme pour ma ligne de code Python? <img src="http://forum.ubuntu-fr.org/img/smilies/roll.png" width="15" height="15" alt="roll" /></p>]]></description>
			<author><![CDATA[dummy@example.com (Pierre Thibault)]]></author>
			<pubDate>Sun, 06 Jan 2013 19:55:26 +0000</pubDate>
			<guid>http://forum.ubuntu-fr.org/viewtopic.php?pid=12114471#p12114471</guid>
		</item>
	</channel>
</rss>
