1 
   2 #this script will mount a remote file system using fuse and then generate a 
   3 #directory tree using os.walk
   4 
   5 #TODO: for some reason the files that are on the top level all have a double slash
   6 #      I know that it is due to the line: 
   7 #          k.write('{}{}/{}'.format(subindent,root.replace(startpath, 'http://jlmarks.org/'),f)+'\n')
   8 #      but it seems like ALL lines should have a double slash after the .org. 
   9 #      I will have to consult with someone about that. 
  10 
  11 #TODO: format the results into a nice folding HTML page, either accordian style,
  12 #      or using <details><summary></summary></details> (This does not seem to 
  13 #      render well in Chrome, though.
  14 
  15 import pw
  16 import os
  17 
  18 host=pw.hn
  19 username=pw.un
  20 remote_folder=pw.rf
  21 local_folder=pw.lf
  22 output=pw.fi
  23 
  24 
  25 connectionString='sshfs '+username+'@'+host+':'+remote_folder+' '+local_folder
  26 
  27 
  28 
  29 
  30 def list_files(startpath):
  31     """
  32     List files is the work horse of the dirTree application. it connects to my
  33     my hosting account by fusing the remote directory to my local file system. 
  34     It then crawls the file system and creates a txt file with the contents of
  35     the file system. 
  36     
  37     It is similar to a site-map creating spider, but it the pages do not need to
  38     be linked to anywhere since it can crawl the entire server via ssh.
  39     """
  40     os.system(connectionString) #This creates the actual connection. 
  41     k=open(output, 'w') #This opens the file for adding contents of the directory
  42     for root, dirs, files in os.walk(startpath):
  43         #The next line replaces the part of the path that takes the user to the 
  44         #folder, and then counts the slashes to determine how deep to indent
  45         #the results.
  46         level = root.replace(startpath, '').count(os.sep)
  47         #This creates four spaces for each level found above.
  48         indent = ' ' * 4 * (level)
  49         #the k.write line basically replaces the brackets with the results from 
  50         #first the indent and then by replacing the path to the folder with the 
  51         #first part of my domain.
  52         k.write('{}{}'.format(indent,root.replace(startpath, 'http://jlmarks.org/')+'\n'))
  53         subindent = ' ' * 4 * (level + 1)
  54         for f in files:
  55             k.write('{}{}/{}'.format(subindent,root.replace(startpath, 'http://jlmarks.org/'),f)+'\n')
  56     k.close()
  57     
  58 print""" Suggested use:\n\tdirTree.list_files(dirTree.local_folder) """