anatoly techtonik avatar anatoly techtonik committed 8d34238

Add bootstrap.py script for the Endgame: Singularity.

Comments (0)

Files changed (1)

02.fetch-music-archive.py

+#!/usr/bin/env python2
+#
+# Use `locally` bootstrap code to setup environment, fetch
+# and unpack resource file and execute code. This script is
+# submitted to Endgame: Singularity project.
+ 
+# This stuff is placed into public domain by
+#    anatoly techtonik <techtonik@gmail.com>
+ 
+# --- bootstrap .locally ---
+#
+# this creates .locally/ subdirectory in the script's dir
+# and sets a few global variables for convenience:
+#
+#   ROOT  - absolute path to source code checkout dir
+#   LOOT  - absolute path to the .locally/ subdir
+#   LIB   - path to downloaded helper libs added to sys.path
+#           (.locally/.bootstrap)
+#
+# this provides some helpers:
+#
+#   localdir(name)   - returns absolute path to the new `name`
+#                      dir created .locally
+#   extract_zip(zippath, subdir, target)
+#                    - extracts subdir from the zip file
+#
+# this also downloads and imports the following helper libs:
+#
+#   wget        - for fetching music files
+# 
+
+import os
+import sys
+import urllib
+
+# 1. create LIB dir for locally modules (.locally/.bootstrap)
+#    and add it to Python sys.path to make them importable
+
+ROOT = os.path.abspath(os.path.dirname(__file__))
+LOOT = os.path.join(os.path.dirname(__file__), '.locally')
+
+def localdir(name):
+  '''create dir in LOOT if needed, return path with ending '/' '''
+  nupath = LOOT + '/' + name + '/'
+  if not os.path.exists(nupath):
+    os.makedirs(nupath)
+  return nupath
+
+LIB = localdir('.bootstrap')
+sys.path += [LIB]
+
+# 2. download required Python helper libs and import them
+
+required_files = [
+  ('wget.py', 'https://bitbucket.org/techtonik/python-wget/raw/2.0/wget.py'),
+]
+
+for f, url in required_files:
+  if os.path.exists(LIB + f):
+    print("Downloading " + f + " skipped (already downloaded).")
+  else:
+    print("Downloading %s into %s" % (f, LIB))
+    urllib.urlretrieve(url, LIB + f)
+
+import wget
+
+# --- /bootstrap
+
+
+# 3. downloading music
+
+print('Downloading music..')
+import shutil
+
+link = 'http://endgame-singularity.googlecode.com/files/endgame-singularity-music-007.zip'
+filename = LOOT + wget.filename_from_url(link)
+if os.path.exists(filename):
+  print("..archive is already downloaded to " + filename)
+  ziparch = os.path.abspath(filename)
+else:
+  print("..getting archive from %s" % link)
+  downfile = wget.download(link)
+  # this moves downloaded file into LOOT directory
+  shutil.move(downfile, filename)
+
+def extract_zip(zippath, subdir, target):
+  '''extract entries from `subdir` of `zipfile` into `target`/ directory'''
+  import shutil
+  import zipfile
+  zf = zipfile.ZipFile(zippath)
+
+  for entry in zf.namelist():
+    if not entry.startswith(subdir + '/'):
+      continue
+    outfilename = target + entry.replace(subdir + '/', '')
+
+    if outfilename.endswith('/'):  # directory
+      if not os.path.exists(outfilename):
+        os.makedirs(outfilename)
+      continue
+    print(outfilename)
+    outfile = open(outfilename, "wb")
+    infile = zf.open(entry)
+    shutil.copyfileobj(infile, outfile)
+    outfile.close()
+    infile.close()
+  zf.close()
+
+if os.path.exists(ROOT + '/music/endgame-singularity-music-README.txt'):
+  print("..already extracted.")
+else:
+  print("..extracting..")
+  extract_zip(filename, 'endgame-singularity-music-007', ROOT + '/music/')
+  print("..done.")
+
+# 4. starting Singularity
+
+print("Unleashing Singularity..")
+
+# add source code root to sys.path
+sys.path.insert(0, ROOT)
+# launch application code
+import code.singularity
+
+print("Endgame.")
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.