Mercurial > hg > installfile
annotate installfile.py @ 0:cf72635e9572 default tip
install a file to site-packages
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 31 Aug 2011 13:49:42 -0700 |
parents | |
children |
rev | line source |
---|---|
0
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 installs python files to sys.path |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 """ |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 import os |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 import shutil |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 import sys |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 import optparse |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 from distutils.sysconfig import get_python_lib |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 def install(path): |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 """install to sys.path""" |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 site_packages = get_python_lib() |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 path = os.path.abspath(path) |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 dirname, filename = os.path.split(path) |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 root, ext = os.path.splitext(filename) |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 pth = file(os.path.join(site_packages, '%s.pth' % root), 'w') |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 pth.write(dirname) |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 pth.close() |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 def main(args=sys.argv[1:]): |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 # parse command line arguments |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 usage = '%prog [options] python_file.py another_python_file.py [...]' |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 # description formatter |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 def format_description(self, description): |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 if description: |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 return description + '\n' |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 else: |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 return '' |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 options, args = parser.parse_args(args) |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 # print usage if no files are given |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 if not args: |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 parser.print_usage() |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 parser.exit() |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 # install the things |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 for python_file in args: |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 install(python_file) |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 if __name__ == '__main__': |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 main() |
cf72635e9572
install a file to site-packages
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
51 |