Popular recipes tagged "meta:requires=subprocess"http://code.activestate.com/recipes/tags/meta:requires=subprocess/2016-01-07T18:02:52-08:00ActiveState Code RecipesPython-controlled Unix pipeline to generate PDF (Python)
2016-01-07T18:02:52-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579146-python-controlled-unix-pipeline-to-generate-pdf/
<p style="color: grey">
Python
recipe 579146
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/unix/">unix</a>).
</p>
<p>This recipe shows how to create a Unix pipeline that generates PDF output, under the control of a Python program. It is tested on Linux. It uses nl, a standard Linux command that adds line numbers to its input, and selpg, a custom Linux command-line utility, that selects only specified pages from its input, together in a pipeline (nl | selpg). The Python program sets up and starts that pipeline running, and then reads input from it and generates PDF output.</p>
How to execute x86 64-bit assembly code directly from Python on Linux (requires Nasm) (Python)
2015-03-24T18:23:19-07:00Simon Harrisonhttp://code.activestate.com/recipes/users/4191738/http://code.activestate.com/recipes/579037-how-to-execute-x86-64-bit-assembly-code-directly-f/
<p style="color: grey">
Python
recipe 579037
by <a href="/recipes/users/4191738/">Simon Harrison</a>
(<a href="/recipes/tags/nasm/">nasm</a>).
Revision 4.
</p>
<p>This particular example pertains to running 64-bit assembly code under 64-bit Python, however if you are fortunate enough to be running on a 32-bit Linux platform you may get away with doing a lot less. Since the introduction of DEP you can no longer just shove some code in a buffer and execute it. You must first allow execute permission. Further, you cannot change memory protection settings for anything other than an entire page, so I've padded the memory allocated to include a page before and a page after, so I can seek back to the start of page for the start of my assembler, then change memory protection from there secure in the knowledge that I won't change protection for anything beyond the end of my allocated region. I could probably have used valloc() instead.</p>
<p>After that it's simply a matter of changing protection. There is some doubt over whether you can leave this memory read/writable after you set the execute bit, I haven't played with that.</p>
<p>Finally, it's important to reset the memory protection before the memory gets freed/reused by Python because Python has no knowledge of the hackery you've just been up to.</p>
<p>For a Windows version, use VirtualProtect() to achieve similar results. I could have conditionally added this and made the code portable, however I rarely do any assembler on Windows, so it's left as an exercise for the reader.</p>
A Basic USe flag EDitor for Gentoo Linux supporting on-the-fly editing (Python)
2015-02-28T07:04:31-08:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/579028-a-basic-use-flag-editor-for-gentoo-linux-supportin/
<p style="color: grey">
Python
recipe 579028
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/popen/">popen</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/user_input/">user_input</a>).
</p>
<p>This allows for on-the-fly editing. Simply drop abused.py into your path, and ensure that -a is not set in EMERGE_DEFAULT_OPTS in /etc/portage/make.conf. Then whenver you are installing new packages, use abused in place of emerge (eg: abused multitail) you will be presented with a list of use flags that are used in this action, and a prompt for editing any of them, simply hit enter with no changes to fire off the build.</p>
Recursive Multimedia (audio, video) M3U Playlist Generator (Python)
2014-02-08T01:03:36-08:00Mano Bastardohttp://code.activestate.com/recipes/users/4182040/http://code.activestate.com/recipes/578771-recursive-multimedia-audio-video-m3u-playlist-gene/
<p style="color: grey">
Python
recipe 578771
by <a href="/recipes/users/4182040/">Mano Bastardo</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/ffmpeg/">ffmpeg</a>, <a href="/recipes/tags/generate/">generate</a>, <a href="/recipes/tags/m3u/">m3u</a>, <a href="/recipes/tags/mulitmedia/">mulitmedia</a>, <a href="/recipes/tags/os/">os</a>, <a href="/recipes/tags/playlist/">playlist</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/video/">video</a>).
Revision 9.
</p>
<p>Generate an m3u playlist searching recursively
for multimedia files (video or audio) in the given
directory.
Information from ID3 tags will be extracted for audio
files with <a href="http://en.wikipedia.org/wiki/FFmpeg">FFmpeg</a> available.</p>
Create a temporary mailbox (Python)
2013-04-29T14:04:20-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/578514-create-a-temporary-mailbox/
<p style="color: grey">
Python
recipe 578514
by <a href="/recipes/users/4173873/">Noufal Ibrahim</a>
(<a href="/recipes/tags/mail/">mail</a>, <a href="/recipes/tags/mbox/">mbox</a>).
</p>
<p>Generates a mailbox with lots of messages.</p>
Sending Email in Python (Python)
2013-02-24T13:47:01-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578472-sending-email-in-python/
<p style="color: grey">
Python
recipe 578472
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/email_sending/">email_sending</a>, <a href="/recipes/tags/sendmail/">sendmail</a>, <a href="/recipes/tags/smtp/">smtp</a>).
</p>
<p>Every Python Application needs to send email at some point. Whether it's for reporting errors, status updates or simply the core functionality of the system this little recipe should help! Documented and Tested.</p>
Threaded communication with subprocess (Python)
2013-03-13T06:06:16-07:00Jan Müllerhttp://code.activestate.com/recipes/users/4183984/http://code.activestate.com/recipes/578488-threaded-communication-with-subprocess/
<p style="color: grey">
Python
recipe 578488
by <a href="/recipes/users/4183984/">Jan Müller</a>
(<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/messaging/">messaging</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/queue/">queue</a>, <a href="/recipes/tags/subprocess/">subprocess</a>).
Revision 3.
</p>
<p>This recipe shows how to domesticate another executable as a service in a subprocess.</p>
Python subprocess: hide console on Windows (Python)
2013-07-29T05:33:49-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578300-python-subprocess-hide-console-on-windows/
<p style="color: grey">
Python
recipe 578300
by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>It creates a new <em>hidden</em> window, so it will work in frozen apps (.exe).</p>
determine google count by automatisation (Python)
2012-12-31T15:33:39-08:00Peer Valhoegenhttp://code.activestate.com/recipes/users/4184749/http://code.activestate.com/recipes/578392-determine-google-count-by-automatisation/
<p style="color: grey">
Python
recipe 578392
by <a href="/recipes/users/4184749/">Peer Valhoegen</a>
(<a href="/recipes/tags/count/">count</a>, <a href="/recipes/tags/google/">google</a>, <a href="/recipes/tags/linux/">linux</a>).
Revision 2.
</p>
<p>Sometimes you need o know the number of results that google gets for a specific query.
There are lots of scripts that claim to do that, but I didn't find any that worked.
They mostly rely on urllib, which is blocked by Google.</p>
<p>This script automates what you would do by hand.
It is therefore incredibly slow, but it works and seems future proof to me.</p>
<p>You may want to adjust the respective timespan that is waited before certain operations.
This script relies on the unix command xsel.
I'm sure there are equivalent solutions on other operating systems.</p>
Running linux command inside your python script (Python)
2012-09-09T08:08:46-07:00Ahmed Kamelhttp://code.activestate.com/recipes/users/4183490/http://code.activestate.com/recipes/578254-running-linux-command-inside-your-python-script/
<p style="color: grey">
Python
recipe 578254
by <a href="/recipes/users/4183490/">Ahmed Kamel</a>
(<a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>Running any linux command line inside your python script.</p>
Simple invocation of shell commands (Python)
2011-10-21T06:44:35-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577891-simple-invocation-of-shell-commands/
<p style="color: grey">
Python
recipe 577891
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/processes/">processes</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 3.
</p>
<p>Some simple wrappers around the subprocess functions for use in system administration utilities that frequently need to interpolate trusted data into shell commands (e.g. filenames from directory listings, etc):</p>
<pre class="prettyprint"><code>import shellcmd
return_code = shellcmd.shell_call('ls -l {}', dirname)
listing = shellcmd.check_shell_output('ls -l {}', dirname)
</code></pre>
<p>Each function invokes the subprocess function of the same name with <code>shell=True</code> and the supplied command string. Any positional and keyword arguments provided to the call are interpolated into the command string with the <code>str.format</code> method.</p>
Manage environment variables on Windows (Python)
2011-03-31T18:27:57-07:00Sridhar Ratnakumarhttp://code.activestate.com/recipes/users/4169511/http://code.activestate.com/recipes/577621-manage-environment-variables-on-windows/
<p style="color: grey">
Python
recipe 577621
by <a href="/recipes/users/4169511/">Sridhar Ratnakumar</a>
(<a href="/recipes/tags/environment/">environment</a>, <a href="/recipes/tags/pywin32/">pywin32</a>, <a href="/recipes/tags/windows/">windows</a>).
Revision 7.
</p>
<p>This recipe facilitates getting and setting of both "user" and "system" environment variables on Windows. It uses PyWin32 (included in ActivePython) ... and works on both Python 2 and Python 3.</p>
Launch virtualbox with emulated USB microphones (Python)
2010-12-14T14:53:07-08:00Joseph Reaglehttp://code.activestate.com/recipes/users/4171494/http://code.activestate.com/recipes/577467-launch-virtualbox-with-emulated-usb-microphones/
<p style="color: grey">
Python
recipe 577467
by <a href="/recipes/users/4171494/">Joseph Reagle</a>
(<a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/virtualbox/">virtualbox</a>).
Revision 4.
</p>
<p>A small Python script that sets sound emulation variables for a particular USB microphone that you want to use, and sets the microphone volume level, since that is often set at zero in the Linux context.</p>
Simple way to execute multiple process in parallel (Python)
2010-08-31T00:11:00-07:00Benjamin Sergeanthttp://code.activestate.com/recipes/users/4039626/http://code.activestate.com/recipes/577376-simple-way-to-execute-multiple-process-in-parallel/
<p style="color: grey">
Python
recipe 577376
by <a href="/recipes/users/4039626/">Benjamin Sergeant</a>
(<a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/process/">process</a>).
Revision 4.
</p>
<p>Does not require multiprocessing, easy to hack, maybe not optimal but did the job for a make like tool I wrote.</p>
Simple command submitter for Win32 (Python)
2010-10-25T01:59:49-07:00Phil Risthttp://code.activestate.com/recipes/users/4171119/http://code.activestate.com/recipes/577439-simple-command-submitter-for-win32/
<p style="color: grey">
Python
recipe 577439
by <a href="/recipes/users/4171119/">Phil Rist</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/expanding/">expanding</a>, <a href="/recipes/tags/win32/">win32</a>).
</p>
<p>This program is a simple Win32 command submitter. It uses a set of macros similar
to those used by the QEditor program, strings such as '{b}'. The macros are replaced
with text from a specified file path. Options to change the working directory and
selected environment variables are provided. Two examples are provided in the code. <br />
The program does not collect output or provide input. It does one call to the expand
routine. It can not recognize parameters containing white space. Do2 which follows
does. Module is used by Do2 and ButtonBarV1Ex which follow.</p>
"tail -f" with inode monitor (Python)
2010-09-21T07:37:38-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577398-tail-f-with-inode-monitor/
<p style="color: grey">
Python
recipe 577398
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/inode/">inode</a>, <a href="/recipes/tags/monitor/">monitor</a>, <a href="/recipes/tags/tail/">tail</a>).
Revision 3.
</p>
<p>Sometimes tail -f launched for log file miss the point when program recreates log file.
Script in this recipe monitors inode changes for specified file and restarts tail if needed.</p>
Access grep from python (Python)
2010-02-24T08:30:25-08:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577069-access-grep-from-python/
<p style="color: grey">
Python
recipe 577069
by <a href="/recipes/users/4172918/">Daniel Cohn</a>
(<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/linux/">linux</a>).
</p>
<p>The recipe below passes a filename and an argument to grep, returning the stdout and stderr. Each line in the stdout will have its line number prepended.</p>
Simple Buttonbar Utility Version 1 (Python)
2010-02-09T23:02:40-08:00Phil Risthttp://code.activestate.com/recipes/users/4171119/http://code.activestate.com/recipes/577038-simple-buttonbar-utility-version-1/
<p style="color: grey">
Python
recipe 577038
by <a href="/recipes/users/4171119/">Phil Rist</a>
(<a href="/recipes/tags/command_execution/">command_execution</a>, <a href="/recipes/tags/user_interface/">user_interface</a>).
Revision 4.
</p>
<p>Display buttonbar generated from .ini style file of command templates. Commandline contains path used to generate commands. Templates can contain macros which will be replaced by parts of the file path. Template 'copy {a} {u}\{f}' could copy selected file {a} to backup directory {u} with same file name {f}. Command line specifies name of .ini file and which sections to include in buttonbar. Not all need be used.</p>
Asynchronous subprocess using asyncore (Python)
2013-01-21T19:51:00-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/576957-asynchronous-subprocess-using-asyncore/
<p style="color: grey">
Python
recipe 576957
by <a href="/recipes/users/4172294/">Glenn Eychaner</a>
(<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/asyncore/">asyncore</a>, <a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/subprocess/">subprocess</a>).
Revision 21.
</p>
<p>A coroutine-based wrapper for subprocess.Popen that uses asyncore to communicate with child processes asynchronously. This allows subprocesses to be called from within socket servers or clients without needing a complicated event loop to check both. Uses <a href="http://code.activestate.com/recipes/576965/">recipe 576965</a> to provide the asynchronous coroutine framework, <a href="http://code.activestate.com/recipes/576967/">recipe 576967</a> to provide asynchronous pipes, and <a href="http://code.activestate.com/recipes/577600/">recipe 577600</a> to provide multiple alarms.</p>
A Moderately Simple Buttonbar Program (Python)
2010-02-10T02:08:37-08:00Phil Risthttp://code.activestate.com/recipes/users/4171119/http://code.activestate.com/recipes/577039-a-moderately-simple-buttonbar-program/
<p style="color: grey">
Python
recipe 577039
by <a href="/recipes/users/4171119/">Phil Rist</a>
(<a href="/recipes/tags/command_execution/">command_execution</a>, <a href="/recipes/tags/user_interface/">user_interface</a>).
</p>
<p>This buttonbar program is similar to my ButtonBarV1 <a href="http://code.activestate.com/recipes/577038/">recipe 577038</a> except a file pattern such as c:\source\python\new*.py can be entered instead of a single file. This will allow you to scroll through a list of matching files selecting the files you want to process. You must still select the command separately for each file. Besides the file pattern or file a menu file and an optional list of section names are specified on the command line.</p>