pash: replace doc_root with DOC_ROOT; r=bkero
authorGregory Szorc <gps@mozilla.com>
Thu, 05 Mar 2015 18:02:00 -0800
changeset 360509 d65b53c26da2d2663a97608f720b65dfbbf5fbe8
parent 360508 530e3df7c659f8fb0d3c994d22670162ea030197
child 360510 b6aded8e5fada0248b593d2e6de65ca3daf935cd
push id16998
push userrwood@mozilla.com
push dateMon, 02 May 2016 19:42:03 +0000
reviewersbkero
pash: replace doc_root with DOC_ROOT; r=bkero We no longer need a dictionary to hold document root instances because we only support 1 server (since we dropped hg.ecmascript.org). This patch replaces doc_root with DOC_ROOT.
scripts/pash/hg_helper.py
--- a/scripts/pash/hg_helper.py
+++ b/scripts/pash/hg_helper.py
@@ -42,17 +42,17 @@ https://bugzilla.mozilla.org/enter_bug.c
 EDIT_DESCRIPTION = """
 You are about to edit the description for hg.mozilla.org/users/%s/%s.
 
 If you need to edit the description for a top level repo, please quit now
 and file a Developer Services :: hg.mozilla.org bug at
 https://bugzilla.mozilla.org/enter_bug.cgi?product=Developer%20Services&component=Mercurial%3A%20hg.mozilla.org
 """.strip()
 
-doc_root = {'hg.mozilla.org': '/repo/hg/mozilla'}
+DOC_ROOT = '/repo/hg/mozilla'
 
 verbose_users = [ 'bkero@mozilla.com2', ]
 
 def is_valid_user (mail):
     mail = mail.strip()
     ## If the regex search below fails, comment out the conditional and the return. Then Uncomment the following line to atleat sanitize the input
     mail = mail.replace("(",'').replace(")",'').replace("'",'').replace('"','').replace(';','').replace("\"",'')
     #if not re.search("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", mail):
@@ -73,65 +73,63 @@ def check_repo_name (repo_name):
     good_chars = re.compile ('^(\w|-|/|\.\w)+\s*$')
     if not good_chars.search (repo_name):
         sys.stderr.write ('Only alpha-numeric characters, ".", and "-" are allowed in the repository names.\n')
         sys.stderr.write ('Please try again with only those characters.\n')
         sys.exit (1)
     return True
 
 def run_hg_clone (cname, user_repo_dir, repo_name, source_repo_path, verbose=False):
-  global doc_root
-  userdir = "%s/users/%s" % (doc_root[cname], user_repo_dir)
+  userdir = "%s/users/%s" % (DOC_ROOT, user_repo_dir)
   dest_dir = "%s/%s" % (userdir, repo_name)
   dest_url = "/users/%s/%s" % (user_repo_dir, repo_name)
 
   if os.path.exists(dest_dir):
       print(USER_REPO_EXISTS % repo_name)
       sys.exit(1)
   else:
-    if (os.path.exists ('%s/%s' % (doc_root[cname], source_repo_path))) and (check_repo_name (source_repo_path)):
+    if (os.path.exists ('%s/%s' % (DOC_ROOT, source_repo_path))) and (check_repo_name (source_repo_path)):
       if not os.path.exists (userdir):
         run_command ('mkdir %s' % userdir)
       print 'Please wait.  Cloning /%s to %s' % (source_repo_path, dest_url)
       if(verbose):
         run_command ('nohup /usr/bin/hg clone --debug --verbose --time --pull -U %s/%s %s' %
-                     (doc_root[cname], source_repo_path, dest_dir),
+                     (DOC_ROOT, source_repo_path, dest_dir),
                      verbose=True)
       else:
         run_command ('nohup /usr/bin/hg clone --pull -U %s/%s %s' %
-                     (doc_root[cname], source_repo_path, dest_dir))
+                     (DOC_ROOT, source_repo_path, dest_dir))
 
       print "Clone complete."
     else:
         print(NO_SOURCE_REPO % source_repo_path)
         sys.exit(1)
 
 def run_repo_push(args):
     """Run repo-push.sh, signaling mirror-pull on mirrors to do something."""
     command = '/usr/bin/sudo -u hg /usr/local/bin/repo-push.sh %s' % args
     return run_command(command)
 
 def make_wsgi_dir (cname, user_repo_dir):
-  global doc_root
   wsgi_dir = "/repo/hg/webroot_wsgi/users/%s" % user_repo_dir
       # Create user's webroot_wsgi folder if it doesn't already exist
   if not os.path.isdir(wsgi_dir):
     os.mkdir(wsgi_dir)
 
   print "Creating hgweb.config file"
       # Create hgweb.config file if it doesn't already exist
   if not os.path.isfile("%s/hgweb.config" % wsgi_dir):
     #try:
     hgconfig = open("%s/hgweb.config" % wsgi_dir, "w")
     #except:
     #  print("Problem opening hgweb.config file, please file a bug against IT and pastebin this error.")
     hgconfig.write("[web]\n")
     hgconfig.write("baseurl = http://%s/users/%s\n" % (cname, user_repo_dir))
     hgconfig.write("[paths]\n")
