├── .gitignore
├── LICENSE
├── README.md
├── example_package
├── .runtime
│ ├── example.py
│ ├── pythonize
│ └── wrapper.env
└── example
└── pythonize
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .working/
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
676 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # pythonize
2 | Download, install, and configure Python in one line.
3 |
4 | 
5 |
6 |
7 | ## Quickstart
8 |
9 | ```
10 | pythonize [--python-version PYTHON-VERSION] [--miniconda]
11 | [--packages PYTHON-PACKAGE [PYTHON-PACKAGE...]]
12 | [--wrapper APPLICATION-NAME]
13 | ```
14 |
15 | **Fetch and run in one line:**
16 |
17 | ```bash
18 | git clone https://github.com/princebot/pythonize.git && pythonize/pythonize
19 | ```
20 | When invoked without options, **pythonize** uses
21 | Continuum's [Anaconda](https://www.continuum.io/why-anaconda/)
22 | to install
23 | [Python 2.7](https://docs.python.org/2/whatsnew/2.7.html),
24 | [conda](http://conda.pydata.org/docs/),
25 | [pip](https://pip.readthedocs.org/en/stable), and
26 | [100+ popular Python packages]
27 | (http://docs.continuum.io/anaconda/pkg-docs):
28 |
29 | ```bash
30 | pythonize
31 | ```
32 |
33 | **Install the latest version of Python 3:**
34 |
35 | ```bash
36 | pythonize --python-version 3
37 | ```
38 |
39 | **Install a specific Python release then add
40 | [httpie](https://github.com/jkbrzt/httpie),
41 | [nose](https://nose.readthedocs.org/en/latest/), and
42 | [click](http://click.pocoo.org/5/):**
43 |
44 | ```bash
45 | pythonize --python-version 3.3 --packages httpie nose click
46 | ```
47 |
48 | ## Overview
49 | **pythonize** performs unattended download, installation, and configuration for
50 | the **[Anaconda Python distribution](https://www.continuum.io/why-anaconda/)**
51 | and its environment / package manager
52 | **[conda](http://conda.pydata.org/docs/)**: In one command line, you get a
53 | ready-to-use Python preloaded with the libraries you want.
54 |
55 | By default, **pythonize** does this:
56 |
57 | * downloads the latest Anaconda,
58 | * installs Anaconda noninteractively,
59 | * adds any additional Python packages you specify,
60 | * sets this new installation as your default Python.
61 |
62 | Anaconda Python gives you 100+ popular Python packages beyond the Python
63 | standard library — but if you want a leaner installation (10x smaller)
64 | including only Python and **conda**, you can install
65 | **[Miniconda](http://conda.pydata.org/miniconda.html)**
66 | instead by using the `--miniconda` option.
67 |
68 | More than making Python set-up ridiculously easy, **pythonize** also
69 | facilitates a novel approach to deploying Python applications on Linux and OS X
70 | systems: With the `--wrapper` option,
71 | **pythonize** *completely abstracts Python version and library dependencies
72 | from the wrapped application's users.*
73 |
74 | That means you can code for the Python environment *you* want. You don't need
75 | to grind out kludges for ancient Pythons, add code-clutter
76 | for [Python 2/3 compatability]
77 | (http://python-future.org/quickstart.html#next-steps),
78 | design complex
79 | [egg](http://peak.telecommunity.com/DevCenter/PythonEggs) /
80 | [wheel](https://pypi.python.org/pypi/wheel)
81 | spec files, or leap down the bottomless rabbit hole of
82 | [freezing](http://docs.python-guide.org/en/latest/shipping/freezing/) or
83 | [cross-compiling](http://www.pyinstaller.org/)
84 | your Python code: **Your application just works** — even if your users don't
85 | have Python installed at all.
86 |
87 |
88 | ## Using Wrapper Mode
89 |
90 | To see a Python application wrapped with **pythonize** in action, use this
91 | pseudo–one-liner to fetch and run the demo:
92 |
93 | ```bash
94 | git clone https://github.com/princebot/pythonize.git \
95 | && pythonize/example_package/example
96 | ```
97 |
98 | ### Invocation
99 |
100 | When invoked with the `--wrapper` option, **pythonize** alters its behavior
101 | as follows:
102 | * It skips switching the user's default Python to Anaconda.
103 | * It uses **conda** to satisfy Python version/library dependencies if **conda**
104 | is already installed rather then creating a fresh installation.
105 | * It always use Miniconda instead of Anaconda.
106 | * It fails rather than merely warns if any items listed with the `--packages`
107 | option can't be installed.
108 |
109 | ### Wrapping a Python Application
110 |
111 | To make wrapping arbitrary Python programs with **pythonize** as easy as
112 | possible, this repo contains an `example_package` directory with the files
113 | you'll need, already in the recommended layout.
114 |
115 | Just follow these steps to get going:
116 |
117 | 1. Clone this repo: `git clone https://github.com/princebot/pythonize.git`
118 |
119 | 2. Copy the `example_package` directory and rename it for your application.
120 | *(Note: For the rest of this section, all pathnames will be relative to this directory.)*
121 |
122 | 3. Rename the shell wrapper `example` as your main executable (for example,
123 | `nmapcli`).
124 |
125 | 4. Replace `.runtime/example.py` with your Python program.
126 |
127 | 5. Set your Python program's filename to match your main executable, but add a
128 | `.py` extension (for example, `nmapcli.py`).
129 |
130 | 6. Edit a couple of variables in `.runtime/wrapper.env` to set your Python
131 | dependencies:
132 |
133 | * `PY_VERSION=`
134 |
135 | * `PY_PACKAGES=()`
136 |
137 | That's it — you're done. Your Python app is now **pythonized**.
138 |
139 | Users will run your application the same way they did before, but as an
140 | implementation detail, the main executable is now a shell wrapper around your
141 | Python program.
142 |
143 | The shell wrapper checks that the current execution environment satisfies your
144 | expressed dependencies and, if it doesn't, invokes **pythonize** to fetch the
145 | required Python version and/or libraries before running your Python
146 | app — and the end user doesn't have to do a damned thing.
147 |
148 | ## More
149 |
150 | To read the full user documentation for **pythonize**, consult its manual page:
151 |
152 | ```bash
153 | pythonize help
154 | ```
155 |
156 | To discover implementation details (including how this can be
157 | imported as a shell library and used by other utilities in an
158 | [OOP](https://en.wikipedia.org/wiki/Object-oriented_programming)-like style),
159 | browse the source: it's extensively commented for that purpose.
160 |
161 | **pythonize** has been tremendously useful to me. I've used it as part of
162 | bootstrapping cloud and local boxen when golden images were unavailable or
163 | nonexistent. I've employed it to package Python apps for sharing
164 | with other engineers at work and to run Python admin scripts in wild-west
165 | server environments where sane configuration management remains a pipe
166 | dream.
167 |
168 | I made **pythonize** mainly to scratch my own itch — and now I'm
169 | sharing it, on the off chance some other people may be itchy, too.
170 | 
171 |
172 | ## Future Development
173 |
174 | There's been more interest in this project than I expected (I wasn't really expecting any, so I was extremely pleasantly surprised to see **pythonize** listed in the [Python Weekly Newsletter](http://www.pythonweekly.com/)).
175 |
176 | Because of that, I'm working on a 1.0 release (I consider the current version a beta).
177 |
178 | The standout feature of the 1.0 release will be **Windows support.**
179 |
180 | Currently, I plan to implement Windows support via a Windows PowerShell program **pythonize.ps1** that I'll add to this repo. I have a strong \*nix bias but no Windows antipathy: I just genuinely prefer Linux. However, now that I've been studying the PowerShell documentation in depth, I find it to be a surprisingly *amazing* control language, and I'm excited about porting the Bash version to it.
181 |
182 | When complete, Windows users will be able to use a command similar to this as a quickstart from a PowerShell console (provided they already have Git for Windows):
183 |
184 | ```powershell
185 | git clone -q https://github.com/princebot/pythonize; `
186 | if ($?) {.\pythonize\pythonize.ps1}
187 | ```
188 |
189 | Depending on other time commitments, I may have this built within a couple of weeks.
190 |
--------------------------------------------------------------------------------
/example_package/.runtime/example.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # -*- coding: utf-8 -*-
3 | """example.py - example Python script wrapped by ../example via pythonize."""
4 |
5 | import os
6 | import shutil
7 | import subprocess
8 | import sys
9 | from zipfile import ZipFile
10 |
11 | from colorama import init, Fore, Style
12 | import requests
13 |
14 |
15 | def main():
16 | """Prints info on current Python environment to verify script worked."""
17 |
18 | print('\n{}example.py -> running{}'
19 | .format(Fore.CYAN + Style.BRIGHT, Style.RESET_ALL))
20 | print('\nFile path:\n{}{}{}'.format(Style.BRIGHT,
21 | os.path.realpath(__file__),
22 | Style.NORMAL))
23 | print('\nPython version:\n{}{}{}'.format(Style.BRIGHT, sys.version,
24 | Style.NORMAL))
25 | print('\nCurrent value of PATH environment variable:')
26 | for i in os.environ['PATH'].split(':'):
27 | print(' {}{}{}'.format(Style.BRIGHT, i, Style.NORMAL))
28 | print('\n')
29 | print("{}Downloading Phillip John's text adventure from github\n{}⟨⟨"
30 | "https://github.com/phillipjohnson/text-adventure-tut⟩⟩"
31 | .format(Style.BRIGHT, Style.NORMAL))
32 | print()
33 | # Just a trivial example program, so no attempts at error-detection.
34 | # Basically, we're freeballing. ^.^
35 | try:
36 | zipped = 'text-adventure-tut.zip'
37 | extract_dir = zipped.rstrip('.zip') + '-master'
38 | with open(zipped, 'wb') as f:
39 | r = requests.get('https://github.com/phillipjohnson/text-adventure'
40 | '-tut/archive/master.zip')
41 | for b in r.iter_content(1024):
42 | f.write(b)
43 | with ZipFile(zipped, 'r') as z:
44 | z.extractall()
45 | os.chdir(extract_dir)
46 | with open('.example.py.stderr', 'wb') as stderr:
47 | print('\n{1}{0}\nBEGIN TEXT ADVENTURE\n{0}{2}{3}'
48 | .format('=' * 79, Style.BRIGHT, Style.NORMAL, Fore.CYAN))
49 | p = subprocess.Popen(['python', 'adventuretutorial/game.py'],
50 | stderr=stderr)
51 | p.communicate()
52 | except KeyboardInterrupt:
53 | print('\n\n{}Quitting at user request'.format(Fore.RESET))
54 | sys.exit()
55 | finally:
56 | if not os.path.exists(zipped):
57 | os.chdir('..')
58 | if os.path.exists(zipped):
59 | os.remove(zipped)
60 | if os.path.exists('.example.py.stderr'):
61 | os.remove('.example.py.stderr')
62 | if os.path.exists(extract_dir):
63 | shutil.rmtree(extract_dir)
64 |
65 |
66 | if __name__ == '__main__':
67 | init()
68 | main()
69 |
--------------------------------------------------------------------------------
/example_package/.runtime/pythonize:
--------------------------------------------------------------------------------
1 | ../../pythonize
--------------------------------------------------------------------------------
/example_package/.runtime/wrapper.env:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 | # Name: wrapper.env
4 | # Source: https://github.com/princebot/pythonize
5 | # Author: prince@princebot.com
6 | # Usage: source wrapper.env
7 | # Synopsis: Configuration for Python programs wrapped with pythonize --wrapper
8 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 | # Details: To separate concerns, it's recommended that you specify Python
10 | # dependencies for pythonize-wrapped programs (or set any custom
11 | # global variables) in this file rather than in the shell wrapper.
12 | # This also reduces the likelihood of inadvertently introducing bugs
13 | # in the shell wrapper source --- leave that to me. ^.^
14 | #
15 | # For more information about pythonize and making Python wrappers,
16 | # run pythonize --help to view its man page. You can also visit the
17 | # project page at https://github.com/princebot/pythonize.
18 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
19 |
20 |
21 | # Customize the global variables below for your pythonize-wrapped program:
22 | #
23 | # PROG: Set by default to use the shell wrapper's filename (which
24 | # from your users' perspective will be the main executable)
25 | # in console messages.
26 | # WRAPPED_PROG: Filepath of the wrapped Python program (by default, we
27 | # expect .runtime/.py)
28 | # PY_VERSION: Python version required for the wrapped Python program,
29 | # e.g., 2.7 (using just "2" or "3" without specifying a
30 | # minor version number will match any Python 2.x or 3.x
31 | # respectively). Python 3 is the default.
32 | # PY_PACKAGES: Bash array of any Python packages outside the standard
33 | # library that the wrapped Python script requires, e.g.,
34 | # PY_PACKAGES=(requests boto3 flask) --- or PY_PACKAGES=()
35 | # if you're only using Python's standard library.
36 |
37 | PROG=$(basename "${BASH_SOURCE[1]}")
38 | WRAPPED_PROG=$(dirname "${BASH_SOURCE[1]}")/.runtime/${PROG}.py
39 | PY_VERSION=3
40 | PY_PACKAGES=(colorama requests)
41 |
42 | # If we're using the default settings, ${PROG}.py will be the filename of our
43 | # wrapped Python program; otherwise, ${WRAPPED_PROG} should be.
44 | if ! [[ -f ${PROG}.py || -f ${WRAPPED_PROG} ]]; then
45 | pythonize::die "Internal Error: The Python application this shell" \
46 | "utility wraps could not be found."
47 | fi
48 |
49 | # Place any global variables you would like to have available in your Python
50 | # program here, e.g., export PYTHONPATH=/path/to/custom/modules
51 |
--------------------------------------------------------------------------------
/example_package/example:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 | # Name: example
4 | # Source: github.com/princebot/pythonize
5 | # Author: prince@princebot.com
6 | # Synopsis: Shell wrapper around target Python program to guarantee Python
7 | # version/library dependencies are met.
8 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 | # Quickstart: - Change name of example_package to your package.
10 | #
11 | # - Change name of this file to whatever filename you want as
12 | # your main executable (for example, "do_awesome_stuff").
13 | #
14 | # - Replace .runtime/example.py with your Python program and set
15 | # its name to match the main executable, adding .py (for
16 | # example, "do_awesome_stuff.py").
17 | #
18 | # - Edit .runtime/wrapper.env to specify the Python version
19 | # and Python libraries you require at runtime.
20 | #
21 | # That's it --- you're done: Running this wrapper script will now
22 | # resolve any specified Python version/library dependencies as
23 | # necessary before launching your Python program.
24 | #
25 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
26 | # Details: Use this shell-wrapper template to completely abstract Python
27 | # version and library dependencies from the end user, eliminate the
28 | # need to write egg/wheel specifications for pip installs, and free
29 | # yourself from the time-consuming (and often frustrating) task of
30 | # demodernizing and otherwise hobbling your code to force compati-
31 | # bility with all Pythons 2.6 - 3.x.
32 | #
33 | # This is a shell script that leverages pythonize -- and through it
34 | # the Anaconda Python distribution --- to guarantee that your exact
35 | # desired Python version and Python packages exist in the current
36 | # running shell. More than ensuring your program has what it needs
37 | # at runtime, it also allows you to write your code using modern
38 | # Python without concerning yourself with what -- if any -- Python
39 | # your target environment has installed.
40 | #
41 | # To use this:
42 | #
43 | # -> Change name of example_package to your package.
44 | #
45 | # -> Change name of this file to whatever filename you want as
46 | # your main executable (for example, "do_awesome_stuff").
47 | #
48 | # -> Replace .runtime/example.py with your Python program and
49 | # set its filename to match your main executable, adding .py
50 | # (for example, "do_awesome_stuff.py").
51 | #
52 | # -> Edit .runtime/wrapper.env to specify the Python version
53 | # and Python libraries you require at runtime.
54 | #
55 | # That's it. Subsequently, the user can call this wrapper script
56 | # (which from his perspective is the main program), and everything
57 | # just works. The implementation, however, actually does this:
58 | #
59 | # -> Try to resolve listed dependencies using system Python
60 | # and, if system Python is suitable, exec the Python
61 | # program --- and we're done.
62 | #
63 | # -> If system Python won't do, try resolving dependencies
64 | # using each installed Anaconda Python environment.
65 | #
66 | # -> If that doesn't work, run pythonize, which will resolve
67 | # dependencies by running conda to create a new Python
68 | # environment containing what we need, either using an
69 | # existing conda or fetching+installing a fresh Miniconda.
70 | #
71 | # -> Now we're set: exec the Python program.
72 | #
73 | # For more information about pythonize and making Python wrappers,
74 | # run pythonize --help to view its man page. You can also visit the
75 | # project page at https://github.com/princebot/pythonize.
76 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 |
78 |
79 | # To separate concerns, it's recommended that you specify Python dependencies
80 | # for pythonize-wrapped programs (or set any custom global variables) in
81 | # .runtime/wrapper.env rather than here. This also reduces the likelihood of
82 | # inadvertently introducing bugs in the source --- leave that to me. ^.^
83 | #
84 | # If your program package is structured differently and/or does not include
85 | # .runtime/wrapper.env, you can edit the global variables here instead:
86 | #
87 | # PROG: Set by default to use this shell wrapper's filename in
88 | # console messages.
89 | # WRAPPED_PROG: Filepath of the wrapped Python program (by default, we
90 | # expect .runtime/.py)
91 | # PY_VERSION: Python version required for the wrapped Python program,
92 | # e.g., 2.7 (using just "2" or "3" without specifying a
93 | # minor version number will match any Python 2.x or 3.x
94 | # respectively). Python 3 is the default.
95 | # PY_PACKAGES: Bash array of any Python packages outside the standard
96 | # library that the wrapped Python script requires, e.g.,
97 | # PY_PACKAGES=(requests boto3 flask) --- or PY_PACKAGES=()
98 | # if you're only using Python's standard library.
99 | #
100 | # Set the next four variables here only if .runtime/wrapper.env doesn't exist.
101 |
102 | if [[ -f $(dirname "${BASH_SOURCE}")/.runtime/wrapper.env ]]; then
103 | source "$(dirname "${BASH_SOURCE}")/.runtime/wrapper.env"
104 | else
105 | PROG=$(basename "${BASH_SOURCE}")
106 | WRAPPED_PROG=$(dirname "${BASH_SOURCE}")/.runtime/${PROG}.py
107 | PY_VERSION=3
108 | PY_PACKAGES=(colorama requests)
109 | fi
110 |
111 |
112 |
113 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114 | # All lines below are implementation details and should not require editing.
115 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116 |
117 | # Set shell options.
118 | set -o pipefail
119 | shopt -s extglob
120 |
121 | # If the terminal believes it supports colors, use them.
122 | COLORS_SUPPORTED=
123 | if which tput >/dev/null; then
124 | if [[ -n $(tput colors) ]] && (($(tput colors) >= 8)); then
125 | declare -r COLORS_SUPPORTED=true
126 | fi
127 | fi 2>/dev/null
128 | if [[ -n ${COLORS_SUPPORTED} ]]; then
129 | # Colors
130 | declare -r BLACK=$(tput setaf 0)
131 | declare -r RED=$(tput setaf 1)
132 | declare -r GREEN=$(tput setaf 2)
133 | declare -r YELLOW=$(tput setaf 3)
134 | declare -r BLUE=$(tput setaf 4)
135 | declare -r MAGENTA=$(tput setaf 5)
136 | declare -r CYAN=$(tput setaf 6)
137 | declare -r WHITE=$(tput setaf 7)
138 | # Styles
139 | declare -r BOLD=$(tput bold)
140 | declare -r UL=$(tput smul)
141 | declare -r STOP_UL=$(tput rmul)
142 | declare -r CMD=$(tput setaf 7)$(tput smul)
143 | declare -r RESET=$(tput sgr0)
144 | # Cursor movement
145 | tput cuu1 &>/dev/null && declare -r UP_1_ROW=$(tput cuu1)
146 | tput cr &>/dev/null && declare -r TO_COL_0=$(tput cr)
147 | tput el &>/dev/null && declare -r CLR_TO_END=$(tput el)
148 | else
149 | # Defensively clear these variables if colors are not supported, on the off
150 | # chance these have exported values.
151 | BLACK=
152 | RED=
153 | GREEN=
154 | YELLOW=
155 | BLUE=
156 | MAGENTA=
157 | CYAN=
158 | WHITE=
159 | BOLD=
160 | UL=
161 | STOP_UL=
162 | CMD=
163 | RESET=
164 | UP_1_ROW=
165 | TO_COL_0=
166 | CLR_TO_END=
167 | fi
168 |
169 |
170 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171 | # Private functions:
172 | # 1. __main
173 | # 2. __die
174 | # 3. __format_python_deps
175 | # 4. __run_wrapped_prog
176 | # 5. __say
177 | # 6. __verify_python_deps
178 | # 7. __watch_pot
179 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180 |
181 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
182 | # Actions: Execute wrapper script functions and pass command-line arguments
183 | # through to target Python program.
184 | # Globals: Color-escape constants
185 | # PROG
186 | # WRAPPED_PROG
187 | # PY_VERSION
188 | # PY_PACKAGES
189 | # Arguments: Command-line args
190 | # Functions: __die
191 | # __format_python_deps
192 | # __run_wrapped_prog
193 | # __say
194 | # __verify_python_deps
195 | # Returns: None
196 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
197 | __main() {
198 | # If we have the right Python version/libraries in the current Python or in
199 | # any installed Anaconda Python environments, run our Python program.
200 | local tmp=$(
201 | mktemp "${PROG}.tmp.XXXXXXXX" || __die "Unable to create temp file")
202 | trap "rm -f -- ${tmp}" EXIT
203 | __verify_python_deps &
204 | __watch_pot
205 | local load_python=$(< "${tmp}")
206 | if [[ ${load_python} ]]; then
207 | __run_wrapped_prog "$@"
208 | fi
209 | # If we can't resolve dependencies using existing Python environments, try
210 | # running pythonize to get what we need.
211 | local __pythonize
212 | # If this software package's .runtime/ directory has a pythonize program,
213 | # use it.
214 | if [[ -f ${WRAPPED_PROG%/*}/pythonize ]]; then
215 | __pythonize=${WRAPPED_PROG%/*}/pythonize
216 | # If there's a pythonize available from PATH, use it.
217 | elif which pythonize &>/dev/null; then
218 | __pythonize=$(which pythonize)
219 | # If there's a pythonize in the current directory, use it.
220 | elif [[ -f pythonize ]]; then
221 | __pythonize="./pythonize"
222 | fi
223 | local deps=$(__format_python_deps)
224 | # If we couldn't find pythonize, exit with an error.
225 | if [[ -z ${__pythonize} ]]; then
226 | __die "Could find neither ${deps} nor a pythonize script to" \
227 | "automatically install them. Rerun ${PROG} after installing" \
228 | "the required Python version and libraries."
229 | fi
230 | # Ask user for permission to install Python dependencies with pythonize.
231 | __say -n -b 1 "${PROG} requires ${deps}. \n\n${PROG} can attempt to" \
232 | "install the dependencies into a local virtual Python" \
233 | "environment using ${BOLD}pythonize${RESET}${GREEN}." \
234 | "\n\npythonize will affect only the current shell; no" \
235 | "system or user settings will be changed." \
236 | "\n\nUse pythonize to satisfy dependencies? (y/n) "
237 | local resp
238 | while read -r resp; do
239 | [[ ${resp} =~ ^(y|yes|n|no)$ ]] && break
240 | __say -b 1 "Please enter 'y' or 'n'"
241 | done
242 | if [[ ${resp} =~ ^n ]]; then
243 | __say -a 1 -b 1 "Quitting at user request. Rerun ${PROG} after" \
244 | "installing the required Python version and" \
245 | "libraries."
246 | exit
247 | fi
248 | if ! "${__pythonize}" --miniconda --python-version "${PY_VERSION}" \
249 | --packages "${PY_PACKAGES[@]}" --wrapper "${PROG}"
250 | then
251 | __die "Failed to automatically install ${deps} using pythonize." \
252 | "Rerun ${PROG} after installing the required Python version" \
253 | "and libraries."
254 | fi
255 | # Check if we now have an Anaconda Python environment that satisfies our
256 | # Python dependencies. If we do, run the wrapped Python program.
257 | __verify_python_deps &
258 | __watch_pot
259 | load_python=$(< "${tmp}")
260 | if [[ ${load_python} ]]; then
261 | __say -a 1 -b 1 \
262 | "\nSuccessfully installed the required Python dependencies with" \
263 | "pythonize.\nLaunching main program ...\n "
264 | __run_wrapped_prog "$@"
265 | fi
266 | __die "Could not find a Python environment with the required" \
267 | "dependencies after running pythonize. Rerun ${PROG} after" \
268 | "installing the required Python version and libraries: ${deps}"
269 | }
270 |
271 |
272 |
273 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
274 | # Actions: Print formatted error messages and quit with exit code 1.
275 | # Globals: Color-escape constants.
276 | # Arguments: Message string.
277 | # Returns: 1
278 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
279 | __die() {
280 | # Rather than add external dependencies, we try to prettyprint messages
281 | # using shell utilties common to both BSD- and GNU-flavored *nix. We first
282 | # calculate the size of the columns we need; then, we shove everything
283 | # through echo+fold+paste to align it all while keeping the lines lengths
284 | # to under 79 characters.
285 | local header=" > ${BOLD}${PROG}: Error:${RESET}${RED}"
286 | local char_ct=$(wc -m <(echo -n "${header}") | awk '{printf $1}')
287 | local inv_char_ct=$(wc -m <(echo -n "${BOLD}${RESET}${RED}") \
288 | | awk '{printf $1}')
289 | char_ct=$((char_ct - inv_char_ct))
290 | local tab_ct
291 | if (( (char_ct % 7) == 0 || char_ct <= 15 )); then
292 | tab_ct=$(( (char_ct / 7) - 1 ))
293 | else
294 | tab_ct=$((char_ct / 7))
295 | fi
296 | local filler="${RESET}${RED} >"
297 | local i
298 | filler+=$'\t'
299 | for ((i=1; i < tab_ct && tab_ct > 0; i++)); do
300 | filler+=$'\t'
301 | done
302 | local line_ct
303 | line_ct=$(wc -l <(fold -s -w 48 <(echo -en "$@")) | awk '{printf $1}')
304 | header+=$'\n'
305 | for ((i=0; i < line_ct && line_ct >= 0; i++)); do
306 | header+=${filler}$'\n'
307 | done
308 | header=${header%$'\n'}
309 | local msg=$(paste <(echo -n "${header}") \
310 | <(fold -s -w 48 <(echo -en "$@")))
311 | echo -e "\n${RED}${msg}${RESET}\n " >&2
312 | exit 1
313 | }
314 |
315 |
316 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
317 | # Actions: Return grammatical version of Python dependencies list.
318 | # Globals: Color-escape constants.
319 | # PY_VERSION
320 | # PY_PACKAGES
321 | # Arguments: None
322 | # Returns: Formatted message string.
323 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
324 | __format_python_deps() {
325 | # I can't justify this function's existence with a straight face. <.<
326 | local msg="Python ${PY_VERSION}"
327 | if ((${#PY_PACKAGES[@]} > 0)); then
328 | msg+=" "
329 | case ${#PY_PACKAGES[@]} in
330 | 1)
331 | msg+="with ${PY_PACKAGES}"
332 | ;;
333 | 2)
334 | msg+="with ${PY_PACKAGES} and ${PY_PACKAGES[1]}"
335 | ;;
336 | *)
337 | msg+="with "
338 | local i
339 | for ((i=0; i < ${#PY_PACKAGES[@]}; i++)); do
340 | if ((i == ${#PY_PACKAGES[@]} - 1)); then
341 | msg+="and ${PY_PACKAGES[i]}"
342 | else
343 | msg+="${PY_PACKAGES[i]}, "
344 | fi
345 | done
346 | ;;
347 | esac
348 | fi
349 | echo "${msg}"
350 | }
351 |
352 |
353 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
354 | # Actions: Execute wrapped Python program.
355 | # Globals: WRAPPED_PROG
356 | # Arguments: Command-line args
357 | # Returns: None.
358 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
359 | __run_wrapped_prog() {
360 | # Clear the shell options we set in this wrapper.
361 | rm -f -- "${tmp}"
362 | trap - EXIT
363 | set +o pipefail
364 | shopt -u extglob
365 | if [[ ${load_python} != "use current python environment" ]]; then
366 | eval "${load_python} 2>/dev/null"
367 | fi
368 | exec python "${WRAPPED_PROG}" "$@"
369 | # If exec finds the Python program, this shell process is replaced with
370 | # a new Python process. If for any reason exec itself fails, however, we'll
371 | # still be in this shell script --- so let's make sure we explicitly exit
372 | # in that case rather than returning to __main().
373 | exit
374 | }
375 |
376 |
377 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
378 | # Actions: Print formatted general messages.
379 | # Globals: Color-escape constants.
380 | # Arguments: Message string.
381 | # Options: -a N Print N trailing newlines.
382 | # -b N Print N leading newlines.
383 | # -n End without a newline --- next text will be on same line.
384 | # Returns: None.
385 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
386 | __say() {
387 | local i
388 | local nl_after
389 | local nl_before
390 | local no_final_nl
391 | while (($# > 0)); do
392 | case $1 in
393 | -n)
394 | no_final_nl=true
395 | shift
396 | ;;
397 | -@(a|b))
398 | [[ $2 =~ ^[0-9]+$ ]] || continue
399 | for ((i=0; i < $2 && $2 >= 0; i++)); do
400 | case $1 in
401 | -a) nl_after+=$'\n' ;;
402 | -b) nl_before+=$'\n' ;;
403 | esac
404 | done
405 | shift 2
406 | ;;
407 | *) break ;;
408 | esac
409 | done
410 | # The comments at the top of __die() apply here as well.
411 | local msg=$(fold -s -w 72 <(echo -e "$@"))
412 | local line_ct=$(wc -l <(echo "${msg}") | awk '{print $1}')
413 | local filler
414 | for ((i=0; i < line_ct && line_ct >= 0; i++)); do
415 | filler+="${RESET}${WHITE} > ${GREEN}\n"
416 | done
417 | filler=${filler%\\n}
418 | msg=$(paste -d ' ' <(echo -e "${filler}") <(echo -e "${msg}"))
419 | if [[ ${no_final_nl} ]]; then
420 | echo -n "${nl_before}${msg}${RESET}"
421 | else
422 | echo "${nl_before}${msg}${nl_after}${RESET}"
423 | fi
424 | }
425 |
426 |
427 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
428 | # Actions: Check current environment for required Python version/libraries.
429 | # Globals: PY_VERSION
430 | # PY_PACKAGES
431 | # Arguments: None
432 | # Returns: 0 (Python dependencies met) or 1 (Python dependencies not met)
433 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
434 | __verify_current_python_deps() {
435 | local retval=1
436 | which python &>/dev/null || return ${retval}
437 | local curr_py_vers=$(
438 | python -V \
439 | |& grep --color=never -Eio 'Python [0-9]+(\.[0-9]+(\.[0-9]+)?)?' \
440 | | awk '{print $2}')
441 | if [[ -n ${curr_py_vers} \
442 | && ${curr_py_vers} =~ ^${PY_VERSION} ]]
443 | then
444 | ((${#PY_PACKAGES[@]} < 1)) && retval=0
445 | while ((${#PY_PACKAGES[@]} > 0)); do
446 | local module
447 | for module in "${PY_PACKAGES[@]}"; do
448 | python -c "import ${module}" &>/dev/null || break 2
449 | done
450 | retval=0
451 | break
452 | done
453 | fi
454 | return ${retval}
455 | }
456 |
457 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
458 | # Actions: Check current Python and any installed Anaconda Pythons for
459 | # required Python version/libraries.
460 | # Globals: None
461 | # Arguments: None
462 | # Functions: __verify_current_python_deps
463 | # Returns: 0 (Python dependencies met) or 1 (Python dependencies not met)
464 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
465 | __verify_python_deps() {
466 | # First check the current Python. If it doesn't meet our needs, check any
467 | # existing conda environments.
468 | local retval=1
469 | if __verify_current_python_deps; then
470 | echo -n "use current python environment" > "${tmp}"
471 | retval=0
472 | fi
473 | local d
474 | local e
475 | local -a envs
476 | if ((retval)); then
477 | for d in ~/{ana,mini}conda{,3}/bin; do
478 | [[ -d ${d} ]] || continue
479 | e=root
480 | source "${d}/activate" root 2>/dev/null || continue
481 | if __verify_current_python_deps; then
482 | retval=0
483 | break
484 | fi
485 | envs=($(conda env list \
486 | | awk '$0 && !/^#/ && $2 !~ /\*/ {print $1}'))
487 | for e in "${envs[@]}"; do
488 | source activate "$e" 2>/dev/null || continue
489 | if __verify_current_python_deps; then
490 | retval=0
491 | break 2
492 | fi
493 | done
494 | done
495 | fi
496 | if ((retval)); then
497 | return ${retval}
498 | else
499 | echo -n "source \"${d}/activate\" \"$e\"" > "${tmp}"
500 | return ${retval}
501 | fi
502 | }
503 |
504 |
505 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
506 | # Actions: Draws a spinner animation.
507 | # Globals: Color-escape constants
508 | # Arguments: None.
509 | # Returns: None.
510 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
511 | __watch_pot() {
512 | # This is just a fun little function to draw a spinner while waiting on
513 | # backgrounded processes. Called immediately after backgrounding a process,
514 | # it grabs the PID and continuously redraws the spinner until that process
515 | # terminates.
516 | local pid=$!
517 | local spinner=('-' '\' '|' '/')
518 | tput civis
519 | # The above command disappears the cursor, so set a trap to get it back
520 | # juuust in case the script unexpectedly quits or gets interrupted.
521 | # (Otherwise, we'll confuse the hell out of the user.)
522 | trap "tput cvvis" RETURN EXIT
523 | __say -b 1 -n \
524 | "[${1:-checking Python dependencies}]${RESET}${WHITE} ${spinner}"
525 | local char
526 | while kill -0 ${pid}; do
527 | for char in "${spinner[@]}"; do
528 | printf "\b${char}"
529 | sleep .1
530 | done
531 | done
532 | echo -n "${TO_COL_0}"
533 | __say "[${2:-dependency check complete}]${CLR_TO_END}"
534 | tput cvvis
535 | } 2>/dev/null
536 |
537 |
538 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
539 | # Begin execution.
540 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
541 |
542 | __main "$@"
543 |
--------------------------------------------------------------------------------
/pythonize:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 | # Name: pythonize
4 | # Source: https://github.com/princebot/pythonize
5 | # Author: prince@princebot.com
6 | # Usage: pythonize [--python-version PYTHON_VERSION] [--miniconda]
7 | # [--packages PYTHON_PACKAGE [PYTHON_PACKAGE...]]
8 | # [--wrapper APPLICATION-NAME]
9 | # Synopsis: Download, install, and configure Python in one line.
10 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11 | # Details: Use pythonize to install the Anaconda Python distribution, an
12 | # an awesome software suite from Continuum that includes Python,
13 | # the conda package- and virtual-Python-environment manager, and 100+
14 | # useful Python packages beyond the standard library. (For a
15 | # lighter-weight installation, use the --miniconda option to install
16 | # Miniconda, which includes only Python and conda)
17 | #
18 | # As an extension of its core functionality, pythonize also supports
19 | # easy use as a wrapper around Python programs. Invoked in wrapper
20 | # mode, pythonize completely abstracts Python version and library
21 | # dependencies from the end user of software packages: It ensures
22 | # your execution environment contains exactly the Python version and
23 | # libraries you specifiy, with no end-user expertise or end-user
24 | # action required.
25 | #
26 | # For more information about pythonize usage, including packaging
27 | # your to use pythonize as a wrapper, run pythonize --help to view
28 | # the man page or visit https://github.com/princebot/pythonize. The
29 | # code is extensively commented, so you can also consult the source
30 | # directly to learn the implementation details.
31 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32 |
33 | # Set program name.
34 | declare -r PROG="pythonize"
35 |
36 | # Set shell options
37 | set -o pipefail
38 | shopt -s extglob
39 |
40 | # If the terminal believes it supports colors, use them.
41 | COLORS_SUPPORTED=
42 | if which tput >/dev/null; then
43 | if [[ -n $(tput colors) ]] && (($(tput colors) >= 8)); then
44 | declare -r COLORS_SUPPORTED=true
45 | fi
46 | fi 2>/dev/null
47 | if [[ -n ${COLORS_SUPPORTED} ]]; then
48 | # Colors
49 | declare -r BLACK=$(tput setaf 0)
50 | declare -r RED=$(tput setaf 1)
51 | declare -r GREEN=$(tput setaf 2)
52 | declare -r YELLOW=$(tput setaf 3)
53 | declare -r BLUE=$(tput setaf 4)
54 | declare -r MAGENTA=$(tput setaf 5)
55 | declare -r CYAN=$(tput setaf 6)
56 | declare -r WHITE=$(tput setaf 7)
57 | # Styles
58 | declare -r BOLD=$(tput bold)
59 | declare -r UL=$(tput smul)
60 | declare -r STOP_UL=$(tput rmul)
61 | declare -r CMD=$(tput setaf 7)$(tput smul)
62 | declare -r RESET=$(tput sgr0)
63 | # Cursor movement
64 | tput cuu1 &>/dev/null && declare -r UP_1_ROW=$(tput cuu1)
65 | tput cr &>/dev/null && declare -r TO_COL_0=$(tput cr)
66 | tput el &>/dev/null && declare -r CLR_TO_END=$(tput el)
67 | else
68 | # Defensively clear these variables if colors are not supported, on the off
69 | # chance these have exported values.
70 | BLACK=
71 | RED=
72 | GREEN=
73 | YELLOW=
74 | BLUE=
75 | MAGENTA=
76 | CYAN=
77 | WHITE=
78 | BOLD=
79 | UL=
80 | STOP_UL=
81 | CMD=
82 | RESET=
83 | UP_1_ROW=
84 | TO_COL_0=
85 | CLR_TO_END=
86 | fi
87 |
88 |
89 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90 | # FUNCTIONS:
91 | # 1. pythonize::main
92 | # 2. pythonize::confirm
93 | # 3. pythonize::create_conda_environment
94 | # 4. pythonize::die
95 | # 5. pythonize::install_anaconda
96 | # 6. pythonize::install_python_packages
97 | # 7. pythonize::run_normal_mode
98 | # 8. pythonize::run_wrapper_mode
99 | # 9. pythonize::say
100 | # 10. pythonize::show_help
101 | # 11. pythonize::write_shell_startup
102 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103 |
104 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105 | # Actions: Parse command-line options and route to other functions.
106 | # Globals: Terminal-movement/terminal-color constants.
107 | # Arguments: Command-line options.
108 | # Functions: pythonize::die
109 | # pythonize::say
110 | # pythonize::show_help
111 | # pythonize::run_normal_mode
112 | # pythonize::run_wrapper_mode
113 | # Returns: None.
114 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115 | pythonize::main() {
116 | local downgrade
117 | local opt_mini
118 | local opt_wrap
119 | local py_vers
120 | local wrapper
121 | local -a packages
122 | # Parse command-line options.
123 | while (($# > 0)); do
124 | case $1 in
125 | help|-h|-help|--help)
126 | pythonize::show_help
127 | ;;
128 | -m|--miniconda)
129 | opt_mini=true
130 | shift
131 | ;;
132 | -v|--python-version)
133 | # Check if specified Python version is 2.6 - 2.7 or 3.3 - 3.5
134 | # This will need to be changed when Anaconda increments its
135 | # supported versions.
136 | if [[ $2 =~ ^(2(\.[6-7])?|3(\.[3-5])?)$ ]]; then
137 | py_vers=$2
138 | if [[ ! ${py_vers} =~ ^(2(\.7)?|3(\.5)?)$ ]]; then
139 | # If we're not using the latest minor version of Python
140 | # 2.x or 3.x, note that (see following comments in this
141 | # function)
142 | downgrade=true
143 | fi
144 | shift 2
145 | elif [[ $2 =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
146 | pythonize::die "Unsupported Python version \"$2\""
147 | else
148 | pythonize::say "No argument for $1 option, skipping"
149 | shift
150 | fi
151 | ;;
152 | -p|--package?(s))
153 | # If we get --package instead of --packages, match anyway.
154 | shift
155 | while (($# > 0)) && [[ -n $1 && ! $1 =~ ^-.+ ]]; do
156 | packages+=($1)
157 | shift
158 | done
159 | ;;
160 | --no-op)
161 | # Used for testing.
162 | pythonize::say -a 1 -b 1 "\nNo-op mode.\n "
163 | exit
164 | ;;
165 | -w|--wrapper)
166 | if [[ -z $2 || $2 =~ ^--.+ ]]; then
167 | pythonize::die "Missing argument for --wrapper option."
168 | fi
169 | opt_wrap=true
170 | # In wrapper mode, always install Miniconda rather than
171 | # Anaconda regardless of contrary command-line options.
172 | opt_mini=true
173 | wrapper=$2
174 | shift 2
175 | ;;
176 | *) pythonize::die "Unparsable option \"$1\"" ;;
177 | esac
178 | done
179 | pythonize::say -a 1 -b 1 \
180 | "\nStarting ${BOLD}pythonize${RESET}${GREEN} ...\n "
181 | local arch
182 | [[ $(uname -m) =~ 32 ]] && arch="x86" || arch="x86_64"
183 | # If we need to download Anaconda later in this script, set the type of
184 | # installer we'll need here: The filename we need to fetch depends on
185 | # whether we want Anaconda or Miniconda, Python 2.x or 3.x, x86 or x64,
186 | # and OS X or Linux.
187 | local ana_type
188 | [[ ${opt_mini} ]] && ana_type="Miniconda" || ana_type="Anaconda"
189 | [[ ${py_vers} =~ ^3 ]] && ana_type+=3
190 | # The directory Anaconda creates during installation will be anaconda2,
191 | # anaconda3, miniconda2, or miniconda3
192 | local ana_dir=~/$(echo -n ${ana_type} | tr A-Z a-z)
193 | ! [[ ${py_vers} =~ ^3 ]] && ana_dir+=2
194 | # The Anaconda installer includes the latest Python 2.x or 3.x in its
195 | # root (default) environment, so if we want an older version, we'll need
196 | # to create a new conda environment after installation.
197 | #
198 | # When that's the case, we install the ten-times smaller Miniconda package
199 | # for the root environment regardless of what the command-line arguments
200 | # specify. Then, we use conda to create a second environment with the
201 | # right Python version and set that as the user's default Python in
202 | # ~/.pythonize.startup.
203 | local ana_root_type
204 | local ana_root_dir
205 | if [[ ${downgrade} ]]; then
206 | ana_root_type=Miniconda${ana_type##*conda}
207 | ana_root_dir=~/$(echo -n ${ana_root_type} | tr A-Z a-z)
208 | ! [[ ${py_vers} =~ ^3 ]] && ana_root_dir+=2
209 | fi
210 | if [[ ${opt_wrap} ]]; then
211 | pythonize::run_wrapper_mode
212 | else
213 | pythonize::run_normal_mode
214 | fi
215 | }
216 |
217 |
218 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219 | # Actions: Get a yes or no from the user.
220 | # Globals: Terminal-movement/terminal-color constants.
221 | # Free vars: main():
222 | # opt_wrap
223 | # Arguments: None.
224 | # Functions: pythonize::say
225 | # Returns: 0 (yes) or 1 (no).
226 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
227 | pythonize::confirm() {
228 | local resp
229 | local retval=0
230 | # We want wrapper mode to be noninteractive, so autoconfirm everything in
231 | # that case.
232 | [[ ${opt_wrap} ]] && return ${retval}
233 | while read -r resp; do
234 | [[ ${resp} =~ ^(y|yes|n|no)$ ]] && break
235 | pythonize::say -b 1 "Please enter 'y' or 'n'"
236 | done
237 | [[ ${resp} =~ ^n ]] && retval=1
238 | return ${retval}
239 | }
240 |
241 |
242 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
243 | # Actions: Create a conda environment with the specified Python version.
244 | # Globals: FUNCNAME
245 | # Terminal-movement/terminal-color constants.
246 | # Free vars: main():
247 | # py_vers
248 | # Arguments:
249 | # Functions: pythonize::die
250 | # Returns: None.
251 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
252 | pythonize::create_conda_environment() {
253 | if [[ -z $1 ]]; then
254 | pythonize::die "Internal Error: No argument for ${FUNCNAME}"
255 | fi
256 | local c_env=$1
257 | # Check if the conda environment we want to create already exists. If it
258 | # does, check if we're currently using it (this can happen when rerunning a
259 | # pythonize-wrapped script which has previously created an environment
260 | # "pythonized_" that no longer satisfies our dependencies).
261 | #
262 | # If we're currently using the target environment, switch to the root
263 | # environment before removing the target environment (conda cannot remove
264 | # an in-use environment). Otherwise, simply remove it directly.
265 | local e=$(conda env list \
266 | | awk '$2 ~ /\*/ && $3 ~ /'"${c_env}"'/ {printf $3}')
267 | if [[ ${e} ]]; then
268 | source deactivate
269 | source "${e%/envs*}/bin/activate" root
270 | conda remove -y --all -n "${c_env}"
271 | elif [[ $(conda env list | awk '$3 ~ /'"${c_env}"'/') ]]; then
272 | conda remove -y --all -n "${c_env}"
273 | fi
274 | if [[ ${opt_mini} ]]; then
275 | conda create -y -n "${c_env}" python=${py_vers}
276 | else
277 | conda create -y -n "${c_env}" python=${py_vers} anaconda
278 | fi
279 | if ! source activate "${c_env}"; then
280 | pythonize::die "Unable to access ${c_env} conda environment"
281 | fi
282 | }
283 |
284 |
285 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
286 | # Actions: Print formatted error messages and quit with exit code 1.
287 | # Globals: Terminal-movement/terminal-color constants.
288 | # Arguments: Message string.
289 | # Returns: None (terminates program with exit status 1).
290 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
291 | pythonize::die() {
292 | # Rather than add external dependencies, we try to prettyprint messages
293 | # using shell utilties common to both BSD- and GNU-flavored *nix. We first
294 | # calculate the size of the columns we need; then, we shove everything
295 | # through echo+fold+paste to align it all while keeping the lines lengths
296 | # to under 79 characters.
297 | #
298 | # Colors present a special problem: "fold" counts nonprinting characters
299 | # when determining where to wrap columns, so it will fold colored lines
300 | # early depending on where nonprinting characters appear on those lines.
301 | #
302 | # Previous versions of this utility used "fmt" rather than "fold," but that
303 | # broke this function on OS X --- so we're using fold as the best cross-
304 | # alternative I currently know until I can devise a better. We just
305 | # manually work around the occasional prematurely wrapped line on a case-
306 | # by-case basis in the meantime.
307 | local header=" > ${BOLD}${PROG}: Error:${RESET}${RED}"
308 | local char_ct=$(wc -m <(echo -n "${header}") | awk '{printf $1}')
309 | local inv_char_ct=$(wc -m <(echo -n "${BOLD}${RESET}${RED}") \
310 | | awk '{printf $1}')
311 | char_ct=$((char_ct - inv_char_ct))
312 | local tab_ct
313 | if (( (char_ct % 7) == 0 || char_ct <= 15 )); then
314 | tab_ct=$(( (char_ct / 7) - 1 ))
315 | else
316 | tab_ct=$((char_ct / 7))
317 | fi
318 | local filler="${RESET}${RED} >"
319 | local i
320 | filler+=$'\t'
321 | for ((i=1; i < tab_ct && tab_ct > 0; i++)); do
322 | filler+=$'\t'
323 | done
324 | local line_ct
325 | line_ct=$(wc -l <(fold -s -w 48 <(echo -en "$@")) | awk '{printf $1}')
326 | header+=$'\n'
327 | for ((i=0; i < line_ct && line_ct >= 0; i++)); do
328 | header+=${filler}$'\n'
329 | done
330 | header=${header%$'\n'}
331 | local msg=$(paste <(echo -n "${header}") \
332 | <(fold -s -w 48 <(echo -en "$@")))
333 | echo -e "\n${RED}${msg}${RESET}\n " >&2
334 | exit 1
335 | }
336 |
337 |
338 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
339 | # Actions: Install Anaconda/Miniconda.
340 | # Globals: FUNCNAME
341 | # PATH
342 | # Terminal-movement/terminal-color constants
343 | # Free vars: main():
344 | # ana_dir
345 | # ana_root_type
346 | # ana_root_dir
347 | # ana_type
348 | # arch
349 | # downgrade
350 | # opt_mini
351 | # Arguments: None.
352 | # Functions: pythonize::create_conda_environment
353 | # pythonize::die
354 | # pythonize::say
355 | # Returns: None.
356 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
357 | pythonize::install_anaconda() {
358 | # BSD mktemp works very differently from GNU mktemp and has no straight-
359 | # forward way to make a temporary file named the way we want --- ergo this
360 | # ugly, but *nix-agnostic, workaround.
361 | local exe=$(mktemp installer.XXXX)
362 | if [[ ! ${exe} ]]; then
363 | pythonize::die "Unable to create a unique filename via mktemp for" \
364 | "downloading the Anaconda installer."
365 | fi
366 | rm -f -- "${exe}"
367 | exe+=".sh"
368 | local get_url
369 | local get_exe
370 | # Most *nixes which lack curl have wget -- and vice versa -- so try both.
371 | if which curl &>/dev/null; then
372 | get_url="curl -s -L"
373 | get_exe="curl -L -o ${exe}"
374 | elif which wget &>/dev/null; then
375 | get_url="wget --quiet -O -"
376 | get_exe="wget --quiet --show-progress -O ${exe}"
377 | else
378 | pythonize::die "Unable to find curl or wget to download ${ana_type}"
379 | fi
380 | local sys
381 | [[ $(uname -s) =~ [Dd]arwin ]] && sys=MacOSX || sys=Linux
382 | local url
383 | local __ana_dir
384 | local __ana_type
385 | # See comments in main() re installing versions of Python other than the
386 | # the latest 2.x or 3.x.
387 | if [[ ${downgrade} ]]; then
388 | __ana_dir=${ana_root_dir}
389 | __ana_type=${ana_root_type}
390 | else
391 | __ana_dir=${ana_dir}
392 | __ana_type=${ana_type}
393 | fi
394 | # The URL to download the latest Miniconda appears to be static, but the
395 | # URL for Anaconda seems to change whenever the version increments.
396 | #
397 | # To standardize the process for every kind of installer we may want to
398 | # fetch, we first search the raw HTML from Anaconda's download page and
399 | # grab the download link to the latest installer. Then, we try to download
400 | # that installer.
401 | local regex="https?://[-a-z0-9_./]+${__ana_type}"
402 | if [[ ${opt_mini} || ${downgrade} ]]; then
403 | get_url+=" http://conda.pydata.org/miniconda.html"
404 | regex+="-latest-"
405 | else
406 | get_url+=" http://continuum.io/downloads"
407 | regex+="[-.0-9]+"
408 | fi
409 | regex+="${sys}-${arch}\.sh"
410 | url=$(${get_url} | grep -Eio "${regex}" | head -1)
411 | if [[ ! ${url} ]]; then
412 | pythonize::die "Could not find download link for latest ${ana_type}"
413 | fi
414 | trap "rm -f -- \"${exe}\"" EXIT
415 | if ! ${get_exe} ${url}; then
416 | pythonize::die "Failed to download ${__ana_type} installer"
417 | fi
418 | pythonize::say -a 1 -b 1 "\nLaunching ${ana_type} installer.\n "
419 | chmod +x "${exe}"
420 | # Run the Anaconda/Miniconda installer in noninteractive (batch) mode.
421 | ./${exe} -b
422 | rm -f -- "${exe}"
423 | # Put our newly installed conda utility in PATH so we can use it throughout
424 | # the rest of this program at need. Note: This will only affect the current
425 | # process's and its child process's shell environments, not the calling
426 | # shell.
427 | export PATH="${__ana_dir}/bin:${PATH}"
428 | # Create a new conda environment containing the needed Python version if
429 | # we're not installing the latest Python 2.x or 3.x (see comments in
430 | # pythonize::main()).
431 | if [[ ${downgrade} ]]; then
432 | pythonize::create_conda_environment "python${py_vers}"
433 | fi
434 | if ! which conda &>/dev/null; then
435 | pythonize::die "Unable to find conda after installation."
436 | fi
437 | }
438 |
439 |
440 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
441 | # Actions: Install additional Python packages then update all packages.
442 | # Globals: Terminal-movement/terminal-color constants
443 | # Free vars: main():
444 | # downgrade
445 | # opt_mini
446 | # opt_wrap
447 | # packages
448 | # py_vers
449 | # Arguments: None.
450 | # Functions: pythonize::die
451 | # Returns: None.
452 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
453 | pythonize::install_python_packages() {
454 | local -a missing
455 | local p
456 | # Pip will _usually_ be installed at this point, but there's a chance it
457 | # may be absent. Let's make double-plus sure we have it.
458 | which pip &>/dev/null || conda install -y pip
459 | for p in "${packages[@]}"; do
460 | conda install -y "$p" && continue
461 | # If the package isn't in conda's default channels, try pip.
462 | pythonize::say -a 1 -b 1 \
463 | "\nAnaconda does not appear to have Python package \"$p\"" \
464 | "in its default repositories --- trying pip instead.\n "
465 | yes y | pip install "$p"
466 | (($?)) || continue
467 | # Pip's error status can't be trusted: When it encounters an exception
468 | # trying to install a package, it will sometimes find an alternate way
469 | # to install it --- but still return a nonzero status. So let's double
470 | # check existence of packages before we report failure.
471 | python -c "import ${p}" &>/dev/null && continue
472 | # In wrapper mode, assume every package argument is a hard dependency,
473 | # so exit with an error on the first package we can't
474 | # install unattended. Otherwise, report what we couldn't install and
475 | # leave it to the user to get the package some other way.
476 | if [[ ${opt_wrap} ]]; then
477 | pythonize::die "Unable to install required Python package" \
478 | "\"${p}\" using conda or pip."
479 | fi
480 | pythonize::say -a 1 -b 1 \
481 | "\nUnable to install Python package \"${p}\" using" \
482 | "conda or pip. Continuing without it.\n "
483 | missing+=($p)
484 | done
485 | # Note: When the standard shell-wrapper template invokes pythonize in
486 | # wrapper mode, it always specifies Miniconda rather than Anaconda.
487 | if [[ ! ${opt_mini} ]]; then
488 | pythonize::say -a 1 -b 1 \
489 | "\nUpdating 100+ Python packages --- this will take some time.\n" \
490 | "\nTo reduce installation runtime as well as disk utilization," \
491 | "in the future you can install ${BOLD}miniconda${RESET}${GREEN}" \
492 | "instead. Miniconda contains only Python, the conda package" \
493 | "manager, and pip; you can then use conda to install just the" \
494 | "packages you currently need.\n "
495 | fi
496 | # Ideally, we'd always run conda --update to get all packages to their most
497 | # recent versions; however, that also replaces the environment's Python
498 | # with the latest release, which is explicitly not what we want.
499 | #
500 | # There's not a simple, clean way around that of which I'm aware, so we use
501 | # this kludge: Downgrade the Python version then fix any resulting package
502 | # mismatches by explicitly downgrading them to that Python version as well.
503 | if [[ ${downgrade} ]]; then
504 | conda install -y python=${py_vers}
505 | conda update -y "${packages[@]}"
506 | else
507 | conda update -y --all
508 | fi
509 | # The binstar package (extends packages available to install with conda by
510 | # allowing search of binstar repositories) is deprecated, so ensure that we
511 | # have anaconda-client instead if we're not using Miniconda.
512 | [[ ${opt_mini} ]] || conda install -y anaconda-client
513 | pythonize::say -b 2 "\nAnaconda Python has been successfully installed.\n "
514 | if ((${#missing[@]})); then
515 | pythonize::say "The following Python packages could not be installed" \
516 | "using either conda or pip: ${missing[@]}"
517 | fi
518 | }
519 |
520 |
521 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
522 | # Actions: Run pythonize in permanent-installation mode (i.e., pythonize is
523 | # invoked without the --wrapper option).
524 | # Globals: PROG
525 | # Terminal-movement/terminal-color constants
526 | # Free vars: main():
527 | # ana_dir
528 | # Arguments: None.
529 | # Functions: pythonize::confirm
530 | # pythonize::die
531 | # pythonize::install_anaconda
532 | # pythonize::install_python_packages
533 | # pythonize::say
534 | # pythonize::write_shell_startup
535 | # Returns: None.
536 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
537 | pythonize::run_normal_mode() {
538 | local d
539 | # If conda is in PATH but its directory is not the one we are using for
540 | # this installation, let the user know that he has an existing Anaconda
541 | # installation and confirm he wants to create another.
542 | if which conda &>/dev/null; then
543 | d=$(which conda | awk -F '\/envs|\/bin' '{print $1}' 2>/dev/null)
544 | if [[ ${ana_dir} != ${d} ]]; then
545 | pythonize::say -n \
546 | "Anaconda appears to already be installed at this location:" \
547 | "\n\n\t${BOLD}${d}${RESET}\n" \
548 | "\nSince we are installing in a different directory, we do" \
549 | "not need to overwrite it.\n\nContinue installing a" \
550 | "new Anaconda? (y/n) "
551 | if ! pythonize::confirm; then
552 | pythonize::say -a 1 -b 1 "\nAborting at user request\n "
553 | return
554 | fi
555 | fi
556 | fi
557 | # If our target installation directory already exists, ask the user if we
558 | # can remove that directory and install a new Anaconda.
559 | if [[ -d ${ana_dir} ]]; then
560 | pythonize::say -n "Anaconda appears to already be installed at this" \
561 | "location:\n\n\t${BOLD}${ana_dir}${RESET}\n" \
562 | "\nIf you choose to continue, ${PROG} will" \
563 | "remove this directory along with any conda" \
564 | "environments and settings within." \
565 | "\n\nContinue? (y/n) "
566 | if ! pythonize::confirm; then
567 | pythonize::say -a 1 -b 1 "\nAborting at user request\n "
568 | return
569 | fi
570 | if ! rm -rf -- "${ana_dir}"; then
571 | pythonize::die "Unable to remove ${ana_dir}"
572 | fi
573 | echo
574 | fi
575 | # In normal mode, we make a permanent installation: We install Anaconda
576 | # Python in either conda environment "root" or, if downgrading, conda
577 | # environment "python (see comments in pythonize::main()).
578 | # We also modify the user's shell startup via such that Anaconda Python
579 | # replaces the system Python for that user.
580 | pythonize::install_anaconda
581 | pythonize::write_shell_startup
582 | pythonize::install_python_packages
583 | pythonize::say -a 1 \
584 | "Start a fresh shell session to begin using your new Python" \
585 | "installation, or run this command:" \
586 | "\n\n\t${BOLD}${WHITE}source ~/.pythonize.startup\n "
587 | }
588 |
589 |
590 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
591 | # Actions: Run pythonize in wrapper mode (i.e., pythonize is invoked with the
592 | # --wrapper option).
593 | # Globals: None.
594 | # Free vars: main():
595 | # ana_dir
596 | # wrapper
597 | # Arguments: None.
598 | # Functions: pythonize::create_conda_environment
599 | # pythonize::install_anaconda
600 | # pythonize::install_python_packages
601 | # Returns: None.
602 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
603 | pythonize::run_wrapper_mode() {
604 | local c_env
605 | local d
606 | # If the user has a functioning Anaconda Python available, find it and use
607 | # conda to set up a conda environment named "pythonized_" to
608 | # be used by the wrapped Python program.
609 | if ! which conda &>/dev/null; then
610 | for d in ~/{ana,mini}conda{,3}/bin; do
611 | [[ -d $d ]] || continue
612 | source "${d}/activate" 2>/dev/null && break
613 | done
614 | fi
615 | if which conda &>/dev/null; then
616 | pythonize::create_conda_environment "pythonized_${wrapper}"
617 | pythonize::install_python_packages
618 | else
619 | # If we can't find an existing Anaconda Python, install it; however,
620 | # unlike normal mode, do not modify the user's shell startup to replace
621 | # the system Python with Anaconda. We'll use it for the wrapped Python
622 | # program without making permanent shell environment changes --- but
623 | # we'll leave that Python on disk so we can use it again later and/or
624 | # so the user can manually add it to his startup if he likes.
625 | [[ -d ${ana_dir} ]] && rm -rf -- "${ana_dir}"
626 | pythonize::install_anaconda
627 | pythonize::install_python_packages
628 | fi
629 | }
630 |
631 |
632 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
633 | # Actions: Print formatted general messages.
634 | # Globals: Terminal-movement/terminal-color constants.
635 | # Arguments: Message string.
636 | # Options: -a N Print N trailing newlines.
637 | # -b N Print N leading newlines.
638 | # -n End without a newline --- next text will be on same line.
639 | # Returns: None.
640 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
641 | pythonize::say() {
642 | # The comments from pythonize::die() apply to this function as well. The
643 | # formatting here is a bit different --- we chop up the lines and append
644 | # " > " to the beginning of each while ensuring all lines remain less
645 | # than 79 characters in length --- but the general mechanism remains the
646 | # same.
647 | local i
648 | local nl_after
649 | local nl_before
650 | local no_final_nl
651 | # Parse formatting options.
652 | while (($# > 0)); do
653 | case $1 in
654 | -n)
655 | no_final_nl=true
656 | shift
657 | ;;
658 | -@(a|b))
659 | [[ $2 =~ ^[0-9]+$ ]] || continue
660 | for ((i=0; i < $2 && $2 >= 0; i++)); do
661 | case $1 in
662 | -a) nl_after+=$'\n' ;;
663 | -b) nl_before+=$'\n' ;;
664 | esac
665 | done
666 | shift 2
667 | ;;
668 | *) break ;;
669 | esac
670 | done
671 | local msg=$(fold -s -w 72 <(echo -e "$@"))
672 | local line_ct=$(wc -l <(echo "${msg}") | awk '{print $1}')
673 | local filler
674 | for ((i=0; i < line_ct && line_ct >= 0; i++)); do
675 | filler+="${RESET}${WHITE} > ${GREEN}\n"
676 | done
677 | filler=${filler%\\n}
678 | msg=$(paste -d ' ' <(echo -e "${filler}") <(echo "${msg}"))
679 | if [[ ${no_final_nl} ]]; then
680 | echo -n "${nl_before}${msg}${RESET}"
681 | else
682 | echo "${nl_before}${msg}${nl_after}${RESET}"
683 | fi
684 | }
685 |
686 |
687 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
688 | # Actions: Display man page for pythonize.
689 | # Globals: INLINE_MAN_PAGE
690 | # Arguments: None.
691 | # Returns: None.
692 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
693 | pythonize::show_help() {
694 | # Writing to the traditional man page location /usr/share/man, or even to
695 | # a directory in /usr/local, usually requires a privileged user. So that
696 | # unprivileged users can install and use pythonize, we store its man page
697 | # source in a global variable. We write the source to a temp file in
698 | # the user's home directory when this function is called then invoke man to
699 | # render it.
700 | mkdir -p ~/tmp
701 | local manpage=~/tmp/pythonize.manpage.1
702 | cat <<<"${INLINE_MAN_PAGE}" > "${manpage}"
703 | if [[ ! -f ${manpage} ]]; then
704 | pythonize::die "Unable to find man page at ${manpage}"
705 | fi
706 | # Note: The exec call overwrites the running program with a new process, so
707 | # if you subclass this (see comments in "BEGIN EXECUTION" below), keep that
708 | # in mind: This function will terminate the current shell, regardless of
709 | # what comes after.
710 | exec man "${manpage}"
711 | }
712 |
713 |
714 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
715 | # Actions: Modify shell startup to use Anaconda rather than system Python.
716 | # Globals: STARTUP
717 | # Arguments: None.
718 | # Returns: None.
719 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
720 | pythonize::write_shell_startup() {
721 | # Matching and substituting strings in a file of unknown composition is a
722 | # highly error-prone process using shell. To avoid that, we create our own
723 | # startup file -- ~/.pythonize.startup -- and recreate it whenever
724 | # pythonize runs in normal mode; that way, we guarantee our startup file is
725 | # always in a predicatable state.
726 | #
727 | # We append a line sourcing ~/.pythonize.startup to the end of the user's
728 | # normal shell startup and handle the logic of figuring out which Python
729 | # environment to bootstrap there rather than in a startup file that the
730 | # user is likely to edit.
731 | local line=$(echo -e \
732 | "\n# Added by pythonize" \
733 | "\n[[ -f ~/.pythonize.startup ]] && source ~/.pythonize.startup\n ")
734 | local startup=~/.pythonize.startup
735 | local path=$(dirname "$(which conda)")
736 | # We replace the string "__REPLACE__" in $STARTUP with the current parent
737 | # directory of conda; this should be the Python environment we want to use.
738 | sed 's,__REPLACE__,'"${path}"',g' <(echo "${STARTUP}") > "${startup}"
739 | # Add $line to the first shell startup file that's writable.
740 | for f in ~/.bash_profile ~/.bashrc ~/.bash_profile ~/.bash_login \
741 | ~/.profile
742 | do
743 | if [[ -w $f ]]; then
744 | # If the shell startup file already has $line, don't add it.
745 | #
746 | # The unusual syntax of the following statement is purely to avoid
747 | # a 126-character line. I can't defend it --- but _every_ other
748 | # line was under 79 characters, so I couldn't help myself. (This
749 | # is, after all, a personal project for fun, so I'm not wasting any
750 | # company's time. ^.^ )
751 | if awk -f <(
752 | echo -n '/^[#] Added by pythonize/ {getline; if (!/^[#]/' \
753 | '&& /\/\.pythonize\.startup/) matched=1} END {if' \
754 | '(!matched) exit 1}'
755 | ) "$f"
756 | then
757 | break
758 | fi
759 | echo -n "${line}" >> "$f" && break
760 | fi
761 | done
762 | }
763 |
764 |
765 |
766 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
767 | # Man page.
768 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
769 |
770 | INLINE_MAN_PAGE=$(cat <]
819 | downloads the latest Anaconda release,
820 | .TP
821 | \[->]
822 | installs Anaconda noninteractively using sane defaults,
823 | .TP
824 | \[->]
825 | uses
826 | .I conda
827 | to update Python packages, and
828 | .TP
829 | \[->]
830 | adds Anaconda to
831 | .I PATH
832 | in your shell startup.
833 | .RE
834 | .PD
835 | .P
836 | Anaconda Python provides 100+ popular Python packages in addition to the
837 | standard library. For a leaner installation (10x smaller) including only
838 | Python and
839 | .IR conda ,
840 | .B pythonize
841 | supports installing
842 | .B Miniconda
843 | instead by using the
844 | .B --miniconda
845 | option (see
846 | .B OPTIONS
847 | below).
848 | .P
849 | .B pythonize
850 | also provides a method for wrapping Python programs: Invoked
851 | with the
852 | .B --wrapper
853 | option,
854 | .B pythonize
855 | abstracts Python version and library dependencies from the wrapped
856 | application\[cq]s users, automatically ensuring the execution environment
857 | satisfies your requirements.
858 | .P
859 | Using
860 | .B pythonize
861 | in wrapper mode eliminates any need to consider your users\[cq] Python skill
862 | level, their ability to find and install Python packages, their current
863 | Python version\[em]or whether they have Python installed at all.
864 | .P
865 | How wrapping works:
866 | .sp
867 | .RS 4
868 | .ad l
869 | .nr step 1 1
870 | .TP 3
871 | \n[step]
872 | In place of your actual Python program, make your application\[cq]s main executable
873 | a copy of the shell wrapper included with
874 | .BR pythonize ,
875 | renamed as your application.
876 | .TP 3
877 | \n+[step]
878 | Move your Python program to
879 | .IR .runtime/.py
880 | in your application package\[cq]s directory.
881 | .TP 3
882 | \n+[step]
883 | User runs your application\[cq]s main executable.
884 | .TP 3
885 | \n+[step]
886 | Shell wrapper checks all currently installed Pythons against your dependencies,
887 | calling
888 | .B pythonize
889 | to fetch anything needed.
890 | .TP 3
891 | \n+[step]
892 | Shell wrapper then runs your Python program.
893 | .RE
894 | .PD
895 | .ad n
896 | .P
897 | For more on wrapper functionality, see
898 | .BR "OPTIONS " and " WRAPPER MODE "
899 | below.
900 | .SH OPTIONS
901 | .TP 7
902 | .BR -h ", " --help
903 | Display this manual page.
904 | .TP
905 | .BR -m ", " --miniconda
906 | Install the more lightweight
907 | .B Miniconda
908 | (10x smaller) rather than Anaconda. Miniconda includes only Python and
909 | .IR conda ;
910 | you can then use conda to install additional Python packages.
911 | .TP
912 | .BR -p ", " --packages " [" \fIpackage " [" \fIpackage ...]]
913 | Install these additional Python packages. For example,
914 | .RS +11
915 | .P
916 | .B pythonize --packages httpie nose requests
917 | .RE
918 | .TP
919 | .BR -v ", " --python-version " \fIversion"
920 | Install Python
921 | .I version
922 | rather than 2.7 into the default Anaconda environment. If the
923 | .I version
924 | argument is 2 or 3 without specifying a minor version number,
925 | .B pythonize
926 | downloads the latest 2.x or 3.x respectively.
927 | .TP
928 | .BR -w ", " --wrapper " \fIapplication-name"
929 | Run
930 | .B pythonize
931 | in wrapper mode, using
932 | .I application-name
933 | as a suffix for the
934 | .I conda
935 | environment we\[cq]ll create if we need to make a new one.
936 | .sp
937 | Rather than invoking wrapper mode directly, it\[cq]s recommended in most cases to use the shell wrapper included with the
938 | .B pythonize
939 | package, which handles this and other wrapper implementation
940 | details for you.
941 | .SH NORMAL MODE
942 | In normal mode (that is, when invoked without the
943 | .B --wrapper
944 | option),
945 | .B pythonize
946 | installs either Anaconda or Miniconda for the current user through this
947 | sequence of actions:
948 | .sp
949 | .RS 4
950 | .ad l
951 | .nr step 1 1
952 | .TP 3
953 | \n[step]
954 | Check if Anaconda is installed. If it is and it occupies our target
955 | installation directory, prompt to overwrite.
956 | .TP 3
957 | \n+[step]
958 | Download latest Anaconda release.
959 | .TP 3
960 | \n+[step]
961 | Run the Anaconda installer in noninteractive mode.
962 | .TP 3
963 | \n+[step]
964 | Install any additional Python packages specified with the
965 | .B --packages
966 | option (see
967 | .B OPTIONS
968 | above).
969 | .TP 3
970 | \n+[step]
971 | Modify user\[cq]s shell startup to set Anaconda Python rather than the system Python as the user\[cq]s default.
972 | .RE
973 | .P
974 | .ad n
975 | When
976 | .B pythonize
977 | succesfully completes, begin using Anaconda by starting a new shell or running
978 | this command in the current shell:
979 | .RS 7
980 | .P
981 | .B source ~/.pythonize.startup
982 | .RE
983 | .SH WRAPPER MODE
984 | .SS Invocation
985 | In wrapper mode (that is, when invoked with the
986 | .B --wrapper
987 | option),
988 | .B pythonize
989 | installs a Miniconda environment intended to be used by the wrapped Python
990 | application. This differs from normal mode as follows:
991 | .sp
992 | .ad l
993 | .RS 4
994 | .TP 3
995 | \[->]
996 | Shell startup files are not modified.
997 | .TP
998 | \[->]
999 | If Anaconda is already installed,
1000 | .B pythonize
1001 | uses an existing
1002 | .I conda
1003 | to satisfy Python dependencies instead of creating a fresh Anaconda
1004 | installation.
1005 | .TP
1006 | \[->]
1007 | If we need to install Anaconda Python, we fetch Miniconda instead of Anaconda,
1008 | regardless of contrary explicit arguments.
1009 | .TP
1010 | \[->]
1011 | If any packages specified with the
1012 | .B --packages
1013 | option (see
1014 | .B OPTIONS
1015 | above) cannot be installed,
1016 | .B pythonize
1017 | halts with an error rather than just issuing a warning.
1018 | .RE
1019 | .ad n
1020 | .P
1021 | The complete sequence of actions undertaken in wrapper mode follows:
1022 | .sp
1023 | .RS 4
1024 | .ad l
1025 | .nr step 1 1
1026 | .TP 3
1027 | \n[step]
1028 | Search for any existing Anaconda or Miniconda installations.
1029 | .TP 3
1030 | \n+[step]
1031 | If we find an existing
1032 | .I conda
1033 | utility, use it to create a
1034 | conda environment named
1035 | .B pythonized_
1036 | .TP 3
1037 | \n+[step]
1038 | If we can\[cq]t find a
1039 | .I conda
1040 | to use, install Miniconda and use its root (default) conda environment.
1041 | .TP 3
1042 | \n+[step]
1043 | Use
1044 | .I conda
1045 | to install the specified Python version and libraries.
1046 | .RE
1047 | .P
1048 | .ad n
1049 | When
1050 | .B pythonize
1051 | successfully completes, you\[cq]ll have a Python environment suitable for the
1052 | wrapped Python application.
1053 | .SS Packaging Python applications
1054 | To make wrapping arbitrary Python programs with
1055 | .B pythonize
1056 | easy, the
1057 | .B pythonize
1058 | utility contains an
1059 | .B example_package directory
1060 | providing the required files in the recommended file layout for your
1061 | application (see
1062 | .B FILES
1063 | below).
1064 | .P
1065 | To get started, follow these steps:
1066 | .sp
1067 | .RS 4
1068 | .ad l
1069 | .nr step 1 1
1070 | .TP 3
1071 | \n[step]
1072 | Copy the
1073 | .B example_package directory
1074 | and rename it for your application. (Note: For the remainder of this section,
1075 | all pathnames will be relative to this directory.)
1076 | .TP 3
1077 | \n+[step]
1078 | Rename the shell wrapper at
1079 | .I example
1080 | to the name you\[cq]d like for your main executable (for example,
1081 | .IR nmapcli ).
1082 | .TP 3
1083 | \n+[step]
1084 | Replace
1085 | .I \%.runtime/example.py
1086 | with your Python program.
1087 | .TP 3
1088 | \n+[step]
1089 | Set your Python program\[cq]s filename to match your main executable, but add a
1090 | .I .py
1091 | extension (for example,
1092 | .IR nmapcli.py ).
1093 | .TP 3
1094 | \n+[step]
1095 | Edit the four variables in
1096 | .I \%.runtime/wrapper.env
1097 | with settings specific to your application (the shell wrapper sources this file
1098 | during execution).
1099 | .sp
1100 | By default,
1101 | .I \%.runtime/wrapper.env
1102 | sets the following:
1103 | .RS 7
1104 | .TP 13
1105 | .B PROG
1106 | Name the shell wrapper displays in console messages before executing your
1107 | Python application (by default, the shell wrapper\[cq]s filename).
1108 | .TP
1109 | .B WRAPPED_PROG
1110 | Filepath to your main Python program (by default,
1111 | .IR \%.runtime/.py ).
1112 | .TP
1113 | .B PY_VERSION
1114 | The Python version your application requires (by default, the most recent
1115 | Python 3.x).
1116 | .TP
1117 | .B PY_PACKAGES
1118 | List of packages outside the Python standard library that your application
1119 | requires (by default, colorama and requests).
1120 | .RE
1121 | .sp
1122 | .in +3
1123 | You can also set environment variables for your Python program in
1124 | .I \%.runtime/wrapper.env
1125 | (for example,
1126 | .IR export\[ul]PYTHONPATH= ).
1127 | .in -3
1128 | .RE
1129 | .P
1130 | .ad n
1131 | You\[cq]re done: The Python application is now wrapped with
1132 | .BR pythonize .
1133 | .P
1134 | Users will run your application the same way they did before. Instead of a
1135 | Python program, however, your application\[cq]s main executable is now a shell
1136 | wrapper that does the following:
1137 | .sp
1138 | .RS 4
1139 | .ad l
1140 | .nr step 1 1
1141 | .TP 3
1142 | \n[step]
1143 | Check if the system Python meets the Python verison and library requirements
1144 | listed in
1145 | .I \%.runtime/wrapper.env.
1146 | If it does,
1147 | .I exec
1148 | the Python application\[em]and we\[cq]re done.
1149 | .TP 3
1150 | \n+[step]
1151 | If the system Python cannot satisfy the dependencies, try each installed
1152 | Anaconda Python environment.
1153 | .TP 3
1154 | \n+[step]
1155 | If none of the currently installed Anaconda Python environments can satisfy
1156 | the dependencies, run
1157 | .B pythonize
1158 | in wrapper mode to fetch the Python version and/or libraries we still need.
1159 | .TP 3
1160 | \n+[step]
1161 | With our dependencies met,
1162 | .I exec
1163 | the Python application.
1164 | .RE
1165 | .P
1166 | .ad n
1167 | This is the standard way to wrap a Python application using
1168 | .BR pythonize ,
1169 | but you can customize this process by editing the source files, which are
1170 | extensively commented for that purpose.
1171 | .P
1172 | Alternatively, because of the style in which
1173 | .B pythonize
1174 | is written, you can source (import)
1175 | .B pythonize
1176 | as a shell library then use it in a manner similar to OOP classes: You can then
1177 | call its functions as psuedo-methods or subclass
1178 | .BR pythonize ,
1179 | customizing its functions as you like.
1180 | .P
1181 | If customizing
1182 | .B pythonize
1183 | in this way interests you, consult the source code comments for implementation
1184 | and usage details.
1185 | .SH ENVIRONMENT
1186 | .TP 7
1187 | .I PATH
1188 | Used during runtime to find the system Python or a currently active Anaconda
1189 | Python environment.
1190 | .sp
1191 | In normal mode,
1192 | .B pythonize
1193 | modifies
1194 | .I PATH
1195 | in shell startup files to set Anaconda Python rather than the system
1196 | Python as the user\[cq]s default (see
1197 | .B NORMAL MODE
1198 | above).
1199 | .sp
1200 | In all modes,
1201 | .B pythonize
1202 | modifies
1203 | .I PATH
1204 | during runtime as it looks in every standard installation location to find an
1205 | existing Anaconda Python before selecting one for its use. This affects the
1206 | running
1207 | .B pythonize
1208 | process, its child processes, and any wrapped Python program\[em]but not the
1209 | parent shell.
1210 | .P
1211 | While by default
1212 | .I \%/.runtime/wrapper.env
1213 | sets no environment variables, as a customization you can use this file to
1214 | export environment variables affecting a wrapped Python application (see
1215 | .B WRAPPER MODE
1216 | above).
1217 | .SH FILES
1218 | .B ~/.bash_profile
1219 | .br
1220 | .B ~/.bashrc
1221 | .br
1222 | .B ~/.bash_login
1223 | .br
1224 | .B ~/.profile
1225 | .br
1226 | .RS 7
1227 | .B pythonize
1228 | appends a line sourcing
1229 | .I ~/.pythonize.startup
1230 | to the first listed shell startup file that\[cq]s
1231 | writable when run in normal mode (see
1232 | .B NORMAL MODE
1233 | above).
1234 | .RE
1235 | .P
1236 | .B ~/anaconda
1237 | .br
1238 | .B ~/anaconda3
1239 | .br
1240 | .B ~/miniconda
1241 | .br
1242 | .B ~/miniconda3
1243 | .RS 7
1244 | .B pythonize
1245 | creates, removes, or alters files within these directories depending on what
1246 | Anaconda Python version is selected for installation (see
1247 | .BR OPTIONS " and " "NORMAL MODE"
1248 | above).
1249 | .RE
1250 | .TP
1251 | .B ~/tmp/pythonize.manpage.1
1252 | When invoked with the
1253 | .B --help
1254 | option,
1255 | .B pythonize
1256 | creates
1257 | .I ~/tmp
1258 | if it doesn\[cq]t exist then writes the groff source for this manual page to
1259 | .IR \%~/tmp/pythonize.manpage.1 ,
1260 | overwriting any preexisting file with that name.
1261 | .TP
1262 | .B /example_package/example
1263 | Initial location of shell wrapper (see
1264 | .B WRAPPER MODE
1265 | above).
1266 | .TP
1267 | .B /example_package/.runtime/example.py
1268 | Initial location of Python application to wrap in wrapper mode; by default,
1269 | this contains a demo Python script (see
1270 | .B WRAPPER MODE
1271 | above.)
1272 | .TP
1273 | .B /example_package/.runtime/pythonize
1274 | Hard link to
1275 | .BR pythonize :
1276 | .I /pythonize
1277 | .TP
1278 | .B /example_package/.runtime/wrapper.env
1279 | Initial location of settings file for wrapped Python applications (see
1280 | .B WRAPPER MODE
1281 | above).
1282 | .SH EXAMPLES
1283 | Default installation (installs Python 2.7,
1284 | .IR conda ,
1285 | .IR pip ,
1286 | and 100+ popular Python packages using Anaconda):
1287 | .P
1288 | .RS +7
1289 | .B pythonize
1290 | .RE
1291 | .P
1292 | Install latest version of Python 3:
1293 | .P
1294 | .RS +7
1295 | .B pythonize --python-version 3
1296 | .RE
1297 | .P
1298 | Install a specific Python 3 release, then add the httpie, nose, and requests
1299 | libraries into the default environment post-installation.
1300 | .P
1301 | .RS +7
1302 | .B pythonize --python-version 3.3 --packages httpie nose requests
1303 | .RE
1304 | .SH SEE ALSO
1305 | .PD 0
1306 | .TP 17
1307 | .B Anaconda Python
1308 | .URL https://store.continuum.io/cshop/anaconda
1309 | .TP
1310 | .B conda
1311 | .URL http://conda.pydata.org/docs
1312 | .TP
1313 | .B pip
1314 | .URL https://pip.readthedocs.org/en/stable/
1315 | .TP
1316 | .B pythonize
1317 | .URL http://github.com/princebot/pythonize
1318 | .PD
1319 | EOF_MAN_PAGE
1320 | )
1321 |
1322 |
1323 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1324 | # ~/.pythonize.startup contents
1325 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1326 |
1327 | declare -r STARTUP=$(cat <<'EOF_PYTHONIZE_STARTUP'
1328 | #!/usr/bin/env bash
1329 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1330 | # Name: pythonize.startup
1331 | # Source: https://github.com/princebot/pythonize
1332 | # Author: prince@princebot.com
1333 | # Synopsis: Sourced at beginning of shell session to load Anaconda Python.
1334 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1335 |
1336 | # pythonize appends a line sourcing this to the end of an existing shell
1337 | # startup script, e.g., ~/.bash_profile.
1338 |
1339 | if [[ -d __REPLACE__ ]]; then
1340 | __dir="__REPLACE__"
1341 | # Append Anaconda Python to PATH if not already present. This works exactly
1342 | # like pathmunge() from most Linuxes' /etc/profile.
1343 | if [[ ! ${PATH} =~ (^|:)"${__dir}"($|:) ]]; then
1344 | PATH="${__dir}:${PATH}"
1345 | fi
1346 | export PATH
1347 | # Conda adds the name of the current conda environment to the prompt
1348 | # unless you're using the root environment, so let's temporarily disable
1349 | # that behavior before switching to our target environment if we're using
1350 | # something other than root.
1351 | if [[ ${__dir} =~ .*(ana|mini)conda/envs ]]; then
1352 | __env=$(echo -n "${__dir}" | awk -F '/' '{printf $(NF - 1)}')
1353 | conda config --set changeps1 false
1354 | source activate "${__env}"
1355 | conda config --set changeps1 true
1356 | fi
1357 | fi 2>/dev/null
1358 |
1359 | unset -v __dir
1360 | unset -v __env
1361 |
1362 | EOF_PYTHONIZE_STARTUP
1363 | )
1364 |
1365 |
1366 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1367 | # Begin execution.
1368 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1369 |
1370 | # When this program is run directly (i.e., invoked with ./ or bash
1371 | # ), $0 is the same filename as $BASH_SOURCE. However, when this program
1372 | # is sourced (i.e., invoked with . or source ), $0 is the calling
1373 | # program, but $BASH_SOURCE is the _current_ program.
1374 | #
1375 | # Because of that (probably unintended) Bash behavior, the below line has this
1376 | # effect: If we're in a direct invokation, run pythonize; otherwise, set its
1377 | # globals as environment variables affecting the calling shell and export the
1378 | # defined functions. This is functionally similar to a common Python idiom:
1379 | #
1380 | # if __name__ == '__main__'
1381 | # main()
1382 | #
1383 | # I've written pythonize this way so it can be more easily extended: You can
1384 | # import it into other shell programs like an object-oriented class and use its
1385 | # functions like methods --- or even subclass it by importing this, overriding
1386 | # methods with custom behavior as you like, then calling pythonise::main() (or
1387 | # ::main()).
1388 |
1389 | if [[ $(basename "$0") == $(basename "${BASH_SOURCE}") ]]; then
1390 | pythonize::main "$@"
1391 | else
1392 | for __method in $(declare -F | awk '$3 ~ /^pythonize::/ {print $3}'); do
1393 | export -f "${__method}"
1394 | done
1395 | fi
1396 | unset -v __method
1397 |
1398 |
--------------------------------------------------------------------------------