#! /usr/bin/python # Upload the neocities publish directory. # Standard imports import os.path import subprocess from runpy import run_path # Third party libs import neocities # program # Load the settings by running the settings file, see # https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path, # specifically ncoghlan's # (https://stackoverflow.com/users/597742/ncoghlan) answer. settings = run_path("/home/kj/.kj/neocities.py") user = settings['user'] pw = settings['pw'] local_root = os.path.expanduser(settings['localroot']) publish_root = os.path.join(local_root, 'publish') publish_mirror = os.path.join(local_root, 'publish_mirror') print(' Publish root: ' + publish_root) print('Publish mirror: ' + publish_mirror) # Connect to the remote host nc = neocities.NeoCities(user, pw) # For proof of concept just upload all. Optimize by using last # modified, etc., later def p_no_nl(s): print s, def name_is_valid(fname): return not('~' in fname or '#' in fname or os.path.basename(fname).startswith('.')) def fname_in_publish(relname): return os.path.join(publish_root, relname) def fname_in_mirror(relname): return os.path.join(publish_mirror, relname) def file_is_newer(relname): mirror_file = fname_in_mirror(relname) return ((not os.path.isfile(mirror_file)) or (os.stat(fname_in_publish(relname)).st_mtime != os.stat(mirror_file).st_mtime)) def should_upload(fname, relname): return (name_is_valid(fname) and file_is_newer(relname)) def link_mirror(relname): mirror_file = fname_in_mirror(relname) mirror_dir = os.path.dirname(mirror_file) if not os.path.lexists(mirror_dir): os.makedirs(mirror_dir) elif os.path.isfile(mirror_file): os.remove(mirror_file) # os.link(fname_in_publish(relname), # mirror_file) subprocess.call(['touch', '-r', fname_in_publish(relname), mirror_file]) def remove_deleted(publish_mirror): print('Delete removed files: ' + publish_mirror) count_files_in_mirror = 0 count_deletable = 0 count_deleted = 0 for dirpath, dirs, files in os.walk(publish_mirror): p_no_nl('dir') for filename in files: p_no_nl('f') count_files_in_mirror += 1 fname = os.path.join(dirpath,filename) relpath = fname[len(publish_mirror)+1:] if not os.path.isfile(fname_in_publish(relpath)): print print("Deleting: " + relname) count_deletable += 1 response = nc.delete('/' + relpath) if response['result'] == u'success': count_deleted += 1 else: print("Error: " + response['message']) return (count_files_in_mirror,count_deletable, count_deleted) def upload_all(publish_root): print('Upload all from: ' + publish_root) count_files_in_publish = 0 count_uploadable = 0 count_uploaded = 0 for dirpath, dirs, files in os.walk(publish_root): if not name_is_valid(dirpath): # Ignore directories that have leading dots, etc. so that # we can have a git repository and other stuff that is not # to be uploaded. continue p_no_nl('dir') for filename in files: p_no_nl('f') count_files_in_publish += 1 fname = os.path.join(dirpath,filename) relname = fname[len(publish_root)+1:] if should_upload(fname, relname): print print("Uploading: " + relname) count_uploadable += 1 response = nc.upload((fname, '/' + relname)) if response['result'] == u'success': count_uploaded += 1 link_mirror(relname) else: print("Error: " + response['message']) return (count_files_in_publish, count_uploadable, count_uploaded) count_files_in_publish, count_uploadable, count_uploaded = upload_all(publish_root) def p(s, i): print(s + str(i)) count_files_in_mirror,count_deletable, count_deleted = remove_deleted(publish_mirror) print print p("Files in publish: ", count_files_in_publish) p("Uploadable files: ", count_uploadable) p(" Uploaded files: ", count_uploaded) p(" Files in mirror: ", count_files_in_mirror) p(" Deletable files: ", count_deletable) p(" Deleted files: ", count_deleted)