-    hgconfig.write("/ = %s/users/%s/*\n" % (doc_root[cname], user_repo_dir))
+    hgconfig.write("/ = %s/users/%s/*\n" % (DOC_ROOT, user_repo_dir))
     hgconfig.close()
 
       # Create hgweb.wsgi file if it doesn't already exist
   if not os.path.isfile("%s/hgweb.wsgi" % wsgi_dir):
       try:
           hgwsgi = open("%s/hgweb.wsgi" % wsgi_dir, "w")
       except Exception:
           print(HGWEB_ERROR)
@@ -142,32 +140,30 @@ def make_wsgi_dir (cname, user_repo_dir)
       hgwsgi.write("from mercurial import demandimport; demandimport.enable()\n")
       hgwsgi.write("from mercurial.hgweb import hgweb\n")
       hgwsgi.write("import os\n")
       hgwsgi.write("os.environ['HGENCODING'] = 'UTF-8'\n")
       hgwsgi.write("application = hgweb(config)\n")
       hgwsgi.close()
 
 def fix_user_repo_perms (cname, repo_name):
-    global doc_root
     user = os.getenv ('USER')
     user_repo_dir = user.replace ('@', '_')
     print "Fixing permissions, don't interrupt."
     try:
-        run_command ('chown %s:scm_level_1 %s/users/%s' % (user, doc_root[cname], user_repo_dir))
-        run_command ('chmod g+w %s/users/%s' % (doc_root[cname], user_repo_dir))
-        run_command ('chmod g+s %s/users/%s' % (doc_root[cname], user_repo_dir))
-        run_command ('chown -R %s:scm_level_1 %s/users/%s/%s' % (user, doc_root[cname], user_repo_dir, repo_name))
-        run_command ('chmod -R g+w %s/users/%s/%s' % (doc_root[cname], user_repo_dir, repo_name))
-        run_command ('find %s/users/%s/%s -depth -type d | xargs chmod g+s' % (doc_root[cname], user_repo_dir, repo_name))
+        run_command ('chown %s:scm_level_1 %s/users/%s' % (user, DOC_ROOT, user_repo_dir))
+        run_command ('chmod g+w %s/users/%s' % (DOC_ROOT, user_repo_dir))
+        run_command ('chmod g+s %s/users/%s' % (DOC_ROOT, user_repo_dir))
+        run_command ('chown -R %s:scm_level_1 %s/users/%s/%s' % (user, DOC_ROOT, user_repo_dir, repo_name))
+        run_command ('chmod -R g+w %s/users/%s/%s' % (DOC_ROOT, user_repo_dir, repo_name))
+        run_command ('find %s/users/%s/%s -depth -type d | xargs chmod g+s' % (DOC_ROOT, user_repo_dir, repo_name))
     except Exception, e:
         print "Exception %s" % (e)
 
 def make_repo_clone (cname, repo_name, quick_src, verbose=False, source_repo=''):
-  global doc_root
   user = os.getenv ('USER')
   user_repo_dir = user.replace ('@', '_')
   dest_url = "/users/%s" % user_repo_dir
   source_repo = ''
   if quick_src:
     if(user in verbose_users):
         verbose=True
         run_hg_clone (cname, user_repo_dir, repo_name, quick_src, True)
