Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WordPress Plugin Uploader
- # Version 0.1
- # By Brandon Hammond
- # Import required modules
- import os
- import sys
- import time
- import logging
- import getopt
- import ftplib
- from wordpress import API
- # Specify the version and author
- __version__ = "0.1"
- __author__ = "Brandon Hammond"
- # Define the activate_plugin() function
- def activate_plugin():
- # Authenticate with the API
- wpapi = API(
- url="",
- consumer_key="", # Public API key
- consumer_secret="", # Private API key
- api="wp-json",
- version="wp/v2",
- wp_user="", # WordPress username
- wp_pass="", # WordPress password
- oauth1a_3leg=True,
- creds_store="~/.wc-api-creds.json"
- )
- # Specify the headers
- headers = {}
- # Specify the endpoint and request
- endpoint = ""
- request = ""
- # Define the upload_plugin() function
- def upload_plugin(plugin, ftp):
- # Function: upload_plugin()
- # Purpose: Upload the WordPress plugin
- # Arguments:
- # plugin - The path to the folder of the WordPress plugin
- # ftp - The FTP object
- # Returns: None
- # List the files and change directory
- files = os.listdir()
- os.chdir(plugin)
- # For every file found
- for f in files:
- # If f is a file
- if os.path.isfile(path + r'\{}'.format(f)):
- # Open it as binary and store it
- fh = open(f, 'rb')
- myFTP.storbinary('STOR %s' % f, fh)
- fh.close()
- # If f is a directory
- elif os.path.isdir(path + r'\{}'.format(f)):
- # Make a directory on the FTP server and change to it, then recursively call upload_plugin()
- myFTP.mkd(f)
- myFTP.cwd(f)
- upload_plugin(path + r'\{}'.format(f))
- ftp.cwd('..')
- os.chdir('..')
- # Define the main() function
- def main():
- # Function: main()
- # Purpose: Process command line arguments and control base program flow
- # Arguments: None
- # Returns: None
- # Attempt to process command line arguments with getopt()
- try:
- opts, args = getopt.getopt(sys.argv[1:], "hvp:t:f:u:k:", ("help", "version", "plugin=", "plugin-path=", "ftp=", "username=", "password="))
- except getopt.GetoptError:
- # Display the error message and exit
- print("[E] Error processing command line arguments: {}!")
- exit(0)
- # Define important variables and set their default values
- plugin = None
- plugin_path = None
- ftp_server = None
- username = None
- password = None
- # Build a for loop to process command line arguments
- for opt, arg in opts:
- # If the -h or --help command line argument is used
- if opt in ("-h", "--help"):
- # Display the help message and exit
- print("USAGE:")
- print("\twp-plugin-upload [-h] [-v] [-p PLUGIN] [-t PLUGIN PATH] [-f FTP SERVER] [-u USERNAME] [-k PASSWORD]")
- print("")
- print("A simple script to upload and install a WordPress plugin")
- print("")
- print("REQUIRED ARGUMENTS:")
- print("\t-p, --plugin\tThe path to the plugin you want to install")
- print("\t-t, --plugin-path\tThe path to the plugins directory on WordPress")
- print("\t-f, --ftp\tSpecify the FTP server to use")
- print("\t-u, --username\tThe username for the FTP server")
- print("\t-k, --password\tThe password for the FTP server")
- print("")
- print("OPTIONAL ARGUMENTS:")
- print("\t-h, --help\tDisplay the help message and exit")
- print("\t-v, --version\tDisplay the version message and exit")
- exit(0)
- # If the -v or --version command line argument is used
- elif opt in ("-v", "--version"):
- # Display the version message and exit
- print("WordPress Plugin Upload")
- print("Version {}".format(__version__))
- print("By {}".format(__author__))
- exit(0)
- # If the -p or --plugin command line argument is used
- elif opt in ("-p", "--plugin"):
- # Specify the plugin path
- plugin = arg
- # If the -t or --plugin-path command line argument is used
- elif opt in ("-t", "--plugin-path"):
- # Specify the installation path on the server
- plugin_path = arg
- # If the -f or --ftp command line argument is used
- elif opt in ("-f", "--ftp"):
- # Specify the FTP server to use
- ftp_server = arg
- # If the -u or --username command line argument is used
- elif opt in ("-u", "--username"):
- # Specify the username
- username = arg
- # If the -k or --password command line argument is used
- elif opt in ("-k", "--password"):
- # Specify the password
- password = arg
- # Connect to the FTP server
- ftp = FTP(ftp_server)
- ftp.login(user=username, passwd=password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement