elblogg

blah, blah, blag, blog

Unreadable code

If you’re really trying, you will always be able to write completely unreadable code in any programming language. All the language designers attemts to force you to write readable code, and even making it so the easiest way ahead, even for quick hacks is to write it as readable and reusable code will fail if you’re really trying to write unreadable code.

This guy is obviously concerned about “job security”. (not the blog author, but the code author)

  1. #!/usr/bin/env python
  2. import os,sys
  3. C=os.chdir
  4. S=os.system
  5. M=os.mkdir
  6. J=os.path.join
  7. A=os.path.abspath
  8. D=os.path.dirname
  9. E=os.path.exists
  10. W=sys.stdout.write
  11. V=sys.argv
  12. X=sys.exit
  13. ERR=lambda m:W(m+"\n")
  14. PRNT=lambda m:W(m+"\n")
  15. assert len(V)==2,"you must provide a sandbox name"
  16. SB=V[1]
  17. H=A(D(__file__))
  18. SBD=J(D(H),SB)
  19. C(SBD)
  20. PST=J(SBD,‘bin/paster’)
  21. VAR=J(SBD,‘var’)
  22. ETC=J(SBD,‘etc’)
  23. S("mkdir -p "+VAR)
  24. PRNT("restarting "+SB)
  25. CMD=";".join([’source %s’%J(SBD,‘bin/activate’),PST+" serve –daemon –pid-file=%s/sandbox.pid –log-file=%s/sandbox.log %s/sandbox.ini start"%(VAR,VAR,ETC)])
  26. PRNT(CMD)
  27. S(CMD)
  28. PRNT("All done!")
  29. X(0)

Aaannd… Heres my understanding of the code:

  1. #!/usr/bin/env python
  2. import os,sys
  3.  
  4. #bail out if there’s no sandbox specified
  5. assert len(sys.argv)==2,"you must provide a sandbox name"
  6. sandbox=sys.argv[1]
  7.  
  8. #get the dir of this script
  9. home=os.path.abspath(os.path.dirname(__file__))
  10.  
  11. #sandboxdir is this dir/sandboxname
  12. sandboxdir=os.path.join(os.path.dirname(home),sandox)
  13.  
  14. #go into the root of the sandbox
  15. os.chdir(sandboxdir)
  16. PST=os.path.join(sandboxdir,‘bin’,‘paster’)
  17. VAR=os.path.join(sandboxdir,‘var’)
  18. ETC=os.path.join(sandboxdir,‘etc’)
  19.  
  20. #make var directory if it doesnt exists
  21. if not os.path.exists(VAR):
  22.     os.mkdir(VAR)
  23.  
  24. print "restarting "+sandbox
  25.  
  26. # Build the system commands required to restart the sandbox
  27. cmds=[]
  28. cmds.append(’source %s’ % os.path.join(sandboxdir,‘bin’,‘activate’))
  29. cmds.append(PST+" serve –daemon –pid-file=%s/sandbox.pid"
  30.             "–log-file=%s/sandbox.log %s/sandbox.ini start" % (VAR,VAR,ETC))
  31. CMD=";".join(cmds)
  32. PRNT(CMD)
  33.  
  34. #run the cmds
  35. if os.system(CMD) == 0:
  36.     print "All done!"
  37.     sys.exit(0)
  38. else:
  39.     print "Ooops. failed."
  40.     sys.exit(1)

Tags: , , , , ,

Leave a Reply

Bloggurat Twingly BlogRank Blogglisten