Created
June 18, 2012 23:29
-
-
Save batandwa/2951420 to your computer and use it in GitHub Desktop.
Centralise Drupal Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys | |
| import os | |
| import time | |
| import tempfile | |
| import subprocess | |
| import optparse | |
| import re | |
| import shutil | |
| from datetime import datetime | |
| #print sys.argv[1] | |
| curr_date = datetime.now() | |
| def processDir(mod_dir): | |
| curr_date = datetime.now() | |
| dest = sys.argv[1] + "_" + curr_date.strftime("%Y%m%d_%H%M%S") | |
| print dest | |
| tmp = tempfile.TemporaryFile() | |
| print dest | |
| def setup(options): | |
| #print options | |
| #sys.exit(1) | |
| # Created ultimate destination | |
| if os.path.exists(options.dest): | |
| print "Destination directory " + options.dest + " exists." | |
| else: | |
| print "Directory " + options.dest + " created." | |
| runBash("mkdir " + options.dest) | |
| #Created ultimate destination | |
| if os.path.exists(options.destdone): | |
| print "Destination directory " + options.destdone + " exists." | |
| else: | |
| print "Directory " + options.destdone + " created." | |
| runBash("mkdir " + options.destdone) | |
| def validateOptions(options): | |
| status = "" | |
| valid = True | |
| if options.source == None or options.source == "": | |
| status = "Source not specified." | |
| valid = False | |
| elif os.path.exists(options.source) == False: | |
| status = "Source path does not exist." | |
| valid = False | |
| elif os.path.isdir(options.source) == False: | |
| status = "A non-dirctory path given for the source." | |
| valid = False | |
| # Only continue if we have a source | |
| if valid == True: | |
| if options.dest == "" or options.dest == None: | |
| status = status + "Destination not specified.\n" | |
| valid = False | |
| if options.source == "" or options.source == None: | |
| valid = False | |
| status = status + "Source not specified.\n" | |
| return {'valid': valid, 'status': status}; | |
| def getFiles(options): | |
| # Get all the projects | |
| cmd = "ls --format=single-column %s" % (options.source) | |
| projects = runBash(cmd) | |
| # Go through projects | |
| for project in projects.splitlines(): | |
| # Skip prooject if it is a symlink | |
| if os.path.islink("%s/%s" % (options.source, project)): | |
| print "Project %s is a symlink. Skipping." % project | |
| continue | |
| # Find the info files in the project. | |
| # Use "find -P $1/$proj...." to NEVER follow symlinks. | |
| # Use "find -L $1/$proj...." to follow symlinks. | |
| cmd = "find -P %s/%s -name *.info | sed -e 's|.*/\(.*\).info|\\1|g' | xargs drush pm-info" % (options.source, project) | |
| proj_info = runBash(cmd) | |
| match = re.search('(?<=Version : )\d{1,3}\.x-?\d{0,2}\.?\d{1,6}[^\s\t\n\r ]*', proj_info) | |
| print match.group(0) | |
| if(match.group(0) == None or match.group(0) == ""): | |
| continue | |
| project_path = "%s/%s" % (options.source, project) | |
| project_dest = "%s/%s_%s" % (options.dest, project, match.group(0)) | |
| if os.path.isdir(project_dest): | |
| print "Destination project already exists at %s." % (project_dest) | |
| else: | |
| print "Moving project:\n %s -> %s" % (project_path, project_dest) | |
| shutil.move(project_path, project_dest) | |
| print "Creating a link to the new locaton at the old location:\n %s -> %s" % (project_dest, project_path) | |
| os.symlink(os.path.abspath(project_dest), project_path) | |
| project_destdone = "%s/%s_%s" % (options.destdone.rstrip('/'), project, match.group(0)) | |
| print "Creating a versioned link to the new locaton at the old location:\n %s -> %s" % (project_dest, project_destdone) | |
| os.symlink(os.path.abspath(project_dest), project_destdone) | |
| def controller(): | |
| p = optparse.OptionParser(description='A unix toolbox', | |
| prog='drupal-centralise', | |
| version='drupal-centralise 0.1', | |
| usage= '%prog [option]') | |
| p.add_option('--source', '-s', action='store', type='string', help='The path containing the extracted modules') | |
| p.add_option('--dest', '-d', action='store', type='string', | |
| help='The path to move the modules to.') | |
| #dest_done='%--sou_$(date +%Y%m%d_%H%M%S)_done) | |
| #p.add_option('--destdone', '-o', action='store', type='string', | |
| # help='Destination done') | |
| # Option Handling passes correct parameter to runBash | |
| options, arguments = p.parse_args() | |
| validation = validateOptions(options) | |
| if validation['valid'] == False: | |
| print validation['status'] + "\n" | |
| p.print_help() | |
| sys.exit(1) | |
| else: | |
| options.source = os.path.abspath(options.source) | |
| options.destdone = '%s_done' % (options.source.rstrip('/')) | |
| setup(options) | |
| getFiles(options) | |
| #processDir(options.source) | |
| def runBash(cmd): | |
| p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) | |
| out = p.stdout.read().strip() | |
| return out #This is the stdout from the shell command | |
| #Runs all the functions | |
| def main(): | |
| controller() | |
| #This idiom means the below code only runs when executed from command line | |
| if __name__ == '__main__': | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment