/docs/devpatterns.txt

https://bitbucket.org/ianb/silverlining/ · Plain Text · 218 lines · 171 code · 47 blank · 0 comment · 0 complexity · 28d96139547bfa4a34baf627bbcdc6e1 MD5 · raw file

  1. Developing Using Silver Lining
  2. ==============================
  3. This document discusses how development looks when using Silver
  4. Lining. Not `how you develop Silver Lining itself
  5. <devel-silverlining.html>`_, but how application development with
  6. Silver Lining can work.
  7. Creating a Project/Layout
  8. -------------------------
  9. The first thing you'll have to do is setup a code layout. An initial
  10. layout can be created like this::
  11. silver init myapp-app
  12. This is a `virtualenv <http://virtualenv.openplans.org>`_ environment
  13. with some added stuff. Here's what the layout looks like::
  14. app.ini This contains the configuration for your
  15. application. To start out with it is filled with
  16. helpful comments.
  17. bin/ All your scripts for managing your application
  18. (probably none of these should be used when
  19. running your application)
  20. You can also run *Python* scripts from here on
  21. the production server by using "silver run".
  22. pip Use this to install things
  23. easy_install You can also use this to install things; I think
  24. you should use pip, but easy_install should work
  25. fine too
  26. activate Do "source myapp-app/bin/activate" when doing work
  27. in your environment for convenience (this just
  28. places bin/ first on $PATH).
  29. python(2.6) The virtualenv interpreter. So long as you use
  30. this interpreter you'll get access to all the
  31. libraries you've installed for your app.
  32. static/ This is where static files go. Any file found
  33. here will be served directly, not passed to your
  34. application as a request. You can symlink things
  35. into here, or make the entire directory a symlink.
  36. lib/python2.6/ Some virtualenv stuff is put in here; you can
  37. ignore it (mostly).
  38. lib/python2.6/sitecustomize.py
  39. This is the one thing you might pay attention to.
  40. This file sets up the environment. Most
  41. particularly it adds lib/python to the path (more
  42. about that later). You shouldn't edit this.
  43. lib/python2.6/site-packages/
  44. This is often where packages would be installed,
  45. but not for this environment. Only
  46. Setuptools/Distribute and pip are installed here.
  47. lib/python/ This is the directory where everything gets
  48. installed. It starts out empty, it's just for
  49. your application's packages. If you do pip
  50. install foobar, then there will be
  51. lib/python/foobar/
  52. silvercustomize.py
  53. This file doesn't exist to start, but you can add
  54. it to customize the setup, for instance to set
  55. os.environ['DJANGO_SETTINGS_MODULE'].
  56. easy-install.pth
  57. If you have libraries in src/, there will be
  58. pointers to those paths in this file. It's
  59. created on demand, so you won't see it at first.
  60. It initially contains absolute paths (that's how
  61. Setuptools puts the file together), but when
  62. you upload an application those paths will be
  63. made relative.
  64. include/ Created by virtualenv; you can ignore this
  65. directory.
  66. src/ Just a place to check out your own libraries.
  67. After checking out a library run:
  68. pip -e src/new-library
  69. Next Steps
  70. ----------
  71. Now that you have a basic environment, either create some code in
  72. ``myapp-app/src/myapp`` or check out some existing code into that
  73. location. If you are using a framework, install it using ``bin/pip``
  74. (e.g., ``myapp-app/bin/pip install Pylons``). Install your app itself
  75. like ``bin/pip install -e src/myapp`` -- this causes any prerequisites
  76. listed in ``src/myapp/setup.py`` to also be installed by pip
  77. (otherwise they might get installed with easy_install, which is *okay*
  78. but not as graceful).
  79. You might want to put ``app.ini`` into your application (and into
  80. version control), and then turn it into a symlink, like::
  81. $ cd myapp-app
  82. $ ln -s src/myapp/silver-app.ini app.ini
  83. You have to define your "runner", which is the script or config file
  84. that starts your application. You need a line like this in
  85. ``app.ini``::
  86. runner = src/myapp/silver-runner.py
  87. If you have a ``.py`` file then it must define ``application``, a WSGI
  88. application. If you have an ``.ini`` file then it is treated as a
  89. Paste Deploy config file (as used by Pylons, TurboGears, Repoze --
  90. though it can be applied to many kinds of applications).
  91. You can also put ``lib/python/`` into version control. You shouldn't
  92. *edit* things in ``lib/python/`` except to install newer versions of
  93. software. But by putting this directory into version control you can
  94. be sure you have a consistent and stable set of libraries, and you can
  95. easily revert problematic library upgrades.
  96. Lastly you can go the extra mile and move ``bin/`` into
  97. ``lib/python/bin/`` and create a symlink back to ``bin/``. This looks
  98. like::
  99. $ mv bin lib/python/
  100. $ ln -s lib/python/bin bin
  101. $ cd lib/python
  102. $ echo "syntax: glob
  103. bin/activate*
  104. bin/pip
  105. bin/easy_install*
  106. bin/python*" > .hgignore
  107. $ hg addremove
  108. This puts all the items in ``bin/`` into version control, *except*
  109. those things created automatically by virtualenv (and so automatically
  110. created by ``silver init``).
  111. Customizing Your Environment
  112. ----------------------------
  113. If you put in a file ``lib/python/silvercustomize.py`` this module
  114. will be loaded everytime you start the environment. This is a great
  115. place to do things like::
  116. import os
  117. os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
  118. Note also that all the services will get setup everytime you start
  119. ``bin/python`` or any other service, so you can access the database
  120. from tests or scripts or anything else.
  121. Creating a Build Script
  122. -----------------------
  123. A nice way to setup an application is to have a script to build the
  124. layout for your application. This helps other developers work on the
  125. project. Since Silver Lining only works on Posix-style (and not
  126. Windows) systems you can just make a shell script. Here's an
  127. example::
  128. #!/usr/bin/env bash
  129. DIR="$1"
  130. if [ -z "$DIR" ] ; then
  131. echo "Usage: $(basename $0) NEW_DIR"
  132. exit 2
  133. fi
  134. if ! which silver ; then
  135. echo "You must install silver and have it on \$PATH"
  136. exit 3
  137. fi
  138. for COMMAND in hg git svn ; do
  139. if ! which $COMMAND ; then
  140. echo "You must install $COMMAND"
  141. exit 4
  142. fi
  143. done
  144. silver init $DIR
  145. pushd $DIR
  146. # Obviously check it out with whatever is appropriate:
  147. if [ ! -e src/myapp/.hg ] ; then
  148. hg clone http://blahblah/myapp src/myapp
  149. fi
  150. if [ ! -e lib/python/.hg ] ; then
  151. # We have to delete it first because silver init creates
  152. # this directory:
  153. rm -rf lib/python
  154. hg clone http://blahblah/myapp-lib lib/python
  155. fi
  156. if [ ! -L app.ini ] ; then
  157. rm -f app.ini
  158. ln -s src/myapp/silver-app.ini app.ini
  159. fi
  160. if [ ! -L bin ] ; then
  161. mv bin bin.tmp
  162. ln -s lib/python/bin bin
  163. mv bin.tmp/* bin/
  164. rmdir bin.tmp
  165. fi
  166. if [ ! -L static ] ; then
  167. rmdir static
  168. ln -s src/myapp/myapp/static static
  169. fi
  170. Then tell people to grab the script you write directly and run it to
  171. get a working rig.