@@ -181,46 +177,46 @@ def make_repo_clone (cname, repo_name, q
     print(MAKING_REPO.format(repo=repo_name, user=user, cname=cname,
                              user_dir=user_repo_dir))
     selection = prompt_user ('Proceed?', ['yes', 'no'])
     if (selection == 'yes'):
       print 'You can clone an existing public repo or a users private repo.'
       print 'You can also create an empty repository.'
       selection = prompt_user ('Source repository:', ['Clone a public repository', 'Clone a private repository', 'Create an empty repository'])
       if (selection == 'Clone a public repository'):
-        exec_command = "/usr/bin/find " + doc_root[cname] + " -maxdepth 3 -mindepth 2 -type d -name .hg"
+        exec_command = "/usr/bin/find " + DOC_ROOT + " -maxdepth 3 -mindepth 2 -type d -name .hg"
         args = shlex.split(exec_command)
         p = Popen(args, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
         repo_list = p.communicate()[0].split("\n")
         if repo_list:
           print "We have the repo_list"
-          repo_list = map (lambda x: x.replace (doc_root[cname] + '/', ''), repo_list)
+          repo_list = map (lambda x: x.replace (DOC_ROOT + '/', ''), repo_list)
           repo_list = map (lambda x: x.replace ('/.hg', ''), repo_list)
           repo_list = sorted(repo_list)
           print 'List of available public repos'
           source_repo = prompt_user ('Pick a source repo:', repo_list, period=False)
       elif (selection == 'Clone a private repository'):
         source_user = raw_input ('Please enter the e-mail address of the user owning the repo: ')
         valid_user = is_valid_user(source_user)
         if valid_user == True:
           source_user = source_user.replace ('@', '_')
         elif valid_user == False:
           sys.stderr.write ('Unknown user.\n')
           sys.exit (1)
         elif valid_user == 'Invalid Email Address':
           sys.stderr.write ('Invalid Email Address.\n')
           sys.exit (1)
-        source_user_path = run_command ('find ' + doc_root[cname] + '/users/' + source_user + ' -maxdepth 1 -mindepth 1 -type d')
+        source_user_path = run_command ('find ' + DOC_ROOT + '/users/' + source_user + ' -maxdepth 1 -mindepth 1 -type d')
         if not source_user_path:
           print 'That user does not have any private repositories.'
           print 'Check https://' + cname + '/users for a list of valid users.'
           sys.exit (1)
         else:
-          user_repo_list = run_command ('find ' + doc_root[cname] + '/users/' + source_user + ' -maxdepth 3 -mindepth 2 -type d -name .hg')
-          user_repo_list = map (lambda x: x.replace (doc_root[cname] + '/users/' + source_user, ''), user_repo_list)
+          user_repo_list = run_command ('find ' + DOC_ROOT + '/users/' + source_user + ' -maxdepth 3 -mindepth 2 -type d -name .hg')
+          user_repo_list = map (lambda x: x.replace (DOC_ROOT + '/users/' + source_user, ''), user_repo_list)
           user_repo_list = map (lambda x: x.replace ('/.hg', ''), user_repo_list)
           user_repo_list = map (lambda x: x.strip ('/'), user_repo_list)
           user_repo_list = sorted(user_repo_list)
           print 'Select the users repo you wish to clone.'
           source_repo = prompt_user ('Pick a source repo:', user_repo_list, period=False)
           source_repo = 'users/' + source_user + '/' + source_repo
       elif (selection == 'Create an empty repository'):
         source_repo=''
@@ -232,36 +228,35 @@ def make_repo_clone (cname, repo_name, q
         response = prompt_user ('Proceed?', ['yes', 'no'])
         if (response == 'yes'):
           print 'Please do not interrupt this operation.'
           run_hg_clone (cname, user_repo_dir, repo_name, source_repo)
       else:
         print "About to create an empty repository at /users/%s/%s" % (user_repo_dir, repo_name)
         response = prompt_user ('Proceed?', ['yes', 'no'])
         if (response == 'yes'):
-          if not os.path.exists ('%s/users/%s' % (doc_root[cname], user_repo_dir)):
+          if not os.path.exists ('%s/users/%s' % (DOC_ROOT, user_repo_dir)):
             try:
-              exec_command = '/bin/mkdir %s/users/%s' % (doc_root[cname], user_repo_dir)
+              exec_command = '/bin/mkdir %s/users/%s' % (DOC_ROOT, user_repo_dir)
               run_command (exec_command)
             except Exception, e:
               print "Exception %s" % (e)
 
-          run_command ('/usr/bin/nohup /usr/bin/hg init %s/users/%s/%s' % (doc_root[cname], user_repo_dir, repo_name))
+          run_command ('/usr/bin/nohup /usr/bin/hg init %s/users/%s/%s' % (DOC_ROOT, user_repo_dir, repo_name))
           run_repo_push('-e users/%s/%s' % (user_repo_dir, repo_name))
       fix_user_repo_perms (cname, repo_name)
       # New user repositories are non-publishing by default.
       set_repo_publishing(cname, repo_name, False)
       sys.exit (0)
 
 def get_and_validate_user_repo(cname, repo_name):
-    global doc_root
     user = os.getenv('USER')
     user_repo_dir = user.replace('@', '_')
     rel_path = '/users/%s/%s' % (user_repo_dir, repo_name)
-    fs_path = '%s%s' % (doc_root[cname], rel_path)
+    fs_path = '%s%s' % (DOC_ROOT, rel_path)
 
     if not os.path.exists(fs_path):
         sys.stderr.write('Could not find repository at %s.\n' % rel_path)
         sys.exit(1)
 
     return fs_path
 
 def get_user_repo_config(repo_dir):
@@ -279,17 +274,16 @@ def get_user_repo_config(repo_dir):
     if not config.read(path):
         sys.stderr.write('Could not read the hgrc file for this repo\n')
         sys.stderr.write('Please file a Developer Services :: hg.mozilla.org bug\n')
         sys.exit(1)
 
     return path, config
 
 def edit_repo_description (cname, repo_name):
-    global doc_root
     user = os.getenv ('USER')
     user_repo_dir = user.replace ('@', '_')
     print(EDIT_DESCRIPTION % (user_repo_dir, repo_name))
     selection = prompt_user ('Proceed?', ['yes', 'no'])
     if (selection == 'yes'):
         repo_path = get_and_validate_user_repo(cname, repo_name)
         repo_description =  raw_input ('Enter a one line descripton for the repository: ')
         if repo_description == '':
@@ -338,35 +332,33 @@ def set_repo_publishing(cname, repo_name
     if publish:
         sys.stderr.write('Repository marked as publishing: changesets will '
             'change to public phase when pushed.\n')
     else:
         sys.stderr.write('Repository marked as non-publishing: draft '
             'changesets will remain in the draft phase when pushed.\n')
 
 def do_delete(cname, repo_dir, repo_name, verbose=False):
-    global doc_root
     if verbose:
         print "Deleting..."
-    run_command ('rm -rf %s/users/%s/%s' % (doc_root[cname], repo_dir, repo_name))
+    run_command ('rm -rf %s/users/%s/%s' % (DOC_ROOT, repo_dir, repo_name))
     run_repo_push('-d -e users/%s/%s' % (repo_dir, repo_name))
     if verbose:
         print "Finished deleting"
     purge_log = open ('/tmp/pushlog_purge.%s' % os.getpid(), "a")
     purge_log.write ('echo users/%s/%s\n' % (repo_dir, repo_name))
     purge_log.close()
 
 def delete_repo (cname, repo_name, do_quick_delete, verbose=False):
-  global doc_root
   user = os.getenv ('USER')
   if(user in verbose_users):
       verbose = True
   user_repo_dir = user.replace ('@', '_')
   url_path = "/users/%s" % user_repo_dir
-  if os.path.exists ('%s/users/%s/%s' % (doc_root[cname], user_repo_dir, repo_name)):
+  if os.path.exists ('%s/users/%s/%s' % (DOC_ROOT, user_repo_dir, repo_name)):
     if do_quick_delete:
       do_delete (cname, user_repo_dir, repo_name, verbose)
     else:
       print '\nAre you sure you want to delete /users/%s/%s?' % (user_repo_dir, repo_name)
       print '\nThis action is IRREVERSIBLE.'
       selection = prompt_user ('Proceed?', ['yes', 'no'])
       if (selection == 'yes'):
         do_delete (cname, user_repo_dir, repo_name, verbose)
@@ -392,39 +384,38 @@ def edit_repo (cname, repo_name, do_quic
             delete_repo(cname, repo_name, False)
         elif action == 'Mark repository as non-publishing':
             set_repo_publishing(cname, repo_name, False)
         elif action == 'Mark repository as publishing':
             set_repo_publishing(cname, repo_name, True)
     return
 
 def serve (cname):
-    global doc_root
     ssh_command = os.getenv ('SSH_ORIGINAL_COMMAND')
     if not ssh_command:
         sys.stderr.write ('No interactive shells allowed here!\n')
         sys.exit (1)
     elif ssh_command.startswith ('hg'):
         repo_expr = re.compile ('(.*)\s+-R\s+([^\s]+\s+)(.*)')
         if (repo_expr.search (ssh_command)):
             [(hg_path, repo_path, hg_command)] = repo_expr.findall (ssh_command)
             if (hg_command == 'serve --stdio') and (check_repo_name (repo_path)):
-                hg_arg_string = '/usr/bin/hg -R ' + doc_root[cname] + '/' + repo_path + hg_command
+                hg_arg_string = '/usr/bin/hg -R ' + DOC_ROOT + '/' + repo_path + hg_command
                 hg_args = hg_arg_string.split ()
                 os.execv ('/usr/bin/hg', hg_args)
             else:
                 sys.stderr.write ("Thank you dchen! but.. I don't think so!\n")
                 sys.exit (1)
     elif ssh_command.startswith('clone '):
         args = ssh_command.replace ('clone', '').split()
         if check_repo_name (args[0]):
             if len(args) == 1:
                 make_repo_clone (cname, args[0], None)
             elif len(args) == 2:
-                if os.path.isdir ('%s/%s/.hg' % (doc_root[cname], args[1])):
+                if os.path.isdir ('%s/%s/.hg' % (DOC_ROOT, args[1])):
                     make_repo_clone (cname, args[0], args[1])
             sys.exit (0)
         sys.stderr.write ('clone usage: ssh hg.mozilla.org clone newrepo [srcrepo]\n')
         sys.exit (1)
     elif ssh_command.startswith('edit '):
         args = ssh_command.replace ('edit', '',  1).split()
         if check_repo_name (args[0]):
             if len(args) == 1: